request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. });
  48. uni.hideLoading();
  49. res = res[1]
  50. if (_checkTokenValid(res)) {
  51. return res
  52. }
  53. throw Error(res.data.msg);
  54. }
  55. async function postRequest(url, data ) {
  56. const datetime = Math.floor(Date.now() / 1000)
  57. data.time = datetime.toString()
  58. let jsonStr = JSON.stringify(selfSort(data))
  59. var encrypted = crypto.SHA256(jsonStr)
  60. var sign = encrypted.toString()
  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. res = res[1]
  76. wx.hideLoading();
  77. if (res.data.code == 200) {
  78. res.succeed = true
  79. res.body = res.data
  80. return res
  81. } else {
  82. uni.showToast({
  83. title: res.data.msg,
  84. icon: 'none'
  85. })
  86. }
  87. if (_checkTokenValid(res)) {
  88. return res
  89. }
  90. throw Error(res.data.msg);
  91. }
  92. function selfSort(data) {
  93. // console.log(data,"签名数据")
  94. let sortedKeys = Object.keys(data).sort((a, b) => {
  95. //对key 进行翻转
  96. let reversedA = a.split("").reverse().join("");
  97. let reversedB = b.split("").reverse().join("");
  98. let len = reversedA.length > reversedB.length ? reversedA.length : reversedB.length
  99. for (let i = 0; i < len; i++) {
  100. if (typeof(reversedA[i]) == 'undefined') {
  101. return 1;
  102. }
  103. if (typeof(reversedB[i]) == 'undefined') {
  104. return -1;
  105. }
  106. if (reversedA[i] > reversedB[i]) {
  107. return -1;
  108. } else if (reversedB[i] > reversedA[i]) {
  109. return 1;
  110. }
  111. }
  112. return 1
  113. }) // 对属性数组进行排序
  114. let sortedJson = {}; // 重构排序后的JSON对象
  115. sortedKeys.forEach(key => {
  116. sortedJson[key] = data[key];
  117. })
  118. return sortedJson
  119. }
  120. async function getApi(url, data) {
  121. var token = storage.getUserToken(); //wx.getStorageSync(config.STORAGE_USER_TOKEN)
  122. data.token = token;
  123. data.v = config.APP_VERSION;
  124. data.appid = appid
  125. //#ifdef MP-ALIPAY
  126. data.from = 'ali'
  127. //#endif
  128. //#ifdef MP-WEIXIN
  129. data.from = 'wx'
  130. //#endif
  131. data.language = language
  132. return await getRequest(url, data);
  133. }
  134. async function postApi(url, data) {
  135. var token = storage.getUserToken(); // wx.getStorageSync(config.STORAGE_USER_TOKEN)
  136. console.log(data, 'token');
  137. data.token = token;
  138. data.v = config.APP_VERSION;
  139. data.appid = appid
  140. //#ifdef MP-ALIPAY
  141. data.from = 'ali'
  142. //#endif
  143. //#ifdef MP-WEIXIN
  144. data.from = 'wx'
  145. //#endif
  146. data.language = language
  147. return await postRequest(url, data);
  148. }
  149. /**
  150. * 上报formid
  151. */
  152. async function reportFormId(formId) {
  153. const accountInfo = uni.getAccountInfoSync();
  154. var postData = {
  155. formId: formId,
  156. appid
  157. };
  158. await postApi(config.API_FORMID_REPORT, postData);
  159. }
  160. async function getAppConfig() {
  161. const pData = {
  162. appid,
  163. terminal: 'wx_app'
  164. };
  165. let resp = await getApi(config.API_INDEX_APP_CONFIG, pData);
  166. if (resp.data.code === 200) {
  167. const appConfig = resp.data.data.appConfig;
  168. storage.setAppConfig(appConfig);
  169. //#ifdef MP-ALIPAY
  170. uni.setNavigationBarTitle({
  171. title: ''
  172. });
  173. //#endif
  174. //#ifdef MP-WEIXIN
  175. wx.setNavigationBarTitle({
  176. title: appConfig.app_name
  177. });
  178. //#endif
  179. return appConfig
  180. } else {}
  181. }
  182. module.exports = {
  183. getRequest: getRequest,
  184. postRequest: postRequest,
  185. getApi: getApi,
  186. postApi: postApi,
  187. reportFormId: reportFormId,
  188. getAppConfig: getAppConfig
  189. };