request.js 1.6 KB

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