AddAccount.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const API = require("../../../lib/API.js");
  2. const axios = require("axios");
  3. const db = require("../../../plugin/DataBase/db.js");
  4. const { BaseStdResponse } = require("../../../BaseStdResponse.js");
  5. const AccessControl = require("../../../lib/AccessControl.js");
  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 lepaoAuth(student_num, password) {
  15. const endpoint = "http://222.178.152.79:100/api_v1/login"
  16. const params = new URLSearchParams()
  17. params.append('password', password)
  18. params.append('account', student_num)
  19. const res = await axios.post(endpoint, params, {
  20. proxy: false,
  21. headers: {
  22. "User-Agent": 'okhttp/4.9.0'
  23. }
  24. })
  25. const data = res.data
  26. if (!data || data.status !== 1) {
  27. throw new Error(data?.message ?? "无法验证乐跑账号,请联系客服或稍后再试")
  28. }
  29. return true
  30. }
  31. async lepaoUserInfo(student_num) {
  32. const params = new URLSearchParams()
  33. params.append('account', student_num)
  34. const endpoint = "http://222.178.152.79:100/api_v1/getUserInfo"
  35. const res = await axios.post(endpoint, params, {
  36. proxy: false,
  37. headers: {
  38. "User-Agent": 'okhttp/4.9.0'
  39. }
  40. })
  41. const data = res.data
  42. if (!data || data.status !== 1 || !data.data || !data.data.id || !data.data.nickName || !data.data.department || !data.data.frequency) {
  43. this.logger.error(`获取乐跑用户信息失败!${data?.message ?? "未知错误"}`)
  44. throw new Error(data?.message ?? "无法获取用户信息,请联系客服或稍后再试")
  45. }
  46. return data.data
  47. }
  48. async onRequest(req, res) {
  49. let { uuid, session, student_num, email, id, area, auto_time, auto_run, target_count, password, notes } = req.body
  50. if ([uuid, session, student_num, email, auto_time, target_count].some(value => value === '' || value === null || value === undefined))
  51. return res.json({
  52. ...BaseStdResponse.MISSING_PARAMETER
  53. })
  54. if (isNaN(target_count) || target_count < 0 || target_count > 999) {
  55. return res.json({
  56. ...BaseStdResponse.ERR,
  57. msg: '乐跑目标次数不在合法范围内'
  58. })
  59. }
  60. if (!this.emailRegex.test(email)) {
  61. return res.json({
  62. ...BaseStdResponse.ERR,
  63. msg: '请检查邮箱格式是否正确'
  64. })
  65. }
  66. const emailDomain = email.split('@')[1].toLowerCase()
  67. if (this.banEmailList.includes(emailDomain))
  68. return res.json({
  69. ...BaseStdResponse.ERR,
  70. msg: `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  71. })
  72. if (!await AccessControl.checkSession(uuid, session))
  73. return res.status(401).json({
  74. ...BaseStdResponse.ACCESS_DENIED
  75. })
  76. let countSql = 'SELECT id, create_user, total_num FROM lepao_account WHERE student_num = ?'
  77. let countRows = await db.query(countSql, [student_num])
  78. if (!countRows)
  79. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  80. // 判断是否重复注册
  81. if (!id) {
  82. if (!password) {
  83. return res.json({ ...BaseStdResponse.ERR, msg: '请输入乐跑账号密码' })
  84. }
  85. if (countRows.length !== 0 && countRows[0].create_user != null) {
  86. if (countRows[0].create_user !== uuid)
  87. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
  88. return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已添加' })
  89. }
  90. // 进行密码校验
  91. try {
  92. password = atob(password)
  93. await this.lepaoAuth(student_num, password)
  94. } catch (err) {
  95. this.logger.info(`乐跑账号验证失败!${err.message}`)
  96. return res.json({ ...BaseStdResponse.ERR, msg: err.message ?? '无法验证乐跑账号,请联系客服或稍后再试' })
  97. }
  98. }
  99. if (countRows.length !== 0) {
  100. if (auto_run && countRows[0].total_num >= target_count && target_count !== 0)
  101. return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到目标次数,请尝试修改目标次数' })
  102. }
  103. const time = new Date().getTime()
  104. let sql, r, userInfo
  105. if (!id) {
  106. // 获取用户信息
  107. try {
  108. userInfo = await this.lepaoUserInfo(student_num)
  109. if (auto_run && userInfo.frequency >= target_count && target_count !== 0)
  110. return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到目标次数,请尝试修改目标次数' })
  111. } catch (error) {
  112. return res.json({ ...BaseStdResponse.ERR, msg: '获取用户信息失败,请联系客服或稍后再试' })
  113. }
  114. if (countRows.length !== 0) {
  115. sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, notes = ?, total_num = ? WHERE id = ?'
  116. r = await db.query(sql, [uuid, email, area, auto_time, auto_run, target_count, time, notes ?? '', userInfo.frequency, countRows[0].id])
  117. }
  118. else {
  119. 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  120. 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])
  121. }
  122. } else {
  123. sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ? WHERE id = ?'
  124. r = await db.query(sql, [student_num, email, area, auto_time, target_count, auto_run, notes ?? '', id])
  125. }
  126. try {
  127. if (r && r.affectedRows > 0) {
  128. res.json({
  129. ...BaseStdResponse.OK,
  130. id: r.insertId
  131. })
  132. } else {
  133. return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
  134. }
  135. } catch (err) {
  136. this.logger.error(`添加乐跑账号失败!${err.stack}`)
  137. res.json({
  138. ...BaseStdResponse.ERR,
  139. msg: "添加乐跑账号失败!",
  140. });
  141. }
  142. }
  143. }
  144. module.exports.AddAccount = AddAccount