xzwebx-httpfilter 2.2.6 → 2.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/HttpFilter.js +444 -406
  2. package/package.json +5 -2
package/HttpFilter.js CHANGED
@@ -2,21 +2,55 @@ const Fs = require('fs')
2
2
  const Util = require("util")
3
3
  const Path = require('path')
4
4
 
5
- let ResultCodeMap = {}
6
- let TipsMap = {}
7
- let ModuleMap = {}
8
- let ModuleInterfaceMap = {}
9
- let FieldMap = {}
10
- let IsCheckedRes = true
5
+ let ConfigMap = {}
11
6
  let Fun = {}
12
- Fun.Init = function(language, resultCodeMap, tipsMap, moduleMap, moduleInterfaceMap, fieldMap) {
13
- ResultCodeMap = resultCodeMap
14
- TipsMap = tipsMap[language] || {}
15
- ModuleMap = moduleMap
16
- ModuleInterfaceMap = moduleInterfaceMap
17
- FieldMap = fieldMap
7
+
8
+ Fun.SetModuleMap = function(port, m) {
9
+ if (!ConfigMap[port]) {
10
+ ConfigMap[port] = new http()
11
+ }
12
+ ConfigMap[port].ModuleMap = m
13
+ }
14
+ Fun.SetModuleInterfaceMap = function(port, m) {
15
+ if (!ConfigMap[port]) {
16
+ ConfigMap[port] = new http()
17
+ }
18
+ ConfigMap[port].ModuleInterfaceMap = m
19
+ }
20
+ Fun.SetFieldMap = function(port, m) {
21
+ if (!ConfigMap[port]) {
22
+ ConfigMap[port] = new http()
23
+ }
24
+ ConfigMap[port].FieldMap = m
25
+ }
26
+ Fun.SetResultCodeMap = function(port, m) {
27
+ if (!ConfigMap[port]) {
28
+ ConfigMap[port] = new http()
29
+ }
30
+ ConfigMap[port].ResultCodeMap = m
31
+ }
32
+ Fun.SetTipsMap = function(port, m) {
33
+ if (!ConfigMap[port]) {
34
+ ConfigMap[port] = new http()
35
+ }
36
+ ConfigMap[port].TipsMap = m
37
+ }
38
+ Fun.SetIsCheckedRes = function(port, isCheckedRes) {
39
+ if (!ConfigMap[port]) {
40
+ ConfigMap[port] = new http()
41
+ }
42
+ ConfigMap[port].IsCheckedRes = isCheckedRes
43
+ }
44
+ Fun.SetFirstFilter = function(app) {
45
+ let p = ConfigMap[app.get('_PORT_')]
46
+ app.use(p.setMsg.bind(p))
47
+ app.use(p.checkUrl.bind(p))
48
+ app.use(p.checkReq.bind(p))
18
49
  }
19
50
  Fun.SetRoutes = function(app, express, projectPath) {
51
+ const port = app.get('_PORT_')
52
+ let ModuleMap = ConfigMap[port].ModuleMap
53
+ let ModuleInterfaceMap = ConfigMap[port].ModuleInterfaceMap
20
54
  for (let uri in ModuleMap) {
21
55
  let module = ModuleMap[uri]
22
56
  let router = express.Router()
@@ -32,7 +66,7 @@ Fun.SetRoutes = function(app, express, projectPath) {
32
66
  if (subUri[0] != '/') {
33
67
  subUri = '/' + subUri
34
68
  }
35
- router[method.toLowerCase()](subUri, ModuleHandle && ModuleHandle[api.moduleFun]? ModuleHandle[api.moduleFun]: response)
69
+ router[method.toLowerCase()](subUri, ModuleHandle && ModuleHandle[api.moduleFun]? ModuleHandle[api.moduleFun]: webCommResponse)
36
70
  }
37
71
  }
38
72
  if (ModuleMap[uri].uri[0] != '/') {
@@ -41,497 +75,501 @@ Fun.SetRoutes = function(app, express, projectPath) {
41
75
  app.use(ModuleMap[uri].uri, router)
42
76
  }
43
77
  }
44
- Fun.CheckUrl = function(req, res, next) {
45
- const baseUrlList = req.url.split('/')
46
- const subUri = baseUrlList[baseUrlList.length - 1]
47
- let baseUrl = ''
48
- for (let idx in baseUrlList) {
49
- if (idx == baseUrlList.length - 1) {
50
- break
51
- }
52
- if (baseUrlList[idx]) {
53
- baseUrl += '/' + baseUrlList[idx]
54
- }
55
- }
56
-
57
- const module = ModuleMap[baseUrl]
58
- if (!module) {
59
- return res.Msg({codeKey: 'CLT_ERR', msg: ['WEBX_ERR_URL']})
60
- }
61
-
62
- if (!ModuleInterfaceMap[module.id] ||
63
- !ModuleInterfaceMap[module.id][subUri] ||
64
- !ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]) {
65
- return res.Msg({codeKey: 'CLT_ERR', msg: ['WEBX_ERR_URL']})
66
- }
67
-
68
- req.interfaceInfo = ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]
69
- res.interfaceInfo = ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]
70
- return next()
71
- }
72
- Fun.CheckReq = function(req, res, next) {
73
- let reqMsgId = req.interfaceInfo.reqMsgId
74
- if (!reqMsgId) {
75
- return next()
76
- }
77
-
78
- let retMsgData = cycleCheckParams(FieldMap[reqMsgId], req.body)
79
- if (retMsgData) {
80
- return res.Msg({codeKey: 'CLT_ERR', msg: retMsgData})
81
- }
82
-
83
- return next()
84
- }
85
78
  Fun.SetMsg = function(req, res, next) {
86
- res.Msg = MSG
79
+ res.Msg = response
87
80
  return next()
88
81
  }
