request.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // 'http://192.168.1.222:8088','https://file-dev.xyyxt.net/'线下
  2. // 'https://api.xyyxt.net' 'https://file.xyyxt.net/'线上
  3. import store from '@/store/index.js'
  4. import api from './api.js'
  5. var num = 1
  6. export const BASE_URL = 'https://api.xyyxt.net'//接口api
  7. export const BASE_IMG_URL = 'https://file.xyyxt.net/'//图片上传api
  8. export const tenantId = '867735392558919680'
  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. TenantId: tenantId
  20. } : {
  21. AuthorizationToken: 'WX ' + uni.getStorageSync('token'),
  22. TenantId: tenantId
  23. },
  24. success: async (res) => {
  25. if (res.data.code == 401) {
  26. if (num <= 3) {
  27. if (!uni.getStorageSync('union_id')) {
  28. uni.navigateTo({
  29. url: '/pages/login/login'
  30. });
  31. } else {
  32. num++
  33. res = await doRequest(options)
  34. }
  35. };
  36. }
  37. resolve(res)
  38. },
  39. fail: (err) => {
  40. uni.showToast({
  41. title: "请求接口失败",
  42. icon: 'none'
  43. })
  44. reject(JSON.stringify(err))
  45. }
  46. // complete:()=>{
  47. // uni.hideLoading()
  48. // }
  49. })
  50. })
  51. async function doRequest(response) {
  52. var datas = {
  53. url: '/refreshToken/' + uni.getStorageSync('union_id'),
  54. method: 'get',
  55. noToken: true
  56. }
  57. const res = await myRequest(datas)
  58. if (res.data.code === 200) {
  59. uni.setStorageSync('token', res.data.data.token)
  60. var userInfo = {
  61. url: '/getInfo',
  62. method: 'get',
  63. }
  64. const resUser = await myRequest(userInfo)
  65. if (resUser.data.code === 200) {
  66. store.state.userInfo = resUser.data.data
  67. }
  68. let onset = await myRequest(response)
  69. return onset
  70. } else {
  71. uni.navigateTo({
  72. url: '/pages/login/login'
  73. });
  74. }
  75. }
  76. }