| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- const API = require("../../lib/API.js");
- const db = require("../../plugin/DataBase/db.js");
- const { BaseStdResponse } = require("../../BaseStdResponse.js");
- const AccessControl = require("../../lib/AccessControl");
- class AddAccount extends API {
- constructor() {
- super();
- this.setPath('/Lepao/Account')
- this.setMethod('POST')
- }
- async onRequest(req, res) {
- let { uuid, session, student_num, email, id, area, max_distance, min_distance, auto_time } = req.body
- if ([uuid, session, student_num, email, auto_time].some(value => value === '' || value === null || value === undefined))
- return res.json({
- ...BaseStdResponse.MISSING_PARAMETER,
- endpoint: 1513126
- })
- if (!await AccessControl.checkSession(uuid, session))
- return res.status(401).json({
- ...BaseStdResponse.ACCESS_DENIED
- })
- // 判断是否重复注册
- if (!id) {
- let countSql = 'SELECT create_user FROM lepao_account WHERE student_num = ?'
- let countRows = await db.query(countSql, [student_num])
- if (!countRows)
- return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
- if (countRows.length !== 0) {
- if (countRows[0].create_user !== uuid)
- return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已被其他用户绑定,请联系客服解绑' })
- return res.json({ ...BaseStdResponse.ERR, msg: '该乐跑账号已添加' })
- }
- }
- // 判断用户是否用了会员权益
- // let userSql = 'SELECT vip FROM users WHERE uuid = ?'
- // let userData = await db.query(userSql, [uuid])
- // if (!userData || userData.length !== 1)
- // return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
- // if (userData[0].vip !== 1) {
- // 限制账号个数
- // let numSql = 'SELECT COUNT(*) AS num FROM lepao_account WHERE create_user = ?'
- // let numRows = await db.query(numSql, [uuid])
- // if(!numRows)
- // return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
- // if(numRows[0].num >= 6)
- // return res.json({...BaseStdResponse.NOTVIP, msg: '非VIP用户最多只能添加6个乐跑账号,请先开通VIP'})
- // if(min_distance != 2.00 || max_distance != 4.00)
- // return res.json({ ...BaseStdResponse.NOTVIP, msg: '仅VIP用户可设置跑步距离区间' })
- // 限制跑区
- // if (area != '')
- // return res.json({ ...BaseStdResponse.NOTVIP, msg: '仅VIP用户可指定跑区' })
- // }
- const time = new Date().getTime()
- let sql, r
- if (!id) {
- sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, create_user, create_time) VALUES (?, ?, ?, ?, ?, ?)'
- r = await db.query(sql, [student_num, email, area, auto_time, uuid, time])
- } else {
- sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, update_time = ? WHERE id = ?'
- r = await db.query(sql, [student_num, email, area, auto_time, time, id])
- }
- try {
- if (r && r.affectedRows > 0) {
- res.json({
- ...BaseStdResponse.OK,
- id: r.insertId
- })
- } else {
- return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
- }
- } catch (err) {
- this.logger.error(`添加乐跑账号失败!${err.stack}`)
- res.json({
- ...BaseStdResponse.ERR,
- msg: "添加乐跑账号失败!",
- });
- }
- }
- }
- module.exports.AddAccount = AddAccount;
|