道路环境监测node后端和java后台代码
wangrong
2021-12-24 7ba218b08e9d70fb9173db5b4782f6f726df1fd2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * null => ''
 * @param {*} data 要处理的数据
 */
let null2str = function null2str(data) {
  for (let x in data) {
    if (data[x] === null) { // 如果是null 把直接内容转为 ''
      data[x] = '';
    } else {
      if (Array.isArray(data[x])) { // 是数组遍历数组 递归继续处理
        data[x] = data[x].map(z => {
          return null2str(z);
        });
      }
      if (typeof (data[x]) === 'object') { // 是json 递归继续处理
        data[x] = null2str(data[x])
      }
    }
  }
  return data;
}
 
/**
 * 生成sql
 * @param [] strs 要处理的数据 sql_count、sql
 * @param {JSON} reqParams 请求参数
 */
let replaceParams = function replaceParams(str, reqParas) {
  let pattern = /([#|$](\s{0,}){(\s{0,})(.*?)(\s{0,})})/g;
  let match = str[1].match(pattern);
  let status = 1;
  let msg = '';
  if (!match) {
    
  } else {
    for (let index = 0; index < match.length; index++) {
      let paramName = getBraceStr(match[index]).replace(/\s*/g, "")
      if (!(paramName in reqParas)) {
        status = 0;
        msg = 'parameter ' + paramName + ' is not provided';
      }
      str[0] = str[0].replace(match[index], reqParas[paramName]);
      str[1] = str[1].replace(match[index], reqParas[paramName]);
    }
  }
  return { "status": status, 'sql_count': str[0], 'sql': str[1], 'msg': msg };
}
 
/**
 * 生成sql
 * @param [] strs 要处理的数据 sql_count、sql
 * @param {JSON} reqParams 请求参数
 */
 let replaceSyncParams = function replaceParams(str, reqParas) {
  let pattern = /([#|$](\s{0,}){(\s{0,})(.*?)(\s{0,})})/g;
  let match = str.match(pattern);
  let status = 1;
  let msg = '';
  if (!match) {
    
  } else {
    for (let index = 0; index < match.length; index++) {
      let paramName = getBraceStr(match[index]).replace(/\s*/g, "")
      if (!(paramName in reqParas)) {
        status = 0;
        msg = 'parameter ' + paramName + ' is not provided';
      }
      str = str.replace(match[index], reqParas[paramName]);
    }
  }
  return { "status": status, 'sql': str, 'msg': msg };
}
 
/**
* 取出大括号内的内容
* @param text
* @returns {string}
*/
let getBraceStr = function getParenthesesStr(text) {
  let result = ''
  if (isObjEmpty(text))
    return result
  let regex = /\{(.+?)\}/g;
  let options = text.match(regex)
  if (!isObjEmpty(options)) {
    let option = options[0]
    if (!isObjEmpty(option)) {
      result = option.substring(1, option.length - 1)
    }
  }
  return result
}
 
/**
 * 取出中括号内的内容
 * @param text
 * @returns {string}
 */
let getBracketStr = function getBracketStr(text) {
  let result = ''
  if (isObjEmpty(text))
    return result
  let regex = /\[(.+?)\]/g;
  let options = text.match(regex)
  if (!isObjEmpty(options)) {
    let option = options[0]
    if (!isObjEmpty(option)) {
      result = option.substring(1, option.length - 1)
    }
  }
  return result
}
 
/**
* 取出小括号内的内容
* @param text
* @returns {string}
*/
let getParenthesesStr = function getParenthesesStr(text) {
  let result = ''
  if (isObjEmpty(text))
    return result
  let regex = /\((.+?)\)/g;
  let options = text.match(regex)
  if (!isObjEmpty(options)) {
    let option = options[0]
    if (!isObjEmpty(option)) {
      result = option.substring(1, option.length - 1)
    }
  }
  return result
}
 
function isObjEmpty(obj) {
  var name;
  for (name in obj) {
    return false;
  }
  return true;
}
 
function isStrEmpty(obj) {
  if(obj === null) return true;
  if(obj == '') return true;
  return false;
}
 
 
module.exports = {
  null2str,
  replaceParams,
  replaceSyncParams,
  getBraceStr,
  getBracketStr,
  getParenthesesStr,
  isObjEmpty,
  isStrEmpty
}