Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Example Tutorial - Student Management System


May 27, 2021 Web Cloud Development and Web Hosting Learning Guide


Table of contents


This document describes how to use cloud development capabilities to quickly build a student information management system without a domain name or server, and the final application is shown as follows:

Example Tutorial - Student Management System

Features provided include:

  • Entry of student information (name, age, avatar).
  • Student information is read and displayed
  • File upload

Preparation

  1. Please register for Tencent Cloud Account, register successfully to use Tencent Cloud Services, registered to ignore this step.
  2. Sign in to Tencent Cloud Development Console and click Create and use now to create a new environment for deployment. Example Tutorial - Student Management System If you've created an environment before, you can continue to use the metered environment you've created, or create a new environment again. Example Tutorial - Student Management System
  3. In the new environment window, fill in the environment name according to the actual needs, select Bill by Volume, click Open Now to open the environment. Example Tutorial - Student Management System
  4. lt; O nce the span id is "step1.3" and the opening is successful, click on the environment name and go to the environment overview page. R emember your environment ID, which will be used in the next steps, as follows: Example Tutorial - Student Management System

Step 1: Turn on anonymous sign-in

To build a student information management system, you need to turn on anonymous login, go to the login authorization console, click the anonymous login switch, as follows:

Example Tutorial - Student Management System

Step 2: Create a cloud function

  1. Click on the cloud function on the left, click New Cloud Function, and start creating a cloud function. Example Tutorial - Student Management System
  2. Create a function named index, run in Nodejs 10.15, copy the sample code below into the index function and save it. Example Tutorial - Student Management System

    ! You need to change the envId of the code to your real-world ID.

