Lepao.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const axios = require('axios')
  2. const db = require('../../plugin/DataBase/db')
  3. const Logger = require('../Logger')
  4. const path = require('path')
  5. const EmailTemplate = require('../../plugin/Email/emailTemplate')
  6. const config = require('../../config.json')
  7. class Lepao {
  8. constructor() {
  9. this.logger = new Logger(path.join(__dirname, '../logs/Lepao.log'), 'INFO')
  10. this.runpy = config.runpy
  11. }
  12. async getPath(account, vip) {
  13. const accountSql = 'SELECT area, max_distance, min_distance FROM lepao_account WHERE student_num = ?'
  14. const rows = await db.query(accountSql, [account])
  15. if (!rows || rows.length !== 1) {
  16. throw new Error('无法获取账号数据')
  17. }
  18. const { area, max_distance, min_distance } = rows[0]
  19. const max = Number(max_distance) || 4.00
  20. const min = Number(min_distance) || 1.60
  21. if (vip !== 1) {
  22. if (area) throw new Error('指定乐跑校区为 VIP 专用功能,请先开通 VIP')
  23. if (max !== 4.00 || min !== 1.60) {
  24. throw new Error('指定乐跑距离区间为 VIP 专用功能,请先开通 VIP')
  25. }
  26. }
  27. let pathSql = 'SELECT id FROM path_data WHERE distance < ? AND distance > ?'
  28. const pathParams = [max, min]
  29. if (area) {
  30. pathSql += ' AND run_zone_name = ?'
  31. pathParams.push(area)
  32. }
  33. const paths = await db.query(pathSql, pathParams)
  34. if (!paths || paths.length === 0) {
  35. throw new Error('未找到符合条件的路线,请改变选择条件')
  36. }
  37. const randomPath = paths[Math.floor(Math.random() * paths.length)]
  38. return randomPath.id
  39. }
  40. async beginLepao(uuid, account, token, uid, school_id) {
  41. try {
  42. const userPermissionSql = 'SELECT vip, lepao_count FROM users WHERE uuid = ?'
  43. const userPermissionData = await db.query(userPermissionSql, [uuid])
  44. if (!userPermissionData || userPermissionData.length !== 1)
  45. throw new Error('无法获取用户信息')
  46. if (userPermissionData[0].lepao_count < 1)
  47. throw new Error('用户乐跑次数不足,请购买乐跑套餐!')
  48. // 获取路径 ID
  49. const path_id = await this.getPath(account, userPermissionData[0].vip)
  50. // 上传 OSS
  51. const ossUrl = this.runpy + '/upload_oss_file'
  52. const ossData = { uid, token, school_id, student_id: account, random_id: path_id }
  53. let oss_path
  54. try {
  55. const ossRes = await axios.post(ossUrl, ossData)
  56. const { data } = ossRes
  57. if (!data || data.code !== 200 || !data.oss_path) {
  58. throw new Error('请检查登录是否过期')
  59. }
  60. oss_path = data.oss_path
  61. } catch (error) {
  62. this.logger.error(`上传OSS记录失败,请检查登录是否过期。${error.stack || error.message}`)
  63. throw new Error('请检查登录是否过期')
  64. }
  65. // 扣除乐跑次数
  66. const useLepaoCountSql = 'UPDATE users SET lepao_count = lepao_count - 1 WHERE uuid = ?'
  67. await db.query(useLepaoCountSql, [uuid])
  68. let run_end_time = Math.floor(Date.now() / 1000)
  69. let hour = new Date().getHours()
  70. if (hour >= 22) run_end_time -= 3600
  71. const lepaoData = {
  72. uid,
  73. token,
  74. school_id,
  75. student_id: account,
  76. random_id: path_id,
  77. record_file: oss_path,
  78. run_end_time
  79. }
  80. console.log(lepaoData)
  81. const lepaoUrl = this.runpy + '/bind_data'
  82. try {
  83. const lepaoRes = await axios.post(lepaoUrl, lepaoData)
  84. const { data } = lepaoRes
  85. console.log(data)
  86. if (!data || data.status !== 1 || !data.data) {
  87. throw new Error(data.info || '未知错误,请尝试重新登录')
  88. }
  89. await this.addRecord(account, data.data)
  90. if (data.data.record_failed_reason === '') {
  91. await this.sendSuccessEmail(account, data)
  92. } else {
  93. await this.sendFailEmail(account, data.data.record_failed_reason)
  94. await this.lepaoFail(uuid)
  95. }
  96. } catch (error) {
  97. await this.lepaoFail(uuid)
  98. throw error
  99. }
  100. } catch (error) {
  101. await this.sendFailEmail(account, error.message || '未知错误,请尝试重新登录')
  102. }
  103. }
  104. async addRecord(account, result) {
  105. try {
  106. const time = Date.now()
  107. const sql = 'INSERT INTO lepao_record (time, lepao_account, result) VALUES (?, ?, ?)'
  108. await db.query(sql, [time, account, result])
  109. } catch (error) {
  110. this.logger.error(error.stack || error.message)
  111. }
  112. }
  113. async sendSuccessEmail(account, lepaoData) {
  114. try {
  115. const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
  116. const rows = await db.query(emailSql, [account])
  117. if (!rows || rows.length !== 1) {
  118. throw new Error('查找用户邮箱失败')
  119. }
  120. const data = {
  121. ...lepaoData,
  122. name: rows[0].name,
  123. account
  124. }
  125. await EmailTemplate.lepaoSuccess(rows[0].email, data)
  126. } catch (error) {
  127. this.logger.error(error.stack || error.message)
  128. }
  129. }
  130. async sendFailEmail(account, reason) {
  131. try {
  132. const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
  133. const rows = await db.query(emailSql, [account])
  134. if (!rows || rows.length !== 1) {
  135. throw new Error('查找用户邮箱失败')
  136. }
  137. const data = {
  138. name: rows[0].name,
  139. account,
  140. reason
  141. }
  142. await EmailTemplate.lepaoFail(rows[0].email, data)
  143. } catch (error) {
  144. this.logger.error(error.stack || error.message)
  145. }
  146. }
  147. async lepaoFail(uuid) {
  148. try {
  149. const sql = 'UPDATE users SET lepao_count = lepao_count + 1 WHERE uuid = ?'
  150. await db.query(sql, [uuid])
  151. } catch (error) {
  152. this.logger.error(`返还用户 ${uuid} 乐跑次数时出错: ${error.stack || error.message}`)
  153. }
  154. }
  155. }
  156. const lepao = new Lepao()
  157. module.exports.lepao = lepao