AddAccount.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. return res.json({
  46. ...BaseStdResponse.ERR,
  47. msg: '请检查邮箱格式是否正确'
  48. })
  49. }
  50. const emailDomain = email.split('@')[1].toLowerCase()
  51. if (this.banEmailList.includes(emailDomain))
  52. return res.json({
  53. ...BaseStdResponse.ERR,
  54. msg: `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  55. })
  56. if (!await AccessControl.checkSession(uuid, session))
  57. return res.status(401).json({
  58. ...BaseStdResponse.ACCESS_DENIED
  59. })
  60. let countSql = 'SELECT id, create_user, total_num FROM lepao_account WHERE student_num = ?'
  61. let countRows = await db.query(countSql, [student_num])
  62. if (!countRows)
  63. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  64. // 判断是否重复注册
  65. if (!id) {
  66. if (countRows.length !== 0 && countRows[0].create_user != null) {
  67. if (countRows[0].create_user !== uuid)
  68. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
  69. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号您已绑定' })
  70. }
  71. }
  72. if (countRows.length !== 0) {
  73. if (auto_run === 1 && countRows[0].total_num >= target_count && target_count !== 0)
  74. return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到预设目标次数,请尝试增大目标次数后再试' })
  75. }
  76. const time = new Date().getTime()
  77. let sql, r
  78. if (!id) {
  79. if (countRows.length !== 0) {
  80. sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, notes = ? WHERE id = ?'
  81. r = await db.query(sql, [uuid, email, area, auto_time, auto_run, target_count, time, notes ?? '', countRows[0].id])
  82. }
  83. else {
  84. const face_code = await this.generateCode()
  85. sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, auto_run, target_count, create_user, create_time, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
  86. r = await db.query(sql, [student_num, email, area, auto_time, auto_run, target_count, uuid, time, notes ?? ''])
  87. let faceSql = 'INSERT INTO lepao_face (student_num, face_code) VALUES (?, ?)'
  88. let faceRows = await db.query(faceSql, [student_num, face_code])
  89. if (!faceRows || faceRows.affectedRows !== 1)
  90. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  91. }
  92. } else {
  93. sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ? WHERE id = ?'
  94. r = await db.query(sql, [student_num, email, area, auto_time, target_count, auto_run, notes ?? '', id])
  95. }
  96. try {
  97. if (r && r.affectedRows > 0) {
  98. res.json({
  99. ...BaseStdResponse.OK,
  100. id: r.insertId
  101. })
  102. } else {
  103. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  104. }
  105. } catch (err) {
  106. this.logger.error(`添加乐跑账号失败!${err.stack}`)
  107. res.json({
  108. ...BaseStdResponse.ERR,
  109. msg: "添加乐跑账号失败!",
  110. });
  111. }
  112. }
  113. }
  114. module.exports.AddAccount = AddAccount;