lt; The full code for the spsn id-"code" and the function is as follows:

  1. // 此处填入您的真实环境 ID
  2. const envId = "your-env-id"
  3. exports.main = async (event) => {
  4. // 网页JS代码
  5. const script =
  6. `
  7. var envId = "${envId}"
  8. class FunctionQuickStarter {
  9. constructor() {
  10. // 绑定 dom
  11. this.addNameInput = document.getElementById("add-name")
  12. this.deleteNameInput = document.getElementById("delete-name")
  13. this.addAgeInput = document.getElementById("add-age")
  14. this.addAvatarInput = document.getElementById("add-avatar")
  15. this.addDataButton = document.getElementById("add-button")
  16. this.infoBox = document.getElementById("info-box")
  17. // 绑定 dom listener
  18. this.addAvatarInput.addEventListener("change", this.addAvatar.bind(this), false)
  19. this.addDataButton.addEventListener("click", this.addData.bind(this), false)
  20. // 初始化 CloudBase
  21. this.app = tcb.init({
  22. env: envId
  23. })
  24. this.setButtonStatus(true)
  25. this.signIn()
  26. }
  27. setButtonStatus(status) {
  28. this.addDataButton.disabled = status
  29. if (!status) {
  30. this.queryData()
  31. }
  32. }
  33. // 匿名登录
  34. signIn() {
  35. var auth = this.app.auth({
  36. persistence: "local"
  37. })
  38. if(!auth.hasLoginState()) {
  39. auth.signInAnonymously().then(() => {
  40. this.setButtonStatus(false)
  41. })
  42. } else {
  43. this.setButtonStatus(false)
  44. }
  45. }
  46. // 录入信息
  47. addData(e) {
  48. e.stopPropagation()
  49. e.preventDefault()
  50. var name = this.addNameInput.value
  51. var age = parseFloat(this.addAgeInput.value)
  52. var coll = this.app.database().collection("test_db")
  53. if (!name) {
  54. window.alert(
  55. "姓名不能为空!"
  56. )
  57. return
  58. }
  59. if (!(age > 0 && age < 200)) {
  60. window.alert(
  61. "年龄需要在 0 ~ 200 之间"
  62. )
  63. return
  64. }
  65. if (!this.avatarUrl) {
  66. window.alert(
  67. "头像不能为空!"
  68. )
  69. return
  70. }
  71. this.setButtonStatus(true)
  72. coll.add({
  73. name: name,
  74. age: age,
  75. avatar: this.avatarUrl
  76. }).then((res) => {
  77. if (res.code) {
  78. console.log("数据库新增失败", res)
  79. // 打印数据库新增失败的信息
  80. window.alert(
  81. "成绩录入失败: [code=" + res.code + "] [message=" + res.message + "]"
  82. )
  83. } else {
  84. console.log("数据库新增成功", res)
  85. this.avatarUrl = ""
  86. window.alert(
  87. "成绩录入成功!"
  88. )
  89. }
  90. this.setButtonStatus(false)
  91. })
  92. }
  93. // 上传头像
  94. addAvatar(e) {
  95. e.stopPropagation()
  96. e.preventDefault()
  97. var file = e.target.files[0]
  98. var name = file.name
  99. this.app.uploadFile({
  100. cloudPath: (new Date()).valueOf() + "-" + name,
  101. filePath: file
  102. }).then(res => {
  103. // 云文件ID
  104. var fileID = res.fileID
  105. // 通过云文件ID 获取 云文件链接
  106. this.app.getTempFileURL({
  107. fileList: [fileID]
  108. }).then(res2 => {
  109. var fileObj = res2.fileList[0]
  110. var url = fileObj.tempFileURL
  111. this.avatarUrl = url
  112. })
  113. })
  114. }
  115. // 查询信息
  116. queryData() {
  117. var coll = this.app.database().collection("test_db")
  118. coll.where({}).get().then((res) => {
  119. if (res.code) {
  120. console.log("数据库查询失败", res)
  121. // 打印数据库查询失败的信息
  122. window.alert(
  123. "成绩查询失败: [code=" + res.code + "] [message=" + res.message + "]"
  124. )
  125. } else {
  126. console.log("数据库查询成功", res)
  127. // 打印数据库查询结果
  128. var html =
  129. "<tr>" +
  130. "<th>姓名</th>" +
  131. "<th>年龄</th>" +
  132. "<th>头像</th>" +
  133. "</tr>"
  134. if (res.data.length > 0) {
  135. res.data.forEach((data) => {
  136. html +=
  137. "<tr>" +
  138. "<td>" + data.name + "</td>" +
  139. "<td>" + data.age + "</td>" +
  140. "<td><img src='" + data.avatar + "' style='width:60px;height:60px'></td>" +
  141. "</tr>"
  142. })
  143. this.infoBox.innerHTML =
  144. "<table style='margin: 0 auto'>" +
  145. html +
  146. "</table>"
  147. } else {
  148. window.alert(
  149. "查无此人!"
  150. )
  151. }
  152. }
  153. })
  154. }
  155. }
  156. window.addEventListener("load", function() {
  157. window.app = new FunctionQuickStarter()
  158. })
  159. `
  160. // 网页HTML代码
  161. const body =
  162. `
  163. <!doctype html>
  164. <html lang="zh">
  165. <head>
  166. <meta charset="utf-8">
  167. <script src="https://imgcache.qq.com/qcloud/tcbjs/1.9.0/tcb.js" rel="external nofollow" ></script>
  168. <script type="text/javascript">${script}</script>
  169. </head>
  170. <body style="text-align:center;">
  171. <header>
  172. <h1>学生信息系统</h1>
  173. </header>
  174. <div style="margin: 0 30%; padding: 20px 0; border: solid;">
  175. <h2>录入信息</h2>
  176. <form>
  177. 姓名:
  178. <input id="add-name">
  179. <br><br>
  180. 年龄:
  181. <input id="add-age">
  182. <br><br>
  183. 头像:
  184. <input type="file" id="add-avatar" accept=".jpg, .jpeg, .png" style="width: 153px"/>
  185. <br><br>
  186. <button id="add-button">录入</button>
  187. <br><br>
  188. </form>
  189. <h2>信息列表</h2>
  190. <div id="info-box"></div>
  191. </div>
  192. </body>
  193. </html>
  194. `
  195. return {
  196. statusCode: 200,
  197. headers: {
  198. 'content-type': 'text/html'
  199. },
  200. body: body
  201. }
  202. }

Step 3: Enable cloud access

  1. Go to the Cloud Development Console Cloud Access page and turn on the cloud access switch, as follows: Example Tutorial - Student Management System
  2. Click New to start a new trigger path with the following description of the configuration item:
    • Trigger path: Fill /quickstart
    • Associated resources: Select the index function created in step 2. Example Tutorial - Student Management System
  3. Click Confirm to add success.

Step 4: Create a database

  1. Go to the Database console to create a new collection page.
  2. Click New Collection to create a collection of test_db the collection. Example Tutorial - Student Management System
  3. Click the collection name to go to the collection management page. Example Tutorial - Student Management System
  4. In the Permission Settings tab, set permissions for the collection to be readable by all users. Example Tutorial - Student Management System
  5. Click Save to complete the configuration.

Step 5: Visit the website

  1. Once configured above, you can copy the domain name of the cloud access on the Cloud Access page, as follows: Example Tutorial - Student Management System
  2. Using a replicated xx.service.tcloudbase.com/quickstart you can access a well-built student management system.