request.js 1.4 KB

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