axios.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { objToUrl } from '@/libs/tools.js';
  2. const baseUrl = getApp().globalData.baseUrl
  3. //带Token请求
  4. const request = (opts, hasToken) => {
  5. const authorization = getApp().globalData.token
  6. const curVer = getApp().globalData.version
  7. // console.log(authorization, 'authorization')
  8. // hasToken是否传入token,为true则不传token
  9. const header = {
  10. // 'USER-TERM-TYPE': 'ipad', // 支付时需要用到 与pad一致
  11. // 'App-Type': 2,
  12. 'Version': curVer.version?curVer.version.replace(/\./g,''):''
  13. }
  14. if(!hasToken){
  15. // header.authorization = authorization
  16. header['access-token'] = authorization
  17. }
  18. //此token是登录成功后后台返回保存在storage中的
  19. let DefaultOpts = {
  20. url: baseUrl+opts.url,
  21. data: opts.data,
  22. method: opts.method,
  23. header: opts.header ? opts.header : header
  24. }
  25. console.log(DefaultOpts)
  26. let promise = new Promise(function(resolve, reject) {
  27. uni.request(DefaultOpts).then(
  28. (res) => {
  29. // 是否超时已跳转
  30. let loginTimeOut = uni.getStorageSync('loginTimeOut');
  31. if(res[0]&&res[0].errMsg.indexOf('request:fail')>=0){
  32. resolve({
  33. status: 6000,
  34. message: "网络请求失败"
  35. })
  36. }
  37. let ret = res[1]
  38. if(!ret.status){
  39. ret.status = ret.statusCode
  40. }
  41. if(res[1].data.status){
  42. ret = res[1].data
  43. }else if(res[1].status){
  44. ret = res[1]
  45. }
  46. console.log(ret)
  47. if(ret.status == '200'){
  48. resolve(ret)
  49. }else{
  50. if(ret.status == '401'){ // 未登录或登录过期
  51. // 除过查询未完成订单的请求
  52. if(opts.url.indexOf("order/unFinishedOrderRunStatus")<0){
  53. uni.showToast({
  54. icon:'none',
  55. title: '未登录或登录已过期,请重新登录',
  56. duration: 5000
  57. })
  58. setTimeout(function() {
  59. const currentRoute = getRoutePath();
  60. const url = `/pages/login/pwLogin?lanuch=${currentRoute.lanuch}&path=` + encodeURIComponent(currentRoute.url);
  61. uni.redirectTo({ url });
  62. }, 1000);
  63. }
  64. }
  65. else{
  66. if(ret.status == 500){
  67. if(opts.url.indexOf("store/validateStoreAndDevice")<0&&opts.url.indexOf("couponReceives/findUsableCoupon")<0){
  68. uni.showToast({
  69. icon:'none',
  70. title: ret.message,
  71. mask: true,
  72. duration: 3000
  73. })
  74. }
  75. }
  76. }
  77. resolve(ret)
  78. }
  79. // ret.status = ret.statusCode // mock数据结构改造
  80. resolve(ret)
  81. }
  82. ).catch(
  83. (response) => {
  84. reject(response)
  85. }
  86. )
  87. })
  88. return promise
  89. }
  90. function getRoutePath() {
  91. // eslint-disable-next-line no-undef
  92. const pages = getCurrentPages(); // 获取加载的页面
  93. const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
  94. let url = currentPage.route; // 当前页面url
  95. const options = objToUrl(currentPage.options); // 如果要获取url中所带的参数可以查看options
  96. const lanuch = [
  97. 'pages/index/index'
  98. ].includes(url);
  99. url = '/' + url;
  100. if (options.length) {
  101. url += '?' + options;
  102. }
  103. return {
  104. url,
  105. lanuch
  106. };
  107. }
  108. export default {
  109. baseUrl,
  110. request
  111. }