123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- import axios from "axios";
- // import { Toast } from 'vant';
- import { Message } from "element-ui";
- import { Decrypt, Encrypt } from "./utils";
- import { oSessionStorage } from "./utils";
- import { router } from "@/router";
- // axios.defaults.baseURL = baseUrl;
- // axios.defaults.baseURL = 'http://10.113.248.4:8060';
- // axios.defaults.baseURL = 'http://49.232.26.44:8060';
- // axios.defaults.baseURL = 'https://child.jue-ming.com:8060/';
- // axios.defaults.baseURL = 'http://10.113.248.5:8070/';
- // axios.defaults.baseURL = 'http://cognitive.wistcm.com:8070/';
- // axios.defaults.baseURL = 'https://gengnao.cn:8070/';
- // axios.defaults.baseURL = 'http://152.136.24.101:8071';
- // axios.defaults.baseURL = 'https://81.70.207.4:8070';
- // axios.defaults.baseURL = 'https://child.hhnao.com:8070';
- // axios.defaults.baseURL = 'https://child.hhnao.com:8070';
- //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx01d1a44906973cf2&redirect_uri=http%3A%2F%2F192.168.18.51%3A8085&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
- // export const basePath='http://10.113.248.3:8086'
- export const basePath = "http://10.113.248.4:8089";
- // const base_url = 'http://10.113.248.4:8090/'
- // export const basePath='http://43.143.198.30:8086'
- axios.defaults.baseURL = basePath;
- // axios.defaults.baseURL = "http://43.143.198.30:8086";
- //设置端口
- // var urlCode ='http://152.136.24.101:8997';
- // var urlCode ='http://43.143.198.30:8085';
- // var urlCode ='https://child.hhnao.com';
- var urlCode = "";
- //接口白名单
- var whiteList = ["/user/blogin", "/user/getCode"];
- //数据是否要加密
- var isMI = false;
- // axios.defaults.baseURL = 'http://cognitive.wistcm.com:8060/';
- // axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
- sessionStorage.setItem("codeImage", urlCode);
- axios.defaults.timeout = 30000; // 超时时间
- axios.defaults.headers.common["Content-Type"] =
- "application/JSON;charset=UTF-8";
- axios.defaults.withCredentials = true;
- // let loadingInstance
- axios.interceptors.request.use(
- function (config) {
- for (let i = 0; i < whiteList.length; i++) {
- if (whiteList[i] !== config.url) {
- ////不在白名单内,需要判断用户是否在,不在的话存储到登录页
- if (oSessionStorage.getItem("userInfo") == "") {
- router.replace({
- path: "/",
- // query: {redirect: router.currentRoute.fullPath}
- });
- }
- //在此判断需要 判断用户缓存是否存在
- // break
- }
- }
- //发送请求前判断token是存在
- // 在发送请求之前做些什么
- // loadingInstance = Loading.service({
- // lock: true,
- // text: '加载中',
- // spinner: 'el-icon-loading',
- // background: 'rgba(0, 0, 0, 0.7)'
- // });
- // eslint-disable-next-line
- // const userId = sessionStorage.getItem("userId");
- // config.headers['type'] = process.env.VUE_APP_VERSION;
- // if (userId) {
- // if (sessionStorage.getItem(`${userId}token`)) {
- // config.headers.Authorization = `Bearer ${sessionStorage.getItem(`${userId}token`)}`;
- // }
- // }
- config.headers.Authorization = oSessionStorage.getItem("token")
- ? `Bearer ${oSessionStorage.getItem("token")}`
- : "";
- // console.log(config.headers.Authorization)
- return config;
- },
- function (error) {
- // 对请求错误做些什么
- return Promise.reject(error);
- }
- );
- // 添加响应拦截器
- axios.interceptors.response.use(
- function (response) {
- // 对响应数据做点什么
- // loadingInstance.close();
- //返回code若为401,则表示token有问题
- if (response.data.code === 401) {
- // 重定向到登录页
- router.replace({
- path: "/",
- // query: {redirect: router.currentRoute.fullPath}
- });
- // Toast.fail(response.data.msg);
- Message.error(response.data.msg);
- }
- return response;
- },
- function (error) {
- // 对响应错误做点什么
- // loadingInstance.close();
- return Promise.reject(error);
- }
- );
- const http = {
- get: (url, data, sCallBack) => {
- if (data) {
- if (isMI) {
- data = Encrypt(JSON.stringify(data));
- } else {
- data = JSON.stringify(data);
- }
- }
- axios.get(url, { params: data }).then((res) => {
- // res.data = JSON.parse(Decrypt(res.data));
- if (isMI) {
- res.data = JSON.parse(Decrypt(res.data));
- } else {
- // res.data = JSON.parse(res.data);
- }
- debugger
- if (sCallBack) {
- sCallBack(res.data);
- }
- });
- },
- getImg: (url, data, sCallBack) => {
- if (data) {
- data = Encrypt(JSON.stringify(data));
- }
- axios(
- {
- method: "get",
- url: url,
- responseType: "blob",
- params: data,
- headers: {
- Accept: "application/octet-stream",
- // "Content-Disposition":"attachment"
- },
- },
- { responseType: "arraybuffer" }
- ).then((res) => {
- sCallBack(res);
- });
- },
- getDown: (url, data, sCallBack) => {
- if (data) {
- data = Encrypt(JSON.stringify(data));
- }
- axios({
- method: "get",
- url: url,
- responseType: "blob",
- params: data,
- headers: {
- // Accept: "application/octet-stream",
- "Content-Disposition": "attachment",
- },
- }).then((res) => {
- sCallBack(res);
- });
- },
- post: (url, data, sucessCallBack, errCallBack) => {
- if (data) {
- if (isMI) {
- data = Encrypt(JSON.stringify(data));
- } else {
- // data = JSON.stringify(data);
- // data = data;
- }
- }
- axios.post(url, data ).then((res) => {
- // res.status
- if (res) {
- if (isMI) {
- res.data = JSON.parse(Decrypt(res.data));
- }else{
- res.data = JSON.parse(res.data);
- }
- }
- if (res.status >= 200 && res.status < 300) {
- if (sucessCallBack) {
- sucessCallBack(res.data);
- }
- } else if (res.status === 401) {
- //token过期了
- } else {
- if (errCallBack) {
- errCallBack(res.data);
- } else {
- // Toast(res.data.msg);
- Message.error(res.data.msg);
- }
- }
- });
- },
- delete: (url, data, sucessCallBack, errCallBack) => {
- if (data) {
- if(isMI){
- data = Encrypt(JSON.stringify(data));
- }else{
- data = JSON.stringify(data);
- }
-
- }
- axios.delete(url, { data: data }).then((res) => {
- // res.status
- if (res) {
- if(isMI){
- res.data = JSON.parse(Decrypt(res.data));
- }else{
- res.data = JSON.parse(res.data);
- }
-
- }
- if (res.status >= 200 && res.status < 300) {
- if (sucessCallBack) {
- sucessCallBack(res.data);
- }
- } else if (res.status === 401) {
- //token过期了
- } else {
- if (errCallBack) {
- errCallBack(res.data);
- } else {
- // Toast(res.data.msg);
- Message.error(res.data.msg);
- }
- }
- // if (res.data.code == 200) {
- // if (sucessCallBack) {
- // sucessCallBack(res.data);
- // }
- // } else if(res.data.code == 2001){
- // if (sucessCallBack) {
- // sucessCallBack(res.data);
- // }
- // }else {
- // if (errCallBack) {
- // errCallBack(res.data);
- // } else {
- // // Toast(res.data.msg);
- // Message.error(res.data.msg);
- // }
- // }
- });
- },
- };
- export default http;
|