http.js 5.9 KB

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