http.js 5.4 KB

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