api.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const sm4 = require('../js/SM4');
  2. const SM4Exten = require('../js/SM4Exten');
  3. const miniprogramsm4 = require("../miniprogram_npm/miniprogram-sm-crypto/index").sm4;
  4. const secretId = "D2BCF8DE-AA24-4BF6-9C34-C8DD325E412B";//小程序应用ID
  5. const Secret = "6C680A47B87740138DFB299FC69A64E1";//小程序应用密钥
  6. const api_root = 'http://192.168.0.202:5006/'
  7. const CurrentBuId = '12345678-9abc-def0-1234-56789abcdef0';
  8. function request(path, method = 'GET', data = null) {
  9. return new Promise((resolve, reject) => {
  10. var sign_method = "HMAC_SM4";
  11. var url = api_root + path;
  12. var nonce = generateRandomString();
  13. const timestamp = Date.now();
  14. let jsonString = (data != null && method != 'GET') ? JSON.stringify(data) : '';
  15. console.log('request :' + jsonString);
  16. var stringToSign = "sign_method=" +sign_method +
  17. "&secret_id=" +secretId+
  18. "&nonce=" + nonce +
  19. "&timestamp=" + timestamp +
  20. jsonString;
  21. debugger
  22. console.log('加密串 :' + stringToSign);
  23. let key = stringToHex(Secret);//key转16进制
  24. key = getFirst32Chars(key);//截取前16位
  25. let signature = miniprogramsm4.encrypt(stringToSign, key)//sm4加密
  26. //signature = signature.toUpperCase();
  27. signature = stringToBase64(signature);//加密字符串转base64
  28. console.log('signature :' + signature);
  29. wx.request({
  30. header: {
  31. "sign_method": sign_method,
  32. "secret_id": secretId,
  33. "nonce": nonce,
  34. "timestamp": timestamp,
  35. "signature": signature,
  36. "CurrentBuId" : CurrentBuId
  37. },
  38. url: url,
  39. method: method,
  40. data: data,
  41. success: res => {
  42. console.log("success");
  43. resolve(res);
  44. },
  45. fail: res => {
  46. console.log("request fail");
  47. resolve(res);
  48. },
  49. })
  50. });
  51. }
  52. //字符串转base64
  53. function stringToBase64(str) {
  54. // 定义Base64字符集
  55. const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  56. // 将字符串转换为UTF-8字节数组
  57. let utf8Bytes = [];
  58. for (let i = 0; i < str.length; i++) {
  59. let charCode = str.charCodeAt(i);
  60. if (charCode < 128) { // 1字节字符
  61. utf8Bytes.push(charCode);
  62. } else if (charCode < 2048) { // 2字节字符
  63. utf8Bytes.push((charCode >> 6) | 192);
  64. utf8Bytes.push((charCode & 63) | 128);
  65. } else if (charCode < 65536) { // 3字节字符
  66. utf8Bytes.push((charCode >> 12) | 224);
  67. utf8Bytes.push(((charCode >> 6) & 63) | 128);
  68. utf8Bytes.push((charCode & 63) | 128);
  69. } else { // 4字节字符
  70. utf8Bytes.push((charCode >> 18) | 240);
  71. utf8Bytes.push(((charCode >> 12) & 63) | 128);
  72. utf8Bytes.push(((charCode >> 6) & 63) | 128);
  73. utf8Bytes.push((charCode & 63) | 128);
  74. }
  75. }
  76. let base64Str = '';
  77. let paddingCount = 0;
  78. // 处理每3个字节(24位),转换为4个Base64字符
  79. for (let i = 0; i < utf8Bytes.length; i += 3) {
  80. let byte1 = utf8Bytes[i];
  81. let byte2 = i + 1 < utf8Bytes.length ? utf8Bytes[i + 1] : 0;
  82. let byte3 = i + 2 < utf8Bytes.length ? utf8Bytes[i + 2] : 0;
  83. let chunk = (byte1 << 16) | (byte2 << 8) | byte3;
  84. let char1 = base64Chars[(chunk >> 18) & 63];
  85. let char2 = base64Chars[(chunk >> 12) & 63];
  86. let char3 = i + 1 < utf8Bytes.length ? base64Chars[(chunk >> 6) & 63] : '=';
  87. let char4 = i + 2 < utf8Bytes.length ? base64Chars[chunk & 63] : '=';
  88. base64Str += char1 + char2 + char3 + char4;
  89. // 计算需要填充的'='号数量
  90. if (i + 2 >= utf8Bytes.length) {
  91. paddingCount = (utf8Bytes.length - i) % 3;
  92. }
  93. }
  94. return base64Str;
  95. }
  96. //截取字符串
  97. function getFirst32Chars(str) {
  98. if (typeof str !== 'string') {
  99. throw new Error('Input must be a string');
  100. }
  101. // 如果字符串长度小于等于32,则返回整个字符串;否则返回前32个字符
  102. return str.length <= 32 ? str : str.slice(0, 32);
  103. }
  104. //字符串转16进制
  105. function stringToHex(str) {
  106. let hex = '';
  107. for (let i = 0; i < str.length; i++) {
  108. // 获取字符的字符编码
  109. const charCode = str.charCodeAt(i);
  110. // 将字符编码转换为16进制,并确保始终为两位数(不足两位前面补0)
  111. const hexValue = charCode.toString(16).padStart(2, '0');
  112. hex += hexValue;
  113. }
  114. return hex;
  115. }
  116. function generateRandomString(minLength = 6, maxLength = 10) {
  117. // 定义字符集
  118. const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  119. // 生成随机长度
  120. const length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength;
  121. let result = '';
  122. for (let i = 0; i < length; i++) {
  123. // 随机选择一个字符
  124. const randomIndex = Math.floor(Math.random() * charset.length);
  125. result += charset[randomIndex];
  126. }
  127. return result;
  128. }
  129. //获取站点信息
  130. function request_getSite(data) {
  131. return request('api/Nozzle/GetNozzleInfo?Nozzleid='+ data, "GET",data);
  132. }
  133. //支付
  134. function request_wechatPay(data) {
  135. return request('api/Transactions/UnifiedOrder?trxid='+ data, "GET",data);
  136. }
  137. //获取站点信息与用户信息
  138. function request_GetSiteInfo(data) {
  139. return request('api/Site/GetSiteInfo', "GET",data);
  140. }
  141. //小程序用户查询未支付订单
  142. function request_GetMiniProgramTransactionsUnpaidQuery(data) {
  143. return request('api/Transactions/GetMiniProgramTransactionsUnpaidQuery', "GET",data);
  144. }
  145. //小程序用户根据抢号查询未支付订单
  146. function request_GetMiniProgramTransactionsUnpaidNozzle(data) {
  147. return request('api/Transactions/GetMiniProgramTransactionsUnpaidNozzle', "GET",data);
  148. }
  149. export default {
  150. request_getSite,
  151. request_wechatPay
  152. }