const axios = require('axios') const db = require('../../plugin/DataBase/db') const Logger = require('../Logger') const path = require('path') const EmailTemplate = require('../../plugin/Email/emailTemplate') const config = require('../../config.json') class Lepao { constructor() { this.logger = new Logger(path.join(__dirname, '../logs/Lepao.log'), 'INFO') this.runpy = config.runpy } async getPath(account, vip) { const accountSql = 'SELECT area, max_distance, min_distance FROM lepao_account WHERE student_num = ?' const rows = await db.query(accountSql, [account]) if (!rows || rows.length === 0) { throw new Error('无法获取账号数据') } const { area, max_distance, min_distance } = rows[0] const max = Number(max_distance) || 4.00 const min = Number(min_distance) || 2.00 // if (vip !== 1) { // if (area) throw new Error('指定乐跑跑区为 VIP 专用功能,请先开通 VIP') // if (max !== 4.00 || min !== 2.00 ) throw new Error('指定乐跑距离区间为 VIP 专用功能,请先开通 VIP') // } let pathSql = 'SELECT id FROM path_data WHERE distance < ? AND distance > ?' const pathParams = [max, min] if (area) { pathSql += ' AND run_zone_name = ?' pathParams.push(area) } const paths = await db.query(pathSql, pathParams) if (!paths || paths.length === 0) { throw new Error('未找到符合条件的路线,请改变路径选择条件') } const randomPath = paths[Math.floor(Math.random() * paths.length)] return randomPath.id } // 乐跑入口函数 async beginLepao(uuid, account, token, uid, school_id, state) { try { const userPermissionSql = 'SELECT vip, lepao_count FROM users WHERE uuid = ?' const userPermissionData = await db.query(userPermissionSql, [uuid]) if (!userPermissionData || userPermissionData.length !== 1) throw new Error('无法获取用户信息') if (userPermissionData[0].lepao_count < 1) throw new Error('用户乐跑次数不足,请购买乐跑套餐!') if(state !== 1) { return this.sendFailEmail(account, '登录已过期,请尝试使用登录器重新登录') } // 获取路径 ID const path_id = await this.getPath(account, userPermissionData[0].vip) // 更换跑区 const zoneUrl = this.runpy + '/set_zone' const ossData = { uid, token, school_id, student_id: account, random_id: path_id } try { const zoneRes = await axios.post(zoneUrl, ossData) const { data } = zoneRes if (!data || data.status !== 1 || !data.data) { this.setStatusFail(account) throw new Error(data?.info || '未知错误,请尝试重新登录') } } catch (error) { throw error } // 上传 OSS const ossUrl = this.runpy + '/upload_oss_file' let oss_path try { const ossRes = await axios.post(ossUrl, ossData) const { data } = ossRes if (!data || data.code !== 200 || !data.oss_path) { throw new Error('请检查登录是否过期,并尝试更新乐跑登录状态') } oss_path = data.oss_path } catch (error) { this.setStatusFail(account) this.logger.error(`上传OSS记录失败,请检查登录是否过期。${error.stack || error.message}`) throw new Error('请检查登录是否过期') } // 扣除乐跑次数 const useLepaoCountSql = 'UPDATE users SET lepao_count = lepao_count - 1 WHERE uuid = ?' await db.query(useLepaoCountSql, [uuid]) // 晚上10点后提前 let run_end_time = Math.floor(Date.now() / 1000) let hour = new Date().getHours() if (hour >= 22) run_end_time -= 18000 // 0点过后无法上传 // else if (hour < 6) run_end_time -= 43920 const lepaoData = { uid, token, school_id, student_id: account, random_id: path_id, record_file: oss_path, run_end_time } this.logger.info(lepaoData) // 绑定乐跑数据 const lepaoUrl = this.runpy + '/bind_data' try { const lepaoRes = await axios.post(lepaoUrl, lepaoData) const { data } = lepaoRes this.logger.info(data) if (!data || data.status !== 1 || !data.data) { this.setStatusFail(account) throw new Error(data?.info || '未知错误,请尝试重新登录') } await this.addRecord(account, data.data) if (data.data.record_failed_reason === '') { await this.sendSuccessEmail(account, data.data) } else { await this.sendFailEmail(account, data.data.record_failed_reason) await this.lepaoFail(uuid) } } catch (error) { await this.lepaoFail(uuid) throw error } } catch (error) { await this.sendFailEmail(account, error.message || '未知错误,请尝试重新登录') } } async addRecord(account, result) { try { const time = Date.now() const sql = 'INSERT INTO lepao_record (time, lepao_account, result) VALUES (?, ?, ?)' await db.query(sql, [time, account, result]) } catch (error) { this.logger.error(error.stack || error.message) } } async sendSuccessEmail(account, lepaoData) { try { const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?' const rows = await db.query(emailSql, [account]) if (!rows || rows.length === 0) { throw new Error('查找用户邮箱失败') } const data = { ...lepaoData, name: rows[0].name, account } await EmailTemplate.lepaoSuccess(rows[0].email, data) } catch (error) { this.logger.error(error.stack || error.message) } } async sendFailEmail(account, reason) { try { const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?' const rows = await db.query(emailSql, [account]) if (!rows || rows.length == 0) { throw new Error('查找用户邮箱失败') } const data = { name: rows[0].name, account, reason } await EmailTemplate.lepaoFail(rows[0].email, data) } catch (error) { this.logger.error(error.stack || error.message) } } async lepaoFail(uuid) { try { const sql = 'UPDATE users SET lepao_count = lepao_count + 1 WHERE uuid = ?' await db.query(sql, [uuid]) } catch (error) { this.logger.error(`返还用户 ${uuid} 乐跑次数时出错: ${error.stack || error.message}`) } } async setStatusFail(account) { try { const sql = 'UPDATE lepao_account SET state = 0 WHERE student_num = ?' await db.query(sql, [account]) } catch (error) { this.logger.error(`设置用户 ${account} state时出错: ${error.stack || error.message}`) } } } const lepao = new Lepao() module.exports.lepao = lepao