|
|
@@ -2,6 +2,17 @@ const API = require("../../../lib/API.js");
|
|
|
const db = require("../../../plugin/DataBase/db.js");
|
|
|
const { BaseStdResponse } = require("../../../BaseStdResponse.js");
|
|
|
const AccessControl = require("../../../lib/AccessControl.js");
|
|
|
+const { getJkesSettings } = require("../../../plugin/jkes/jkesSettings.js");
|
|
|
+const {
|
|
|
+ DEFAULT_AUTO_RUN_DISTANCE_KM,
|
|
|
+ validateAutoRunPresetForSave
|
|
|
+} = require("../../../plugin/jkes/autoRunAccountOptions.js");
|
|
|
+
|
|
|
+function pickBodyOrDb(bodyVal, dbVal, fallback) {
|
|
|
+ if (bodyVal !== undefined && bodyVal !== null && bodyVal !== "") return bodyVal;
|
|
|
+ if (dbVal !== undefined && dbVal !== null && dbVal !== "") return dbVal;
|
|
|
+ return fallback;
|
|
|
+}
|
|
|
|
|
|
class AddAccount extends API {
|
|
|
constructor() {
|
|
|
@@ -41,7 +52,25 @@ class AddAccount extends API {
|
|
|
}
|
|
|
|
|
|
async onRequest(req, res) {
|
|
|
- let { uuid, session, student_num, email, id, area, auto_time, auto_run, target_count, auto_day, notice_type, notes } = req.body
|
|
|
+ let {
|
|
|
+ uuid,
|
|
|
+ session,
|
|
|
+ student_num,
|
|
|
+ email,
|
|
|
+ id,
|
|
|
+ area,
|
|
|
+ auto_time,
|
|
|
+ auto_run,
|
|
|
+ target_count,
|
|
|
+ auto_day,
|
|
|
+ notice_type,
|
|
|
+ notes,
|
|
|
+ auto_run_distance_km,
|
|
|
+ auto_run_distance_min_km,
|
|
|
+ auto_run_distance_max_km,
|
|
|
+ pace_min_sec_per_km,
|
|
|
+ pace_max_sec_per_km
|
|
|
+ } = req.body
|
|
|
|
|
|
if ([uuid, session, student_num, auto_time, target_count, auto_day].some(value => value === '' || value === null || value === undefined))
|
|
|
return res.json({
|
|
|
@@ -83,12 +112,88 @@ class AddAccount extends API {
|
|
|
...BaseStdResponse.ACCESS_DENIED
|
|
|
})
|
|
|
|
|
|
- let countSql = 'SELECT id, create_user, total_num, term_num FROM lepao_account WHERE student_num = ?'
|
|
|
+ let mergeForPreset = null
|
|
|
+ if (id) {
|
|
|
+ const ownerRows = await db.query(
|
|
|
+ "SELECT auto_run_distance_min_km, auto_run_distance_max_km, pace_min_sec_per_km, pace_max_sec_per_km FROM lepao_account WHERE id = ? AND create_user = ?",
|
|
|
+ [id, uuid]
|
|
|
+ );
|
|
|
+ if (!ownerRows || ownerRows.length === 0) {
|
|
|
+ return res.json({ ...BaseStdResponse.ERR, msg: "未找到该乐跑账号或无权限编辑" });
|
|
|
+ }
|
|
|
+ mergeForPreset = ownerRows[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ let countSql =
|
|
|
+ "SELECT id, create_user, total_num, term_num, auto_run_distance_min_km, auto_run_distance_max_km, pace_min_sec_per_km, pace_max_sec_per_km FROM lepao_account WHERE student_num = ?";
|
|
|
let countRows = await db.query(countSql, [student_num])
|
|
|
|
|
|
if (!countRows)
|
|
|
return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
|
|
|
|
|
|
+ if (!id && countRows.length > 0) {
|
|
|
+ mergeForPreset = countRows[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ const cfg = getJkesSettings();
|
|
|
+ const paceDefLo = cfg.paceRandomMinSecPerKm ?? 180;
|
|
|
+ const paceDefHi = cfg.paceRandomMaxSecPerKm ?? 600;
|
|
|
+ const minMissing =
|
|
|
+ auto_run_distance_min_km === undefined ||
|
|
|
+ auto_run_distance_min_km === null ||
|
|
|
+ auto_run_distance_min_km === ''
|
|
|
+ const maxMissing =
|
|
|
+ auto_run_distance_max_km === undefined ||
|
|
|
+ auto_run_distance_max_km === null ||
|
|
|
+ auto_run_distance_max_km === ''
|
|
|
+ const legacyOnly =
|
|
|
+ minMissing &&
|
|
|
+ maxMissing &&
|
|
|
+ auto_run_distance_km !== undefined &&
|
|
|
+ auto_run_distance_km !== null &&
|
|
|
+ auto_run_distance_km !== ''
|
|
|
+
|
|
|
+ const presetBody = legacyOnly
|
|
|
+ ? {
|
|
|
+ auto_run_distance_km,
|
|
|
+ pace_min_sec_per_km: pickBodyOrDb(
|
|
|
+ pace_min_sec_per_km,
|
|
|
+ mergeForPreset?.pace_min_sec_per_km,
|
|
|
+ paceDefLo
|
|
|
+ ),
|
|
|
+ pace_max_sec_per_km: pickBodyOrDb(
|
|
|
+ pace_max_sec_per_km,
|
|
|
+ mergeForPreset?.pace_max_sec_per_km,
|
|
|
+ paceDefHi
|
|
|
+ )
|
|
|
+ }
|
|
|
+ : {
|
|
|
+ auto_run_distance_min_km: pickBodyOrDb(
|
|
|
+ auto_run_distance_min_km,
|
|
|
+ mergeForPreset?.auto_run_distance_min_km,
|
|
|
+ DEFAULT_AUTO_RUN_DISTANCE_KM
|
|
|
+ ),
|
|
|
+ auto_run_distance_max_km: pickBodyOrDb(
|
|
|
+ auto_run_distance_max_km,
|
|
|
+ mergeForPreset?.auto_run_distance_max_km,
|
|
|
+ DEFAULT_AUTO_RUN_DISTANCE_KM
|
|
|
+ ),
|
|
|
+ pace_min_sec_per_km: pickBodyOrDb(
|
|
|
+ pace_min_sec_per_km,
|
|
|
+ mergeForPreset?.pace_min_sec_per_km,
|
|
|
+ paceDefLo
|
|
|
+ ),
|
|
|
+ pace_max_sec_per_km: pickBodyOrDb(
|
|
|
+ pace_max_sec_per_km,
|
|
|
+ mergeForPreset?.pace_max_sec_per_km,
|
|
|
+ paceDefHi
|
|
|
+ )
|
|
|
+ }
|
|
|
+ const preset = validateAutoRunPresetForSave(presetBody);
|
|
|
+ if (!preset.ok) {
|
|
|
+ return res.json({ ...BaseStdResponse.ERR, msg: preset.msg });
|
|
|
+ }
|
|
|
+
|
|
|
// 判断是否重复注册
|
|
|
if (!id) {
|
|
|
if (countRows.length !== 0 && countRows[0].create_user != null) {
|
|
|
@@ -113,19 +218,60 @@ class AddAccount extends API {
|
|
|
if (email && email.split('@')[1].toLowerCase() === 'qq.com' && this.isQQ(email.split('@')[0])) {
|
|
|
user_avatar = `https://q2.qlogo.cn/headimg_dl?dst_uin=${email}&spec=640`
|
|
|
} else {
|
|
|
- user_avatar = userInfo.sex === 1 ? 'https://lepao-cloud.xxoo365.top/view.php/aee85ff43fd30d0df03c6a7dd9797d22.png' : 'https://lepao-cloud.xxoo365.top/view.php/fcb54dcc5e6209381e972ef73bdb4a93.png'
|
|
|
+ let userSex = 2;
|
|
|
+ const urows = await db.query("SELECT sex FROM users WHERE uuid = ? LIMIT 1", [uuid]);
|
|
|
+ if (urows?.[0]?.sex === 1) userSex = 1;
|
|
|
+ user_avatar =
|
|
|
+ userSex === 1
|
|
|
+ ? "https://lepao-cloud.xxoo365.top/view.php/aee85ff43fd30d0df03c6a7dd9797d22.png"
|
|
|
+ : "https://lepao-cloud.xxoo365.top/view.php/fcb54dcc5e6209381e972ef73bdb4a93.png";
|
|
|
}
|
|
|
|
|
|
if (!id) {
|
|
|
if (countRows.length !== 0) {
|
|
|
- sql = 'UPDATE lepao_account SET create_user = ?, email = ?, user_avatar = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, update_time = ?, notes = ?, auto_day = ?, notice_type = ? WHERE id = ?'
|
|
|
- r = await db.query(sql, [uuid, email ?? '', user_avatar ?? '', area, auto_time, auto_run, targetKm, time, time, notes ?? '', JSON.stringify(auto_day), notice_type, countRows[0].id])
|
|
|
+ sql = "UPDATE lepao_account SET create_user = ?, email = ?, user_avatar = ?, area = ?, auto_time = ?, auto_run = ?, target_count = ?, create_time = ?, update_time = ?, notes = ?, auto_day = ?, notice_type = ?, auto_run_distance_min_km = ?, auto_run_distance_max_km = ?, pace_min_sec_per_km = ?, pace_max_sec_per_km = ? WHERE id = ?";
|
|
|
+ r = await db.query(sql, [
|
|
|
+ uuid,
|
|
|
+ email ?? "",
|
|
|
+ user_avatar ?? "",
|
|
|
+ area,
|
|
|
+ auto_time,
|
|
|
+ auto_run,
|
|
|
+ targetKm,
|
|
|
+ time,
|
|
|
+ time,
|
|
|
+ notes ?? "",
|
|
|
+ JSON.stringify(auto_day),
|
|
|
+ notice_type,
|
|
|
+ preset.autoRunDistanceMinKm,
|
|
|
+ preset.autoRunDistanceMaxKm,
|
|
|
+ preset.paceMinSecPerKm,
|
|
|
+ preset.paceMaxSecPerKm,
|
|
|
+ countRows[0].id
|
|
|
+ ]);
|
|
|
}
|
|
|
else {
|
|
|
const bind_code = await this.generateCode()
|
|
|
|
|
|
- sql = 'INSERT INTO lepao_account (student_num, email, user_avatar, area, auto_time, auto_run, target_count, create_user, create_time, notes, auto_day, notice_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
|
|
- r = await db.query(sql, [student_num, email ?? '', user_avatar ?? '', area, auto_time, auto_run, targetKm, uuid, time, notes ?? '', JSON.stringify(auto_day), notice_type])
|
|
|
+ sql = "INSERT INTO lepao_account (student_num, email, user_avatar, area, auto_time, auto_run, target_count, create_user, create_time, notes, auto_day, notice_type, auto_run_distance_min_km, auto_run_distance_max_km, pace_min_sec_per_km, pace_max_sec_per_km) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
+ r = await db.query(sql, [
|
|
|
+ student_num,
|
|
|
+ email ?? "",
|
|
|
+ user_avatar ?? "",
|
|
|
+ area,
|
|
|
+ auto_time,
|
|
|
+ auto_run,
|
|
|
+ targetKm,
|
|
|
+ uuid,
|
|
|
+ time,
|
|
|
+ notes ?? "",
|
|
|
+ JSON.stringify(auto_day),
|
|
|
+ notice_type,
|
|
|
+ preset.autoRunDistanceMinKm,
|
|
|
+ preset.autoRunDistanceMaxKm,
|
|
|
+ preset.paceMinSecPerKm,
|
|
|
+ preset.paceMaxSecPerKm
|
|
|
+ ]);
|
|
|
|
|
|
let faceSql = 'INSERT INTO lepao_extra (student_num, bind_code) VALUES (?, ?)'
|
|
|
let faceRows = await db.query(faceSql, [student_num, bind_code])
|
|
|
@@ -133,8 +279,25 @@ class AddAccount extends API {
|
|
|
return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
|
|
|
}
|
|
|
} else {
|
|
|
- sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ?, auto_day = ?, update_time = ?, notice_type = ? WHERE id = ?'
|
|
|
- r = await db.query(sql, [student_num, email ?? '', area, auto_time, targetKm, auto_run, notes ?? '', JSON.stringify(auto_day), time, notice_type, id])
|
|
|
+ sql = "UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, target_count = ?, auto_run = ?, notes = ?, auto_day = ?, update_time = ?, notice_type = ?, auto_run_distance_min_km = ?, auto_run_distance_max_km = ?, pace_min_sec_per_km = ?, pace_max_sec_per_km = ? WHERE id = ? AND create_user = ?";
|
|
|
+ r = await db.query(sql, [
|
|
|
+ student_num,
|
|
|
+ email ?? "",
|
|
|
+ area,
|
|
|
+ auto_time,
|
|
|
+ targetKm,
|
|
|
+ auto_run,
|
|
|
+ notes ?? "",
|
|
|
+ JSON.stringify(auto_day),
|
|
|
+ time,
|
|
|
+ notice_type,
|
|
|
+ preset.autoRunDistanceMinKm,
|
|
|
+ preset.autoRunDistanceMaxKm,
|
|
|
+ preset.paceMinSecPerKm,
|
|
|
+ preset.paceMaxSecPerKm,
|
|
|
+ id,
|
|
|
+ uuid
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
@@ -159,7 +322,20 @@ class AddAccount extends API {
|
|
|
...BaseStdResponse.OK,
|
|
|
id: r.insertId,
|
|
|
data: {
|
|
|
- student_num, email, id, area, auto_time, auto_run, target_count: targetKm, auto_day, notice_type, notes,
|
|
|
+ student_num,
|
|
|
+ email,
|
|
|
+ id,
|
|
|
+ area,
|
|
|
+ auto_time,
|
|
|
+ auto_run,
|
|
|
+ target_count: targetKm,
|
|
|
+ auto_day,
|
|
|
+ notice_type,
|
|
|
+ notes,
|
|
|
+ auto_run_distance_min_km: preset.autoRunDistanceMinKm,
|
|
|
+ auto_run_distance_max_km: preset.autoRunDistanceMaxKm,
|
|
|
+ pace_min_sec_per_km: preset.paceMinSecPerKm,
|
|
|
+ pace_max_sec_per_km: preset.paceMaxSecPerKm,
|
|
|
bind_code: selectRows.length !== 0 ? selectRows[0].bind_code : undefined,
|
|
|
bot_account: selectRows.length !== 0 ? selectRows[0].bot_account : undefined
|
|
|
}
|