request.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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://42.192.164.187:19005' //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 (res.data.code == 401) {
  34. if (num <= 2) {
  35. if (!uni.getStorageSync('user_account')) {
  36. uni.navigateTo({
  37. url: '/pages/login/login'
  38. });
  39. } else {
  40. num++
  41. res = await doRequest(options)
  42. }
  43. }else{
  44. uni.removeStorageSync('user_account')
  45. }
  46. }
  47. resolve(res)
  48. },
  49. fail: (err) => {
  50. uni.showToast({
  51. title: "请求接口失败",
  52. icon: 'none'
  53. })
  54. reject(JSON.stringify(err))
  55. },
  56. complete: () => {
  57. uni.hideLoading()
  58. // uni.hideToast()
  59. }
  60. })
  61. })
  62. async function doRequest(response) {
  63. var datas = {
  64. url: '/refreshToken/' + uni.getStorageSync('user_account'),
  65. method: 'get',
  66. noToken: true
  67. }
  68. const res = await myRequest(datas)
  69. if (res.data.code === 200) {
  70. uni.setStorageSync('token', res.data.data.token)
  71. var userInfo = {
  72. url: '/app/user/getInfo',
  73. method: 'get',
  74. }
  75. const resUser = await myRequest(userInfo)
  76. if (resUser.data.code === 200) {
  77. store.state.userInfo = resUser.data.data
  78. }
  79. let onset = await myRequest(response)
  80. return onset
  81. } else {
  82. uni.navigateTo({
  83. url: '/pages/login/login'
  84. });
  85. }
  86. }
  87. }