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. }
  24. resolve(res)
  25. },
  26. fail: (err) => {
  27. uni.showToast({
  28. title: "请求接口失败"
  29. })
  30. reject(JSON.stringify(err))
  31. }
  32. // complete:()=>{
  33. // uni.hideLoading()
  34. // }
  35. })
  36. })
  37. async function doRequest(response) {
  38. var datas = {
  39. url: '/refreshToken/' + uni.getStorageSync('union_id'),
  40. method: 'get',
  41. noToken: true
  42. }
  43. const res = await myRequest(datas)
  44. if (res.data.code === 200) {
  45. uni.setStorageSync('token', res.data.data.token)
  46. let onset = await myRequest(response)
  47. console.log(datas)
  48. return onset
  49. } else {
  50. uni.removeStorageSync('union_id')
  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. }