Lepao.js 7.9 KB

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