const API = require("../../../lib/API.js"); const db = require("../../../plugin/DataBase/db.js"); const { BaseStdResponse } = require("../../../BaseStdResponse.js"); const AccessControl = require("../../../lib/AccessControl.js"); class AddAccount extends API { constructor() { super(); this.setPath('/Lepao/Account') this.setMethod('POST') this.emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ this.banEmailList = ['icloud.com'] } isQQ(str) { const reg = /^[1-9][0-9]{4,10}$/ return reg.test(str) } // 生成 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)) } let sql = 'SELECT id FROM lepao_extra WHERE bind_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 onRequest(req, res) { let { uuid, session, student_num, email, id, area, auto_time, auto_run, target_count, auto_day, notice_type, notes } = req.body if ([uuid, session, student_num, auto_time, target_count, auto_day].some(value => value === '' || value === null || value === undefined)) return res.json({ ...BaseStdResponse.MISSING_PARAMETER }) const targetKm = Number(target_count) if (!Number.isFinite(targetKm) || targetKm < 0 || targetKm > 200) { return res.json({ ...BaseStdResponse.ERR, msg: '乐跑目标里程(公里)不在合法范围内(0–200)' }) } if (notice_type === 'email') { if (!this.emailRegex.test(email)) { return res.json({ ...BaseStdResponse.ERR, msg: '请检查邮箱格式是否正确' }) } const emailDomain = email.split('@')[1].toLowerCase() if (this.banEmailList.includes(emailDomain)) return res.json({ ...BaseStdResponse.ERR, msg: `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试` }) } if (auto_run === 1 && (!Array.isArray(auto_day) || !auto_day.every(v => Number.isInteger(v) && v >= 0 && v <= 6))) return res.json({ ...BaseStdResponse.ERR, msg: '自动乐跑日期格式不合法' }) if (!await AccessControl.checkSession(uuid, session)) return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED }) let countSql = 'SELECT id, create_user, total_num, term_num FROM lepao_account WHERE student_num = ?' let countRows = await db.query(countSql, [student_num]) if (!countRows) return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' }) // 判断是否重复注册 if (!id) { 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: '该乐跑账号您已绑定' }) } } if (countRows.length !== 0) { if (auto_run === 1 && countRows[0].term_num >= targetKm && targetKm !== 0) return res.json({ ...BaseStdResponse.ERR, msg: '该账号本月跑步里程已达到或超过预设目标里程,请增大目标后再试' }) } const time = new Date().getTime() let sql, r, user_avatar // 生成用户头像 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' } 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]) } 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]) let faceSql = 'INSERT INTO lepao_extra (student_num, bind_code) VALUES (?, ?)' let faceRows = await db.query(faceSql, [student_num, bind_code]) if (!faceRows || faceRows.affectedRows !== 1) 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]) } try { if (r && r.affectedRows > 0) { const selectSql = ` SELECT a.id, a.create_user, a.total_num, e.bind_code, e.bot_account FROM lepao_account a LEFT JOIN lepao_extra e ON a.student_num = e.student_num WHERE a.student_num = ? ` const selectRows = await db.query(selectSql, [student_num]) if (!selectRows) return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' }) res.json({ ...BaseStdResponse.OK, id: r.insertId, data: { student_num, email, id, area, auto_time, auto_run, target_count: targetKm, auto_day, notice_type, notes, bind_code: selectRows.length !== 0 ? selectRows[0].bind_code : undefined, bot_account: selectRows.length !== 0 ? selectRows[0].bot_account : undefined } }) } else { return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' }) } } catch (err) { this.logger.error(`添加乐跑账号失败!${err.stack}`) res.json({ ...BaseStdResponse.ERR, msg: "添加乐跑账号失败!", }); } } } module.exports.AddAccount = AddAccount;