chengjie před 5 měsíci
rodič
revize
edead0ef8b
3 změnil soubory, kde provedl 156 přidání a 98 odebrání
  1. 105 98
      src/api/mps/mpsSchoolController.js
  2. 13 0
      src/util/stringClass.js
  3. 38 0
      test-string-validation.js

+ 105 - 98
src/api/mps/mpsSchoolController.js

@@ -31,130 +31,137 @@ export async function GetMPSSchool(ctx) {
31 31
             RowCount: ctx.query.RowCount || 4,
32 32
         };
33 33
 
34
-        // 生成缓存键
35
-        const cacheKey = `GetMPSSchool?SchoolType1=${param.SchoolType1}&DistrictID=${param.DistrictID}&SelectType=${param.SelectType}&Key=${param.Key}&RowCount=${param.RowCount}&Usage=${param.Usage}`;
34
+        if (param.Key && !stringUtils.IsValidChineseNumberParentheses(param.Key)){
35
+            ctx.body = { errcode: 500, message: "获取学校列表失败" };
36
+        }
37
+        else{
36 38
 
37
-        // 尝试从缓存获取
38
-        let cachedData = await BufferMemoryClass.get(cacheKey);
39
-        let result;
39
+            // 生成缓存键
40
+            const cacheKey = `GetMPSSchool?SchoolType1=${param.SchoolType1}&DistrictID=${param.DistrictID}&SelectType=${param.SelectType}&Key=${param.Key}&RowCount=${param.RowCount}&Usage=${param.Usage}`;
40 41
 
41
-        if (!cachedData || cachedData.length === 0) {
42
-            param.District = getDistrict(Number(param.DistrictID));
43
-            
44
-            let sql="SELECT ID,SchoolFullName,SchoolShortName,SchoolOtherName,SchoolNumber,SchoolType1,SchoolType2," +
45
-            "SchoolType2Short,PublicOrPrivate,District,Address,Accommodation,Fees,ImageLogo,Pinyin,Coordinates " +
46
-            "FROM kylx365_db.MPS_School where Flag=0";
47
-            if (param.SchoolType1)
48
-                sql+=" and SchoolType1='"+param.SchoolType1+"' ";
49
-
50
-            if (param.Key) {
51
-                if (stringUtils.IsNumber(param.Key)){
52
-                    sql+=" and ID="+param.Key+";";
42
+            // 尝试从缓存获取
43
+            let cachedData = await BufferMemoryClass.get(cacheKey);
44
+            let result;
45
+
46
+            if (!cachedData || cachedData.length === 0) {
47
+                param.District = getDistrict(Number(param.DistrictID));
48
+                
49
+                let sql="SELECT ID,SchoolFullName,SchoolShortName,SchoolOtherName,SchoolNumber,SchoolType1,SchoolType2," +
50
+                "SchoolType2Short,PublicOrPrivate,District,Address,Accommodation,Fees,ImageLogo,Pinyin,Coordinates " +
51
+                "FROM kylx365_db.MPS_School where Flag=0";
52
+                if (param.SchoolType1)
53
+                    sql+=" and SchoolType1='"+param.SchoolType1+"' ";
54
+
55
+                if (param.Key) {
56
+                    if (stringUtils.IsNumber(param.Key)){
57
+                        sql+=" and ID="+param.Key+";";
58
+                    }
59
+                    else{
60
+                        sql += " and (SchoolFullName like '%" + param.Key + "%' or SchoolShortName like '%" + param.Key + "%' or SchoolOtherName like '%" + param.Key + "%' or SchoolNumber = '" + param.Key + "')"
61
+                        if (Number(param.RowCount)>0)
62
+                            sql+=" order by SchoolType1 desc,OrderID limit "+param.RowCount+";";
63
+                        else
64
+                            sql+=" order by SchoolType1 desc,OrderID;";
65
+                    }
66
+                    
53 67
                 }
54 68
                 else{
55
-                    sql += " and (SchoolFullName like '%" + param.Key + "%' or SchoolShortName like '%" + param.Key + "%' or SchoolOtherName like '%" + param.Key + "%' or SchoolNumber = '" + param.Key + "')"
56
-                    if (Number(param.RowCount)>0)
57
-                        sql+=" order by SchoolType1 desc,OrderID limit "+param.RowCount+";";
58
-                    else
59
-                        sql+=" order by SchoolType1 desc,OrderID;";
60
-                }
61
-                
62
-            }
63
-            else{
64
-                if (param.District)
65
-                    sql+=" and (District='"+param.District+"' or District='上海市')";
69
+                    if (param.District)
70
+                        sql+=" and (District='"+param.District+"' or District='上海市')";
66 71
 
67
-                if (param.SchoolType1=="高中")
68
-                    if (param.SchoolType2Short=="市重点")
69
-                        sql+=" order by ID;";
72
+                    if (param.SchoolType1=="高中")
73
+                        if (param.SchoolType2Short=="市重点")
74
+                            sql+=" order by ID;";
75
+                        else
76
+                            sql+=" order by CONVERT(SchoolFullName USING gbk);";
77
+                    else if (param.Usage=="web")
78
+                        sql+=" order by DistrictID,SchoolType2ShortID,ID;";
70 79
                     else
71 80
                         sql+=" order by CONVERT(SchoolFullName USING gbk);";
72
-                else if (param.Usage=="web")
73
-                    sql+=" order by DistrictID,SchoolType2ShortID,ID;";
74
-                else
75
-                    sql+=" order by CONVERT(SchoolFullName USING gbk);";
76
-            }
81
+                }
77 82
 
78
-            //console.log(sql);
79
-            
80
-            let list = await commonModel.RunSql({}, sql);
81
-
82
-            if (param.SelectType === "structure") {
83
-                let item=[];
84
-                item.push({Name:"市重点",List:[]});
85
-                item.push({Name:"区重点",List:[]});
86
-                item.push({Name:"一般高中",List:[]});
87
-                item.push({Name:"初中",List:[]});
88
-
89
-                for(let i=0;i<list.length;i++){
90
-                    if (list[i].SchoolType1=="初中"){
91
-                        item[3].List.push(list[i]);
92
-                    }
93
-                    else if (list[i].SchoolType2Short=="市重点"){
94
-                        if (list[i].District=="上海市") {
95
-                            if (list[i].SchoolShortName=="上海中学" && param.DistrictID=="2")
96
-                                item[0].List.push(list[i]);
97
-                            else if (list[i].SchoolShortName=="交大附中" && param.DistrictID=="7")
98
-                                item[0].List.push(list[i]);
99
-                            else if (list[i].SchoolShortName=="复旦附中" && param.DistrictID=="7")
100
-                                item[0].List.push(list[i]);
101
-                            else if (list[i].SchoolShortName=="华师二附" && param.DistrictID=="11")
102
-                                item[0].List.push(list[i]);
103
-                            else if (list[i].SchoolShortName=="上师附中" && param.DistrictID=="11")
104
-                                item[0].List.push(list[i]);
105
-                            else if (list[i].SchoolShortName=="上海实验" && param.DistrictID=="11")
106
-                                item[0].List.push(list[i]);
107
-                            else if (list[i].SchoolShortName=="上外附中" && param.DistrictID=="11")
108
-                                item[0].List.push(list[i]);
83
+                //console.log(sql);
84
+                
85
+                let list = await commonModel.RunSql({}, sql);
86
+
87
+                if (param.SelectType === "structure") {
88
+                    let item=[];
89
+                    item.push({Name:"市重点",List:[]});
90
+                    item.push({Name:"区重点",List:[]});
91
+                    item.push({Name:"一般高中",List:[]});
92
+                    item.push({Name:"初中",List:[]});
93
+
94
+                    for(let i=0;i<list.length;i++){
95
+                        if (list[i].SchoolType1=="初中"){
96
+                            item[3].List.push(list[i]);
97
+                        }
98
+                        else if (list[i].SchoolType2Short=="市重点"){
99
+                            if (list[i].District=="上海市") {
100
+                                if (list[i].SchoolShortName=="上海中学" && param.DistrictID=="2")
101
+                                    item[0].List.push(list[i]);
102
+                                else if (list[i].SchoolShortName=="交大附中" && param.DistrictID=="7")
103
+                                    item[0].List.push(list[i]);
104
+                                else if (list[i].SchoolShortName=="复旦附中" && param.DistrictID=="7")
105
+                                    item[0].List.push(list[i]);
106
+                                else if (list[i].SchoolShortName=="华师二附" && param.DistrictID=="11")
107
+                                    item[0].List.push(list[i]);
108
+                                else if (list[i].SchoolShortName=="上师附中" && param.DistrictID=="11")
109
+                                    item[0].List.push(list[i]);
110
+                                else if (list[i].SchoolShortName=="上海实验" && param.DistrictID=="11")
111
+                                    item[0].List.push(list[i]);
112
+                                else if (list[i].SchoolShortName=="上外附中" && param.DistrictID=="11")
113
+                                    item[0].List.push(list[i]);
109 114
 
115
+                            }
116
+                            else
117
+                                item[0].List.push(list[i]);
110 118
                         }
111
-                        else
112
-                            item[0].List.push(list[i]);
113
-                    }
114
-                    else if (list[i].SchoolType2Short=="一般高中"){
115
-                        if (list[i].District=="上海市") {
116
-                            if (list[i].SchoolShortName=="上科大附校" && param.DistrictID=="11")
119
+                        else if (list[i].SchoolType2Short=="一般高中"){
120
+                            if (list[i].District=="上海市") {
121
+                                if (list[i].SchoolShortName=="上科大附校" && param.DistrictID=="11")
122
+                                    item[2].List.push(list[i]);
123
+                            }
124
+                            else
117 125
                                 item[2].List.push(list[i]);
118 126
                         }
119
-                        else
120
-                            item[2].List.push(list[i]);
121
-                    }
122
-                    else {
123
-                        item[1].List.push(list[i]);
127
+                        else {
128
+                            item[1].List.push(list[i]);
129
+                        }
124 130
                     }
125
-                }
126
-                for (let j=0;j<4;j++) {
131
+                    for (let j=0;j<4;j++) {
127 132
 
128
-                    item[j].List=_.orderBy(item[j].List, ['Pinyin','SchoolFullName'], ['asc','asc']);
133
+                        item[j].List=_.orderBy(item[j].List, ['Pinyin','SchoolFullName'], ['asc','asc']);
129 134
 
130
-                    for (let i = 0; i < item[j].List.length; i++) {
131
-                        if (i > 0)
132
-                            item[j].List[i].Pinyin2 = item[j].List[i - 1].Pinyin;
133
-                        else
134
-                            item[j].List[i].Pinyin2 = "";
135
+                        for (let i = 0; i < item[j].List.length; i++) {
136
+                            if (i > 0)
137
+                                item[j].List[i].Pinyin2 = item[j].List[i - 1].Pinyin;
138
+                            else
139
+                                item[j].List[i].Pinyin2 = "";
140
+                        }
135 141
                     }
142
+                    result=item;
143
+
144
+                } else {
145
+                    result = list;
136 146
                 }
137
-                result=item;
138 147
 
148
+                // 缓存结果
149
+                await BufferMemoryClass.add({
150
+                    KeyName: cacheKey,
151
+                    ValueString: JSON.stringify(result)
152
+                });
153
+                //console.log("数据已缓存");
139 154
             } else {
140
-                result = list;
155
+                result = JSON.parse(cachedData[0].ValueString);
141 156
             }
142 157
 
143
-            // 缓存结果
144
-            await BufferMemoryClass.add({
145
-                KeyName: cacheKey,
146
-                ValueString: JSON.stringify(result)
147
-            });
148
-            //console.log("数据已缓存");
149
-        } else {
150
-            result = JSON.parse(cachedData[0].ValueString);
158
+            ctx.body = { errcode: 10000, result };
151 159
         }
152
-
153
-        ctx.body = { errcode: 10000, result };
154 160
     } catch (error) {
155 161
         console.error("获取学校列表失败:", error);
156 162
         ctx.body = { errcode: 500, message: "获取学校列表失败" };
157 163
     }
164
+    
158 165
 }
159 166
 
160 167
 

+ 13 - 0
src/util/stringClass.js

@@ -539,6 +539,19 @@ export const stringUtils = {
539 539
         const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
540 540
         return ipv6Regex.test(ip);
541 541
     },
542
+    // 判断字符串是否只包含汉字、数字、大小写字母和全/半角小括号
543
+    IsValidChineseNumberParentheses: (str) => {
544
+        if (!str) return true; // 空字符串视为有效
545
+        // 匹配规则:
546
+        // [\u4e00-\u9fa5]: 匹配汉字
547
+        // 0-9: 匹配数字
548
+        // a-zA-Z: 匹配大小写字母
549
+        // (): 匹配半角小括号
550
+        // (): 匹配全角小括号
551
+        const regex = /^[\u4e00-\u9fa5\da-zA-Z()()]+$/;
552
+        return regex.test(str);
553
+    },
554
+
542 555
     // 获取客户端真实IP地址的通用函数
543 556
     GetClientIP: (ctx) => {
544 557
         // 检查所有可能的代理头

+ 38 - 0
test-string-validation.js

@@ -0,0 +1,38 @@
1
+'use strict';
2
+import { stringUtils } from './src/util/stringClass.js';
3
+
4
+// 测试用例
5
+const testCases = [
6
+    { input: '你好123', expected: true, desc: '汉字和数字' },
7
+    { input: '测试(123)', expected: true, desc: '汉字、数字和半角括号' },
8
+    { input: '测试(456)', expected: true, desc: '汉字、数字和全角括号' },
9
+    { input: '测试(123)(456)', expected: true, desc: '混合使用全角和半角括号' },
10
+    { input: '', expected: true, desc: '空字符串' },
11
+    { input: '测试abc', expected: true, desc: '包含小写英文字母' },
12
+    { input: '测试ABC', expected: true, desc: '包含大写英文字母' },
13
+    { input: '测试aBC123', expected: true, desc: '混合大小写字母和数字' },
14
+    { input: 'Hello世界123', expected: true, desc: '混合英文、汉字和数字' },
15
+    { input: '测试123!', expected: false, desc: '包含特殊字符' },
16
+    { input: '测试[123]', expected: false, desc: '包含方括号' },
17
+    { input: '测试{123}', expected: false, desc: '包含花括号' },
18
+    { input: 'Test@123', expected: false, desc: '包含@符号' }
19
+];
20
+
21
+// 运行测试
22
+console.log('开始测试 IsValidChineseNumberParentheses 函数...');
23
+console.log('----------------------------------------');
24
+
25
+let passCount = 0;
26
+for (const test of testCases) {
27
+    const result = stringUtils.IsValidChineseNumberParentheses(test.input);
28
+    const passed = result === test.expected;
29
+    
30
+    console.log(`测试: ${test.desc}`);
31
+    console.log(`输入: "${test.input}"`);
32
+    console.log(`期望: ${test.expected}, 实际: ${result}, 结果: ${passed ? '通过' : '失败'}`);
33
+    console.log('----------------------------------------');
34
+    
35
+    if (passed) passCount++;
36
+}
37
+
38
+console.log(`测试完成: ${passCount}/${testCases.length} 通过`);