本文档介绍如何使用云开发能力,无需域名、无需服务器,即可快速搭建一个学生信息管理系统,最后成型的应用展示如下:
提供的功能有:
搭建学生信息管理系统,需开启匿名登录,前往 登录授权 控制台,单击匿名登录开关,如下:
envId
改为您的真实环境 ID。<spsn id="code"></span> 函数完整代码如下:
// 此处填入您的真实环境 ID
const envId = "your-env-id"
exports.main = async (event) => {
// 网页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("数据库新增失败", res)
// 打印数据库新增失败的信息
window.alert(
"成绩录入失败: [code=" + res.code + "] [message=" + res.message + "]"
)
} else {
console.log("数据库新增成功", 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("数据库查询失败", res)
// 打印数据库查询失败的信息
window.alert(
"成绩查询失败: [code=" + res.code + "] [message=" + res.message + "]"
)
} else {
console.log("数据库查询成功", res)
// 打印数据库查询结果
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()
})
`
// 网页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>学生信息系统</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
的数据库集合。
xx.service.tcloudbase.com/quickstart
,即可访问搭建好的学生管理系统。