AddAccount.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const API = require("../../../lib/API.js");
  2. const db = require("../../../plugin/DataBase/db.js");
  3. const { BaseStdResponse } = require("../../../BaseStdResponse.js");
  4. const AccessControl = require("../../../lib/AccessControl.js");
  5. class AddAccount extends API {
  6. constructor() {
  7. super();
  8. this.setPath('/Lepao/Account')
  9. this.setMethod('POST')
  10. this.emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
  11. this.banEmailList = ['icloud.com']
  12. }
  13. // 生成 6 位数字 + 字母混合码
  14. async generateCode() {
  15. try {
  16. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  17. let code = ""
  18. for (let i = 0; i < 6; i++) {
  19. code += chars.charAt(Math.floor(Math.random() * chars.length))
  20. }
  21. let sql = 'SELECT id FROM lepao_face WHERE face_code = ?'
  22. let rows = await db.query(sql, [code])
  23. if (!rows)
  24. throw new Error('数据库错误,请稍后再试')
  25. if (rows.length > 0)
  26. return await this.generateCode()
  27. return code
  28. } catch (error) {
  29. throw error
  30. }
  31. }
  32. async onRequest(req, res) {
  33. let { uuid, session, student_num, email, id, area, auto_time, auto_run, target_count, notes } = req.body
  34. if ([uuid, session, student_num, email, auto_time, target_count].some(value => value === '' || value === null || value === undefined))
  35. return res.json({
  36. ...BaseStdResponse.MISSING_PARAMETER
  37. })
  38. if (isNaN(target_count) || target_count < 0 || target_count > 99) {
  39. return res.json({
  40. ...BaseStdResponse.ERR,
  41. msg: '乐跑目标次数不在合法范围内'
  42. })
  43. }
  44. if (!this.emailRegex.test(email)) {
  45. Message.error('请检查邮箱格式是否正确')
  46. return res.json({
  47. ...BaseStdResponse.ERR,
  48. msg: '请检查邮箱格式是否正确'
  49. })
  50. }
  51. const emailDomain = email.split('@')[1].toLowerCase()
  52. if (this.banEmailList.includes(emailDomain))
  53. return res.json({
  54. ...BaseStdResponse.ERR,
  55. msg: `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  56. })
  57. if (!await AccessControl.checkSession(uuid, session))
  58. return res.status(401).json({
  59. ...BaseStdResponse.ACCESS_DENIED
  60. })
  61. let countSql = 'SELECT id, create_user, total_num FROM lepao_account WHERE student_num = ?'
  62. let countRows = await db.query(countSql, [student_num])
  63. if (!countRows)
  64. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  65. // 判断是否重复注册
  66. if (!id) {
  67. if (countRows.length !== 0 && countRows[0].create_user != null) {
  68. if (countRows[0].create_user !== uuid)
  69. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
  70. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已添加' })
  71. }
  72. }
  73. if (countRows.length !== 0) {
  74. if (countRows[0].total_num >= target_count && target_count !== 0)
  75. return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到目标次数,请尝试修改目标次数' })
  76. }
  77. const time = new Date().getTime()
  78. let sql, r
  79. if (!id) {
  80. if (countRows.length !== 0) {
  81. sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, notes = ? WHERE id = ?'
  82. r = await db.query(sql, [uuid, email, area, auto_time, auto_run, target_count, time, notes ?? '', countRows[0].id])
  83. }
  84. else {
  85. const face_code = await this.generateCode()
  86. sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, auto_run, target_count, create_user, create_time, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
  87. r = await db.query(sql, [student_num, email, area, auto_time, auto_run, target_count, uuid, time, notes ?? ''])
  88. let faceSql = 'INSERT INTO lepao_face (student_num, face_code) VALUES (?, ?)'
  89. let faceRows = await db.query(faceSql, [student_num, face_code])
  90. if (!faceRows || faceRows.affectedRows !== 1)
  91. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  92. }
  93. } else {
  94. sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ? WHERE id = ?'
  95. r = await db.query(sql, [student_num, email, area, auto_time, target_count, auto_run, notes ?? '', id])
  96. }
  97. try {
  98. if (r && r.affectedRows > 0) {
  99. res.json({
  100. ...BaseStdResponse.OK,
  101. id: r.insertId
  102. })
  103. } else {
  104. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  105. }
  106. } catch (err) {
  107. this.logger.error(`添加乐跑账号失败!${err.stack}`)
  108. res.json({
  109. ...BaseStdResponse.ERR,
  110. msg: "添加乐跑账号失败!",
  111. });
  112. }
  113. }
  114. }
  115. module.exports.AddAccount = AddAccount;