AddAccount.js 5.7 KB

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