request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // 'http://192.168.1.222:8088','https://file-dev.xyyxt.net/'线下
  2. // 'https://api.xyyxt.net' 'https://file.xyyxt.net/'线上
  3. import store from '@/store/index.js'
  4. import api from './api.js'
  5. var num = 1
  6. //接口api
  7. // export const BASE_URL = 'https://api.xyyxt.net' //release
  8. export const BASE_URL = 'http://120.79.166.78:19009' //预发布
  9. // export const BASE_URL = 'http://42.192.164.187:19005' //test
  10. // export const BASE_URL = 'http://192.168.1.222:5055' //dev
  11. //图片上传api
  12. // export const BASE_IMG_URL = 'https://file.xyyxt.net/' //release
  13. export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/' //test
  14. // export const socket_url = 'ws://42.192.164.187:19005/webSocket/' //test
  15. // export const socket_url = 'ws://120.79.166.78:19009/webSocket/' //预发布
  16. // export const socket_url = 'wss://api.xyyxt.net/webSocket/' //release
  17. export const version = '5.2.2'
  18. export const tenantId = '867735392558919680'
  19. export const myRequest = (options) => {
  20. if (store.state.allowLoading && !options.noLoading) {
  21. uni.showLoading({
  22. title: '拼命加载中...',
  23. mask:true,
  24. })
  25. }
  26. return new Promise((resolve, reject) => {
  27. let token = uni.getStorageSync('token')
  28. uni.request({
  29. url: BASE_URL + options.url,
  30. method: options.method || 'GET',
  31. data: options.data,
  32. header: options.noToken ? {
  33. TenantId: tenantId,
  34. } : {
  35. AuthorizationToken: 'WX ' + (token ? token : uni.getStorageSync('token_temp')),
  36. TenantId: tenantId
  37. },
  38. success: async (res) => {
  39. // if(num == 2) {
  40. // return;
  41. // } else {
  42. // num++
  43. // }
  44. // doRequest(options)
  45. if (res.data.code == 401) {
  46. if (num <= 2) {
  47. if (!uni.getStorageSync('user_account')) {
  48. var pages = getCurrentPages() // 获取栈实例
  49. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  50. if(currentRoute != 'pages/login/login') {
  51. uni.navigateTo({
  52. url: '/pages/login/login'
  53. });
  54. }
  55. } else {
  56. num++
  57. res = await doRequest(options)
  58. }
  59. }else{
  60. uni.removeStorageSync('user_account');
  61. var pages = getCurrentPages() // 获取栈实例
  62. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  63. if(currentRoute != 'pages/login/login') {
  64. uni.navigateTo({
  65. url: '/pages/login/login'
  66. });
  67. }
  68. }
  69. }
  70. resolve(res)
  71. },
  72. fail: (err) => {
  73. uni.showToast({
  74. title: "请求接口失败",
  75. icon: 'none'
  76. })
  77. reject(JSON.stringify(err))
  78. },
  79. complete: () => {
  80. uni.hideLoading()
  81. // uni.hideToast()
  82. }
  83. })
  84. })
  85. async function doRequest(response) {
  86. let user_account = uni.getStorageSync('user_account')
  87. var datas = {
  88. url: '/refreshToken/' + user_account,
  89. method: 'get',
  90. noToken: true
  91. }
  92. const res = await myRequest(datas)
  93. if (res.data.code === 200) {
  94. uni.setStorageSync('token', res.data.data.token)
  95. var userInfo = {
  96. url: '/app/user/getInfo',
  97. method: 'get',
  98. }
  99. const resUser = await myRequest(userInfo)
  100. if (resUser.data.code === 200) {
  101. store.state.userInfo = resUser.data.data
  102. uni.setStorageSync('user_account',user_account)
  103. num = 1;
  104. }
  105. let onset = await myRequest(response)
  106. return onset
  107. } else {
  108. var pages = getCurrentPages() // 获取栈实例
  109. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  110. if(currentRoute != 'pages/login/login') {
  111. uni.navigateTo({
  112. url: '/pages/login/login'
  113. });
  114. }
  115. }
  116. }
  117. }