signMd5Utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import md5 from 'js-md5'
  2. //签名密钥串(前后端要一致,正式发布请自行修改)
  3. const signatureSecret = 'dd05f1c54d63749eda95f9fa6d49v442a';
  4. export default class signMd5Utils {
  5. /**
  6. * json参数升序
  7. * @param jsonObj 发送参数
  8. */
  9. static sortAsc(jsonObj) {
  10. let arr = new Array();
  11. let num = 0;
  12. for (let i in jsonObj) {
  13. arr[num] = i;
  14. num++;
  15. }
  16. let sortArr = arr.sort();
  17. let sortObj = {};
  18. for (let i in sortArr) {
  19. sortObj[sortArr[i]] = jsonObj[sortArr[i]];
  20. }
  21. return sortObj;
  22. }
  23. /**
  24. * @param url 请求的url,应该包含请求参数(url的?后面的参数)
  25. * @param requestParams 请求参数(POST的JSON参数)
  26. * @returns {string} 获取签名
  27. */
  28. static getSign(url, requestParams) {
  29. let urlParams = this.parseQueryString(url);
  30. let jsonObj = this.mergeObject(urlParams, requestParams);
  31. let requestBody = this.sortAsc(jsonObj);
  32. delete requestBody._t;
  33. // console.log('sign requestBody:', requestBody);
  34. return md5(JSON.stringify(requestBody) + signatureSecret).toUpperCase();
  35. }
  36. /**
  37. * @param url 请求的url
  38. * @returns {{}} 将url中请求参数组装成json对象(url的?后面的参数)
  39. */
  40. static parseQueryString(url) {
  41. let urlReg = /^[^\?]+\?([\w\W]+)$/,
  42. paramReg = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  43. urlArray = urlReg.exec(url),
  44. result = {};
  45. // 获取URL上最后带逗号的参数变量 sys/dict/getDictItems/sys_user,realname,username
  46. //【这边条件没有encode】带条件参数例子:/sys/dict/getDictItems/sys_user,realname,id,username!='admin'%20order%20by%20create_time
  47. let lastpathVariable = url.substring(url.lastIndexOf('/') + 1);
  48. if (lastpathVariable.includes(',')) {
  49. if (lastpathVariable.includes('?')) {
  50. lastpathVariable = lastpathVariable.substring(0, lastpathVariable.indexOf('?'));
  51. }
  52. //update-begin---author:wangshuai ---date:20221103 for:[issues/183]下拉搜索,使用动态字典,在线页面不报错,生成的代码报错 ------------
  53. //解决Sign 签名校验失败 #2728
  54. //decodeURI对特殊字符没有没有编码和解码的能力,需要使用decodeURIComponent
  55. result['x-path-variable'] = decodeURIComponent(lastpathVariable);
  56. //update-end---author:wangshuai ---date:20221103 for:[issues/183]下拉搜索,使用动态字典,在线页面不报错,生成的代码报错 ------------
  57. }
  58. if (urlArray && urlArray[1]) {
  59. let paramString = urlArray[1],
  60. paramResult;
  61. while ((paramResult = paramReg.exec(paramString)) != null) {
  62. //数字值转为string类型,前后端加密规则保持一致
  63. if (this.myIsNaN(paramResult[2])) {
  64. paramResult[2] = paramResult[2].toString();
  65. }
  66. result[paramResult[1]] = paramResult[2];
  67. }
  68. }
  69. return result;
  70. }
  71. /**
  72. * @returns {*} 将两个对象合并成一个
  73. */
  74. static mergeObject(objectOne, objectTwo) {
  75. if (objectTwo && Object.keys(objectTwo).length > 0) {
  76. for (let key in objectTwo) {
  77. if (objectTwo.hasOwnProperty(key) === true) {
  78. //数字值转为string类型,前后端加密规则保持一致
  79. if (this.myIsNaN(objectTwo[key])) {
  80. objectTwo[key] = objectTwo[key].toString();
  81. }
  82. objectOne[key] = objectTwo[key];
  83. }
  84. }
  85. }
  86. return objectOne;
  87. }
  88. static urlEncode(param, key, encode) {
  89. if (param == null) return '';
  90. let paramStr = '';
  91. let t = typeof param;
  92. if (t == 'string' || t == 'number' || t == 'boolean') {
  93. paramStr += '&' + key + '=' + (encode == null || encode ? encodeURIComponent(param) : param);
  94. } else {
  95. for (let i in param) {
  96. let k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
  97. paramStr += this.urlEncode(param[i], k, encode);
  98. }
  99. }
  100. return paramStr;
  101. }
  102. /**
  103. * 接口签名用 生成header中的时间戳
  104. * @returns {number}
  105. */
  106. static getTimestamp() {
  107. return new Date().getTime();
  108. }
  109. // static getDateTimeToString() {
  110. // const date_ = new Date()
  111. // const year = date_.getFullYear()
  112. // let month = date_.getMonth() + 1
  113. // let day = date_.getDate()
  114. // if (month < 10) month = '0' + month
  115. // if (day < 10) day = '0' + day
  116. // let hours = date_.getHours()
  117. // let mins = date_.getMinutes()
  118. // let secs = date_.getSeconds()
  119. // const msecs = date_.getMilliseconds()
  120. // if (hours < 10) hours = '0' + hours
  121. // if (mins < 10) mins = '0' + mins
  122. // if (secs < 10) secs = '0' + secs
  123. // if (msecs < 10) secs = '0' + msecs
  124. // return year + '' + month + '' + day + '' + hours + '' + mins + '' + secs
  125. // }
  126. // true:数值型的,false:非数值型
  127. static myIsNaN(value) {
  128. return typeof value === 'number' && !isNaN(value);
  129. }
  130. }