AddAccount.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. const targetKm = Number(target_count)
  39. if (!Number.isFinite(targetKm) || targetKm < 0 || targetKm > 200) {
  40. return res.json({
  41. ...BaseStdResponse.ERR,
  42. msg: '乐跑目标里程(公里)不在合法范围内(0–200)'
  43. })
  44. }
  45. if (notice_type === 'email') {
  46. if (!this.emailRegex.test(email)) {
  47. return res.json({
  48. ...BaseStdResponse.ERR,
  49. msg: '请检查邮箱格式是否正确'
  50. })
  51. }
  52. const emailDomain = email.split('@')[1].toLowerCase()
  53. if (this.banEmailList.includes(emailDomain))
  54. return res.json({
  55. ...BaseStdResponse.ERR,
  56. msg: `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  57. })
  58. }
  59. if (auto_run === 1 && (!Array.isArray(auto_day) || !auto_day.every(v => Number.isInteger(v) && v >= 0 && v <= 6)))
  60. return res.json({
  61. ...BaseStdResponse.ERR,
  62. msg: '自动乐跑日期格式不合法'
  63. })
  64. if (!await AccessControl.checkSession(uuid, session))
  65. return res.status(401).json({
  66. ...BaseStdResponse.ACCESS_DENIED
  67. })
  68. let countSql = 'SELECT id, create_user, total_num, term_num FROM lepao_account WHERE student_num = ?'
  69. let countRows = await db.query(countSql, [student_num])
  70. if (!countRows)
  71. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  72. // 判断是否重复注册
  73. if (!id) {
  74. if (countRows.length !== 0 && countRows[0].create_user != null) {
  75. if (countRows[0].create_user !== uuid)
  76. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
  77. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号您已绑定' })
  78. }
  79. }
  80. if (countRows.length !== 0) {
  81. if (auto_run === 1 && countRows[0].term_num >= targetKm && targetKm !== 0)
  82. return res.json({
  83. ...BaseStdResponse.ERR,
  84. msg: '该账号本月跑步里程已达到或超过预设目标里程,请增大目标后再试'
  85. })
  86. }
  87. const time = new Date().getTime()
  88. let sql, r, user_avatar
  89. // 生成用户头像
  90. if (email.split('@')[1].toLowerCase() === 'qq.com' && this.isQQ(email.split('@')[0])) {
  91. user_avatar = `https://q2.qlogo.cn/headimg_dl?dst_uin=${email}&spec=640`
  92. } else {
  93. user_avatar = userInfo.sex === 0 ? 'https://lepao-cloud.xxoo365.top/view.php/aee85ff43fd30d0df03c6a7dd9797d22.png' : 'https://lepao-cloud.xxoo365.top/view.php/fcb54dcc5e6209381e972ef73bdb4a93.png'
  94. }
  95. if (!id) {
  96. if (countRows.length !== 0) {
  97. sql = 'UPDATE lepao_account SET create_user = ?, email = ?, user_avatar = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, update_time = ?, notes = ?, auto_day = ?, notice_type = ? WHERE id = ?'
  98. r = await db.query(sql, [uuid, email ?? '', user_avatar ?? '', area, auto_time, auto_run, targetKm, time, time, notes ?? '', JSON.stringify(auto_day), notice_type, countRows[0].id])
  99. }
  100. else {
  101. const bind_code = await this.generateCode()
  102. sql = 'INSERT INTO lepao_account (student_num, email, user_avatar, area, auto_time, auto_run, target_count, create_user, create_time, notes, auto_day, notice_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  103. r = await db.query(sql, [student_num, email ?? '', user_avatar ?? '', area, auto_time, auto_run, targetKm, uuid, time, notes ?? '', JSON.stringify(auto_day), notice_type])
  104. let faceSql = 'INSERT INTO lepao_extra (student_num, bind_code) VALUES (?, ?)'
  105. let faceRows = await db.query(faceSql, [student_num, bind_code])
  106. if (!faceRows || faceRows.affectedRows !== 1)
  107. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  108. }
  109. } else {
  110. sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ?, auto_day = ?, update_time = ?, notice_type = ? WHERE id = ?'
  111. r = await db.query(sql, [student_num, email ?? '', area, auto_time, targetKm, auto_run, notes ?? '', JSON.stringify(auto_day), time, notice_type, id])
  112. }
  113. try {
  114. if (r && r.affectedRows > 0) {
  115. const selectSql = `
  116. SELECT
  117. a.id, a.create_user, a.total_num, e.bind_code, e.bot_account
  118. FROM
  119. lepao_account a
  120. LEFT JOIN
  121. lepao_extra e
  122. ON
  123. a.student_num = e.student_num
  124. WHERE
  125. a.student_num = ?
  126. `
  127. const selectRows = await db.query(selectSql, [student_num])
  128. if (!selectRows)
  129. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  130. res.json({
  131. ...BaseStdResponse.OK,
  132. id: r.insertId,
  133. data: {
  134. student_num, email, id, area, auto_time, auto_run, target_count: targetKm, auto_day, notice_type, notes,
  135. bind_code: selectRows.length !== 0 ? selectRows[0].bind_code : undefined,
  136. bot_account: selectRows.length !== 0 ? selectRows[0].bot_account : undefined
  137. }
  138. })
  139. } else {
  140. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  141. }
  142. } catch (err) {
  143. this.logger.error(`添加乐跑账号失败!${err.stack}`)
  144. res.json({
  145. ...BaseStdResponse.ERR,
  146. msg: "添加乐跑账号失败!",
  147. });
  148. }
  149. }
  150. }
  151. module.exports.AddAccount = AddAccount;