chengjie 6 달 전
부모
커밋
0de76122d9
4개의 변경된 파일68개의 추가작업 그리고 4개의 파일을 삭제
  1. 53 0
      src/api/common/commonController.js
  2. 1 0
      src/api/common/routes.js
  3. 1 1
      src/api/mps/mpsScoreController.js
  4. 13 3
      src/util/globalState.js

+ 53 - 0
src/api/common/commonController.js

@@ -5,8 +5,10 @@ import config from '../../config/index.js';
5 5
 import _ from 'lodash';
6 6
 import path from 'path';
7 7
 import gm from 'gm';
8
+import axios from 'axios';
8 9
 import COS from 'cos-nodejs-sdk-v5';
9 10
 import { uploadSingle } from '../../middleware/upload.js';
11
+import { globalState } from '../../util/globalState.js';
10 12
 
11 13
 const imageMagick = gm.subClass({ imageMagick: true });
12 14
 
@@ -191,4 +193,55 @@ async function uploadFile(filename, filepath) {
191 193
     });
192 194
 }
193 195
 
196
+export async function GetBaiduToken (ctx) {
197
+    const { Code, ProgramID } = ctx.query;
198
+    
199
+    let appid = '', secret = '';
200
+    
201
+    switch (ProgramID) {
202
+        case '99':
203
+            appid = config.wx.phonics_appid;
204
+            secret = config.wx.phonics_appsecret;
205
+            break;
206
+        case '166':
207
+            appid = config.wx.miaoguo_appid;
208
+            secret = config.wx.miaoguo_appsecret;
209
+            break;
210
+        case '105':
211
+            appid = config.wx.math_appid;
212
+            secret = config.wx.math_appsecret;
213
+            break;
214
+        case '164':
215
+            appid = config.wx.mathStar_appid;
216
+            secret = config.wx.mathStar_appsecret;
217
+            break;
218
+    }
219
+
220
+    const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${Code}&grant_type=authorization_code`;
221
+
222
+    const resultLogin = await axios.get(url)
223
+        .then(response => {
224
+            const json = response.data;
225
+            return json.openid ? { errcode: 10000 } : { errcode: 102 };
226
+        })
227
+        .catch(err => ({ errcode: 101, errStr: err }));
228
+
229
+    let result = 0;
230
+    if (resultLogin.errcode === 10000) {
231
+        result = globalState.getBufferMemory('BaiduToken');
232
+        if (result === 0) {
233
+            const baiduUrl = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=9r1Idx1mgMONvElxU3KGE5Gi&client_secret=f4f606f1a5c0b4eaf800e1e046802d81';
234
+            result = await axios.get(baiduUrl)
235
+                .then(response => {
236
+                    const json = response.data;
237
+                    if (json.access_token) {
238
+                        globalState.SetBufferMemory('BaiduToken', json.access_token, config.BufferMemoryTimeHigh);
239
+                        return json.access_token;
240
+                    }
241
+                    return 0;
242
+                });
243
+        }
244
+    }
194 245
 
246
+    ctx.body = { errcode: 10000, result };
247
+}

+ 1 - 0
src/api/common/routes.js

@@ -6,5 +6,6 @@ const router = new Router();
6 6
 
7 7
 // 文件上传路由
8 8
 router.post('/api/MiaoguoUploadFile2',uploadMiddleware,commonController.UploadFile);
9
+router.get('/api/GetBaiduToken',commonController.GetBaiduToken);
9 10
 
10 11
 export default router;

+ 1 - 1
src/api/mps/mpsScoreController.js

@@ -235,7 +235,7 @@ export async function GetMPSScore(ctx) {
235 235
 
236 236
         sql += sqlOrder;
237 237
 
238
-        console.log(sql);
238
+        //console.log(sql);
239 239
 
240 240
         var list = await mps.RunSql({}, sql);
241 241
 

+ 13 - 3
src/util/globalState.js

@@ -6,9 +6,19 @@ class GlobalState {
6 6
         this.htBufferMemory = new stringUtils.HashTable();
7 7
     }
8 8
 
9
-    // 获取HashTable实例
10
-    getBufferMemory() {
11
-        return this.htBufferMemory;
9
+    // 获取指定key的值
10
+    getBufferMemory(key) {
11
+        return this.htBufferMemory.getValue(key) || 0;
12
+    }
13
+
14
+    // 设置指定key的值,可选过期时间
15
+    SetBufferMemory(key, value, expireTime = null) {
16
+        this.htBufferMemory.add(key, value);
17
+        if (expireTime) {
18
+            setTimeout(() => {
19
+                this.htBufferMemory.remove(key);
20
+            }, expireTime);
21
+        }
12 22
     }
13 23
 }
14 24