本文檔介紹如何使用云開發(fā)能力,無需域名、無需服務(wù)器,即可快速搭建一個(gè)學(xué)生信息管理系統(tǒng),最后成型的應(yīng)用展示如下:
提供的功能有:
搭建學(xué)生信息管理系統(tǒng),需開啟匿名登錄,前往 登錄授權(quán) 控制臺(tái),單擊匿名登錄開關(guān),如下:
! 需要把代碼第2行的
envId
改為您的真實(shí)環(huán)境 ID。
<spsn id="code"></span> 函數(shù)完整代碼如下:
// 此處填入您的真實(shí)環(huán)境 ID
const envId = "your-env-id"
exports.main = async (event) => {
// 網(wǎng)頁JS代碼
const script =
`
var envId = "${envId}"
class FunctionQuickStarter {
constructor() {
// 綁定 dom
this.addNameInput = document.getElementById("add-name")
this.deleteNameInput = document.getElementById("delete-name")
this.addAgeInput = document.getElementById("add-age")
this.addAvatarInput = document.getElementById("add-avatar")
this.addDataButton = document.getElementById("add-button")
this.infoBox = document.getElementById("info-box")
// 綁定 dom listener
this.addAvatarInput.addEventListener("change", this.addAvatar.bind(this), false)
this.addDataButton.addEventListener("click", this.addData.bind(this), false)
// 初始化 CloudBase
this.app = tcb.init({
env: envId
})
this.setButtonStatus(true)
this.signIn()
}
setButtonStatus(status) {
this.addDataButton.disabled = status
if (!status) {
this.queryData()
}
}
// 匿名登錄
signIn() {
var auth = this.app.auth({
persistence: "local"
})
if(!auth.hasLoginState()) {
auth.signInAnonymously().then(() => {
this.setButtonStatus(false)
})
} else {
this.setButtonStatus(false)
}
}
// 錄入信息
addData(e) {
e.stopPropagation()
e.preventDefault()
var name = this.addNameInput.value
var age = parseFloat(this.addAgeInput.value)
var coll = this.app.database().collection("test_db")
if (!name) {
window.alert(
"姓名不能為空!"
)
return
}
if (!(age > 0 && age < 200)) {
window.alert(
"年齡需要在 0 ~ 200 之間"
)
return
}
if (!this.avatarUrl) {
window.alert(
"頭像不能為空!"
)
return
}
this.setButtonStatus(true)
coll.add({
name: name,
age: age,
avatar: this.avatarUrl
}).then((res) => {
if (res.code) {
console.log("數(shù)據(jù)庫新增失敗", res)
// 打印數(shù)據(jù)庫新增失敗的信息
window.alert(
"成績錄入失敗: [code=" + res.code + "] [message=" + res.message + "]"
)
} else {
console.log("數(shù)據(jù)庫新增成功", res)
this.avatarUrl = ""
window.alert(
"成績錄入成功!"
)
}
this.setButtonStatus(false)
})
}
// 上傳頭像
addAvatar(e) {
e.stopPropagation()
e.preventDefault()
var file = e.target.files[0]
var name = file.name
this.app.uploadFile({
cloudPath: (new Date()).valueOf() + "-" + name,
filePath: file
}).then(res => {
// 云文件ID
var fileID = res.fileID
// 通過云文件ID 獲取 云文件鏈接
this.app.getTempFileURL({
fileList: [fileID]
}).then(res2 => {
var fileObj = res2.fileList[0]
var url = fileObj.tempFileURL
this.avatarUrl = url
})
})
}
// 查詢信息
queryData() {
var coll = this.app.database().collection("test_db")
coll.where({}).get().then((res) => {
if (res.code) {
console.log("數(shù)據(jù)庫查詢失敗", res)
// 打印數(shù)據(jù)庫查詢失敗的信息
window.alert(
"成績查詢失敗: [code=" + res.code + "] [message=" + res.message + "]"
)
} else {
console.log("數(shù)據(jù)庫查詢成功", res)
// 打印數(shù)據(jù)庫查詢結(jié)果
var html =
"<tr>" +
"<th>姓名</th>" +
"<th>年齡</th>" +
"<th>頭像</th>" +
"</tr>"
if (res.data.length > 0) {
res.data.forEach((data) => {
html +=
"<tr>" +
"<td>" + data.name + "</td>" +
"<td>" + data.age + "</td>" +
"<td><img src='" + data.avatar + "' style='width:60px;height:60px'></td>" +
"</tr>"
})
this.infoBox.innerHTML =
"<table style='margin: 0 auto'>" +
html +
"</table>"
} else {
window.alert(
"查無此人!"
)
}
}
})
}
}
window.addEventListener("load", function() {
window.app = new FunctionQuickStarter()
})
`
// 網(wǎng)頁HTML代碼
const body =
`
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<script src="https://imgcache.qq.com/qcloud/tcbjs/1.9.0/tcb.js" rel="external nofollow" ></script>
<script type="text/javascript">${script}</script>
</head>
<body style="text-align:center;">
<header>
<h1>學(xué)生信息系統(tǒng)</h1>
</header>
<div style="margin: 0 30%; padding: 20px 0; border: solid;">
<h2>錄入信息</h2>
<form>
姓名:
<input id="add-name">
<br><br>
年齡:
<input id="add-age">
<br><br>
頭像:
<input type="file" id="add-avatar" accept=".jpg, .jpeg, .png" style="width: 153px"/>
<br><br>
<button id="add-button">錄入</button>
<br><br>
</form>
<h2>信息列表</h2>
<div id="info-box"></div>
</div>
</body>
</html>
`
return {
statusCode: 200,
headers: {
'content-type': 'text/html'
},
body: body
}
}
/quickstart
。test_db
的數(shù)據(jù)庫集合。
xx.service.tcloudbase.com/quickstart
,即可訪問搭建好的學(xué)生管理系統(tǒng)。
更多建議: