Lepao.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // 乐跑入口函数
  41. async beginLepao(uuid, account, token, uid, school_id, state) {
  42. try {
  43. const userPermissionSql = 'SELECT vip, lepao_count FROM users WHERE uuid = ?'
  44. const userPermissionData = await db.query(userPermissionSql, [uuid])
  45. if (!userPermissionData || userPermissionData.length !== 1)
  46. throw new Error('无法获取用户信息')
  47. if (userPermissionData[0].lepao_count < 1)
  48. throw new Error('用户乐跑次数不足,请购买乐跑套餐!')
  49. if(state !== 1) {
  50. return this.sendFailEmail(account, '登录已过期,请尝试使用登录器重新登录')
  51. }
  52. // 获取路径 ID
  53. const path_id = await this.getPath(account, userPermissionData[0].vip)
  54. // 更换跑区
  55. const zoneUrl = this.runpy + '/set_zone'
  56. const ossData = { uid, token, school_id, student_id: account, random_id: path_id }
  57. try {
  58. const zoneRes = await axios.post(zoneUrl, ossData)
  59. const { data } = zoneRes
  60. if (!data || data.status !== 1 || !data.data) {
  61. this.setStatusFail(account)
  62. throw new Error(data?.info || '未知错误,请尝试重新登录')
  63. }
  64. } catch (error) {
  65. throw error
  66. }
  67. // 上传 OSS
  68. const ossUrl = this.runpy + '/upload_oss_file'
  69. let oss_path
  70. try {
  71. const ossRes = await axios.post(ossUrl, ossData)
  72. const { data } = ossRes
  73. if (!data || data.code !== 200 || !data.oss_path) {
  74. throw new Error('请检查登录是否过期')
  75. }
  76. oss_path = data.oss_path
  77. } catch (error) {
  78. this.setStatusFail(account)
  79. this.logger.error(`上传OSS记录失败,请检查登录是否过期。${error.stack || error.message}`)
  80. throw new Error('请检查登录是否过期')
  81. }
  82. // 扣除乐跑次数
  83. const useLepaoCountSql = 'UPDATE users SET lepao_count = lepao_count - 1 WHERE uuid = ?'
  84. await db.query(useLepaoCountSql, [uuid])
  85. let run_end_time = Math.floor(Date.now() / 1000)
  86. let hour = new Date().getHours()
  87. if (hour >= 22) run_end_time -= 3600
  88. const lepaoData = {
  89. uid,
  90. token,
  91. school_id,
  92. student_id: account,
  93. random_id: path_id,
  94. record_file: oss_path,
  95. run_end_time
  96. }
  97. console.log(lepaoData)
  98. // 绑定乐跑数据
  99. const lepaoUrl = this.runpy + '/bind_data'
  100. try {
  101. const lepaoRes = await axios.post(lepaoUrl, lepaoData)
  102. const { data } = lepaoRes
  103. console.log(data)
  104. if (!data || data.status !== 1 || !data.data) {
  105. this.setStatusFail(account)
  106. throw new Error(data?.info || '未知错误,请尝试重新登录')
  107. }
  108. await this.addRecord(account, data.data)
  109. if (data.data.record_failed_reason === '') {
  110. await this.sendSuccessEmail(account, data.data)
  111. } else {
  112. await this.sendFailEmail(account, data.data.record_failed_reason)
  113. await this.lepaoFail(uuid)
  114. }
  115. } catch (error) {
  116. await this.lepaoFail(uuid)
  117. throw error
  118. }
  119. } catch (error) {
  120. await this.sendFailEmail(account, error.message || '未知错误,请尝试重新登录')
  121. }
  122. }
  123. async addRecord(account, result) {
  124. try {
  125. const time = Date.now()
  126. const sql = 'INSERT INTO lepao_record (time, lepao_account, result) VALUES (?, ?, ?)'
  127. await db.query(sql, [time, account, result])
  128. } catch (error) {
  129. this.logger.error(error.stack || error.message)
  130. }
  131. }
  132. async sendSuccessEmail(account, lepaoData) {
  133. try {
  134. const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
  135. const rows = await db.query(emailSql, [account])
  136. if (!rows || rows.length !== 1) {
  137. throw new Error('查找用户邮箱失败')
  138. }
  139. const data = {
  140. ...lepaoData,
  141. name: rows[0].name,
  142. account
  143. }
  144. await EmailTemplate.lepaoSuccess(rows[0].email, data)
  145. } catch (error) {
  146. this.logger.error(error.stack || error.message)
  147. }
  148. }
  149. async sendFailEmail(account, reason) {
  150. try {
  151. const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
  152. const rows = await db.query(emailSql, [account])
  153. if (!rows || rows.length !== 1) {
  154. throw new Error('查找用户邮箱失败')
  155. }
  156. const data = {
  157. name: rows[0].name,
  158. account,
  159. reason
  160. }
  161. await EmailTemplate.lepaoFail(rows[0].email, data)
  162. } catch (error) {
  163. this.logger.error(error.stack || error.message)
  164. }
  165. }
  166. async lepaoFail(uuid) {
  167. try {
  168. const sql = 'UPDATE users SET lepao_count = lepao_count + 1 WHERE uuid = ?'
  169. await db.query(sql, [uuid])
  170. } catch (error) {
  171. this.logger.error(`返还用户 ${uuid} 乐跑次数时出错: ${error.stack || error.message}`)
  172. }
  173. }
  174. async setStatusFail(account) {
  175. try {
  176. const sql = 'UPDATE lepao_account SET state = 0 WHERE student_num = ?'
  177. await db.query(sql, [account])
  178. } catch (error) {
  179. this.logger.error(`设置用户 ${account} state时出错: ${error.stack || error.message}`)
  180. }
  181. }
  182. }
  183. const lepao = new Lepao()
  184. module.exports.lepao = lepao