http.js 5.9 KB

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