Lepao.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, sex FROM lepao_account WHERE student_num = ?'
  14. const rows = await db.query(accountSql, [account])
  15. if (!rows || rows.length === 0) {
  16. throw new Error('无法获取账号数据')
  17. }
  18. const { area, max_distance, min_distance, sex } = rows[0]
  19. let max = Number(max_distance) || 4.00
  20. let min = Number(min_distance) || 2.00
  21. if (sex === 2) {
  22. max = Number(max_distance) || 4.00
  23. min = Number(min_distance) || 1.60
  24. }
  25. // if (vip !== 1) {
  26. // if (area) throw new Error('指定乐跑跑区为 VIP 专用功能,请先开通 VIP')
  27. // if (max !== 4.00 || min !== 2.00 ) throw new Error('指定乐跑距离区间为 VIP 专用功能,请先开通 VIP')
  28. // }
  29. let pathSql = 'SELECT id FROM path_data WHERE distance < ? AND distance > ?'
  30. const pathParams = [max, min]
  31. if (area) {
  32. pathSql += ' AND run_zone_name = ?'
  33. pathParams.push(area)
  34. }
  35. const paths = await db.query(pathSql, pathParams)
  36. if (!paths || paths.length === 0) {
  37. throw new Error('未找到符合条件的路线,请改变路径选择条件')
  38. }
  39. const randomPath = paths[Math.floor(Math.random() * paths.length)]
  40. return randomPath.id
  41. }
  42. // 乐跑入口函数
  43. async beginLepao(uuid, account, token, uid, school_id, state) {
  44. try {
  45. const userPermissionSql = 'SELECT vip, lepao_count FROM users WHERE uuid = ?'
  46. const userPermissionData = await db.query(userPermissionSql, [uuid])
  47. if (!userPermissionData || userPermissionData.length !== 1)
  48. throw new Error('无法获取用户信息')
  49. if (userPermissionData[0].lepao_count < 1)
  50. throw new Error('用户乐跑次数不足,请购买乐跑套餐!')
  51. if (state !== 1) {
  52. return this.sendFailEmail(account, '登录已过期,请尝试使用登录器重新登录')
  53. }
  54. // 获取路径 ID
  55. const path_id = await this.getPath(account, userPermissionData[0].vip)
  56. // 更换跑区
  57. const zoneUrl = this.runpy + '/set_zone'
  58. const ossData = { uid, token, school_id, student_id: account, random_id: path_id }
  59. try {
  60. const zoneRes = await axios.post(zoneUrl, ossData)
  61. const { data } = zoneRes
  62. if (!data || data.status !== 1 || !data.data) {
  63. this.setStatusFail(account)
  64. throw new Error(data?.info || '未知错误,请尝试重新登录')
  65. }
  66. } catch (error) {
  67. throw error
  68. }
  69. // 上传 OSS
  70. const ossUrl = this.runpy + '/upload_oss_file'
  71. let oss_path
  72. try {
  73. const ossRes = await axios.post(ossUrl, ossData)
  74. const { data } = ossRes
  75. if (!data || data.code !== 200 || !data.oss_path) {
  76. throw new Error('请检查登录是否过期,并尝试更新乐跑登录状态')
  77. }
  78. oss_path = data.oss_path
  79. } catch (error) {
  80. this.setStatusFail(account)
  81. this.logger.error(`上传OSS记录失败,请检查登录是否过期。${error.stack || error.message}`)
  82. throw new Error('请检查登录是否过期')
  83. }
  84. // 扣除乐跑次数
  85. const useLepaoCountSql = 'UPDATE users SET lepao_count = lepao_count - 1 WHERE uuid = ?'
  86. await db.query(useLepaoCountSql, [uuid])
  87. // 晚上10点后提前
  88. let run_end_time = Math.floor(Date.now() / 1000)
  89. let hour = new Date().getHours()
  90. if (hour >= 22) run_end_time -= 18000
  91. // 0点过后无法上传
  92. // else if (hour < 6) run_end_time -= 43920
  93. const lepaoData = {
  94. uid,
  95. token,
  96. school_id,
  97. student_id: account,
  98. random_id: path_id,
  99. record_file: oss_path,
  100. run_end_time
  101. }
  102. this.logger.info(lepaoData)
  103. // 绑定乐跑数据
  104. const lepaoUrl = this.runpy + '/bind_data'
  105. try {
  106. const lepaoRes = await axios.post(lepaoUrl, lepaoData)
  107. const { data } = lepaoRes
  108. this.logger.info(data)
  109. if (!data || data.status !== 1 || !data.data) {
  110. this.setStatusFail(account)
  111. throw new Error(data?.info || '未知错误,请尝试重新登录')
  112. }
  113. await this.addRecord(account, data.data)
  114. if (data.data.record_failed_reason === '') {
  115. await this.sendSuccessEmail(account, data.data)
  116. } else {
  117. await this.sendFailEmail(account, data.data.record_failed_reason)
  118. await this.lepaoFail(uuid)
  119. }
  120. } catch (error) {
  121. await this.lepaoFail(uuid)
  122. throw error
  123. }
  124. } catch (error) {
  125. await this.sendFailEmail(account, error.message || '未知错误,请尝试重新登录')
  126. }
  127. }
  128. async addRecord(account, result) {
  129. try {
  130. const time = Date.now()
  131. const sql = 'INSERT INTO lepao_record (time, lepao_account, result) VALUES (?, ?, ?)'
  132. await db.query(sql, [time, account, result])
  133. } catch (error) {
  134. this.logger.error(error.stack || error.message)
  135. }
  136. }
  137. async sendSuccessEmail(account, lepaoData) {
  138. try {
  139. const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
  140. const rows = await db.query(emailSql, [account])
  141. if (!rows || rows.length === 0) {
  142. throw new Error('查找用户邮箱失败')
  143. }
  144. const data = {
  145. ...lepaoData,
  146. name: rows[0].name,
  147. account
  148. }
  149. await EmailTemplate.lepaoSuccess(rows[0].email, data)
  150. } catch (error) {
  151. this.logger.error(error.stack || error.message)
  152. }
  153. }
  154. async sendFailEmail(account, reason) {
  155. try {
  156. const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
  157. const rows = await db.query(emailSql, [account])
  158. if (!rows || rows.length == 0) {
  159. throw new Error('查找用户邮箱失败')
  160. }
  161. const data = {
  162. name: rows[0].name,
  163. account,
  164. reason
  165. }
  166. await EmailTemplate.lepaoFail(rows[0].email, data)
  167. } catch (error) {
  168. this.logger.error(error.stack || error.message)
  169. }
  170. }
  171. async lepaoFail(uuid) {
  172. try {
  173. const sql = 'UPDATE users SET lepao_count = lepao_count + 1 WHERE uuid = ?'
  174. await db.query(sql, [uuid])
  175. } catch (error) {
  176. this.logger.error(`返还用户 ${uuid} 乐跑次数时出错: ${error.stack || error.message}`)
  177. }
  178. }
  179. async setStatusFail(account) {
  180. try {
  181. const sql = 'UPDATE lepao_account SET state = 0 WHERE student_num = ?'
  182. await db.query(sql, [account])
  183. } catch (error) {
  184. this.logger.error(`设置用户 ${account} state时出错: ${error.stack || error.message}`)
  185. }
  186. }
  187. }
  188. const lepao = new Lepao()
  189. module.exports.lepao = lepao