Lepao.js 8.0 KB

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