axios.js 3.2 KB

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