AddAccount.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, auto_day, notes } = req.body
  16. if ([uuid, session, student_num, email, auto_time, target_count, auto_day].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 (auto_run === 1 && (!Array.isArray(auto_day) || !auto_day.every(v => Number.isInteger(v) && v >= 0 && v <= 6)))
  39. return res.json({
  40. ...BaseStdResponse.ERR,
  41. msg: '自动乐跑日期格式不合法'
  42. })
  43. if (!await AccessControl.checkSession(uuid, session))
  44. return res.status(401).json({
  45. ...BaseStdResponse.ACCESS_DENIED
  46. })
  47. let countSql = 'SELECT id, create_user, total_num FROM lepao_account WHERE student_num = ?'
  48. let countRows = await db.query(countSql, [student_num])
  49. if (!countRows)
  50. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  51. // 判断是否重复注册
  52. if (!id) {
  53. if (!password) {
  54. return res.json({ ...BaseStdResponse.ERR, msg: '请输入乐跑账号密码' })
  55. }
  56. if (countRows.length !== 0 && countRows[0].create_user != null) {
  57. if (countRows[0].create_user !== uuid)
  58. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
  59. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已添加' })
  60. }
  61. // 进行密码校验
  62. try {
  63. password = atob(password)
  64. await lepaoAuth(student_num, password)
  65. } catch (err) {
  66. this.logger.info(`乐跑账号验证失败!${err.message}`)
  67. return res.json({ ...BaseStdResponse.ERR, msg: err.message ?? '无法验证乐跑账号,请联系客服或稍后再试' })
  68. }
  69. }
  70. if (countRows.length !== 0) {
  71. if (auto_run && countRows[0].total_num >= target_count && target_count !== 0)
  72. return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到目标次数,请尝试修改目标次数' })
  73. }
  74. const time = new Date().getTime()
  75. let sql, r, userInfo
  76. if (!id) {
  77. // 获取用户信息
  78. try {
  79. userInfo = await lepaoUserInfo(student_num)
  80. if (auto_run && userInfo.frequency >= target_count && target_count !== 0)
  81. return res.json({ ...BaseStdResponse.ERR, msg: `该账号累计跑步次数(${userInfo.frequency})已达到目标次数,请尝试修改目标次数` })
  82. } catch (error) {
  83. return res.json({ ...BaseStdResponse.ERR, msg: '获取用户信息失败,请联系客服或稍后再试' })
  84. }
  85. if (countRows.length !== 0) {
  86. sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, notes = ?, total_num = ?, auto_day = ?WHERE id = ?'
  87. r = await db.query(sql, [uuid, email, area, auto_time, auto_run, target_count, time, notes ?? '', userInfo.frequency, JSON.stringify(auto_day), countRows[0].id])
  88. }
  89. else {
  90. const user_avatar = userInfo.sex === 0 ? 'https://lepao-cloud.xxoo365.top/view.php/aee85ff43fd30d0df03c6a7dd9797d22.png' : 'https://lepao-cloud.xxoo365.top/view.php/fcb54dcc5e6209381e972ef73bdb4a93.png'
  91. sql = 'INSERT INTO lepao_account (student_num, name, user_avatar, grade_id, uid, sex, total_num, email, area, auto_time, auto_run, target_count, create_user, create_time, notes, password, auto_day) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  92. r = await db.query(sql, [student_num, userInfo.nickName, user_avatar, userInfo.department, userInfo.id, userInfo.sex, userInfo.frequency, email, area, auto_time, auto_run, target_count, uuid, time, notes ?? '', password, JSON.stringify(auto_day)])
  93. }
  94. } else {
  95. sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ?, auto_day = ? WHERE id = ?'
  96. r = await db.query(sql, [student_num, email, area, auto_time, target_count, auto_run, notes ?? '', JSON.stringify(auto_day), id])
  97. }
  98. try {
  99. if (r && r.affectedRows > 0) {
  100. res.json({
  101. ...BaseStdResponse.OK,
  102. id: r.insertId
  103. })
  104. } else {
  105. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  106. }
  107. } catch (err) {
  108. this.logger.error(`添加乐跑账号失败!${err.stack}`)
  109. res.json({
  110. ...BaseStdResponse.ERR,
  111. msg: "添加乐跑账号失败!",
  112. });
  113. }
  114. }
  115. }
  116. module.exports.AddAccount = AddAccount