request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // import common = require("./common");
  2. const appid = 'wxefa2810018787a41'
  3. const language = 'zh'
  4. var common = require('./common.js');
  5. var storage = require('./storage.js');
  6. var config = require('./config.js'); // 检查token是否失效 如失效 清除缓存 提示重新登录
  7. // 402: 解析token出错 403: token格式错误 404: 公共声明中缺少必要字段 405: token验证失败
  8. import crypto from '@/node_modules/crypto-js';
  9. function _checkTokenValid(res) {
  10. if (
  11. res.data.code === 402 ||
  12. res.data.code === 403 ||
  13. res.data.code === 404 ||
  14. res.data.code === 405 ||
  15. res.data.code === 300 ||
  16. res.data.code === 302 ||
  17. res.data.code === 303 ||
  18. res.data.code === 304 ||
  19. res.data.code === 305 ||
  20. res.data.code === 306
  21. ) {
  22. //wx.clearStorageSync();
  23. //TODO
  24. // var shareCode = storage.getShareCode();
  25. // storage.clearStorage();
  26. // storage.setShareCode(shareCode);
  27. uni.reLaunch({
  28. url: '/pages/index/index'
  29. });
  30. return false;
  31. }
  32. return true;
  33. }
  34. async function getRequest(url, data) {
  35. // function getRequest(url1, data, successCallBack, failCallBack) {
  36. // if (!failCallBack) failCallBack = function() {};
  37. // var url=url1?url1:''
  38. if (url.indexOf('?') > 0) {
  39. url = url + '&' + common.obj2UrlQuery(data);
  40. } else {
  41. url = '?' + common.obj2UrlQuery(data);
  42. // return
  43. }
  44. let res = await uni.request({
  45. url: url,
  46. method: 'GET',
  47. uni.hideLoading();
  48. if (_checkTokenValid(res)) {
  49. return res
  50. }
  51. throw Error(res.data.msg);
  52. });
  53. }
  54. async function postRequest(url, data,) {
  55. const datetime = Math.floor(Date.now() / 1000)
  56. data.time = datetime.toString()
  57. let jsonStr = JSON.stringify(selfSort(data))
  58. var encrypted = crypto.SHA256(jsonStr)
  59. var sign = encrypted.toString()
  60. if (!failCallBack) failCallBack = function() {}
  61. var token = storage.getUserToken()
  62. // 申请拖车服务接口加密签名校验
  63. // if (url === config.API_DAYHIRE_DRAG_CAR_SERVICE) {
  64. data.sign = sign
  65. // }
  66. let res = await uni.request({
  67. url: url,
  68. data: data,
  69. header: {
  70. 'content-type': 'application/json',
  71. 'X-Token': token
  72. },
  73. method: 'POST',
  74. });
  75. wx.hideLoading();
  76. if (res.data.code == 200) {
  77. res.succeed = true
  78. res.body = res.data
  79. } else {
  80. uni.showToast({
  81. title: res.data.msg,
  82. icon: 'none'
  83. })
  84. }
  85. if (_checkTokenValid(res)) {
  86. return res
  87. }
  88. throw Error(res.data.msg);
  89. }
  90. function selfSort(data) {
  91. // console.log(data,"签名数据")
  92. let sortedKeys = Object.keys(data).sort((a, b) => {
  93. //对key 进行翻转
  94. let reversedA = a.split("").reverse().join("");
  95. let reversedB = b.split("").reverse().join("");
  96. let len = reversedA.length > reversedB.length ? reversedA.length : reversedB.length
  97. for (let i = 0; i < len; i++) {
  98. if (typeof(reversedA[i]) == 'undefined') {
  99. return 1;
  100. }
  101. if (typeof(reversedB[i]) == 'undefined') {
  102. return -1;
  103. }
  104. if (reversedA[i] > reversedB[i]) {
  105. return -1;
  106. } else if (reversedB[i] > reversedA[i]) {
  107. return 1;
  108. }
  109. }
  110. return 1
  111. }) // 对属性数组进行排序
  112. let sortedJson = {}; // 重构排序后的JSON对象
  113. sortedKeys.forEach(key => {
  114. sortedJson[key] = data[key];
  115. })
  116. return sortedJson
  117. }
  118. async function getApi(url, data) {
  119. var token = storage.getUserToken(); //wx.getStorageSync(config.STORAGE_USER_TOKEN)
  120. data.token = token;
  121. data.v = config.APP_VERSION;
  122. data.appid = appid
  123. //#ifdef MP-ALIPAY
  124. data.from = 'ali'
  125. //#endif
  126. //#ifdef MP-WEIXIN
  127. data.from = 'wx'
  128. //#endif
  129. data.language = language
  130. return await getRequest(url, data);
  131. }
  132. async function postApi(url, data) {
  133. var token = storage.getUserToken(); // wx.getStorageSync(config.STORAGE_USER_TOKEN)
  134. console.log(data, 'token');
  135. data.token = token;
  136. data.v = config.APP_VERSION;
  137. data.appid = appid
  138. //#ifdef MP-ALIPAY
  139. data.from = 'ali'
  140. //#endif
  141. //#ifdef MP-WEIXIN
  142. data.from = 'wx'
  143. //#endif
  144. data.language = language
  145. return await postRequest(url, data);
  146. }
  147. /**
  148. * 上报formid
  149. */
  150. async function reportFormId(formId) {
  151. const accountInfo = uni.getAccountInfoSync();
  152. var postData = {
  153. formId: formId,
  154. appid
  155. };
  156. await postApi(config.API_FORMID_REPORT, postData);
  157. }
  158. async function getAppConfig() {
  159. const pData = {
  160. appid,
  161. terminal: 'wx_app'
  162. };
  163. let resp = await getApi(config.API_INDEX_APP_CONFIG, pData);
  164. if (resp.data.code === 200) {
  165. const appConfig = resp.data.data.appConfig;
  166. storage.setAppConfig(appConfig);
  167. //#ifdef MP-ALIPAY
  168. uni.setNavigationBarTitle({
  169. title: ''
  170. });
  171. //#endif
  172. //#ifdef MP-WEIXIN
  173. wx.setNavigationBarTitle({
  174. title: appConfig.app_name
  175. });
  176. //#endif
  177. return appConfig
  178. } else {}
  179. }
  180. module.exports = {
  181. getRequest: getRequest,
  182. postRequest: postRequest,
  183. getApi: getApi,
  184. postApi: postApi,
  185. reportFormId: reportFormId,
  186. getAppConfig: getAppConfig
  187. };