AddAccount.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. isQQ(str) {
  15. const reg = /^[1-9][0-9]{4,10}$/
  16. return reg.test(str)
  17. }
  18. async onRequest(req, res) {
  19. let { uuid, session, student_num, email, id, area, auto_time, auto_run, target_count, password, auto_day, notes } = req.body
  20. if ([uuid, session, student_num, email, auto_time, target_count, auto_day].some(value => value === '' || value === null || value === undefined))
  21. return res.json({
  22. ...BaseStdResponse.MISSING_PARAMETER
  23. })
  24. if (isNaN(target_count) || target_count < 0 || target_count > 999) {
  25. return res.json({
  26. ...BaseStdResponse.ERR,
  27. msg: '乐跑目标次数不在合法范围内'
  28. })
  29. }
  30. if (!this.emailRegex.test(email)) {
  31. return res.json({
  32. ...BaseStdResponse.ERR,
  33. msg: '请检查邮箱格式是否正确'
  34. })
  35. }
  36. const emailDomain = email.split('@')[1].toLowerCase()
  37. if (this.banEmailList.includes(emailDomain))
  38. return res.json({
  39. ...BaseStdResponse.ERR,
  40. msg: `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  41. })
  42. if (auto_run === 1 && (!Array.isArray(auto_day) || !auto_day.every(v => Number.isInteger(v) && v >= 0 && v <= 6)))
  43. return res.json({
  44. ...BaseStdResponse.ERR,
  45. msg: '自动乐跑日期格式不合法'
  46. })
  47. if (!await AccessControl.checkSession(uuid, session))
  48. return res.status(401).json({
  49. ...BaseStdResponse.ACCESS_DENIED
  50. })
  51. let countSql = 'SELECT id, create_user, total_num FROM lepao_account WHERE student_num = ?'
  52. let countRows = await db.query(countSql, [student_num])
  53. if (!countRows)
  54. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  55. // 判断是否重复注册
  56. if (!id) {
  57. if (!password) {
  58. return res.json({ ...BaseStdResponse.ERR, msg: '请输入乐跑账号密码' })
  59. }
  60. if (countRows.length !== 0 && countRows[0].create_user != null) {
  61. if (countRows[0].create_user !== uuid)
  62. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
  63. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已添加' })
  64. }
  65. // 进行密码校验
  66. try {
  67. password = atob(password)
  68. await lepaoAuth(student_num, password)
  69. } catch (err) {
  70. this.logger.info(`乐跑账号验证失败!${err.message}`)
  71. return res.json({ ...BaseStdResponse.ERR, msg: err.message ?? '无法验证乐跑账号,请联系客服或稍后再试' })
  72. }
  73. }
  74. if (countRows.length !== 0) {
  75. if (auto_run && countRows[0].total_num >= target_count && target_count !== 0)
  76. return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到目标次数,请尝试修改目标次数' })
  77. }
  78. const time = new Date().getTime()
  79. let sql, r, userInfo
  80. if (!id) {
  81. // 获取用户信息
  82. try {
  83. userInfo = await lepaoUserInfo(student_num)
  84. if (auto_run && userInfo.frequency >= target_count && target_count !== 0)
  85. return res.json({ ...BaseStdResponse.ERR, msg: `该账号累计跑步次数(${userInfo.frequency})已达到目标次数,请尝试修改目标次数` })
  86. } catch (error) {
  87. return res.json({ ...BaseStdResponse.ERR, msg: '获取用户信息失败,请联系客服或稍后再试' })
  88. }
  89. if (countRows.length !== 0) {
  90. sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, notes = ?, total_num = ?, auto_day = ?WHERE id = ?'
  91. 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])
  92. }
  93. else {
  94. let user_avatar = ''
  95. if (email.split('@')[1].toLowerCase() === 'qq.com' && this.isQQ(email.split('@')[0])) {
  96. user_avatar = `https://q2.qlogo.cn/headimg_dl?dst_uin=${email}&spec=640`
  97. } else {
  98. user_avatar = userInfo.sex === 0 ? 'https://lepao-cloud.xxoo365.top/view.php/aee85ff43fd30d0df03c6a7dd9797d22.png' : 'https://lepao-cloud.xxoo365.top/view.php/fcb54dcc5e6209381e972ef73bdb4a93.png'
  99. }
  100. 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  101. 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)])
  102. }
  103. } else {
  104. sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ?, auto_day = ? WHERE id = ?'
  105. r = await db.query(sql, [student_num, email, area, auto_time, target_count, auto_run, notes ?? '', JSON.stringify(auto_day), id])
  106. }
  107. try {
  108. if (r && r.affectedRows > 0) {
  109. res.json({
  110. ...BaseStdResponse.OK,
  111. id: r.insertId
  112. })
  113. } else {
  114. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  115. }
  116. } catch (err) {
  117. this.logger.error(`添加乐跑账号失败!${err.stack}`)
  118. res.json({
  119. ...BaseStdResponse.ERR,
  120. msg: "添加乐跑账号失败!",
  121. });
  122. }
  123. }
  124. }
  125. module.exports.AddAccount = AddAccount