request.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import uni from "@/utils/uniHooks";
  2. import { getUTMSource } from "@/utils/utils";
  3. import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
  4. // import { Message } from 'element-ui';
  5. // import { jumpLogin } from '@/utils';
  6. // import { Loading } from 'element-ui';
  7. // import { ElLoadingComponent } from 'element-ui/types/loading';
  8. // import vm from "@/main";
  9. import { Toast } from 'vant';
  10. import vue from 'vue';
  11. import store from '@/store'
  12. import { v4 as uuidv4 } from 'uuid';
  13. let loadingInstance = null;
  14. let requestNum = 0;
  15. const CONTENT_TYPE_ARRAY = {
  16. json: 'application/json',
  17. form: 'application/x-www-form-urlencoded',
  18. };
  19. function getHeaders(config = {}) {
  20. const { contentType = 'json' } = config;
  21. const ct = CONTENT_TYPE_ARRAY[contentType];
  22. let header = {
  23. appId: uni.getStorageSync('appid'),
  24. 'Content-Type': ct,
  25. };
  26. const token = getToken();
  27. if (token) {
  28. header['Authorization'] = `Bearer ${token}`;
  29. }
  30. const groupId = uni.getStorageSync('groupId');
  31. const mallId = uni.getStorageSync('mallid');
  32. if (groupId) {
  33. header['brandId'] = groupId;
  34. }
  35. if (mallId) {
  36. header['lbsId'] = mallId;
  37. }
  38. const sourceObj = getUTMSource();
  39. return Object.assign(header, sourceObj);
  40. }
  41. function handleConfig(config = {}) {
  42. const header = getHeaders(config);
  43. const noToken = config.noToken;
  44. if (noToken) {
  45. delete header.Authorization;
  46. }
  47. console.log(525252, header);
  48. return { header, ...config };
  49. }
  50. function getToken() {
  51. const token = uni.getStorageSync('kipAccessToken');
  52. // if (!token || token?.trim() == '' || token == 'null' || token == 'undefined') {
  53. if (!token || token == 'null' || token == 'undefined') {
  54. return false;
  55. }
  56. return token;
  57. }
  58. const addLoading = () => {
  59. // 增加loading 如果pending请求数量等于1,弹出loading, 防止重复弹出
  60. requestNum++;
  61. if (requestNum === 1) {
  62. // 展示加载中的状态
  63. loadingInstance = Toast.loading({
  64. message: '正在努力加载中....',
  65. });
  66. }
  67. };
  68. const cancelLoading = () => {
  69. // 取消loading 如果pending请求数量等于0,关闭loading
  70. requestNum--;
  71. if (requestNum === 0) loadingInstance?.clear();
  72. };
  73. export const createAxiosByinterceptors = (config) => {
  74. const instance = axios.create({
  75. timeout: 1000, //超时配置
  76. baseURL: `${window.profileApi}/temporary-parking-service`,
  77. withCredentials: true, //跨域携带cookie
  78. ...config, // 自定义配置覆盖基本配置
  79. });
  80. // 添加请求拦截器
  81. instance.interceptors.request.use(
  82. function (config) {
  83. // 在发送请求之前做些什么
  84. const { loading = true } = config;
  85. console.log('config:', config);
  86. if (loading) addLoading();
  87. // 设置 headers
  88. config.headers = {
  89. ...config.headers,
  90. ...handleConfig().header,
  91. 'X-Conversation-Id': uuidv4(),
  92. 'X-User': JSON.stringify({
  93. userId: store.state.member.id, // K+用户ID
  94. sourceType: 'WECHAT', // 登录来源: WECHAT/ALIPAY/H5/APP
  95. cId: store.state.openId, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  96. projectId: store.state.member.groupId, // 楼盘id
  97. // projectId: 'ZYJXQV2', // 楼盘id
  98. buildingId: store.state.member.mallid, // 楼栋id
  99. // buildingId: '%E6%B2%AAA99999', // 楼栋id
  100. mobile: store.state.mobile, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  101. vipCode: store.state.member.vipcode, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  102. })
  103. }
  104. // console.log(111111, config.headers, handleConfig().header)
  105. console.log(111111, handleConfig().header)
  106. return config;
  107. },
  108. function (error) {
  109. // 对请求错误做些什么
  110. return Promise.reject(error);
  111. }
  112. );
  113. // 添加响应拦截器
  114. instance.interceptors.response.use(
  115. function (response) {
  116. // 对响应数据做点什么
  117. console.log('response:', response);
  118. const { loading = true } = response.config;
  119. if (loading) cancelLoading();
  120. const { code, data, message } = response.data;
  121. console.log('62response', response);
  122. if (code === 200) return data;
  123. else if (code === 401) {
  124. // 跳转到登陆
  125. // jumpLogin();
  126. } else {
  127. // 错误信息提示
  128. // Message.error(message);
  129. Toast({
  130. message: message,
  131. });
  132. return Promise.reject(response.data);
  133. }
  134. },
  135. function (error) {
  136. // 对响应错误做点什么
  137. console.log('error-response:', error.response);
  138. console.log('error-config:', error.config);
  139. console.log('error-request:', error.request);
  140. const { loading = true } = error.config || {};
  141. if (loading) cancelLoading();
  142. console.log('79error', error);
  143. if (error.response) {
  144. if (error.response.status === 401) {
  145. // 跳转到登陆
  146. // jumpLogin();
  147. }
  148. }
  149. // 错误信息提示
  150. // Message.error(error?.response?.data?.message || '服务端异常');
  151. return Promise.reject(error);
  152. }
  153. );
  154. return instance;
  155. };