request.js 2.0 KB

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