request.js 1.7 KB

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