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. });
  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. if(!data.from){
  121. data.from = 'app'
  122. //#ifdef MP-ALIPAY
  123. data.from = 'ali'
  124. //#endif
  125. //#ifdef MP-WEIXIN
  126. data.from = 'wx'
  127. //#endif
  128. }
  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. if(!data.from){
  139. data.from = 'app'
  140. //#ifdef MP-ALIPAY
  141. data.from = 'ali'
  142. //#endif
  143. //#ifdef MP-WEIXIN
  144. data.from = 'wx'
  145. //#endif
  146. }
  147. data.language = language
  148. return await postRequest(url, data);
  149. }
  150. /**
  151. * 上报formid
  152. */
  153. async function reportFormId(formId) {
  154. const accountInfo = uni.getAccountInfoSync();
  155. var postData = {
  156. formId: formId,
  157. appid
  158. };
  159. await postApi(config.API_FORMID_REPORT, postData);
  160. }
  161. async function getAppConfig() {
  162. const pData = {
  163. appid,
  164. terminal: 'wx_app'
  165. };
  166. let resp = await getApi(config.API_INDEX_APP_CONFIG, pData);
  167. if (resp.data.code === 200) {
  168. const appConfig = resp.data.data.appConfig;
  169. storage.setAppConfig(appConfig);
  170. //#ifdef MP-ALIPAY
  171. uni.setNavigationBarTitle({
  172. title: ''
  173. });
  174. //#endif
  175. //#ifdef MP-WEIXIN
  176. wx.setNavigationBarTitle({
  177. title: appConfig.app_name
  178. });
  179. //#endif
  180. return appConfig
  181. } else {}
  182. }
  183. module.exports = {
  184. getRequest: getRequest,
  185. postRequest: postRequest,
  186. getApi: getApi,
  187. postApi: postApi,
  188. reportFormId: reportFormId,
  189. getAppConfig: getAppConfig
  190. };