api.js 5.8 KB

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