request.js 1.9 KB

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