request.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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://42.192.164.187:19005' //test
  9. // export const BASE_URL = 'http://192.168.1.222:5055' //dev
  10. //图片上传api
  11. // export const BASE_IMG_URL = 'https://file.xyyxt.net/' //release
  12. export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/' //test
  13. export const tenantId = '867735392558919680'
  14. export const myRequest = (options) => {
  15. if (store.state.allowLoading) {
  16. uni.showLoading({
  17. title: '拼命加载中...'
  18. })
  19. }
  20. return new Promise((resolve, reject) => {
  21. let token = uni.getStorageSync('token')
  22. uni.request({
  23. url: BASE_URL + options.url,
  24. method: options.method || 'GET',
  25. data: options.data,
  26. header: options.noToken ? {
  27. TenantId: tenantId,
  28. } : {
  29. AuthorizationToken: 'WX ' + (token ? token : uni.getStorageSync('token_temp')),
  30. TenantId: tenantId
  31. },
  32. success: async (res) => {
  33. // if(num == 2) {
  34. // return;
  35. // } else {
  36. // num++
  37. // }
  38. // doRequest(options)
  39. if (res.data.code == 401) {
  40. if (num <= 2) {
  41. if (!uni.getStorageSync('user_account')) {
  42. uni.navigateTo({
  43. url: '/pages/login/login'
  44. });
  45. } else {
  46. num++
  47. res = await doRequest(options)
  48. }
  49. }else{
  50. uni.removeStorageSync('user_account');
  51. // uni.navigateTo({
  52. // url: '/pages/login/login'
  53. // });
  54. }
  55. }
  56. resolve(res)
  57. },
  58. fail: (err) => {
  59. uni.showToast({
  60. title: "请求接口失败",
  61. icon: 'none'
  62. })
  63. reject(JSON.stringify(err))
  64. },
  65. complete: () => {
  66. uni.hideLoading()
  67. // uni.hideToast()
  68. }
  69. })
  70. })
  71. async function doRequest(response) {
  72. let user_account = uni.getStorageSync('user_account')
  73. var datas = {
  74. url: '/refreshToken/' + user_account,
  75. method: 'get',
  76. noToken: true
  77. }
  78. const res = await myRequest(datas)
  79. if (res.data.code === 200) {
  80. uni.setStorageSync('token', res.data.data.token)
  81. var userInfo = {
  82. url: '/app/user/getInfo',
  83. method: 'get',
  84. }
  85. const resUser = await myRequest(userInfo)
  86. if (resUser.data.code === 200) {
  87. store.state.userInfo = resUser.data.data
  88. uni.setStorageSync('user_account',user_account)
  89. num = 1;
  90. }
  91. let onset = await myRequest(response)
  92. return onset
  93. } else {
  94. uni.navigateTo({
  95. url: '/pages/login/login'
  96. });
  97. }
  98. }
  99. }