/* * @Descripttion: * @version: * @Author: Lone * @Date: 2021-10-28 22:39:00 * @LastEditors: Lone * @LastEditTime: 2022-01-03 23:32:06 */ import axios from 'axios' import qs from 'qs' import { message } from 'ant-design-vue';//引入antd-vue的提示组件 import Store from '../stores/index.js' import Vue from 'vue'; import router from '../router' import util from './util'; const httpAxios = axios.create();//创建实例 let Config = { TIMEOUT: 30000, baseURL: { dev: window.BASEURL_01, prod: '' } }; // axios 配置 httpAxios.defaults.timeout = Config.TIMEOUT; httpAxios.defaults.withCredentials = true; // console.log('axios的配置: ', httpAxios.defaults); // axios.defaults.timeout = 30000;// 请求超时时间,默认5s // axios.defaults.withCredentials = true; //请求拦截器 httpAxios.interceptors.request.use(config => { // console.log('===============================', config); //根绝参数判断,然后设置不同的Content-Type if (~config.url.indexOf('linemessage/sendMessage')) {//如果是line 发送消息接口 config.headers['Content-Type'] = 'multipart/form-data'; } else { config.headers['Content-Type'] = 'application/json'; }; //添加token if (~config.url.indexOf('auth/oauth/token') || ~config.url.indexOf('partner/adminUser/registerUser')) {//当注册登录时不添加token,不进行其他操作 return config; } else if (Store.state.user.token) { config.headers['Authorization'] = Store.state.user.token; } else { // util.console('1111111111111用户主动退出,已没有token'); // // util.console(config, '第三种情况,没有获取到token'); // message.error('登录状态失效,将自动返回登录页'); // setTimeout(() => { // router.push({ // path:'/login', // }) // }, 1500) } return config; },error => { return Promise.reject(error.response); }); //响应拦截器 httpAxios.interceptors.response.use(function (response) { // console.log('相应拦截器', response); if (['401','302',401,302].includes(response.data.code) && response.status == 200){ message.error('登录状态失效,将自动返回登录页'); setTimeout(() => { router.push({ path:'/login', query:{ redirect:location.hostname } }) }, 1500) }; return response; }, function (error) { // Do something with response error message.error('网络请求失败'); return Promise.reject(error); }); //基地址 //let base = 'http://localhost:8080/api/'; // let base = 'http://3.139.251.163:9230/'; // let base = 'http://52.221.25.56:9230/api/'; let base = 'http://localhost:8080/api/'; base = process.env.NODE_ENV === 'production' ? 'http://52.221.25.56:9230/' : 'http://localhost:8080/api/' //base = process.env.NODE_ENV === 'production' ? 'http://localhost:8085/' : 'http://localhost:8080/api/' // console.log('axios-util 的打印的判断当前环境: ', process.env.NODE_ENV, base); base = 'http://52.221.25.56:9230/'; //通用方法 export const POST = (url, params, isSNS) => { base = isSNS ? 'http://localhost:8080/lineapi/' : 'https://www.mysales.com.tw/'; if (process.env.NODE_ENV === 'production') base = 'https://www.mysales.com.tw/'; return httpAxios.post(`${base}${url}`, params).then(res => res.data); } export const TEST_POST = (url, params) => { // base = 'http://localhost:8080/testApi/'; base = '' if (process.env.NODE_ENV === 'production') base = 'https://www.mysales.com.tw/'; return httpAxios.post(`${base}${url}`, params).then(res => res.data); } export const GET = (url, params) => { return httpAxios.get(`${base}${url}`, {params: params}).then(res => res.data) } export const PUT = (url, params) => { return httpAxios.put(`${base}${url}`, params).then(res => res.data) } export const DELETE = (url, params) => { return httpAxios.delete(`${base}${url}`, {params: params}).then(res => res.data) } export const PATCH = (url, params) => { return httpAxios.patch(`${base}${url}`, params).then(res => res.data) } export const DEFAULT_GET = (url, params) => { return httpAxios.get(`${url}`, {params: params}).then(res => res.data) } export default { POST, TEST_POST, DEFAULT_GET }