123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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'),
- appId: 'wx92c3e55fbef6b2af',
- '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();
- };
- function XUser(config) {
- /*'X-User': JSON.stringify({"userId": "5106534569910475","sourceType": "WECHAT","nickName": "yedUsFwdkelQbxeTeQOvaScfqIOOmaa","mobile": "18521563898","projectId": "paroject1","buildingId": "shenzhen01","cid": "8aaa809d835ba76d018378bc57180006","vipCode":"KERRY100213505"})*/
- /*'X-User': JSON.stringify({
- // userId: store.state.member.id, // K+用户ID
- userId: '36cec68f3a9a4f4b9e04a59268ba20f2', // 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: store.state.projectId || '4028e3817c2b3f79017c2b48c54c0000', // 楼盘id
- // buildingId: store.state.member.mallid, // 楼栋id
- // buildingId: '8aaa80b47c784020017c78b00d060022', // 楼栋id
- // buildingId: store.state.buildingId || 'shenzhen01', // 楼栋id
- buildingId: store.state.buildingId || 'QHKC-P1', // 楼栋id
- phoneNumber: store.state.mobile, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- // vipCode: store.state.member.vipcode, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- // vipCode: 'KERRY100213815', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- vipCode: 'KERRY100213505', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- lbsId: '8aaa87bc7ce98224017ce995fd8a0002', // 楼盘ID
- })*/
- // console.log('101101101101', store.state.member);
- const params = {
- // userId: '8aaa828583bbbc030183cf34b536000a',
- // userId: store.state.kipUserId, // K+用户ID
- userId: '2c9d85868652dee50186532bdbbb0001', // K+用户ID
- sourceType: 'WECHAT',
- // phoneNumber: '18521563898',
- phoneNumber: store.state.mobile, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- // projectId: 'paroject1',
- projectId: store.state.projectId || '4028e3817c2b3f79017c2b48c54c0000', // 楼盘id
- buildingId: 'QHKC-P1',
- // buildingId: window.localStorage.getItem('buildingId'),
- brandId: store.state.groupId,
- // brandId: '8aaa81947c6e1ca0017c73c13cc30006',
- // brandId: store.state.brandId,
- // cid: '8aaa809d835ba76d018378bc57180006',
- cid: store.state.openid, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- // vipCode: 'KERRY100213505',
- // vipCode: store.state.member.vipcode, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- vipCode: 'KERRY100213853', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- // vipCode: 'KERRY100213851', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
- lbsId: '8aaa82ea804d07cd0180516ff03b0008',
- // lbsId: store.state.lbsId,
- };
- /*const params = {
- userId: '5106534569910475',
- sourceType: 'WECHAT',
- nickName: 'yedUsFwdkelQbxeTeQOvaScfqIOOmaa',
- mobile: '18521563898',
- projectId: 'paroject1',
- buildingId: 'shenyang01',
- cid: '8aaa809d835ba76d018378bc57180006',
- vipCode: 'KERRY100213853',
- lbsId: '8aaa82ea804d07cd0180516ff03b0008',
- };*/
- if (/orders-and-prepay|calculate-discount|unlicensed-car-check-in/.test(config.url)) {
- params.buildingId = window.localStorage.getItem('buildingId');
- }
- console.log(140140140140, params);
- return JSON.stringify(params);
- }
- export const createAxiosByinterceptors = (config) => {
- const instance = axios.create({
- timeout: 100000, //超时配置
- baseURL: `${window.profileApi}/temporary-parking-service`,
- // baseURL: `https://dev-kip-service-internal.kerryonvip.com/temporary-parking-service`,
- withCredentials: true, //跨域携带cookie
- ...config, // 自定义配置覆盖基本配置
- });
- // 添加请求拦截器
- instance.interceptors.request.use(
- function (config) {
- // 在发送请求之前做些什么
- const { loading = true } = config;
- console.log('configconfig:', config);
- if (loading) addLoading();
- // 设置 headers
- config.headers = {
- ...config.headers,
- ...handleConfig().header,
- 'XConversationId': uuidv4(),
- 'X-User': XUser(config),
- };
- // console.log(111111, config.headers, handleConfig().header)
- // console.log(111111, handleConfig().header)
- // console.log(123, config);
- 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);
- // TODO: 临时注释
- // Promise.reject(response.data)
- return response.data;
- /*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(158, error.response.data)
- // 对响应错误做点什么
- 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();
- }
- }
- // 错误信息提示
- const { code, langMessage } = error.response.data;
- const codeList = ['INTERNAL_SERVER_ERROR', 'VALIDATION_FAILED', 'CAR_NOT_FOUND', 'CAR_HAS_PLATE']; // 默认处理的错误code
- if (codeList.indexOf(code) > -1) {
- uni.showToast({ title: langMessage, duration: 3000, icon: 'fail' });
- }
- // Message.error(error?.response?.data?.message || '服务端异常');
- return Promise.reject(error.response.data);
- }
- );
- return instance;
- };
|