AddAccount.js 8.1 KB

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