xzwebx-httpfilter 2.2.2 → 2.2.4

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 +79 -80
  2. package/package.json +1 -1
package/HttpFilter.js CHANGED
@@ -8,75 +8,15 @@ let ModuleMap = {}
8
8
  let ModuleInterfaceMap = {}
9
9
  let FieldMap = {}
10
10
  let IsCheckedRes = true
11
- async function response(req, res, next) {
12
- let retData = {
13
- codeKey: 'SUCC',
14
- data: []
15
- }
16
- return res.Msg(retData)
17
- }
18
- function SetIsCheckedRes(isCheckedRes) {
19
- if (isCheckedRes) {
20
- IsCheckedRes = true
21
- } else {
22
- IsCheckedRes = false
23
- }
24
- }
25
- function Init(language, resultCodeMap, tipsMap, moduleMap, moduleInterfaceMap, fieldMap) {
11
+ let Fun = {}
12
+ Fun.Init = function(language, resultCodeMap, tipsMap, moduleMap, moduleInterfaceMap, fieldMap) {
26
13
  ResultCodeMap = resultCodeMap
27
14
  TipsMap = tipsMap[language] || {}
28
15
  ModuleMap = moduleMap
29
16
  ModuleInterfaceMap = moduleInterfaceMap
30
17
  FieldMap = fieldMap
31
18
  }
32
- function MSG(params, rspMsgId) {
33
- if (IsCheckedRes && this.interfaceInfo.rspMsgId) {
34
- let retMsgData = cycleCheckParams(FieldMap[rspMsgId], params.data)
35
- if (retMsgData) {
36
- return MSG({codeKey: 'CLT_ERR', msg: retMsgData, data: params.data})
37
- }
38
- } else {
39
- let code = ''
40
- if (ResultCodeMap[params.codeKey]) {
41
- code = ResultCodeMap[params.codeKey].rstCode
42
- }
43
-
44
- let msg = ''
45
- if (!params.msg) {
46
- if (TipsMap[params.codeKey]) {
47
- msg = TipsMap[params.codeKey].tips
48
- } else {
49
- msg = ResultCodeMap[params.codeKey].codeDesc
50
- }
51
- } else {
52
- if (typeof params.msg == 'string') {
53
- msg = params.msg
54
- }
55
-
56
- if (typeof params.msg == 'object' && params.msg.length) {
57
- if (TipsMap[params.msg[0]]) {
58
- msg = TipsMap[params.msg[0]].tips
59
- if (params.msg.length > 1) {
60
- for (let i = 1; i < params.msg.length; i++) {
61
- msg = Util.format(msg, params.msg[i])
62
- }
63
- }
64
- } else {
65
- msg = params.msg[0]
66
- }
67
- }
68
- }
69
-
70
- return this.send(
71
- {
72
- code: code,
73
- msg: msg,
74
- data: params.data
75
- }
76
- )
77
- }
78
- }
79
- function SetRoutes(app, express, projectPath) {
19
+ Fun.SetRoutes = function(app, express, projectPath) {
80
20
  for (let uri in ModuleMap) {
81
21
  let module = ModuleMap[uri]
82
22
  let router = express.Router()
@@ -101,8 +41,7 @@ function SetRoutes(app, express, projectPath) {
101
41
  app.use(ModuleMap[uri].uri, router)
102
42
  }
103
43
  }
104
- function CheckReq(req, res, next) {
105
- res.Msg = MSG
44
+ Fun.CheckUrl = function(req, res, next) {
106
45
  const baseUrlList = req.url.split('/')
107
46
  const subUri = baseUrlList[baseUrlList.length - 1]
108
47
  let baseUrl = ''
@@ -126,9 +65,12 @@ function CheckReq(req, res, next) {
126
65
  return res.Msg({codeKey: 'CLT_ERR', msg: ['WEBX_ERR_URL']})
127
66
  }
128
67
 
129
- req['interfaceInfo'] = ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]
130
- res['interfaceInfo'] = ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]
131
- let reqMsgId = ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()].reqMsgId
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
132
74
  if (!reqMsgId) {
133
75
  return next()
134
76
  }
@@ -140,6 +82,74 @@ function CheckReq(req, res, next) {
140
82
 
141
83
  return next()
142
84
  }
85
+ Fun.SetMsg = function(req, res, next) {
86
+ res.Msg = MSG
87
+ return next()
88
+ }
89
+ Fun.SetIsCheckedRes = function(isCheckedRes) {
90
+ if (isCheckedRes) {
91
+ IsCheckedRes = true
92
+ } else {
93
+ IsCheckedRes = false
94
+ }
95
+ }
96
+
97
+ async function response(req, res, next) {
98
+ let retData = {
99
+ codeKey: 'SUCC',
100
+ data: []
101
+ }
102
+ return res.Msg(retData)
103
+ }
104
+ function MSG(params) {
105
+ if (IsCheckedRes &&
106
+ this.interfaceInfo.rspMsgId &&
107
+ params.codeKey == 'SUCC') {
108
+ let retMsgData = cycleCheckParams(FieldMap[this.interfaceInfo.rspMsgId], params.data)
109
+ if (retMsgData) {
110
+ params.codeKey = 'SVC_ERR'
111
+ params.msg = retMsgData
112
+ }
113
+ }
114
+ let code = ''
115
+ if (ResultCodeMap[params.codeKey]) {
116
+ code = ResultCodeMap[params.codeKey].rstCode
117
+ }
118
+
119
+ let msg = ''
120
+ if (!params.msg) {
121
+ if (TipsMap[params.codeKey]) {
122
+ msg = TipsMap[params.codeKey].tips
123
+ } else {
124
+ msg = ResultCodeMap[params.codeKey].codeDesc
125
+ }
126
+ } else {
127
+ if (typeof params.msg == 'string') {
128
+ msg = params.msg
129
+ }
130
+
131
+ if (typeof params.msg == 'object' && params.msg.length) {
132
+ if (TipsMap[params.msg[0]]) {
133
+ msg = TipsMap[params.msg[0]].tips
134
+ if (params.msg.length > 1) {
135
+ for (let i = 1; i < params.msg.length; i++) {
136
+ msg = Util.format(msg, params.msg[i])
137
+ }
138
+ }
139
+ } else {
140
+ msg = params.msg[0]
141
+ }
142
+ }
143
+ }
144
+
145
+ return this.send(
146
+ {
147
+ code: code,
148
+ msg: msg,
149
+ data: params.data
150
+ }
151
+ )
152
+ }
143
153
  function cycleCheckParams(msgFieldMap, data) {
144
154
  let retMsgData = null
145
155
 
@@ -523,15 +533,4 @@ function IsListOk(fCfgItem, paramValue) {
523
533
 
524
534
  return null
525
535
  }
526
-
527
- function SetMsg(req, res, next) {
528
- res.Msg = MSG
529
- return next()
530
- }
531
- module.exports = {
532
- Init: Init,
533
- SetRoutes: SetRoutes,
534
- CheckReq: CheckReq,
535
- SetMsg: SetMsg,
536
- SetIsCheckedRes: SetIsCheckedRes
537
- }
536
+ module.exports = Fun
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xzwebx-httpfilter",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "main": "HttpFilter.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"