|
|
@@ -1,4 +1,5 @@
|
|
|
const API = require("../../../lib/API.js");
|
|
|
+const axios = require("axios");
|
|
|
const db = require("../../../plugin/DataBase/db.js");
|
|
|
const { BaseStdResponse } = require("../../../BaseStdResponse.js");
|
|
|
const AccessControl = require("../../../lib/AccessControl.js");
|
|
|
@@ -14,36 +15,53 @@ class AddAccount extends API {
|
|
|
this.banEmailList = ['icloud.com']
|
|
|
}
|
|
|
|
|
|
- // 生成 6 位数字 + 字母混合码
|
|
|
- async generateCode() {
|
|
|
- try {
|
|
|
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
- let code = ""
|
|
|
- for (let i = 0; i < 6; i++) {
|
|
|
- code += chars.charAt(Math.floor(Math.random() * chars.length))
|
|
|
+ async lepaoAuth(student_num, password) {
|
|
|
+ const endpoint = "http://222.178.152.79:100/api_v1/login"
|
|
|
+ const params = new URLSearchParams()
|
|
|
+ params.append('password', password)
|
|
|
+ params.append('account', student_num)
|
|
|
+
|
|
|
+ const res = await axios.post(endpoint, params, {
|
|
|
+ proxy: false,
|
|
|
+ headers: {
|
|
|
+ "User-Agent": 'okhttp/4.9.0'
|
|
|
}
|
|
|
+ })
|
|
|
+ const data = res.data
|
|
|
+ if (!data || data.status !== 1) {
|
|
|
+ throw new Error(data?.message ?? "无法验证乐跑账号,请联系客服或稍后再试")
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
|
|
|
- let sql = 'SELECT id FROM lepao_face WHERE face_code = ?'
|
|
|
- let rows = await db.query(sql, [code])
|
|
|
- if (!rows)
|
|
|
- throw new Error('数据库错误,请稍后再试')
|
|
|
- if (rows.length > 0)
|
|
|
- return await this.generateCode()
|
|
|
- return code
|
|
|
- } catch (error) {
|
|
|
- throw error
|
|
|
+ async lepaoUserInfo(student_num) {
|
|
|
+ const params = new URLSearchParams()
|
|
|
+ params.append('account', student_num)
|
|
|
+
|
|
|
+ const endpoint = "http://222.178.152.79:100/api_v1/getUserInfo"
|
|
|
+ const res = await axios.post(endpoint, params, {
|
|
|
+ proxy: false,
|
|
|
+ headers: {
|
|
|
+ "User-Agent": 'okhttp/4.9.0'
|
|
|
+ }
|
|
|
+ })
|
|
|
+ const data = res.data
|
|
|
+ if (!data || data.status !== 1 || !data.data || !data.data.id || !data.data.nickName || !data.data.department || !data.data.frequency) {
|
|
|
+ this.logger.error(`获取乐跑用户信息失败!${data?.message ?? "未知错误"}`)
|
|
|
+ throw new Error(data?.message ?? "无法获取用户信息,请联系客服或稍后再试")
|
|
|
}
|
|
|
+ return data.data
|
|
|
}
|
|
|
|
|
|
async onRequest(req, res) {
|
|
|
- let { uuid, session, student_num, email, id, area, auto_time, auto_run, target_count, notes } = req.body
|
|
|
+ let { uuid, session, student_num, email, id, area, auto_time, auto_run, target_count, password, notes } = req.body
|
|
|
|
|
|
if ([uuid, session, student_num, email, auto_time, target_count].some(value => value === '' || value === null || value === undefined))
|
|
|
return res.json({
|
|
|
...BaseStdResponse.MISSING_PARAMETER
|
|
|
})
|
|
|
|
|
|
- if (isNaN(target_count) || target_count < 0 || target_count > 99) {
|
|
|
+ if (isNaN(target_count) || target_count < 0 || target_count > 999) {
|
|
|
return res.json({
|
|
|
...BaseStdResponse.ERR,
|
|
|
msg: '乐跑目标次数不在合法范围内'
|
|
|
@@ -51,7 +69,6 @@ class AddAccount extends API {
|
|
|
}
|
|
|
|
|
|
if (!this.emailRegex.test(email)) {
|
|
|
- Message.error('请检查邮箱格式是否正确')
|
|
|
return res.json({
|
|
|
...BaseStdResponse.ERR,
|
|
|
msg: '请检查邮箱格式是否正确'
|
|
|
@@ -78,37 +95,53 @@ class AddAccount extends API {
|
|
|
|
|
|
// 判断是否重复注册
|
|
|
if (!id) {
|
|
|
+ if (!password) {
|
|
|
+ return res.json({ ...BaseStdResponse.ERR, msg: '请输入乐跑账号密码' })
|
|
|
+ }
|
|
|
+
|
|
|
if (countRows.length !== 0 && countRows[0].create_user != null) {
|
|
|
if (countRows[0].create_user !== uuid)
|
|
|
return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
|
|
|
return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已添加' })
|
|
|
}
|
|
|
+
|
|
|
+ // 进行密码校验
|
|
|
+ try {
|
|
|
+ password = atob(password)
|
|
|
+ await this.lepaoAuth(student_num, password)
|
|
|
+ } catch (err) {
|
|
|
+ this.logger.info(`乐跑账号验证失败!${err.message}`)
|
|
|
+ return res.json({ ...BaseStdResponse.ERR, msg: err.message ?? '无法验证乐跑账号,请联系客服或稍后再试' })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (countRows.length !== 0) {
|
|
|
- if (countRows[0].total_num >= target_count && target_count !== 0)
|
|
|
+ if (auto_run && countRows[0].total_num >= target_count && target_count !== 0)
|
|
|
return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到目标次数,请尝试修改目标次数' })
|
|
|
}
|
|
|
|
|
|
const time = new Date().getTime()
|
|
|
|
|
|
- let sql, r
|
|
|
+ let sql, r, userInfo
|
|
|
|
|
|
if (!id) {
|
|
|
+ // 获取用户信息
|
|
|
+ try {
|
|
|
+ userInfo = await this.lepaoUserInfo(student_num)
|
|
|
+
|
|
|
+ if (auto_run && userInfo.frequency >= target_count && target_count !== 0)
|
|
|
+ return res.json({ ...BaseStdResponse.ERR, msg: '该账号累计跑步次数已达到目标次数,请尝试修改目标次数' })
|
|
|
+ } catch (error) {
|
|
|
+ return res.json({ ...BaseStdResponse.ERR, msg: '获取用户信息失败,请联系客服或稍后再试' })
|
|
|
+ }
|
|
|
+
|
|
|
if (countRows.length !== 0) {
|
|
|
- sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, notes = ? WHERE id = ?'
|
|
|
- r = await db.query(sql, [uuid, email, area, auto_time, auto_run, target_count, time, notes ?? '', countRows[0].id])
|
|
|
+ sql = 'UPDATE lepao_account SET create_user = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, notes = ?, total_num = ? WHERE id = ?'
|
|
|
+ r = await db.query(sql, [uuid, email, area, auto_time, auto_run, target_count, time, notes ?? '', userInfo.frequency, countRows[0].id])
|
|
|
}
|
|
|
else {
|
|
|
- const face_code = await this.generateCode()
|
|
|
-
|
|
|
- sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, auto_run, target_count, create_user, create_time, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
|
|
|
- r = await db.query(sql, [student_num, email, area, auto_time, auto_run, target_count, uuid, time, notes ?? ''])
|
|
|
-
|
|
|
- let faceSql = 'INSERT INTO lepao_face (student_num, face_code) VALUES (?, ?)'
|
|
|
- let faceRows = await db.query(faceSql, [student_num, face_code])
|
|
|
- if (!faceRows || faceRows.affectedRows !== 1)
|
|
|
- return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
|
|
|
+ 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
|
|
+ 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])
|
|
|
}
|
|
|
} else {
|
|
|
sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ? WHERE id = ?'
|
|
|
@@ -134,4 +167,4 @@ class AddAccount extends API {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-module.exports.AddAccount = AddAccount;
|
|
|
+module.exports.AddAccount = AddAccount
|