http.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. function getRequest(url, data, successCallBack, failCallBack) {
  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. uni.request({
  45. url: url,
  46. method: 'GET',
  47. success: function(res) {
  48. uni.hideLoading();
  49. if (_checkTokenValid(res)) {
  50. successCallBack(res);
  51. }
  52. },
  53. fail: function(res) {
  54. uni.hideLoading();
  55. if (!(res.errMsg.indexOf('interrupted') != -1)) {
  56. uni.showModal({
  57. title: '错误',
  58. content: '网络错误' + res.errMsg,
  59. showCancel: false
  60. });
  61. }
  62. if (failCallBack) failCallBack(res);
  63. }
  64. });
  65. }
  66. function postRequest(url, data, successCallBack, failCallBack) {
  67. const datetime = Math.floor(Date.now() / 1000)
  68. data.time = datetime.toString()
  69. let jsonStr = JSON.stringify(selfSort(data))
  70. var encrypted = crypto.SHA256(jsonStr)
  71. var sign = encrypted.toString()
  72. if (!failCallBack) failCallBack = function() {}
  73. var token = storage.getUserToken()
  74. // 申请拖车服务接口加密签名校验
  75. // if (url === config.API_DAYHIRE_DRAG_CAR_SERVICE) {
  76. data.sign = sign
  77. // }
  78. uni.request({
  79. url: url,
  80. data: data,
  81. header: {
  82. 'content-type': 'application/json',
  83. 'X-Token': token
  84. },
  85. method: 'POST',
  86. success: function(res) {
  87. //wx.hideLoading();
  88. if (res.data.code == 200) {
  89. res.succeed = true
  90. res.body = res.data
  91. } else {
  92. uni.showToast({ title: res.data.msg, icon: 'none' })
  93. }
  94. if (_checkTokenValid(res)) {
  95. successCallBack(res);
  96. }
  97. },
  98. fail: function(res) {
  99. uni.hideLoading();
  100. if (!(res.errMsg.indexOf('interrupted') != -1)) {
  101. uni.showModal({
  102. title: '错误',
  103. content: '网络错误' + res.errMsg,
  104. showCancel: false
  105. });
  106. }
  107. failCallBack(res);
  108. }
  109. });
  110. }
  111. function selfSort(data) {
  112. // console.log(data,"签名数据")
  113. let sortedKeys = Object.keys(data).sort((a, b) => {
  114. //对key 进行翻转
  115. let reversedA = a.split("").reverse().join("");
  116. let reversedB = b.split("").reverse().join("");
  117. let len = reversedA.length > reversedB.length ? reversedA.length : reversedB.length
  118. for (let i = 0; i < len; i++) {
  119. if (typeof(reversedA[i]) == 'undefined') {
  120. return 1;
  121. }
  122. if (typeof(reversedB[i]) == 'undefined') {
  123. return -1;
  124. }
  125. if (reversedA[i] > reversedB[i]) {
  126. return -1;
  127. } else if (reversedB[i] > reversedA[i]) {
  128. return 1;
  129. }
  130. }
  131. return 1
  132. }) // 对属性数组进行排序
  133. let sortedJson = {}; // 重构排序后的JSON对象
  134. sortedKeys.forEach(key => {
  135. sortedJson[key] = data[key];
  136. })
  137. return sortedJson
  138. }
  139. function getApi(url, data, successCallBack, failCallBack) {
  140. var token = storage.getUserToken(); //wx.getStorageSync(config.STORAGE_USER_TOKEN)
  141. data.token = token;
  142. data.v = config.APP_VERSION;
  143. data.appid = appid
  144. data.from = 'app'
  145. //#ifdef MP-ALIPAY
  146. data.from = 'ali'
  147. //#endif
  148. //#ifdef MP-WEIXIN
  149. data.from = 'wx'
  150. //#endif
  151. data.language = language
  152. getRequest(url, data, successCallBack, failCallBack);
  153. }
  154. function postApi(url, data, successCallBack, failCallBack) {
  155. var token = storage.getUserToken(); // wx.getStorageSync(config.STORAGE_USER_TOKEN)
  156. console.log(data,'token');
  157. data.token = token;
  158. data.v = config.APP_VERSION;
  159. data.appid = appid
  160. data.from = 'app'
  161. //#ifdef MP-ALIPAY
  162. data.from = 'ali'
  163. //#endif
  164. //#ifdef MP-WEIXIN
  165. data.from = 'wx'
  166. //#endif
  167. data.language = language
  168. postRequest(url, data, successCallBack, failCallBack);
  169. }
  170. /**
  171. * 上报formid
  172. */
  173. function reportFormId(formId) {
  174. const accountInfo = uni.getAccountInfoSync();
  175. var postData = {
  176. formId: formId,
  177. appid
  178. };
  179. postApi(config.API_FORMID_REPORT, postData, function() {});
  180. }
  181. function getAppConfig(successCallBack) {
  182. const pData = {
  183. appid,
  184. terminal: 'wx_app'
  185. };
  186. getApi(config.API_INDEX_APP_CONFIG, pData, function(resp) {
  187. if (resp.data.code === 200) {
  188. const appConfig = resp.data.data.appConfig;
  189. storage.setAppConfig(appConfig);
  190. successCallBack(appConfig)
  191. //#ifdef MP-ALIPAY
  192. uni.setNavigationBarTitle({
  193. title: ''
  194. });
  195. //#endif
  196. //#ifdef MP-WEIXIN
  197. wx.setNavigationBarTitle({
  198. title: appConfig.app_name
  199. });
  200. //#endif
  201. }else{
  202. }
  203. });
  204. }
  205. module.exports = {
  206. getRequest: getRequest,
  207. postRequest: postRequest,
  208. getApi: getApi,
  209. postApi: postApi,
  210. reportFormId: reportFormId,
  211. getAppConfig: getAppConfig
  212. };