AddAccount.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_extra WHERE bind_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, auto_day, notice_type, notes } = req.body
  34. if ([uuid, session, student_num, auto_time, target_count, auto_day].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 (notice_type === 'email') {
  45. if (!this.emailRegex.test(email)) {
  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. }
  58. if (auto_run === 1 && (!Array.isArray(auto_day) || !auto_day.every(v => Number.isInteger(v) && v >= 0 && v <= 6)))
  59. return res.json({
  60. ...BaseStdResponse.ERR,
  61. msg: '自动乐跑日期格式不合法'
  62. })
  63. if (!await AccessControl.checkSession(uuid, session))
  64. return res.status(401).json({
  65. ...BaseStdResponse.ACCESS_DENIED
  66. })
  67. let countSql = 'SELECT id, create_user, total_num FROM lepao_account WHERE student_num = ?'
  68. let countRows = await db.query(countSql, [student_num])
  69. if (!countRows)
  70. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  71. // 判断是否重复注册
  72. if (!id) {
  73. if (countRows.length !== 0 && countRows[0].create_user != null) {
  74. if (countRows[0].create_user !== uuid)
  75. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
  76. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号您已绑定' })
  77. }
  78. }
  79. if (countRows.length !== 0) {
  80. if (auto_run === 1 && countRows[0].total_num >= target_count && target_count !== 0)
  81. return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到预设目标次数,请尝试增大目标次数后再试' })
  82. }
  83. const time = new Date().getTime()
  84. let sql, r
  85. if (!id) {
  86. if (countRows.length !== 0) {
  87. sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, update_time = ?, notes = ?, auto_day = ?, notice_type = ? WHERE id = ?'
  88. r = await db.query(sql, [uuid, email ?? '', area, auto_time, auto_run, target_count, time, time, notes ?? '', JSON.stringify(auto_day), notice_type, countRows[0].id])
  89. }
  90. else {
  91. const bind_code = await this.generateCode()
  92. sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, auto_run, target_count, create_user, create_time, notes, auto_day, notice_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  93. r = await db.query(sql, [student_num, email ?? '', area, auto_time, auto_run, target_count, uuid, time, notes ?? '', JSON.stringify(auto_day), notice_type])
  94. let faceSql = 'INSERT INTO lepao_extra (student_num, bind_code) VALUES (?, ?)'
  95. let faceRows = await db.query(faceSql, [student_num, bind_code])
  96. if (!faceRows || faceRows.affectedRows !== 1)
  97. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  98. }
  99. } else {
  100. sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ?, auto_day = ?, update_time = ?, notice_type = ? WHERE id = ?'
  101. r = await db.query(sql, [student_num, email ?? '', area, auto_time, target_count, auto_run, notes ?? '', JSON.stringify(auto_day), time, notice_type, id])
  102. }
  103. try {
  104. if (r && r.affectedRows > 0) {
  105. const selectSql = `
  106. SELECT
  107. a.id, a.create_user, a.total_num, e.bind_code, e.bot_account
  108. FROM
  109. lepao_account a
  110. LEFT JOIN
  111. lepao_extra e
  112. ON
  113. a.student_num = e.student_num
  114. WHERE
  115. a.student_num = ?
  116. `
  117. const selectRows = await db.query(selectSql, [student_num])
  118. if (!selectRows)
  119. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  120. res.json({
  121. ...BaseStdResponse.OK,
  122. id: r.insertId,
  123. data: {
  124. student_num, email, id, area, auto_time, auto_run, target_count, auto_day, notice_type, notes,
  125. bind_code: selectRows.length !== 0 ? selectRows[0].bind_code : undefined,
  126. bot_account: selectRows.length !== 0 ? selectRows[0].bot_account : undefined
  127. }
  128. })
  129. } else {
  130. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  131. }
  132. } catch (err) {
  133. this.logger.error(`添加乐跑账号失败!${err.stack}`)
  134. res.json({
  135. ...BaseStdResponse.ERR,
  136. msg: "添加乐跑账号失败!",
  137. });
  138. }
  139. }
  140. }
  141. module.exports.AddAccount = AddAccount;