http.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if(res.data.msg){
  117. uni.showToast({ title: res.data.msg, icon: 'none' })
  118. }
  119. return
  120. }
  121. if (_checkTokenValid(res)) {
  122. successCallBack(res);
  123. }
  124. },
  125. fail: function(res) {
  126. uni.hideLoading();
  127. if (!(res.errMsg.indexOf('interrupted') != -1)) {
  128. uni.showModal({
  129. title: '错误',
  130. content: '网络错误' + res.errMsg,
  131. showCancel: false
  132. });
  133. }
  134. failCallBack(res);
  135. }
  136. });
  137. }
  138. function selfSort(data) {
  139. // console.log(data,"签名数据")
  140. let sortedKeys = Object.keys(data).sort((a, b) => {
  141. //对key 进行翻转
  142. let reversedA = a.split("").reverse().join("");
  143. let reversedB = b.split("").reverse().join("");
  144. let len = reversedA.length > reversedB.length ? reversedA.length : reversedB.length
  145. for (let i = 0; i < len; i++) {
  146. if (typeof(reversedA[i]) == 'undefined') {
  147. return 1;
  148. }
  149. if (typeof(reversedB[i]) == 'undefined') {
  150. return -1;
  151. }
  152. if (reversedA[i] > reversedB[i]) {
  153. return -1;
  154. } else if (reversedB[i] > reversedA[i]) {
  155. return 1;
  156. }
  157. }
  158. return 1
  159. }) // 对属性数组进行排序
  160. let sortedJson = {}; // 重构排序后的JSON对象
  161. sortedKeys.forEach(key => {
  162. sortedJson[key] = data[key];
  163. })
  164. return sortedJson
  165. }
  166. function getApi(url, data, successCallBack, failCallBack) {
  167. var token = storage.getUserToken(); //wx.getStorageSync(config.STORAGE_USER_TOKEN)
  168. data.token = token;
  169. data.v = config.APP_VERSION;
  170. data.appid = appid
  171. data.from = 'app'
  172. //#ifdef MP-ALIPAY
  173. data.from = 'ali'
  174. //#endif
  175. //#ifdef MP-WEIXIN
  176. data.from = 'wx'
  177. //#endif
  178. data.language = language
  179. getRequest(url, data, successCallBack, failCallBack);
  180. }
  181. function postApi(url, data, successCallBack, failCallBack) {
  182. var token = storage.getUserToken(); // wx.getStorageSync(config.STORAGE_USER_TOKEN)
  183. data.token = token;
  184. data.v = config.APP_VERSION;
  185. data.appid = appid
  186. data.from = 'app'
  187. //#ifdef MP-ALIPAY
  188. data.from = 'ali'
  189. //#endif
  190. //#ifdef MP-WEIXIN
  191. data.from = 'wx'
  192. //#endif
  193. data.language = language
  194. postRequest(url, data, successCallBack, failCallBack);
  195. }
  196. /**
  197. * 上报formid
  198. */
  199. function reportFormId(formId) {
  200. const accountInfo = uni.getAccountInfoSync();
  201. var postData = {
  202. formId: formId,
  203. appid
  204. };
  205. postApi(config.API_FORMID_REPORT, postData, function() {});
  206. }
  207. function getAppConfig(successCallBack) {
  208. const pData = {
  209. appid,
  210. terminal: 'wx_app'
  211. };
  212. getApi(config.API_INDEX_APP_CONFIG, pData, function(resp) {
  213. if (resp.data.code === 200) {
  214. const appConfig = resp.data.data.appConfig;
  215. storage.setAppConfig(appConfig);
  216. successCallBack(appConfig)
  217. //#ifdef MP-ALIPAY
  218. uni.setNavigationBarTitle({
  219. title: ''
  220. });
  221. //#endif
  222. //#ifdef MP-WEIXIN
  223. wx.setNavigationBarTitle({
  224. title: appConfig.app_name
  225. });
  226. //#endif
  227. }else{
  228. }
  229. });
  230. }
  231. module.exports = {
  232. getRequest: getRequest,
  233. postRequest: postRequest,
  234. getApi: getApi,
  235. postApi: postApi,
  236. reportFormId: reportFormId,
  237. getAppConfig: getAppConfig
  238. };