signMd5Utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. let requestBody = this.sortAsc(jsonObj);
  32. delete requestBody._t;
  33. return md5(JSON.stringify(requestBody) + signatureSecret).toUpperCase();
  34. }
  35. /**
  36. * @param url 请求的url
  37. * @returns {{}} 将url中请求参数组装成json对象(url的?后面的参数)
  38. */
  39. static parseQueryString(url) {
  40. let urlReg = /^[^\?]+\?([\w\W]+)$/,
  41. paramReg = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  42. urlArray = urlReg.exec(url),
  43. result = {};
  44. // 获取URL上最后带逗号的参数变量 sys/dict/getDictItems/sys_user,realname,username
  45. //【这边条件没有encode】带条件参数例子:/sys/dict/getDictItems/sys_user,realname,id,username!='admin'%20order%20by%20create_time
  46. let lastpathVariable = url.substring(url.lastIndexOf('/') + 1);
  47. if(lastpathVariable.includes(",")){
  48. if(lastpathVariable.includes("?")){
  49. lastpathVariable = lastpathVariable.substring(0, lastpathVariable.indexOf("?"));
  50. }
  51. //解决Sign 签名校验失败 #2728
  52. result["x-path-variable"] = decodeURI(lastpathVariable);
  53. }
  54. if (urlArray && urlArray[1]) {
  55. let paramString = urlArray[1], paramResult;
  56. while ((paramResult = paramReg.exec(paramString)) != null) {
  57. //数字值转为string类型,前后端加密规则保持一致
  58. if(this.myIsNaN(paramResult[2])){
  59. paramResult[2] = paramResult[2].toString()
  60. }
  61. result[paramResult[1]] = paramResult[2];
  62. }
  63. }
  64. return result;
  65. }
  66. /**
  67. * @returns {*} 将两个对象合并成一个
  68. */
  69. static mergeObject(objectOne, objectTwo) {
  70. if (objectTwo && Object.keys(objectTwo).length > 0) {
  71. for (let key in objectTwo) {
  72. if (objectTwo.hasOwnProperty(key) === true) {
  73. //数字值转为string类型,前后端加密规则保持一致
  74. if(this.myIsNaN(objectTwo[key])){
  75. objectTwo[key] = objectTwo[key].toString()
  76. }
  77. objectOne[key] = objectTwo[key];
  78. }
  79. }
  80. }
  81. return objectOne;
  82. }
  83. static urlEncode(param, key, encode) {
  84. if (param == null) return '';
  85. let paramStr = '';
  86. let t = typeof (param);
  87. if (t == 'string' || t == 'number' || t == 'boolean') {
  88. paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
  89. } else {
  90. for (let i in param) {
  91. let k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
  92. paramStr += this.urlEncode(param[i], k, encode);
  93. }
  94. }
  95. return paramStr;
  96. };
  97. /**
  98. * 接口签名用 生成header中的时间戳
  99. * @returns {number}
  100. */
  101. static getTimestamp(){
  102. return new Date().getTime()
  103. }
  104. // static getDateTimeToString() {
  105. // const date_ = new Date()
  106. // const year = date_.getFullYear()
  107. // let month = date_.getMonth() + 1
  108. // let day = date_.getDate()
  109. // if (month < 10) month = '0' + month
  110. // if (day < 10) day = '0' + day
  111. // let hours = date_.getHours()
  112. // let mins = date_.getMinutes()
  113. // let secs = date_.getSeconds()
  114. // const msecs = date_.getMilliseconds()
  115. // if (hours < 10) hours = '0' + hours
  116. // if (mins < 10) mins = '0' + mins
  117. // if (secs < 10) secs = '0' + secs
  118. // if (msecs < 10) secs = '0' + msecs
  119. // return year + '' + month + '' + day + '' + hours + '' + mins + '' + secs
  120. // }
  121. // true:数值型的,false:非数值型
  122. static myIsNaN(value) {
  123. return typeof value === 'number' && !isNaN(value);
  124. }
  125. }