request.js 4.5 KB

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