request.js 1.9 KB

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