http.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import axios from 'axios';
  2. import store from '@/store';
  3. import ENV_CONFIG from '@/config/env.js'
  4. import {
  5. getToken
  6. } from '@/utils/auth';
  7. const BASEHost = process.env.NODE_ENV === 'development' ? 'api' : window.location.origin;
  8. const service = axios.create({
  9. baseURL: BASEHost,
  10. timeout: 30000
  11. });
  12. // 请求拦截器
  13. service.interceptors.request.use(
  14. (config) => {
  15. config.headers['Content-Type'] = 'application/json';
  16. return config;
  17. },
  18. (error) => {
  19. return Promise.reject(error);
  20. }
  21. );
  22. // 响应拦截器
  23. service.interceptors.response.use(
  24. (response) => {
  25. const res = response.data;
  26. const {
  27. code,
  28. status,
  29. message,
  30. error
  31. } = res;
  32. if (code === 404) {
  33. uni.showToast({
  34. title: '请求的资源不存在',
  35. icon: 'error',
  36. duration: 3 * 1000
  37. });
  38. return Promise.reject(new Error(message || 'Error'));
  39. }
  40. // 返回数据失败
  41. if (status === 0) {
  42. uni.showToast({
  43. title: error,
  44. icon: 'error',
  45. duration: 3 * 1000
  46. });
  47. return Promise.reject(new Error(`${response.config?.params?.MethodName} ${error}` || 'Error'));
  48. }
  49. // 无效的操作用户
  50. if (status === -1) {
  51. uni.showToast({
  52. title: error,
  53. icon: 'error',
  54. duration: 3 * 1000
  55. });
  56. store.dispatch('user/signOut');
  57. window.location.href = '/';
  58. }
  59. return res;
  60. },
  61. (error) => {
  62. if (error.code === 'ERR_CANCELED') {
  63. return uni.showToast('取消上传成功');
  64. }
  65. uni.showToast({
  66. title: error.message,
  67. icon: 'error',
  68. duration: 3 * 1000
  69. });
  70. return Promise.reject(error);
  71. }
  72. );
  73. /**
  74. * 得到必需的请求参数
  75. * @returns {object} 返回必需的请求参数
  76. * */
  77. function getRequestParams() {
  78. const token = getToken();
  79. let access_token = token?.access_token ?? '';
  80. if (access_token == "") {
  81. uni.getStorage({
  82. key: 'AccessToken',
  83. success: function(res) {
  84. access_token = res.data;
  85. }
  86. });
  87. }
  88. return {
  89. AccessToken: access_token,
  90. UserCode: token?.user_code ?? '',
  91. UserType: token?.user_type ?? '',
  92. SessionID: token?.session_id ?? ''
  93. };
  94. }
  95. /**
  96. * @description http 请求封装
  97. */
  98. export const http = {
  99. /**
  100. * @param {String} url 请求地址
  101. * @param {object} config 请求配置
  102. */
  103. get: (url, config) => service.get(url, config),
  104. /**
  105. * @param {string} url 请求地址
  106. * @param {object} data 请求数据
  107. * @param {object} config 请求配置
  108. */
  109. post: (url, data = {}, config = {}) => {
  110. config.params = {
  111. ...config.params,
  112. ...getRequestParams()
  113. };
  114. return service.post(url, data, config);
  115. },
  116. postForm: (url, data = {}, config = {}) => {
  117. config.params = {
  118. ...config.params,
  119. ...getRequestParams(),
  120. };
  121. return service.postForm(url, data, config);
  122. },
  123. put: (url, data, config) => service.put(url, data, config),
  124. delete: (url, data, config) => service.delete(url, data, config)
  125. };