request.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const BASE_URL = 'http://192.168.1.222:8088'
  2. // const BASE_URL = 'http://192.168.1.20' //
  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. return
  32. }
  33. resolve(res)
  34. },
  35. fail: (err) => {
  36. uni.showToast({
  37. title: "请求接口失败",
  38. icon: 'none'
  39. })
  40. reject(JSON.stringify(err))
  41. }
  42. // complete:()=>{
  43. // uni.hideLoading()
  44. // }
  45. })
  46. })
  47. async function doRequest(response) {
  48. var datas = {
  49. url: '/refreshToken/' + uni.getStorageSync('union_id'),
  50. method: 'get',
  51. noToken: true
  52. }
  53. const res = await myRequest(datas)
  54. if (res.data.code === 200) {
  55. uni.setStorageSync('token', res.data.data.token)
  56. var userInfo = {
  57. url: '/getInfo',
  58. method: 'get',
  59. }
  60. const resUser = await myRequest(userInfo)
  61. if (resUser.data.code === 200) {
  62. store.state.userInfo = resUser.data.data
  63. }
  64. let onset = await myRequest(response)
  65. return onset
  66. } else {
  67. uni.navigateTo({
  68. url: '/pages/login/login'
  69. });
  70. }
  71. }
  72. }