request.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. <<<<<<< HEAD
  7. export const BASE_URL = 'http://42.192.164.187:19005'//接口api http://42.192.164.187:19005
  8. =======
  9. export const BASE_URL = 'http://42.192.164.187:19005'//接口api
  10. >>>>>>> feature-mycourse
  11. export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/'//图片上传api 'https://file.xyyxt.net/
  12. export const tenantId = '867735392558919680'
  13. export const myRequest = (options) => {
  14. if(store.state.allowLoading){
  15. uni.showLoading({
  16. title:'拼命加载中...'
  17. })
  18. }
  19. return new Promise((resolve, reject) => {
  20. let token =uni.getStorageSync('token')
  21. uni.request({
  22. url: BASE_URL + options.url,
  23. method: options.method || 'GET',
  24. data: options.data,
  25. header: options.noToken ? {
  26. TenantId: tenantId
  27. } : {
  28. AuthorizationToken: 'WX ' + (token?token:uni.getStorageSync('token_temp')),
  29. TenantId: tenantId
  30. },
  31. success: async (res) => {
  32. if (res.data.code == 401) {
  33. if (num <= 3) {
  34. if (!uni.getStorageSync('user_account')) {
  35. uni.navigateTo({
  36. url: '/pages/login/login'
  37. });
  38. } else {
  39. num++
  40. res = await doRequest(options)
  41. }
  42. };
  43. }
  44. resolve(res)
  45. },
  46. fail: (err) => {
  47. uni.showToast({
  48. title: "请求接口失败",
  49. icon: 'none'
  50. })
  51. reject(JSON.stringify(err))
  52. },
  53. complete:()=>{
  54. uni.hideLoading()
  55. // uni.hideToast()
  56. }
  57. })
  58. })
  59. async function doRequest(response) {
  60. var datas = {
  61. url: '/refreshToken/' + uni.getStorageSync('user_account'),
  62. method: 'get',
  63. noToken: true
  64. }
  65. const res = await myRequest(datas)
  66. if (res.data.code === 200) {
  67. uni.setStorageSync('token', res.data.data.token)
  68. var userInfo = {
  69. url: '/app/user/getInfo',
  70. method: 'get',
  71. }
  72. const resUser = await myRequest(userInfo)
  73. if (resUser.data.code === 200) {
  74. store.state.userInfo = resUser.data.data
  75. }
  76. let onset = await myRequest(response)
  77. return onset
  78. } else {
  79. uni.navigateTo({
  80. url: '/pages/login/login'
  81. });
  82. }
  83. }
  84. }