89
- Fun.SetIsCheckedRes = function(isCheckedRes) {
90
- if (isCheckedRes) {
91
- IsCheckedRes = true
92
- } else {
93
- IsCheckedRes = false
94
- }
82
+
83
+ Fun.SetCheckUrl = function(app) {
84
+ let p = ConfigMap[app.get('_PORT_')]
85
+ app.use(p.setMsg.bind(p))
86
+ app.use(p.checkUrl.bind(p))
95
87
  }
96
88
 
97
- async function response(req, res, next) {
98
- let retData = {
99
- codeKey: 'SUCC',
100
- data: []
89
+ class http {
90
+ constructor(){
91
+ this.ModuleMap = {}
92
+ this.ModuleInterfaceMap = {}
93
+ this.FieldMap = {}
94
+ this.ResultCodeMap = {}
95
+ this.TipsMap = {}
96
+ this.IsCheckedRes = false
101
97
  }
102
- return res.Msg(retData)
103
- }
104
- function MSG(params) {
105
- if (IsCheckedRes &&
106
- this.interfaceInfo &&
107
- this.interfaceInfo.rspMsgId &&
108
- params.codeKey == 'SUCC') {
109
- let retMsgData = cycleCheckParams(FieldMap[this.interfaceInfo.rspMsgId], params.data)
110
- if (retMsgData) {
111
- params.codeKey = 'SVC_ERR'
112
- params.msg = retMsgData
98
+ checkUrl(req, res, next) {
99
+ const baseUrlList = req.url.split('/')
100
+ const subUri = baseUrlList[baseUrlList.length - 1]
101
+ let baseUrl = ''
102
+ for (let idx in baseUrlList) {
103
+ if (idx == baseUrlList.length - 1) {
104
+ break
105
+ }
106
+ if (baseUrlList[idx]) {
107
+ baseUrl += '/' + baseUrlList[idx]
108
+ }
109
+ }
110
+
111
+ const module = this.ModuleMap[baseUrl]
112
+ if (!module) {
113
+ return res.Msg({codeKey: 'CLT_ERR', msg: ['WEBX_ERR_URL']})
113
114
  }
115
+
116
+ if (!this.ModuleInterfaceMap[module.id] ||
117
+ !this.ModuleInterfaceMap[module.id][subUri] ||
118
+ !this.ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]) {
119
+ return res.Msg({codeKey: 'CLT_ERR', msg: ['WEBX_ERR_URL']})
120
+ }
121
+
122
+ req.interfaceInfo = this.ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]
123
+ res.interfaceInfo = this.ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]
124
+ return next()
114
125
  }
115
- let code = ''
116
- if (ResultCodeMap[params.codeKey]) {
117
- code = ResultCodeMap[params.codeKey].rstCode
126
+ setMsg(req, res, next) {
127
+ res.Msg = response
128
+ return next()
118
129
  }
119
-
120
- let msg = ''
121
- if (!params.msg) {
122
- if (TipsMap[params.codeKey]) {
123
- msg = TipsMap[params.codeKey].tips
124
- } else {
125
- msg = ResultCodeMap[params.codeKey].codeDesc
130
+ checkReq(req, res, next) {
131
+ let reqMsgId = req.interfaceInfo.reqMsgId
132
+ if (!reqMsgId) {
133
+ return next()
126
134
  }
127
- } else {
128
- if (typeof params.msg == 'string') {
129
- msg = params.msg
135
+
136
+ let retMsgData = this.cycleCheckParams(this.FieldMap[reqMsgId], req.body)
137
+ if (retMsgData) {
138
+ return res.Msg({codeKey: 'CLT_ERR', msg: retMsgData})
130
139
  }
131
-
132
- if (typeof params.msg == 'object' && params.msg.length) {
133
- if (TipsMap[params.msg[0]]) {
134
- msg = TipsMap[params.msg[0]].tips
135
- if (params.msg.length > 1) {
136
- for (let i = 1; i < params.msg.length; i++) {
137
- msg = Util.format(msg, params.msg[i])
140
+
141
+ return next()
142
+ }
143
+ cycleCheckParams(msgFieldMap, data) {
144
+ let retMsgData = null
145
+
146
+ if (!msgFieldMap) {
147
+ return null
148
+ }
149
+
150
+ let fCfgItem = null
151
+ let isRoot = true
152
+ if (msgFieldMap['__FieldCfg']) {
153
+ isRoot = false
154
+ fCfgItem = msgFieldMap['__FieldCfg']
155
+ }
156
+
157
+ for (let fieldName in msgFieldMap) {
158
+ if (isRoot) {
159
+ fCfgItem = msgFieldMap[fieldName]['__FieldCfg']
160
+ }
161
+
162
+ if (fCfgItem.fieldType == 'STR') {
163
+ retMsgData = this.isStringOk(fCfgItem, data)
164
+ } else if (fCfgItem.fieldType == 'INT') {
165
+ retMsgData = this.isIntOk(fCfgItem, data)
166
+ } else if (fCfgItem.fieldType == 'OBJ') {
167
+ retMsgData = this.isObjOk(fCfgItem, data)
168
+ } else if (fCfgItem.fieldType == 'LIST') {
169
+ retMsgData = this.isListOk(fCfgItem, data)
170
+ }
171
+
172
+ if (retMsgData) {
173
+ return retMsgData
174
+ }
175
+
176
+ if (fieldName == '__FieldCfg') {
177
+ continue
178
+ }
179
+ let fatherFieldType = fCfgItem.fieldType
180
+ if (fatherFieldType == 'LIST' || (fatherFieldType == 'OBJ' && fCfgItem.keyType == 'VOBJ')) {
181
+ if (isRoot) {
182
+ retMsgData = this.cycleCheckParams(msgFieldMap[fieldName], data)
183
+ } else {
184
+ for (let idx in data) {
185
+ retMsgData = this.cycleCheckParams(msgFieldMap[fieldName], data[idx])
186
+ if (retMsgData) {
187
+ return retMsgData
188
+ }
138
189
  }
139
190
  }
140
191
  } else {
141
- msg = params.msg[0]
192
+ if (isRoot) {
193
+ retMsgData = this.cycleCheckParams(msgFieldMap[fieldName], data)
194
+ } else {
195
+ retMsgData = this.cycleCheckParams(msgFieldMap[fieldName], data[fieldName])
196
+ }
197
+ }
198
+
199
+ if (retMsgData) {
200
+ return retMsgData
142
201
  }
143
202
  }
144
- }
145
-
146
- return this.send(
147
- {
148
- code: code,
149
- msg: msg,
150
- data: params.data
151
- }
152
- )
153
- }
154
- function cycleCheckParams(msgFieldMap, data) {
155
- let retMsgData = null
156
-
157
- if (!msgFieldMap) {
158
203
  return null
159
204
  }
160
-
161
- let fCfgItem = null
162
- let isRoot = true
163
- if (msgFieldMap['__FieldCfg']) {
164
- isRoot = false
165
- fCfgItem = msgFieldMap['__FieldCfg']
166
- }
167
-
168
- for (let fieldName in msgFieldMap) {
169
- if (isRoot) {
170
- fCfgItem = msgFieldMap[fieldName]['__FieldCfg']
205
+ isStringOk(fCfgItem, paramValue) {
206
+ if (!fCfgItem || !fCfgItem.ifMust) {
207
+ return null
171
208
  }
172
-
173
- if (fCfgItem.fieldType == 'STR') {
174
- retMsgData = IsStringOk(fCfgItem, data)
175
- } else if (fCfgItem.fieldType == 'INT') {
176
- retMsgData = IsIntOk(fCfgItem, data)
177
- } else if (fCfgItem.fieldType == 'OBJ') {
178
- retMsgData = IsObjOk(fCfgItem, data)
179
- } else if (fCfgItem.fieldType == 'LIST') {
180
- retMsgData = IsListOk(fCfgItem, data)
209
+
210
+ if (fCfgItem.ifMust == 'NO' && (
211
+ paramValue == undefined ||
212
+ paramValue == null ||
213
+ paramValue == '' ||
214
+ (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
215
+ ) {
216
+ return null
181
217
  }
182
-
183
- if (retMsgData) {
184
- return retMsgData
185
- }
186
-
187
- if (fieldName == '__FieldCfg') {
188
- continue
218
+
219
+ if (!paramValue || typeof paramValue != 'string') {
220
+ if (fCfgItem.nullTips) {
221
+ return this.getCustomTips(fCfgItem.nullTips)
222
+ }
223
+ return ['WEBX_NULL_FIELD', 'string', fCfgItem.fieldUrl]
189
224
  }
190
- let fatherFieldType = fCfgItem.fieldType
191
- if (fatherFieldType == 'LIST' || (fatherFieldType == 'OBJ' && fCfgItem.keyType == 'VOBJ')) {
192
- if (isRoot) {
193
- retMsgData = cycleCheckParams(msgFieldMap[fieldName], data)
194
- } else {
195
- for (let idx in data) {
196
- retMsgData = cycleCheckParams(msgFieldMap[fieldName], data[idx])
197
- if (retMsgData) {
198
- return retMsgData
225
+
226
+ for (let idx in fCfgItem.rules) {
227
+ let rule = fCfgItem.rules[idx]
228
+ if (rule.checkType == 'RANGE') {
229
+ let isPass = false
230
+ if (typeof rule.exprVal[0] == 'object') {
231
+ for (let i in rule.exprVal) {
232
+ if (paramValue.length >= parseInt(rule.exprVal[i][0]) && paramValue.length <= parseInt(rule.exprVal[i][1])) {
233
+ isPass = true
234
+ break
235
+ }
236
+ }
237
+ } else {
238
+ if (paramValue.length >= parseInt(rule.exprVal[0]) && paramValue.length <= parseInt(rule.exprVal[1])) {
239
+ isPass = true
199
240
  }
200
241
  }
201
- }
202
- } else {
203
- if (isRoot) {
204
- retMsgData = cycleCheckParams(msgFieldMap[fieldName], data)
205
- } else {
206
- retMsgData = cycleCheckParams(msgFieldMap[fieldName], data[fieldName])
242
+
243
+ if (!isPass) {
244
+ if (rule.ruleDesc) {
245
+ return this.getCustomTips(rule.ruleDesc)
246
+ }
247
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
248
+ }
249
+ } else if (rule.checkType == 'ENU') {
250
+ if (rule.isCaseSensitive == 1) {
251
+ if (rule.exprVal && rule.exprVal.length) {
252
+ if (rule.isMatched === 1) {
253
+ if (!rule.exprVal.includes(paramValue)) {
254
+ if (rule.ruleDesc) {
255
+ return this.getCustomTips(rule.ruleDesc)
256
+ }
257
+ return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
258
+ }
259
+ } else {
260
+ if (rule.exprVal.includes(paramValue)) {
261
+ if (rule.ruleDesc) {
262
+ return this.getCustomTips(rule.ruleDesc)
263
+ }
264
+ return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
265
+ }
266
+ }
267
+ }
268
+ } else {
269
+ let isPass = false
270
+ for (let k in rule.exprVal) {
271
+ if (rule.isMatched === 1) {
272
+ if (rule.exprVal[k].toUpperCase() === paramValue.toUpperCase()) {
273
+ isPass = true
274
+ break
275
+ }
276
+ } else {
277
+ isPass = true
278
+ if (rule.exprVal[k].toUpperCase() == paramValue.toUpperCase()) {
279
+ isPass = false
280
+ break
281
+ }
282
+ }
283
+ }
284
+ if (!isPass) {
285
+ if (rule.ruleDesc) {
286
+ return this.getCustomTips(rule.ruleDesc)
287
+ }
288
+ if (rule.isMatched === 1) {
289
+ return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
290
+ } else {
291
+ return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
292
+ }
293
+ }
294
+ }
295
+ } else if (rule.checkType == 'REGEX') {
296
+ let isPass = false
297
+ let exprVal = rule.exprVal + ''
298
+ for (let i in rule.exprVal) {
299
+ let reg = eval(rule.exprVal[i])
300
+ let found = reg.test(paramValue)
301
+ if (rule.matchType == 'AND') {
302
+ if (!found) {
303
+ exprVal = rule.exprVal[i] + ''
304
+ break
305
+ }
306
+ } else {
307
+ if (found) {
308
+ isPass = true
309
+ break
310
+ }
311
+ }
312
+ }
313
+ if (!isPass) {
314
+ if (rule.ruleDesc) {
315
+ return this.getCustomTips(rule.ruleDesc)
316
+ }
317
+ return ['WEBX_WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
318
+ }
207
319
  }
208
320
  }
209
-
210
- if (retMsgData) {
211
- return retMsgData
212
- }
213
- }
214
- return null
215
- }
216
-
217
- function getCustomTips(tips) {
218
- if (tips.length > 3 && tips.substring(0, 2) === '${' && tips.substring(tips.length -1, tips.length) === '}') {
219
- let tipsKey = tips.substring(2, tips.length -1)
220
- tipsKey = tipsKey.trim()
221
- if (TipsMap[tipsKey]) {
222
- return TipsMap[tipsKey].tips
223
- }
224
- }
225
- return tips
226
- }
227
- function IsStringOk(fCfgItem, paramValue) {
228
- if (!fCfgItem || !fCfgItem.ifMust) {
321
+
229
322
  return null
230
323
  }
231
-
232
- if (fCfgItem.ifMust == 'NO' && (
233
- paramValue == undefined ||
234
- paramValue == null ||
235
- paramValue == '' ||
236
- (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
237
- ) {
238
- return null
239
- }
240
-
241
- if (!paramValue || typeof paramValue != 'string') {
242
- if (fCfgItem.nullTips) {
243
- return getCustomTips(fCfgItem.nullTips)
324
+ isIntOk(fCfgItem, paramValue) {
325
+ if (!fCfgItem || !fCfgItem.ifMust) {
326
+ return null
244
327
  }
245
- return ['WEBX_NULL_FIELD', 'string', fCfgItem.fieldUrl]
246
- }
247
-
248
- for (let idx in fCfgItem.rules) {
249
- let rule = fCfgItem.rules[idx]
250
- if (rule.checkType == 'RANGE') {
251
- let isPass = false
252
- if (typeof rule.exprVal[0] == 'object') {
253
- for (let i in rule.exprVal) {
254
- if (paramValue.length >= parseInt(rule.exprVal[i][0]) && paramValue.length <= parseInt(rule.exprVal[i][1])) {
328
+
329
+ if (fCfgItem.ifMust == 'NO' && (
330
+ paramValue == undefined ||
331
+ paramValue == null ||
332
+ paramValue == '' ||
333
+ (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
334
+ ) {
335
+ return null
336
+ }
337
+
338
+ if (paramValue == undefined || typeof paramValue != 'number') {
339
+ if (fCfgItem.nullTips) {
340
+ return this.getCustomTips(fCfgItem.nullTips)
341
+ }
342
+ return ['WEBX_NULL_FIELD', 'number', fCfgItem.fieldUrl]
343
+ }
344
+
345
+ for (let idx in fCfgItem.rules) {
346
+ let rule = fCfgItem.rules[idx]
347
+ if (rule.checkType == 'RANGE') {
348
+ let isPass = false
349
+ if (typeof rule.exprVal[0] == 'object') {
350
+ for (let i in rule.exprVal) {
351
+ if (paramValue >= +rule.exprVal[i][0] && paramValue <= +rule.exprVal[i][1]) {
352
+ isPass = true
353
+ break
354
+ }
355
+ }
356
+ } else {
357
+ if (+rule.exprVal[0] <= paramValue && +rule.exprVal[1] >= paramValue) {
255
358
  isPass = true
256
- break
257
359
  }
258
360
  }
259
- } else {
260
- if (paramValue.length >= parseInt(rule.exprVal[0]) && paramValue.length <= parseInt(rule.exprVal[1])) {
261
- isPass = true
262
- }
263
- }
264
-
265
- if (!isPass) {
266
- if (rule.ruleDesc) {
267
- return getCustomTips(rule.ruleDesc)
361
+
362
+ if (!isPass) {
363
+ if (rule.ruleDesc) {
364
+ return this.getCustomTips(rule.ruleDesc)
365
+ }
366
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
268
367
  }
269
- return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
270
- }
271
- } else if (rule.checkType == 'ENU') {
272
- if (rule.isCaseSensitive == 1) {
368
+ } else if (rule.checkType == 'ENU') {
273
369
  if (rule.exprVal && rule.exprVal.length) {
274
370
  if (rule.isMatched === 1) {
275
371
  if (!rule.exprVal.includes(paramValue)) {
276
372
  if (rule.ruleDesc) {
277
- return getCustomTips(rule.ruleDesc)
373
+ return this.getCustomTips(rule.ruleDesc)
278
374
  }
279
375
  return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
280
376
  }
281
377
  } else {
282
378
  if (rule.exprVal.includes(paramValue)) {
283
379
  if (rule.ruleDesc) {
284
- return getCustomTips(rule.ruleDesc)
380
+ return this.getCustomTips(rule.ruleDesc)
285
381
  }
286
382
  return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
287
383
  }
288
384
  }
289
385
  }
290
- } else {
386
+ } else if (rule.checkType == 'REGEX') {
291
387
  let isPass = false
292
- for (let k in rule.exprVal) {
293
- if (rule.isMatched === 1) {
294
- if (rule.exprVal[k].toUpperCase() === paramValue.toUpperCase()) {
295
- isPass = true
388
+ let exprVal = rule.exprVal + ''
389
+ for (let i in rule.exprVal) {
390
+ let reg = eval(rule.exprVal[i])
391
+ let found = reg.test(paramValue + '')
392
+ if (rule.matchType == 'AND') {
393
+ if (!found) {
394
+ exprVal = rule.exprVal[i] + ''
296
395
  break
297
396
  }
298
397
  } else {
299
- isPass = true
300
- if (rule.exprVal[k].toUpperCase() == paramValue.toUpperCase()) {
301
- isPass = false
398
+ if (found) {
399
+ isPass = true
302
400
  break
303
401
  }
304
402
  }
305
403
  }
306
404
  if (!isPass) {
307
405
  if (rule.ruleDesc) {
308
- return getCustomTips(rule.ruleDesc)
309
- }
310
- if (rule.isMatched === 1) {
311
- return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
312
- } else {
313
- return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
406
+ return this.getCustomTips(rule.ruleDesc)
314
407
  }
408
+ return ['WEBX_WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
315
409
  }
316
410
  }
317
- } else if (rule.checkType == 'REGEX') {
318
- let isPass = false
319
- let exprVal = rule.exprVal + ''
320
- for (let i in rule.exprVal) {
321
- let reg = eval(rule.exprVal[i])
322
- let found = reg.test(paramValue)
323
- if (rule.matchType == 'AND') {
324
- if (!found) {
325
- exprVal = rule.exprVal[i] + ''
326
- break
411
+ }
412
+
413
+ return null
414
+ }
415
+ isObjOk(fCfgItem, paramValue) {
416
+ if (!fCfgItem || !fCfgItem.ifMust) {
417
+ return null
418
+ }
419
+
420
+ if (fCfgItem.ifMust == 'NO' && (
421
+ paramValue == undefined ||
422
+ paramValue == null ||
423
+ paramValue == '' ||
424
+ (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
425
+ ) {
426
+ return null
427
+ }
428
+
429
+ if (!paramValue || typeof paramValue != 'object' || !Object.keys(paramValue).length || paramValue.length) {
430
+ if (fCfgItem.nullTips) {
431
+ return this.getCustomTips(fCfgItem.nullTips)
432
+ }
433
+ return ['WEBX_NULL_FIELD', 'map', fCfgItem.fieldUrl]
434
+ }
435
+
436
+ let length = Object.keys(paramValue).length
437
+ for (let idx in fCfgItem.rules) {
438
+ let rule = fCfgItem.rules[idx]
439
+ if (rule.checkType == 'RANGE') {
440
+ let isPass = false
441
+ if (typeof rule.exprVal[0] == 'object') {
442
+ for (let i in rule.exprVal) {
443
+ if (length >= parseInt(rule.exprVal[i][0]) && length <= parseInt(rule.exprVal[i][1])) {
444
+ isPass = true
445
+ break
446
+ }
327
447
  }
328
448
  } else {
329
- if (found) {
449
+ if (length >= parseInt(rule.exprVal[0]) && length <= parseInt(rule.exprVal[1])) {
330
450
  isPass = true
331
- break
332
451
  }
333
452
  }
334
- }
335
- if (!isPass) {
336
- if (rule.ruleDesc) {
337
- return getCustomTips(rule.ruleDesc)
453
+
454
+ if (!isPass) {
455
+ if (rule.ruleDesc) {
456
+ return this.getCustomTips(rule.ruleDesc)
457
+ }
458
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
338
459
  }
339
- return ['WEBX_WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
340
460
  }
341
461
  }
342
- }
343
-
344
- return null
345
- }
346
- function IsIntOk(fCfgItem, paramValue) {
347
- if (!fCfgItem || !fCfgItem.ifMust) {
462
+
348
463
  return null
349
464
  }
350
-
351
- if (fCfgItem.ifMust == 'NO' && (
352
- paramValue == undefined ||
353
- paramValue == null ||
354
- paramValue == '' ||
355
- (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
356
- ) {
357
- return null
358
- }
359
-
360
- if (paramValue == undefined || typeof paramValue != 'number') {
361
- if (fCfgItem.nullTips) {
362
- return getCustomTips(fCfgItem.nullTips)
465
+ isListOk(fCfgItem, paramValue) {
466
+ if (!fCfgItem || !fCfgItem.ifMust) {
467
+ return null
363
468
  }
364
- return ['WEBX_NULL_FIELD', 'number', fCfgItem.fieldUrl]
365
- }
366
-
367
- for (let idx in fCfgItem.rules) {
368
- let rule = fCfgItem.rules[idx]
369
- if (rule.checkType == 'RANGE') {
370
- let isPass = false
371
- if (typeof rule.exprVal[0] == 'object') {
372
- for (let i in rule.exprVal) {
373
- if (paramValue >= +rule.exprVal[i][0] && paramValue <= +rule.exprVal[i][1]) {
374
- isPass = true
375
- break
376
- }
377
- }
378
- } else {
379
- if (+rule.exprVal[0] <= paramValue && +rule.exprVal[1] >= paramValue) {
380
- isPass = true
381
- }
382
- }
383
-
384
- if (!isPass) {
385
- if (rule.ruleDesc) {
386
- return getCustomTips(rule.ruleDesc)
387
- }
388
- return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
469
+
470
+ if (fCfgItem.ifMust == 'NO' && (
471
+ paramValue == undefined ||
472
+ paramValue == null ||
473
+ paramValue == '' ||
474
+ (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
475
+ ) {
476
+ return null
477
+ }
478
+
479
+ if (!paramValue || typeof paramValue != 'object' || !paramValue.length) {
480
+ if (fCfgItem.nullTips) {
481
+ return this.getCustomTips(fCfgItem.nullTips)
389
482
  }
390
- } else if (rule.checkType == 'ENU') {
391
- if (rule.exprVal && rule.exprVal.length) {
392
- if (rule.isMatched === 1) {
393
- if (!rule.exprVal.includes(paramValue)) {
394
- if (rule.ruleDesc) {
395
- return getCustomTips(rule.ruleDesc)
396
- }
397
- return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
398
- }
399
- } else {
400
- if (rule.exprVal.includes(paramValue)) {
401
- if (rule.ruleDesc) {
402
- return getCustomTips(rule.ruleDesc)
483
+ return ['WEBX_NULL_FIELD', 'list', fCfgItem.fieldUrl]
484
+ }
485
+
486
+ let length = paramValue.length
487
+ for (let idx in fCfgItem.rules) {
488
+ let rule = fCfgItem.rules[idx]
489
+ if (rule.checkType == 'RANGE') {
490
+ let isPass = false
491
+ if (typeof rule.exprVal[0] == 'object') {
492
+ for (let i in rule.exprVal) {
493
+ if (length >= parseInt(rule.exprVal[i][0]) && length <= parseInt(rule.exprVal[i][1])) {
494
+ isPass = true
495
+ break
403
496
  }
404
- return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
405
- }
406
- }
407
- }
408
- } else if (rule.checkType == 'REGEX') {
409
- let isPass = false
410
- let exprVal = rule.exprVal + ''
411
- for (let i in rule.exprVal) {
412
- let reg = eval(rule.exprVal[i])
413
- let found = reg.test(paramValue + '')
414
- if (rule.matchType == 'AND') {
415
- if (!found) {
416
- exprVal = rule.exprVal[i] + ''
417
- break
418
497
  }
419
498
  } else {
420
- if (found) {
499
+ if (length >= parseInt(rule.exprVal[0]) && length <= parseInt(rule.exprVal[1])) {
421
500
  isPass = true
422
- break
423
501
  }
424
502
  }
425
- }
426
- if (!isPass) {
427
- if (rule.ruleDesc) {
428
- return getCustomTips(rule.ruleDesc)
503
+
504
+ if (!isPass) {
505
+ if (rule.ruleDesc) {
506
+ return this.getCustomTips(rule.ruleDesc)
507
+ }
508
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
429
509
  }
430
- return ['WEBX_WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
431
510
  }
432
511
  }
433
- }
434
-
435
- return null
436
- }
437
- function IsObjOk(fCfgItem, paramValue) {
438
- if (!fCfgItem || !fCfgItem.ifMust) {
512
+
439
513
  return null
440
514
  }
441
-
442
- if (fCfgItem.ifMust == 'NO' && (
443
- paramValue == undefined ||
444
- paramValue == null ||
445
- paramValue == '' ||
446
- (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
447
- ) {
448
- return null
449
- }
450
-
451
- if (!paramValue || typeof paramValue != 'object' || !Object.keys(paramValue).length || paramValue.length) {
452
- if (fCfgItem.nullTips) {
453
- return getCustomTips(fCfgItem.nullTips)
454
- }
455
- return ['WEBX_NULL_FIELD', 'map', fCfgItem.fieldUrl]
456
- }
457
-
458
- let length = Object.keys(paramValue).length
459
- for (let idx in fCfgItem.rules) {
460
- let rule = fCfgItem.rules[idx]
461
- if (rule.checkType == 'RANGE') {
462
- let isPass = false
463
- if (typeof rule.exprVal[0] == 'object') {
464
- for (let i in rule.exprVal) {
465
- if (length >= parseInt(rule.exprVal[i][0]) && length <= parseInt(rule.exprVal[i][1])) {
466
- isPass = true
467
- break
468
- }
469
- }
470
- } else {
471
- if (length >= parseInt(rule.exprVal[0]) && length <= parseInt(rule.exprVal[1])) {
472
- isPass = true
473
- }
474
- }
475
-
476
- if (!isPass) {
477
- if (rule.ruleDesc) {
478
- return getCustomTips(rule.ruleDesc)
479
- }
480
- return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
515
+ getCustomTips(tips) {
516
+ if (tips.length > 3 && tips.substring(0, 2) === '${' && tips.substring(tips.length -1, tips.length) === '}') {
517
+ let tipsKey = tips.substring(2, tips.length -1)
518
+ tipsKey = tipsKey.trim()
519
+ if (this.TipsMap[tipsKey]) {
520
+ return this.TipsMap[tipsKey].tips
481
521
  }
482
522
  }
523
+ return tips
483
524
  }
484
-
485
- return null
486
525
  }
487
- function IsListOk(fCfgItem, paramValue) {
488
- if (!fCfgItem || !fCfgItem.ifMust) {
489
- return null
490
- }
491
526
 
492
- if (fCfgItem.ifMust == 'NO' && (
493
- paramValue == undefined ||
494
- paramValue == null ||
495
- paramValue == '' ||
496
- (typeof paramValue === 'object' && !paramValue.length && !Object.keys(paramValue).length))
497
- ) {
498
- return null
527
+ function response(params) {
528
+ let p = ConfigMap[this.app.get('_PORT_')]
529
+ if (p.IsCheckedRes &&
530
+ this.interfaceInfo &&
531
+ this.interfaceInfo.rspMsgId &&
532
+ params.codeKey == 'SUCC') {
533
+ let retMsgData = p.cycleCheckParams(p.FieldMap[this.interfaceInfo.rspMsgId], params.data)
534
+ if (retMsgData) {
535
+ params.codeKey = 'SVC_ERR'
536
+ params.msg = retMsgData
537
+ }
538
+ }
539
+ let code = ''
540
+ if (p.ResultCodeMap[params.codeKey]) {
541
+ code = p.ResultCodeMap[params.codeKey].rstCode
499
542
  }
500
543
 
501
- if (!paramValue || typeof paramValue != 'object' || !paramValue.length) {
502
- if (fCfgItem.nullTips) {
503
- return getCustomTips(fCfgItem.nullTips)
544
+ let msg = ''
545
+ if (!params.msg) {
546
+ if (p.TipsMap[params.codeKey]) {
547
+ msg = p.TipsMap[params.codeKey].tips
548
+ } else {
549
+ msg = p.ResultCodeMap[params.codeKey].codeDesc
550
+ }
551
+ } else {
552
+ if (typeof params.msg == 'string') {
553
+ msg = params.msg
504
554
  }
505
- return ['WEBX_NULL_FIELD', 'list', fCfgItem.fieldUrl]
506
- }
507
555
 
508
- let length = paramValue.length
509
- for (let idx in fCfgItem.rules) {
510
- let rule = fCfgItem.rules[idx]
511
- if (rule.checkType == 'RANGE') {
512
- let isPass = false
513
- if (typeof rule.exprVal[0] == 'object') {
514
- for (let i in rule.exprVal) {
515
- if (length >= parseInt(rule.exprVal[i][0]) && length <= parseInt(rule.exprVal[i][1])) {
516
- isPass = true
517
- break
556
+ if (typeof params.msg == 'object' && params.msg.length) {
557
+ if (p.TipsMap[params.msg[0]]) {
558
+ msg = p.TipsMap[params.msg[0]].tips
559
+ if (params.msg.length > 1) {
560
+ for (let i = 1; i < params.msg.length; i++) {
561
+ msg = Util.format(msg, params.msg[i])
518
562
  }
519
563
  }
520
564
  } else {
521
- if (length >= parseInt(rule.exprVal[0]) && length <= parseInt(rule.exprVal[1])) {
522
- isPass = true
523
- }
524
- }
525
-
526
- if (!isPass) {
527
- if (rule.ruleDesc) {
528
- return getCustomTips(rule.ruleDesc)
529
- }
530
- return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
565
+ msg = params.msg[0]
531
566
  }
532
567
  }
533
568
  }
534
569
 
535
- return null
570
+ return this.send({ code: code, msg: msg, data: params.data })
571
+ }
572
+ async function webCommResponse(req, res, next) {
573
+ return res.Msg({ codeKey: 'SUCC', data: [] })
536
574
  }
537
575
  module.exports = Fun
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xzwebx-httpfilter",
3
- "version": "2.2.6",
3
+ "version": "2.2.8",
4
4
  "main": "HttpFilter.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -12,5 +12,8 @@
12
12
  ],
13
13
  "author": "xzwebx",
14
14
  "license": "ISC",
15
- "description": ""
15
+ "description": "",
16
+ "publishConfig": {
17
+ "registry": "https://registry.npmjs.org/"
18
+ }
16
19
  }