Browse Source

增加乐跑剩余次数获取

Pchen. 11 months ago
parent
commit
0a870761bb
3 changed files with 47 additions and 8 deletions
  1. 5 0
      apis/Lepao/GetAccount.js
  2. 38 5
      lib/Lepao/Lepao.js
  3. 4 3
      plugin/Email/emailTemplate.js

+ 5 - 0
apis/Lepao/GetAccount.js

@@ -36,6 +36,11 @@ class GetAccount extends API {
                     a.area,
                     a.min_distance,
                     a.max_distance,
+                    a.total_num,
+                    a.term_num,
+                    a.academy_name,
+                    a.sex,
+                    a.grade_id,
                     a.race,
                     a.email,
                     a.auto_run,

+ 38 - 5
lib/Lepao/Lepao.js

@@ -49,6 +49,26 @@ class Lepao {
         return randomPath.id
     }
 
+    // 获取跑步次数
+    async getRecord(uid, token, school_id, student_id) {
+        try {
+            const reqData = { uid, token, school_id, student_id }
+            const recordUrl = this.runpy + '/get_record'
+            const recordRes = await axios.post(recordUrl, reqData)
+            const { recordData } = recordRes
+            if (!recordData || recordData.status !== 1 || !recordData.data) {
+                this.logger.info('获取剩余跑步次数失败!')
+                console.log(recordData)
+                return
+            }
+            return recordRes.data
+        } catch (error) {
+            this.logger.error('获取跑步次数失败!')
+            console.log(error.stack)
+            return
+        }
+    }
+
     // 乐跑入口函数
     async beginLepao(uuid, account, token, uid, school_id, state) {
         try {
@@ -109,9 +129,6 @@ class Lepao {
             let hour = new Date().getHours()
             if (hour >= 22) run_end_time -= 18000
 
-            // 0点过后无法上传
-            // else if (hour < 6) run_end_time -= 43920
-
             const lepaoData = {
                 uid,
                 token,
@@ -139,7 +156,21 @@ class Lepao {
 
                 await this.addRecord(account, data.data)
                 if (data.data.record_failed_reason === '') {
-                    await this.sendSuccessEmail(account, data.data)
+                    // 获取剩余跑步次数
+                    const recordData = await this.getRecord(uid, token, school_id, account)
+
+                    // await this.sendSuccessEmail(account, data.data)
+                    let term_num = 0, total_num = 0
+                    if (recordData) {
+                        term_num = recordData.term_num
+                        total_num = recordData.total_num
+                        if (!recordRows || recordRows.affectedRows !== 1)
+                            this.logger.info('更新乐跑次数失败!')
+                    }
+                    await this.sendSuccessEmail(account, data.data, term_num, total_num)
+                    
+                    let recordSql = 'UPDATE lepao_accounts SET term_num = ?, total_num = ? WHERE student_num = ?'
+                    let recordRows = await db.query(recordSql, [term_num, total_num, account])
                 } else {
                     await this.sendFailEmail(account, data.data.record_failed_reason)
                     await this.lepaoFail(uuid)
@@ -165,7 +196,7 @@ class Lepao {
         }
     }
 
-    async sendSuccessEmail(account, lepaoData) {
+    async sendSuccessEmail(account, lepaoData, term_num, total_num) {
         try {
             const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
             const rows = await db.query(emailSql, [account])
@@ -175,6 +206,8 @@ class Lepao {
 
             const data = {
                 ...lepaoData,
+                term_num,
+                total_num,
                 name: rows[0].name,
                 account
             }

+ 4 - 3
plugin/Email/emailTemplate.js

@@ -98,7 +98,7 @@ class emailTemplate {
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>RunForge - 邮箱验证码</title>
+    <title>RunForge - 注册成功提醒</title>
     <style>
         body {
             font-family: Arial, sans-serif;
@@ -159,7 +159,7 @@ class emailTemplate {
 <body>
     <div class="container">
         <div class="head">
-            <h2>RunForge - 邮箱验证码</h2>
+            <h2>RunForge - 注册成功提醒</h2>
         </div>
 
         <p>尊敬的 ${username}:</p>
@@ -345,7 +345,8 @@ class emailTemplate {
                         <p><strong>跑步时间:</strong> ${this.formatSecondsToMinSec(data.time)}</p>
                         <p><strong>平均配速:</strong> ${this.calculatePace(data.time, data.distance)}</p>
                         <p><strong>跑步距离:</strong> ${data.distance} Km</p>
-                        
+                        <p><strong>累计次数:</strong> ${data.total_num} 次</p>
+                        <p><strong>剩余次数:</strong> ${data.term_num} 次</p>
                     </div>
 
                     <p class="important">请您开启了自动乐跑,请避免在其他设备上登录“智慧体育”小程序,否则将导致登录失效。</p>