AnotherTFRequest.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { ANOTHER_TF_INTERFACE } from '../config'
  2. import { T_STORAGE_KEY } from '../constant'
  3. import store from '../store'
  4. const request = (base_url) => function (url, data = {}, method = 'GET', cb, header = {}) {
  5. return new Promise((resolve, reject) => {
  6. header['Content-Type'] = 'application/json'
  7. header.tenant = 'MDAwMA=='
  8. const res = uni.getStorageSync(T_STORAGE_KEY) || {}
  9. if (res.token) header.Authorization = res.token
  10. if (res.ssoUserInfo && res.ssoUserInfo.token) header['satoken-user'] = res.ssoUserInfo.token
  11. // showLoading()
  12. uni.request({
  13. url: base_url + url,
  14. data,
  15. method,
  16. header,
  17. success: (res) => {
  18. // console.log('拦截器', JSON.stringify(res))
  19. // console.log('拦截器', res)
  20. // hideLoading()
  21. if (res.statusCode == 200) {
  22. if ((res.data.code === '200') || (res.data.code === '')) {
  23. resolve(res.data)
  24. } else if ((res.data.code === '20004') || (res.data.code === '20005') || (res.data.code == 40005)) {
  25. uni.showModal({
  26. title: '提示',
  27. content: '账号未登录,请重新登陆!',
  28. success(res) {
  29. if (res.confirm) {
  30. store.dispatch('auth/logoutAction')
  31. setTimeout(() => {
  32. uni.navigateTo({
  33. url: '/pages/login/login'
  34. })
  35. }, 1000)
  36. } else if (res.cancel) {
  37. // uni.navigateBack();
  38. }
  39. }
  40. })
  41. reject(res.data)
  42. } else {
  43. uni.showToast({
  44. title: res.data.message,
  45. icon: 'none'
  46. })
  47. reject(res.data)
  48. }
  49. } else {
  50. reject(res)
  51. }
  52. },
  53. fail: (res) => {
  54. // hideLoading()
  55. reject(res)
  56. },
  57. complete: () => {
  58. cb && typeof cb === 'function' && cb()
  59. }
  60. })
  61. })
  62. }
  63. export const AnotherTFRequest = request(ANOTHER_TF_INTERFACE)