chengjie 5 kuukautta sitten
vanhempi
commit
5d05517d4e

+ 329 - 0
src/api/mathcalculate/mathController.js

@@ -0,0 +1,329 @@
1
+import moment from 'moment';
2
+import commonModel from '../../model/commonModel.js';
3
+import mathcalculate from '../../model/mathcalculate.js';
4
+import config from '../../config/index.js';
5
+import _ from 'lodash';
6
+import axios from 'axios';
7
+import { Encrypt, Decrypt } from '../../util/crypto/index.js';
8
+import { stringUtils } from '../../util/stringClass.js';
9
+import WXBizDataCrypt from '../../util/WXBizDataCrypt.js';
10
+import { globalCache } from '../../util/GlobalCache.js';
11
+import constantClass from '../../util/constant/index.js';
12
+import fs from 'fs';
13
+import { promises as fsPromises } from 'fs';
14
+import COS from 'cos-nodejs-sdk-v5';
15
+
16
+export async function MathLogin(ctx) {
17
+    let param = ctx.request.body;
18
+    if (param.param) {
19
+        const paramStr = Decrypt(param.param, config.urlSecrets.aes_key, config.urlSecrets.aes_iv);
20
+        //console.log("paramStr:"+paramStr);
21
+        param = JSON.parse(paramStr);
22
+    }
23
+    const code = param.Code;
24
+    //console.log("code:"+code);
25
+    const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${config.wx.math_appid}&secret=${config.wx.math_appsecret}&js_code=${code}&grant_type=authorization_code`;
26
+
27
+    let result = await axios.get(url)
28
+        .then(res => {
29
+            const json = res.data;
30
+            //console.log("json:"+json);
31
+            if (json && json.openid) {
32
+                param.OpenID = json.openid;
33
+                param.sessionKey = json.session_key;
34
+                if (json.unionid)
35
+                    param.UnionID = json.unionid;
36
+                return {errcode: 10000};
37
+            }
38
+            else {
39
+                return json;
40
+            }
41
+        })
42
+        .catch(err => {
43
+            return {errcode: 101, errStr: err};
44
+        });
45
+
46
+    if (result.errcode == 10000) {
47
+        delete param.Code;
48
+        if (param.sessionKey && param.iv && param.encryptedData){
49
+            //console.log("param.sessionKey:"+param.sessionKey);
50
+            const pc = new WXBizDataCrypt(config.wx.math_appid, param.sessionKey);
51
+            const dataUnionID = pc.decryptData(param.encryptedData , param.iv);
52
+            //console.log(dataUnionID);
53
+            param.UnionID = dataUnionID.unionId;
54
+        }
55
+
56
+        delete param.sessionKey;
57
+        delete param.iv;
58
+        delete param.encryptedData;
59
+
60
+        //todo
61
+        //param.OpenID="o4UHq4gaNlHfdTWxgl3fTgC1mFsI";
62
+
63
+        let userList = await mathcalculate.GetUsersInfo(param);
64
+        if (userList.length > 0) {
65
+            param.LastLoginTime = new Date();
66
+            const time1 = moment(userList[0].ProductServiceTime).format('YYYY-MM-DD HH:mm:ss');
67
+            const time3 = moment().format('YYYY-MM-DD HH:mm:ss');
68
+            if (time1 < time3)
69
+                param.IsMember = 0;
70
+
71
+            delete param.Introducer;
72
+            delete param.UserSource;
73
+            delete param.SourceID;
74
+            //console.log(param.NickName);
75
+            if (param.NickName == "陌生用户") {
76
+                delete param.NickName;
77
+                delete param.AvatarUrl;
78
+                delete param.Language;
79
+                delete param.Gender;
80
+                delete param.City;
81
+                delete param.Province;
82
+                delete param.Country;
83
+            }
84
+
85
+            await mathcalculate.UpdateUsers(param);
86
+            userList = await mathcalculate.GetUsersInfo(param);
87
+        }
88
+        else {
89
+            param.NickName = "陌生用户";
90
+            param.AvatarUrl = "../images/userface_default.png";
91
+            param.CreateTime = new Date();
92
+            param.LastLoginTime = param.CreateTime;
93
+            param.ProductServiceTime = param.CreateTime;
94
+            const inseredID = await mathcalculate.AddUsers(param);
95
+            userList = await mathcalculate.GetUsersInfo(param);
96
+        }
97
+
98
+        delete userList[0].OpenID;
99
+        delete userList[0].UnionID;
100
+
101
+        //产品支付是否显示
102
+        if (param.ProgramVersion) {
103
+            let param2 = {
104
+                ProgramID: 105,
105
+                Version: param.ProgramVersion,
106
+            };
107
+            let result3 = await commonModel.GetProductVersionList(param2);
108
+            if (result3) {
109
+                if ((param2.Version == result3[0].Version && result3[0].IsShowPay <= 0)
110
+                    || param2.Version > result3[0].Version) {
111
+                    userList[0].IsShow = result3[0].IsShowPay;
112
+                }
113
+                else {
114
+                    userList[0].IsShow = 1;
115
+                }
116
+
117
+                //针对iphone测试用户,永远是无支付状态
118
+                if (userList[0].Brand == 'iPhone' && userList[0].WXLanguage == 'en-US'
119
+                    && userList[0].UserSource == '1001' && userList[0].IsPay == 0) {
120
+                    userList[0].IsShow = 0;
121
+                }
122
+
123
+                //针对微信测试用户,永远是无支付状态
124
+                if ((userList[0].UserSource=='1001' && userList[0].System=="iOS 10.0.1")
125
+                    || (!userList[0].UserSource && (!userList[0].LastUserSource || userList[0].LastUserSource>10000))
126
+                    || userList[0].NickName.indexOf("dgztest")>=0){
127
+                    userList[0].IsShow=-1;
128
+                }
129
+
130
+                if (userList[0].IsMember===1)
131
+                    userList[0].IsShow=1;
132
+            }
133
+        }
134
+
135
+        result = {errcode: 10000, result: userList[0]};
136
+    }
137
+
138
+    ctx.body = result;
139
+}
140
+
141
+export async function GetQuestionTypes(ctx) {
142
+    const param = {
143
+        QuestionMode: ctx.query.Mode,
144
+    };
145
+    const list = await mathcalculate.GetQuestionTypesList(param);
146
+    const result = [];
147
+    let temp;
148
+    let arr = [];
149
+    
150
+    for (let i = 0; i < list.length; i++) {
151
+        const item = list[i];
152
+        if (temp && temp.Category != item.Category) {
153
+            const obj = {
154
+                ID: temp.CategoryID,
155
+                Name: temp.CategoryName + "·" + temp.CategoryLevel,
156
+                Description: temp.CategoryDescription,
157
+                ImageUrl: temp.CategoryImageUrl,
158
+                QuestionCategoryInfoShow: true,
159
+                List: arr,
160
+            };
161
+            result.push(obj);
162
+            arr = [];
163
+        }
164
+        
165
+        item.Example = item.Example.split(",");
166
+        item.SelectItemClass = "";
167
+        
168
+        arr.push(item);
169
+        temp = item;
170
+    }
171
+
172
+    const obj = {
173
+        ID: temp.CategoryID,
174
+        Name: temp.CategoryName + temp.CategoryLevel,
175
+        Description: temp.CategoryDescription,
176
+        ImageUrl: temp.CategoryImageUrl,
177
+        QuestionCategoryInfoShow: true,
178
+        List: arr,
179
+    };
180
+    result.push(obj);
181
+
182
+    for (let i = 0; i < result.length; i++) {
183
+        for (let j = 0; j < result[i].List.length; j++) {
184
+            const item = result[i].List[j];
185
+            delete item.CategoryID;
186
+            delete item.CategoryName;
187
+            delete item.CategoryLevel;
188
+            delete item.CategoryDescription;
189
+            delete item.CategoryImageUrl;
190
+        }
191
+    }
192
+
193
+    for (let i = 0; i < result.length; i++) {
194
+        for (let j = 0; j < result[i].List.length; j++) {
195
+            const item = result[i].List[j];
196
+            switch (item.ID){
197
+                case 167:
198
+                case 174:
199
+                case 166:
200
+                case 190:
201
+                case 191:
202
+                case 192:
203
+                case 193:
204
+                case 164:
205
+                case 165:
206
+                case 195:
207
+                    item.CategoryName = "乘除运算";
208
+                    item.CategoryLevel = "3级";
209
+                    break;
210
+            }
211
+        }
212
+    }
213
+
214
+    ctx.body = {"errcode": 10000, result: result};
215
+}
216
+
217
+// exports.GetQuestionTypesPrint = function* () {
218
+//     var param = {
219
+//         QuestionTypeID: this.query.ID || 1,
220
+//         QuestionTypeCategory:this.query.QuestionTypeCategory || 2,
221
+//         PageNum:this.query.PageNum || 1,
222
+//     }
223
+//     var questionType = yield mathcalculate.GetQuestionTypesInfo(param);
224
+//     questionType = questionType[0];
225
+
226
+
227
+//     var questionNumber = questionType.QuestionNumber2;
228
+//     if (param.QuestionTypeCategory==3)
229
+//         questionNumber = questionType.QuestionNumber3;
230
+
231
+//     questionNumber=questionNumber*param.PageNum;
232
+
233
+//     questionType.QuestionNumber=questionNumber;
234
+//     //console.log("questionType.QuestionNumber:"+questionType.QuestionNumber);
235
+//     var list = getQuestionTypeInfo(questionType);
236
+
237
+//     var questionList = [];
238
+//     //如果要求题目数小于原有题目数,则直接随机获取题目
239
+//     if (list.length > questionNumber) {
240
+
241
+//         do {
242
+//             var rnd = stringClass.Random(0, list.length - 1);
243
+//             if (list[rnd]) {
244
+//                 questionList.push(list[rnd]);
245
+//                 list[rnd] = null;
246
+//             }
247
+//         }
248
+//         while (questionList.length < questionNumber);
249
+//     }
250
+//     //如果要求题目数大于原有题目数,则获取全部原有题目的倍数,再将剩余数随机获取
251
+//     else {
252
+//         var nCount = Math.floor(questionNumber / list.length);
253
+//         for (var n = 0; n < nCount; n++) {
254
+//             for (var i = 0; i < list.length; i++) {
255
+//                 questionList.push(list[i]);
256
+//             }
257
+//         }
258
+
259
+//         while (questionList.length < questionNumber) {
260
+//             var rnd = stringClass.Random(0, list.length - 1);
261
+//             if (list[rnd]) {
262
+//                 questionList.push(list[rnd]);
263
+//                 list[rnd] = null;
264
+//             }
265
+//         }
266
+//     }
267
+
268
+//     questionList = stringClass.RemoveJSONNull(questionList);
269
+
270
+//     questionList.sort(function () {
271
+//         return 0.5 - Math.random()
272
+//     });
273
+
274
+//     var questionListResult = [];
275
+//     for (var i = 0; i < questionList.length; i++) {
276
+//         var item = {};
277
+//         for (var k in questionList[i]) {
278
+//             item[k] = questionList[i][k];
279
+
280
+//             if (item[k]=="−") {
281
+//                 item[k] = "-";
282
+//             }
283
+//         }
284
+//         item.ID = i + 1;
285
+//         var arr = questionType.HiddenColumn.split(",");
286
+//         var hidden = arr[stringClass.Random(0, arr.length - 1)];
287
+//         item.HiddenColumn = hidden;
288
+//         item.Result = item[item.HiddenColumn];
289
+
290
+//         if (param.QuestionTypeCategory==3) {
291
+//             item.Vertical=computeMath(item);
292
+//         }
293
+
294
+//         questionListResult.push(item);
295
+//     }
296
+
297
+//     var categoryArray = [{}, { Num: 1, Name: "一年级", Array: [1, 2] }, { Num: 2, Name: "二年级", Array: [3, 4] }, { Num: 3, Name: "三年级", Array: [5, 6] }, { Num: 4, Name: "四年级及以上", Array: [7] }];
298
+//     var categoryName="";
299
+//     for(var i=1;i<categoryArray.length;i++){
300
+//         for(var j=0;j<categoryArray[i].Array.length;j++){
301
+//             if (categoryArray[i].Array[j]==questionType.Category){
302
+//                 categoryName=categoryArray[i].Name;
303
+//                 break;
304
+//             }
305
+//         }
306
+//     }
307
+
308
+//     var result = {
309
+//         ID: questionType.ID,
310
+//         Category: questionType.Category,
311
+//         CategoryName: categoryName,
312
+//         //错误接口参数
313
+//         Cagegory: questionType.Category,
314
+//         CagegoryName: categoryName,
315
+
316
+//         CreateTime:moment(new Date()).format('YYYY年MM月DD日 HH:mm'),
317
+//         Name: questionType.Name,
318
+//         QuestionTypeCategory:Number(param.QuestionTypeCategory),
319
+//         QuestionTypeLevel: questionType.CategoryName + questionType.CategoryLevel,
320
+//         Difficulty: questionType.Difficulty,
321
+//         PageCount:questionNumber/param.PageNum,
322
+//         QuestionList: questionListResult,
323
+//     }
324
+//     //console.log(result);
325
+//     //var time=buildPrint(result);
326
+
327
+//     this.body = {"errcode": 10000,result:result};
328
+
329
+// }

+ 11 - 0
src/api/mathcalculate/routes.js

@@ -0,0 +1,11 @@
1
+import Router from 'koa-router';
2
+import * as math from './mathController.js';
3
+
4
+
5
+const router = new Router();
6
+
7
+router.post('/api/MathLogin',math.MathLogin);
8
+router.get('/api/GetQuestionTypes',math.GetQuestionTypes);
9
+//router.get('/api/GetQuestionTypesPrint',math.GetQuestionTypesPrint);
10
+
11
+export default router;

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

@@ -145,7 +145,7 @@ export async function GetMPSSchool(ctx) {
145 145
                 KeyName: cacheKey,
146 146
                 ValueString: JSON.stringify(result)
147 147
             });
148
-            console.log("数据已缓存");
148
+            //console.log("数据已缓存");
149 149
         } else {
150 150
             result = JSON.parse(cachedData[0].ValueString);
151 151
         }

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

@@ -9,10 +9,10 @@ import _ from 'lodash';
9 9
 // 更新学校点赞数
10 10
 export async function UpdateMPSUserSchoolLikeNum(ctx) {
11 11
     const param = {
12
-        UserID: ctx.request.body.UserID || 0,
13
-        SchoolID: ctx.request.body.SchoolID || 0,
14
-        SchoolType1: ctx.request.body.SchoolType1 || '高中',
15
-        LikeNum: ctx.request.body.LikeNum || 0
12
+        UserID: ctx.query.UserID || 0,
13
+        SchoolID: ctx.query.SchoolID || 0,
14
+        SchoolType1: ctx.query.SchoolType1 || '高中',
15
+        LikeNum: ctx.query.LikeNum || 0
16 16
     };
17 17
 
18 18
     var sql="select * from MPS_UserSchoolClickLike where UserID="+param.UserID+" and SchoolID="+param.SchoolID;

+ 1 - 1
src/api/web/webController.js

@@ -195,7 +195,7 @@ export async function GetKylx365TableColumnByTable(ctx) {
195 195
         TableName: ctx.query.table || "",
196 196
     };
197 197
     let result = await commonModel.RunSql(null,"SHOW COLUMNS FROM `"+param.TableName+"`;");
198
-    console.log("字段数:"+result.length);
198
+    //console.log("字段数:"+result.length);
199 199
     ctx.body = {"errcode": 10000, result: result};
200 200
 };
201 201
         

+ 3 - 0
src/app.js

@@ -13,6 +13,7 @@ import mpsRouter from './api/mps/routes.js';
13 13
 import phonicsRouter from './api/phonics/routes.js';
14 14
 import pinyinRouter from './api/pinyin/routes.js';
15 15
 import hanziRouter from './api/hanzi/routes.js';
16
+import mathcalculateRouter from './api/mathcalculate/routes.js';
16 17
 import webRouter from './api/web/routes.js';
17 18
 
18 19
 const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -74,6 +75,8 @@ app.use(pinyinRouter.routes());
74 75
 app.use(pinyinRouter.allowedMethods());
75 76
 app.use(hanziRouter.routes());
76 77
 app.use(hanziRouter.allowedMethods());
78
+app.use(mathcalculateRouter.routes());
79
+app.use(mathcalculateRouter.allowedMethods());
77 80
 app.use(webRouter.routes());
78 81
 app.use(webRouter.allowedMethods());
79 82
 

+ 98 - 0
src/model/mathcalculate.js

@@ -0,0 +1,98 @@
1
+import { query } from '../util/db.js';
2
+
3
+/**
4
+ * MathCalculation 模型
5
+ */
6
+class MathCalculation {
7
+    /**
8
+     * 获取用户信息(通过OpenID)
9
+     * @param {Object} obj 查询参数
10
+     * @returns {Promise<Array>} 用户信息
11
+     */
12
+    static async GetUsersInfo(obj) {
13
+        try {
14
+            const sql = "select * from MathCalculationWXUsers where OpenID=?";
15
+            return await query(sql, [obj.OpenID]);
16
+        } catch (error) {
17
+            console.error('获取用户信息失败:', error);
18
+            throw error;
19
+        }
20
+    }
21
+
22
+    /**
23
+     * 获取用户信息(通过UserID)
24
+     * @param {Object} obj 查询参数
25
+     * @returns {Promise<Array>} 用户信息
26
+     */
27
+    static async GetUsersInfoByUserID(obj) {
28
+        try {
29
+            const sql = "select * from MathCalculationWXUsers where UserID=?";
30
+            return await query(sql, [obj.UserID]);
31
+        } catch (error) {
32
+            console.error('获取用户信息失败:', error);
33
+            throw error;
34
+        }
35
+    }
36
+
37
+    /**
38
+     * 添加用户
39
+     * @param {Object} obj 用户信息
40
+     * @returns {Promise<Object>} 插入结果
41
+     */
42
+    static async AddUsers(obj) {
43
+        try {
44
+            const sql = "INSERT INTO MathCalculationWXUsers SET ?";
45
+            return await query(sql, [obj]);
46
+        } catch (error) {
47
+            console.error('添加用户失败:', error);
48
+            throw error;
49
+        }
50
+    }
51
+
52
+    /**
53
+     * 更新用户信息(通过OpenID)
54
+     * @param {Object} obj 更新参数
55
+     * @returns {Promise<Object>} 更新结果
56
+     */
57
+    static async UpdateUsers(obj) {
58
+        try {
59
+            const sql = "Update MathCalculationWXUsers SET ? where OpenID=?";
60
+            return await query(sql, [obj, obj.OpenID]);
61
+        } catch (error) {
62
+            console.error('更新用户信息失败:', error);
63
+            throw error;
64
+        }
65
+    }
66
+
67
+    /**
68
+     * 更新用户信息(通过UserID)
69
+     * @param {Object} obj 更新参数
70
+     * @returns {Promise<Object>} 更新结果
71
+     */
72
+    static async UpdateUsersByUserID(obj) {
73
+        try {
74
+            const sql = "Update MathCalculationWXUsers SET ? where UserID=?";
75
+            return await query(sql, [obj, obj.UserID]);
76
+        } catch (error) {
77
+            console.error('更新用户信息失败:', error);
78
+            throw error;
79
+        }
80
+    }
81
+
82
+    static async GetQuestionTypesList (obj) {
83
+        let str=" and qt.QuestionNumber>0";
84
+        if (obj && obj.QuestionMode==2){
85
+            str="";
86
+        }
87
+        try {
88
+            const sql = "SELECT qc.*,qt.* FROM QuestionTypes qt inner join QuestionCategory qc on qt.Category=qc.CategoryID where qt.Flag=0 "+str+" order by qc.CategoryID,qt.Difficulty,qt.Sort,qt.ID;";
89
+            return await query(sql, [str]);
90
+        } catch (error) {
91
+            console.error('更新用户信息失败:', error);
92
+            throw error;
93
+        }
94
+
95
+    }
96
+}
97
+
98
+export default MathCalculation;