king
2019-12-12 790dfe8172be5027940a04594224531d15143389
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import moment from 'moment'
import md5 from 'md5'
 
const service = window.GLOB.service ? (/\/$/.test(window.GLOB.service) ? window.GLOB.service : window.GLOB.service + '/') : ''
 
export default class Utils {
  /**
   * @description 生成32位uuid string + 时间
   * @return {String}  uuid
   */
  static getuuid () {
    let uuid = []
    let timestamp = new Date().getTime()
    let options = '0123456789abcdefghigklmnopqrstuv'
    for (let i = 0; i < 19; i++) {
      uuid.push(options.substr(Math.floor(Math.random() * 0x20), 1))
    }
    uuid = timestamp + uuid.join('')
    return uuid
  }
 
  /**
   * @description 生成GUID
   * @return {String}  guid
   */
  static getguid () {
    // 产生一个新的GUID值
    let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
      let r = Math.random() * 16 | 0
      // eslint-disable-next-line
      let v = (c === 'x') ? r : (r & 0x3 | 0x8)
      return v.toString(16)
    })
    return uuid
  }
 
  /**
   * @description md5加密
   * @return {String}  str         加密串
   * @return {String}  timestamp   时间戳  
   */
  static encrypt (str, timestamp) {
    let salt = 'mingke' // 盐值
    return md5(str + salt + timestamp)
  }
 
  /**
   * @description sql加密
   * @return {String}  value
   */
  static formatOptions (value) {
    if (!value) return ''
 
    let salt = 'minKe' // 盐值
    // 关键字转换规则
    let format = [{
      key: 'select',
      value: ' msltk '
    }, {
      key: 'from',
      value: ' mfrmk '
    }, {
      key: 'where',
      value: ' mwhrk '
    }, {
      key: 'order by',
      value: ' modbk '
    }, {
      key: 'asc',
      value: ' modack '
    }, {
      key: 'desc',
      value: ' moddesk '
    }, {
      key: 'top',
      value: ' mtpk '
    }, {
      key: 'like',
      value: ' mlkk '
    }, {
      key: 'not like',
      value: ' mnlkk '
    }, {
      key: 'between',
      value: ' mbtnk '
    }, {
      key: 'and',
      value: ' madk '
    }, {
      key: 'insert',
      value: ' mistk '
    }, {
      key: 'into',
      value: ' mitk '
    }, {
      key: 'update',
      value: ' muptk '
    }, {
      key: 'delete',
      value: ' mdelk '
    }, {
      key: 'begin',
      value: ' mbgink '
    }, {
      key: 'end',
      value: ' medk '
    }, {
      key: 'if',
      value: ' mefk '
    }, {
      key: 'while',
      value: ' mwilk '
    }, {
      key: 'create',
      value: ' mcrtk '
    }, {
      key: 'alter',
      value: ' matek '
    }, {
      key: 'len',
      value: ' mlnk '
    }, {
      key: 'left',
      value: ' mlftk '
    }, {
      key: 'right',
      value: ' mritk '
    }, {
      key: 'union',
      value: ' munok '
    }, {
      key: 'varchar',
      value: ' mvcrk '
    }, {
      key: 'getdate',
      value: ' mgtdtk '
    }, {
      key: 'TRY',
      value: ' mtryonek '
    }, {
      key: 'TRAN',
      value: ' mtrnk '
    }, {
      key: 'goto',
      value: ' mgtk '
    }, {
      key: 'set',
      value: ' mstk '
    }, {
      key: 'ROLLBACK',
      value: ' mrlbkk '
    }]
 
    // 替换关键字
    format.forEach(item => {
      let reg  =  new RegExp('(^|\\s)' + item.key + '(\\s|$)', 'ig')
      value = value.replace(reg, item.value)
    })
 
    // 1、替换%符(数据库中解析后sql报错),2、去除收尾多余空格
    value = value.replace(/%/ig, 'mpercent')
    value = value.replace(/(^\s|\s$)/ig, '')
 
    // value = value.replace(/>/ig, 'greateror')
    //   .replace(/</ig, 'lessor')
    //   .replace(/!=/ig, 'noequal')
    //   .replace(/=/ig, 'equal')
    //   .replace(/,/ig, 'comma')
    //   .replace(/>=/ig, 'greaterorequal')
    //   .replace(/<=/ig, 'lessorequal')
    //   .replace(/@/ig, 'matk')
    //   .replace(/\(/ig, 'mlbrktsk')
    //   .replace(/\)/ig, 'mrbrktsk')
    //   .replace(/\*/ig, 'mastrsk')
    //   .replace(/'/ig, 'mqotek')
    //   .replace(/\s/ig, 'mspace')
 
    // 1、encode编码(中文字符超出base64加密范围),2、base64加密
    value = window.btoa(window.encodeURIComponent(value))
 
    // 随机插入字符
    let index = Math.floor(Math.random() * value.length)
    value = value.slice(0, index) + salt + value.slice(index)
 
    // base64加密
    value = window.btoa(value)
 
    return value
  }
 
  /**
   * @description 拼接搜索条件
   * @param {Array}   searches     搜索条件
   * @return {String}  searchText  拼接结果
   */
  static mainjointsearchkey (searches) {
    if (!searches || searches.length === 0) return ''
 
    if (searches[0].hasOwnProperty('initval')) {
      let newsearches = []
      searches.forEach(search => {
        let item = {
          key: search.field,
          match: search.match,
          type: search.type,
          value: search.initval
        }
        if (item.type === 'date') {
          item.value = item.value ? moment().subtract(item.value, 'days').format('YYYY-MM-DD') : ''
        } else if (item.type === 'datemonth') {
          item.value = item.value ? moment().subtract(item.value, 'month').format('YYYY-MM') : ''
        } else if (item.type === 'dateweek') {
          item.value = item.value ? [moment().subtract(item.value * 7, 'days').startOf('week').format('YYYY-MM-DD'),
            moment().subtract(item.value * 7, 'days').endOf('week').format('YYYY-MM-DD')] : ''
        } else if (item.type === 'daterange') {
          item.value = item.value ? [moment().subtract(item.value, 'days').format('YYYY-MM-DD'),
            moment().subtract(item.value === 1 ? 1 : 0, 'days').format('YYYY-MM-DD')] : ''
        }
        newsearches.push(item)
      })
      searches = newsearches
    }
 
    let searchText = ''
    searches.forEach(item => {
      if (!item.value) return
      // eslint-disable-next-line
      searchText += (searchText !== '' ? ' ' + 'AND' + ' ' : '')
      if (item.type === 'text' || item.type === 'select') {
        // eslint-disable-next-line
        let str = item.match === '=' ? '' : '%'
        // eslint-disable-next-line
        searchText += item.key + ' ' + item.match + ' ' + '\'' + str + item.value + str + '\''
      } else if (item.type === 'date' || item.type === 'datemonth') {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + '\')'
      } else if (item.type === 'dateweek') {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + '\' AND \'' + item.value[1] + '\')'
      } else if (item.type === 'daterange') {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + '\' AND \'' + item.value[1] + '\')'
      } else {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + '\')'
      }
    })
    return searchText
  }
 
  /**
   * @description 拼接搜索条件
   * @param {Array}   searches     搜索条件
   * @return {String}  searchText  拼接结果
   */
  static jointsearchkey (searches) {
    if (!searches || searches.length === 0) return ''
    let searchText = ''
    searches.forEach(item => {
      if (!item.value) return
      // eslint-disable-next-line
      searchText += (searchText !== '' ? ' ' + 'AND' + ' ' : '')
      if (item.type === 'text') {
        let options = item.key.split(',').map(op => {
          // equal时不添加%
          // eslint-disable-next-line
          let str = item.op === 'equal' ? '' : '%'
          // eslint-disable-next-line
          return op + ' ' + item.op + ' ' + '"' + str + item.value + str + '"'
        })
        // eslint-disable-next-line
        searchText += '(' + options.join(' ' + 'OR' + ' ') + ')'
      } else if (item.type === 'date') {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.op + ' ' + '"' + item.value + '")'
      } else {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.op + ' ' + '"' + item.value + '")'
      }
    })
    return searchText
  }
 
  /**
   * @description 获取图片真实路径
   * @return {String}    url 图片路径
   */
  static getrealurl (url) {
    if (!url) return ''
    let baseurl = ''
    if (process.env.NODE_ENV === 'production') {
      baseurl = document.location.origin + '/' + service
    } else {
      baseurl = 'http://qingqiumarket.cn/' + service
    }
    let realurl = url.match(/^http/) || url.match(/^\/\//) ? url : baseurl + url
    return realurl
  }
 
  /**
   * @description 删除存储过程sql
   * @return {String} name 存储过程名称
   */
  static dropfunc (name) {
    return `IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('${name}') AND type in (N'P', N'PC'))  mdrpk PROCEDURE ${name}`
  }
 
  /**
   * @description 创建存储过程
   * @return {String}
   */
  static getfunc (param) {
    let form = ''
    if (param.fields && param.fields.length > 0) {
      let _fields = []
      param.fields.forEach(item => {
        if (item.field) {
          _fields.push(item.field)
        }
      })
 
      let field1 = _fields.join(',')
      let field2 = _fields.join(',@')
      let field3 = _fields.map(cell => {
        return cell + '=@' + cell
      })
 
      field2 = field2 ? '@' + field2 : ''
      field3 = field3.join(',')
 
      form = `
        insert into ${param.name} (${field1}) select ${field2},@UserID
        
        update ${param.name} set ${field3},modifydate=getdate(),modifyuserid=@UserID
      `
    }
 
    let Ltext = `create proc ${param.name}
    (
    @BID nvarchar(50)='',
    @ID nvarchar(50)='',
    表单传值,文本用nvarchar(50)='',日期用datetime is null ,数值用decimal(18,小数点位数)=0,
    @sEPTMenuNo='', --传菜单参数
    @lang nvarchar(50)='',
    @debug nvarchar(50)='',
    @LoginUID nvarchar(50)='',
    @SessionUid nvarchar(50)='',
    @UserID nvarchar(50),
    @ErrorCode nvarchar(50) out,
    @retmsg nvarchar(4000) out
    )
    as
    begin
    declare  @BegindateTest datetime,@EnddateTest datetime
    select  @BegindateTest=getdate()
    set @ErrorCode=''
    set @retmsg=''
    BEGIN TRY
      /*事务操作*/
      BEGIN TRAN
        /*具体业务操作*/
        
         /* 
        select top 10 * from sProcExcep order by id desc
        
        declare @UserName  nvarchar(50),@FullName nvarchar(50) 
        
        select @UserName=UserName,@FullName=FullName from SUsers where UID=@UserID
        ${form}
        if 1=2
        begin
          set @ErrorCode='E'
          set @retmsg='在此写报错'
          goto GOTO_RETURN
        end
        
        insert into sNote (remark,createuserid,CreateUser,CreateStaff)
        select '在此写日志',@UserID,@UserName,@FullName
        */
        
      COMMIT TRAN
      SET NOCOUNT ON
      RETURN
    END TRY
    BEGIN CATCH
      /*错误处理*/
      ROLLBACK TRAN
      DECLARE @ErrorMessage NVARCHAR(4000);
      DECLARE @ErrorSeverity INT;
      DECLARE @ErrorState INT;
      
      /*把自定义的友好的错误信息提示加上*/
      set @ErrorCode=cast(ERROR_NUMBER() as nvarchar(50))
      SET @retmsg=ERROR_MESSAGE();
      SELECT @ErrorMessage=ERROR_MESSAGE(),
        @ErrorSeverity=ERROR_SEVERITY(),
        @ErrorState=ERROR_STATE();
        
      RAISERROR(@ErrorMessage, /*-- Message text.*/
        @ErrorSeverity, /*-- Severity.*/
        @ErrorState  /*-- State.*/
        );
    END CATCH
    
    GOTO_RETURN:
      ROLLBACK TRAN
      
    END`
    Ltext = Ltext.replace(/\n\s{4}/ig, 'mchr13k')
    console.log(Ltext)
    return Ltext
  }
}