b47f73d67ca5892bc684e40d2cf9876a51ad1c0f.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import md5 from '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. //console.log("sign jsonObj: ",jsonObj)
  32. let requestBody = this.sortAsc(jsonObj);
  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. //解决Sign 签名校验失败 #2728
  53. result["x-path-variable"] = decodeURI(lastpathVariable);
  54. }
  55. if (urlArray && urlArray[1]) {
  56. let paramString = urlArray[1], paramResult;
  57. while ((paramResult = paramReg.exec(paramString)) != null) {
  58. //数字值转为string类型,前后端加密规则保持一致
  59. if(this.myIsNaN(paramResult[2])){
  60. paramResult[2] = paramResult[2].toString()
  61. }
  62. result[paramResult[1]] = paramResult[2];
  63. }
  64. }
  65. return result;
  66. }
  67. /**
  68. * @returns {*} 将两个对象合并成一个
  69. */
  70. static mergeObject(objectOne, objectTwo) {
  71. if (objectTwo && Object.keys(objectTwo).length > 0) {
  72. for (let key in objectTwo) {
  73. if (objectTwo.hasOwnProperty(key) === true) {
  74. //数字值转为string类型,前后端加密规则保持一致
  75. if(this.myIsNaN(objectTwo[key])){
  76. objectTwo[key] = objectTwo[key].toString()
  77. }
  78. objectOne[key] = objectTwo[key];
  79. }
  80. }
  81. }
  82. return objectOne;
  83. }
  84. static urlEncode(param, key, encode) {
  85. if (param == null) return '';
  86. let paramStr = '';
  87. let t = typeof (param);
  88. if (t == 'string' || t == 'number' || t == 'boolean') {
  89. paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
  90. } else {
  91. for (let i in param) {
  92. let k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
  93. paramStr += this.urlEncode(param[i], k, encode);
  94. }
  95. }
  96. return paramStr;
  97. };
  98. static getDateTimeToString() {
  99. const date_ = new Date()
  100. const year = date_.getFullYear()
  101. let month = date_.getMonth() + 1
  102. let day = date_.getDate()
  103. if (month < 10) month = '0' + month
  104. if (day < 10) day = '0' + day
  105. let hours = date_.getHours()
  106. let mins = date_.getMinutes()
  107. let secs = date_.getSeconds()
  108. const msecs = date_.getMilliseconds()
  109. if (hours < 10) hours = '0' + hours
  110. if (mins < 10) mins = '0' + mins
  111. if (secs < 10) secs = '0' + secs
  112. if (msecs < 10) secs = '0' + msecs
  113. return year + '' + month + '' + day + '' + hours + '' + mins + '' + secs
  114. }
  115. // true:数值型的,false:非数值型
  116. static myIsNaN(value) {
  117. return typeof value === 'number' && !isNaN(value);
  118. }
  119. }