import uni from "@/utils/uniHooks"; import { getUTMSource } from "@/utils/utils"; import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; // import { Message } from 'element-ui'; // import { jumpLogin } from '@/utils'; // import { Loading } from 'element-ui'; // import { ElLoadingComponent } from 'element-ui/types/loading'; // import vm from "@/main"; import { Toast } from 'vant'; import vue from 'vue'; import store from '@/store' import { v4 as uuidv4 } from 'uuid'; let loadingInstance = null; let requestNum = 0; const CONTENT_TYPE_ARRAY = { json: 'application/json', form: 'application/x-www-form-urlencoded', }; function getHeaders(config = {}) { const { contentType = 'json' } = config; const ct = CONTENT_TYPE_ARRAY[contentType]; let header = { appId: uni.getStorageSync('appid'), 'Content-Type': ct, }; const token = getToken(); if (token) { header['Authorization'] = `Bearer ${token}`; } const groupId = uni.getStorageSync('groupId'); const mallId = uni.getStorageSync('mallid'); if (groupId) { header['brandId'] = groupId; } if (mallId) { header['lbsId'] = mallId; } const sourceObj = getUTMSource(); return Object.assign(header, sourceObj); } function handleConfig(config = {}) { const header = getHeaders(config); const noToken = config.noToken; if (noToken) { delete header.Authorization; } console.log(525252, header); return { header, ...config }; } function getToken() { const token = uni.getStorageSync('kipAccessToken'); // if (!token || token?.trim() == '' || token == 'null' || token == 'undefined') { if (!token || token == 'null' || token == 'undefined') { return false; } return token; } const addLoading = () => { // 增加loading 如果pending请求数量等于1,弹出loading, 防止重复弹出 requestNum++; if (requestNum === 1) { // 展示加载中的状态 loadingInstance = Toast.loading({ message: '正在努力加载中....', }); } }; const cancelLoading = () => { // 取消loading 如果pending请求数量等于0,关闭loading requestNum--; if (requestNum === 0) loadingInstance?.clear(); }; export const createAxiosByinterceptors = (config) => { const instance = axios.create({ timeout: 1000, //超时配置 baseURL: `${window.profileApi}/temporary-parking-service`, withCredentials: true, //跨域携带cookie ...config, // 自定义配置覆盖基本配置 }); // 添加请求拦截器 instance.interceptors.request.use( function (config) { // 在发送请求之前做些什么 const { loading = true } = config; console.log('config:', config); if (loading) addLoading(); // 设置 headers config.headers = { ...config.headers, ...handleConfig().header, 'X-Conversation-Id': uuidv4(), 'X-User': JSON.stringify({ userId: store.state.member.id, // K+用户ID sourceType: 'WECHAT', // 登录来源: WECHAT/ALIPAY/H5/APP cId: store.state.openId, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId projectId: store.state.member.groupId, // 楼盘id // projectId: 'ZYJXQV2', // 楼盘id buildingId: store.state.member.mallid, // 楼栋id // buildingId: '%E6%B2%AAA99999', // 楼栋id mobile: store.state.mobile, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId vipCode: store.state.member.vipcode, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId }) } // console.log(111111, config.headers, handleConfig().header) console.log(111111, handleConfig().header) return config; }, function (error) { // 对请求错误做些什么 return Promise.reject(error); } ); // 添加响应拦截器 instance.interceptors.response.use( function (response) { // 对响应数据做点什么 console.log('response:', response); const { loading = true } = response.config; if (loading) cancelLoading(); const { code, data, message } = response.data; console.log('62response', response); if (code === 200) return data; else if (code === 401) { // 跳转到登陆 // jumpLogin(); } else { // 错误信息提示 // Message.error(message); Toast({ message: message, }); return Promise.reject(response.data); } }, function (error) { // 对响应错误做点什么 console.log('error-response:', error.response); console.log('error-config:', error.config); console.log('error-request:', error.request); const { loading = true } = error.config || {}; if (loading) cancelLoading(); console.log('79error', error); if (error.response) { if (error.response.status === 401) { // 跳转到登陆 // jumpLogin(); } } // 错误信息提示 // Message.error(error?.response?.data?.message || '服务端异常'); return Promise.reject(error); } ); return instance; };