request.js 1.6 KB

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