zhgdyun/src/assets/js/http.js

405 lines
17 KiB
JavaScript
Raw Normal View History

2024-06-12 16:14:55 +08:00
import Vue from "vue";
import axios from "axios";
2022-06-08 14:51:11 +08:00
// import QS from 'qs'
2024-06-12 16:14:55 +08:00
import store from "@/store";
2022-06-08 14:51:11 +08:00
// import router from '../../router'
2024-10-09 12:03:59 +08:00
import {
Loading,
Message,
MessageBox
} from "element-ui";
2025-06-13 18:06:27 +08:00
import '@/assets/style/main.less'
2022-06-08 14:51:11 +08:00
2024-06-12 16:14:55 +08:00
Vue.prototype.$http = axios;
// Vue.prototype.qs = QS;
// Vue.use(ElementUI)
var loading;
let needLoadingRequestCount = 0;
var COMPANY = "";
2024-06-06 09:12:50 +08:00
export function showFullScreenLoading() {
if (needLoadingRequestCount === 0) {
2024-06-12 21:18:39 +08:00
startLoading();
2024-06-06 09:12:50 +08:00
}
2024-06-12 21:18:39 +08:00
needLoadingRequestCount++;
2024-06-06 09:12:50 +08:00
}
export function tryHideFullScreenLoading() {
2024-06-12 21:18:39 +08:00
if (needLoadingRequestCount <= 0) return;
needLoadingRequestCount--;
2024-06-06 09:12:50 +08:00
if (needLoadingRequestCount === 0) {
2024-06-12 21:18:39 +08:00
endLoading();
2024-06-06 09:12:50 +08:00
}
}
export function get(url, params, config) {
2024-06-12 21:18:39 +08:00
url = url + "?lang=" + getLang();
2024-06-06 09:12:50 +08:00
return new Promise((resolve, reject) => {
axios
.get(url, {
params: params,
})
.then((res) => {
if (
2024-06-12 21:18:39 +08:00
res.config.url == "/xmgl/progressTask/downloadTemplate?lang=zh_CN"
2024-06-06 09:12:50 +08:00
) {
2024-06-12 21:18:39 +08:00
resolve(res.data);
return;
2024-06-06 09:12:50 +08:00
}
if (res.data.code == 200) {
2024-06-12 21:18:39 +08:00
resolve(res.data);
2024-06-06 09:12:50 +08:00
} else {
2024-06-12 21:18:39 +08:00
Message.error(res.data.message);
2024-06-06 09:12:50 +08:00
}
})
.catch((err) => {
2024-06-12 21:18:39 +08:00
reject(err.data);
});
});
2024-06-06 09:12:50 +08:00
}
/**
* post方法对应post请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
export function post(url, params, config) {
2024-06-12 21:18:39 +08:00
url = url + "?lang=" + getLang();
2024-06-06 09:12:50 +08:00
return new Promise((resolve, reject) => {
axios
.post(url, params)
.then((res) => {
if (res.data.code == 200) {
2024-06-12 21:18:39 +08:00
resolve(res.data);
2024-06-06 09:12:50 +08:00
} else {
2024-06-12 21:18:39 +08:00
if (res.data.status !== "SUCCESS") {
Message.error(res.data.message);
reject(res.data.message);
2024-06-06 09:12:50 +08:00
} else {
2024-06-12 21:18:39 +08:00
resolve(res.data);
2024-06-06 09:12:50 +08:00
}
}
})
.catch((err) => {
2024-06-12 21:18:39 +08:00
reject(err.data);
});
});
2024-06-06 09:12:50 +08:00
}
/**
* put方法对应put请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
export function put(url, params, config) {
2024-06-12 21:18:39 +08:00
url = url + "?lang=" + getLang();
2024-06-06 09:12:50 +08:00
return new Promise((resolve, reject) => {
axios
.put(url, params)
.then((res) => {
if (res.data.code == 200) {
2024-06-12 21:18:39 +08:00
resolve(res.data);
2024-06-06 09:12:50 +08:00
} else {
2024-06-12 21:18:39 +08:00
Message.error(res.data.message);
2024-06-06 09:12:50 +08:00
}
})
.catch((err) => {
2024-06-12 21:18:39 +08:00
reject(err.data);
});
});
2024-06-06 09:12:50 +08:00
}
2024-06-07 19:58:48 +08:00
2022-06-08 14:51:11 +08:00
function getLang() {
2024-06-12 21:18:39 +08:00
let currentLanguage = store.state.currentLanguage;
let langParam = "";
2024-05-28 19:47:29 +08:00
switch (currentLanguage) {
2024-06-12 21:18:39 +08:00
case "zh":
langParam = "zh_CN";
break;
case "en":
langParam = "en_US";
break;
2024-05-28 19:47:29 +08:00
}
2024-06-12 21:18:39 +08:00
return langParam;
2022-06-08 14:51:11 +08:00
}
// 环境的切换
2024-06-04 22:42:11 +08:00
if (process.env.NODE_ENV == "development") {
2024-06-07 19:58:48 +08:00
// axios.defaults.baseURL = 'http://8.142.139.165:7080' // 湖里区
// axios.defaults.baseURL = 'http://124.71.87.250/'
// axios.defaults.baseURL = 'http://139.9.66.234:30/'
// axios.defaults.baseURL = 'http://14.29.220.211:6023/'
// axios.defaults.baseURL = 'https://zhgd.loganwy.com/' //龙光
// axios.defaults.baseURL = 'https://zhgd-uat.logan.com.cn/'
// axios.defaults.baseURL = 'http://36.137.53.203:9090/' // 南昌地铁 17512009894 123456789 gdjt 87654321
// axios.defaults.baseURL = 'http://192.168.34.125:6023/'
// axios.defaults.baseURL = 'http://124.71.178.44:100/' // 河南
// tag: 本地
// axios.defaults.baseURL = 'http://139.9.66.234:6324/' // 河南春笋
// axios.defaults.baseURL ='http://124.71.67.160:8088/' //中建四局线上(旧)地址
// axios.defaults.baseURL ='http://47.97.202.104:6023/' //金林湾线上旧
// axios.defaults.baseURL = 'http://47.97.202.104:6023/';
// axios.defaults.baseURL = 'http://139.9.66.234:8/';
// axios.defaults.baseURL = 'http://120.196.217.6:7000/'; // 鹤洲
// axios.defaults.baseURL = 'http://182.90.224.237:7000/'; // 广西
// axios.defaults.baseURL = 'http://120.196.217.6:7000/' //鹤洲
// axios.defaults.baseURL = 'http://117.156.17.59:9090/'; //
// axios.defaults.baseURL = 'http://120.236.247.200:9000/'; //横琴线上
// axios.defaults.baseURL = 'http://223.82.100.80:9000/'; //南昌地铁
// axios.defaults.baseURL = 'http://139.9.66.234:40001/'; //南昌地铁二期
// axios.defaults.baseURL = 'http://124.71.178.44:30/';
// axios.defaults.baseURL = 'http://192.168.34.125:9090/';
// axios.defaults.baseURL = 'http://192.168.88.220:6023/';
// axios.defaults.baseURL = 'http://124.71.178.44:9000/'; //中建 cscec101 123456
// axios.defaults.baseURL = 'https://jk.cscec1b1.com:5138/';//沃尔
// axios.defaults.baseURL = 'http://61.190.32.219:9050/'
// axios.defaults.baseURL = 'http://183.95.84.54:5800/'//合展-宿迁 zxsd 123456
// axios.defaults.baseURL = 'http://183.95.84.54:9500/'//贵州-敏尚 17512075572 123456
// axios.defaults.baseURL = 'http://183.95.84.34:7185/' //测试服
// axios.defaults.baseURL = 'http://183.60.227.61:30246/'
// axios.defaults.baseURL = 'http://183.60.227.61:30249/' //测试服2
// axios.defaults.baseURL = 'http://183.60.227.61:20561/' //龙光地产 龙光地产 123456
// axios.defaults.baseURL = 'http://218.92.215.138:9090/' //盐城 新佳城小区 123456
// axios.defaults.baseURL = 'http://36.137.53.203:9090/' //盐城 gdjt 123456789
// axios.defaults.baseURL = 'http://183.234.150.152:9090/' //华发 huaxin 123456789
// axios.defaults.baseURL = 'http://182.90.224.147:18170' //瑞士恒通线上
// axios.defaults.baseURL = 'http://58.250.210.9:9090/' //深汕线上
// axios.defaults.baseURL = 'http://101.43.164.214:12345/'//内蒙古线上
// axios.defaults.baseURL = 'http://121.196.214.246/api/'//金林湾线上新
// axios.defaults.baseURL = 'http://42.194.144.62:8088/'//中建四局线上(新)地址
// axios.defaults.baseURL = 'http://101.43.164.214:12350/'//中建五局线上
// axios.defaults.baseURL = 'http://182.90.224.147:15551/'//港投(成润)项目线上(1)
// axios.defaults.baseURL = 'http://118.121.198.147:23232/'//港投(成润)正式
2025-05-30 14:54:52 +08:00
// axios.defaults.baseURL = 'http://jxj.zhgdyun.com:100/' //演示平台/·
2025-06-13 18:06:27 +08:00
axios.defaults.baseURL = 'http://192.168.34.221:8111/' //郭圣雄本地
// axios.defaults.baseURL ='http://192.168.34.221:8188/' //郭圣雄本地
// axios.defaults.baseURL ='http://192.168.34.221:30001/' //郭圣雄本地
// axios.defaults.baseURL ='http://192.168.34.221:12350/' //金林湾本地
// axios.defaults.baseURL ='http://42.194.144.62:8099/' //坪山沙湖
// axios.defaults.baseURL ='http://125.88.207.86:8099/' //坪山沙湖(最新)地址
// axios.defaults.baseURL ='http://huli.zjzhiliao.com/jxjgdapi/' //金林湾测试线上
// axios.defaults.baseURL = 'http://101.43.164.214:45001/' //上海张江
// axios.defaults.baseURL ='http://101.43.164.214:45011/' //上海优益(上海建工)
// axios.defaults.baseURL = 'http://192.168.34.221:28888/' //郭圣雄本地
// axios.defaults.baseURL = 'http://192.168.34.221:28889/' //郭圣雄本地
// axios.defaults.baseURL = 'http://192.168.34.221:28890/' //郭圣雄本地
// axios.defaults.baseURL = "http://192.168.34.221:9111/"; //郭圣雄本地
// axios.defaults.baseURL = "http://192.168.34.221:19112/"; //郭圣雄本地
// axios.defaults.baseURL = 'http://192.168.34.155:19111/' //彭洁本地
2025-05-30 14:54:52 +08:00
// axios.defaults.baseURL = 'http://182.90.224.237:51234/' //郭圣雄远程
// axios.defaults.baseURL = 'http://jxj.zhgdyun.com:61212/' //彭洁远程
// axios.defaults.baseURL ='http://101.43.164.214:45020/' //沈阳和盈
// axios.defaults.baseURL ='http://183.249.224.118:9000/' //嘉兴王江泾公用码头
// axios.defaults.baseURL = 'http://101.43.164.214:11111/' // 百色三标段项目
// axios.defaults.baseURL = 'http://125.88.207.86:8088/'//中建四局线上(最新)地址
// axios.defaults.baseURL = 'http://125.88.207.86:8099/'//中建四局(沙湖)线上(最新)地址
// axios.defaults.baseURL = 'http://jxj.zhgdyun.com:15551/'//测试地址
// axios.defaults.baseURL = 'http://47.93.215.234:9809/'//鞍钢正式地址(弃用)
// axios.defaults.baseURL = 'http://42.180.188.17:9809/' //鞍钢正式地址
// axios.defaults.baseURL = 'http://47.93.215.234:11211/'//鞍钢测试地址(弃用)
// axios.defaults.baseURL = 'http://42.180.188.17:11211/' //鞍钢测试地址
// axios.defaults.baseURL = 'http://8.136.222.164:8808/' //中科安信正式地址
// axios.defaults.baseURL = 'http://1.13.185.209:9820/' //中科佳成正式地址
// axios.defaults.baseURL = 'http://192.168.9.249:9820/' //四川网城正式地址
// axios.defaults.baseURL = 'http://jxj.zhgdyun.com:34568/' //四川网城外网映射地址
// axios.defaults.baseURL = 'http://192.168.110.220:9809/' //同济正式地址
// axios.defaults.baseURL = 'http://192.168.100.4:9809/' //乌丹(合肥启程)新正式地址
// axios.defaults.baseURL = "http://jxj.zhgdyun.com:18000"; //包头化工
// axios.defaults.baseURL = "http://222.80.185.228:6090"; //木垒
// axios.defaults.baseURL = "https://zm.zhgdyun.com:11111";
// // axios.defaults.baseURL = "http://121.37.106.37:9809";
// axios.defaults.baseURL = "http://139.9.66.234:20628";
// axios.defaults.baseURL = "http://jxj.zhgdyun.com:9500";
2025-08-27 18:24:06 +08:00
// axios.defaults.baseURL = "http://jxj.zhgdyun.com:21000";
2024-06-04 22:42:11 +08:00
} else if (process.env.NODE_ENV == "debug") {
2024-06-07 19:58:48 +08:00
axios.defaults.baseURL = "https://www.ceshi.com";
2024-06-04 22:42:11 +08:00
} else if (process.env.NODE_ENV == "production") {
2024-06-07 19:58:48 +08:00
let host = window.location.host;
// axios.defaults.baseURL = window.location.protocol + '//' + host + '/api/'//金林湾地址
2025-05-30 14:54:52 +08:00
2024-06-07 19:58:48 +08:00
axios.defaults.baseURL = window.location.protocol + "//" + host + "/";
2025-05-30 14:54:52 +08:00
// axios.defaults.baseURL = 'http://192.168.34.221:8111/'
2024-06-07 19:58:48 +08:00
// axios.defaults.baseURL ='http://192.168.34.216:6023/'
// axios.defaults.baseURL = window.location.protocol + "//" + host.split(":")[0] + ":6023" + "/"
// axios.defaults.baseURL = 'http://zhgd.loganwy.com/`'
2022-06-08 14:51:11 +08:00
}
// tag: 河南
2024-06-12 16:14:55 +08:00
if (COMPANY == "henan") {
2024-05-28 19:47:29 +08:00
// axios.defaults.baseURL = 'http://124.71.178.44:100/' // 备注 2022 10 12 后端说作废
2024-06-12 21:18:39 +08:00
axios.defaults.baseURL = "http://139.9.66.234:6324/"; // 2022 10 12 邱平毅 创建的服务器地址
2022-06-08 14:51:11 +08:00
}
// axios.defaults.baseURL = store.getters.BASEURL //'http://124.71.178.44:100/'axios.defaults.timeout = 600000;
// console.log('store.state.userInfo',store)
if (store.state.userInfo) {
2024-06-12 21:18:39 +08:00
axios.defaults.headers.common["Authorization"] =
"Bearer" + " " + store.state.userInfo.token;
axios.defaults.headers.common["operateId"] = store.state.userInfo.userId;
2022-06-08 14:51:11 +08:00
}
// http请求拦截器
axios.interceptors.request.use(
2024-06-12 21:18:39 +08:00
(config) => {
if (config.url.includes("downloadTemplate")) {
config.headers.common["responseType"] = "blob";
2024-05-28 19:47:29 +08:00
}
2024-06-12 21:18:39 +08:00
if (config.url.includes("live?port")) return config;
// 分屏插件兼容配置
const screensBaseURL = sessionStorage.getItem("screens-baseURL");
if (config.url.indexOf(screensBaseURL) !== -1) {
delete config.headers.common["operateId"];
if (config.url === screensBaseURL + "/login") {
const Authorization = sessionStorage.getItem("screens-Authorization");
config.headers.common["Authorization"] = Authorization;
2024-05-28 19:47:29 +08:00
} else {
2024-06-12 21:18:39 +08:00
delete config.headers.common["Authorization"];
const token = sessionStorage.getItem("screens-token");
config.headers.common["token"] = token;
2024-05-28 19:47:29 +08:00
}
2024-06-12 21:18:39 +08:00
return config;
}
return config;
},
(error) => {
tryHideFullScreenLoading();
// Message.error('加载超时');
return Promise.reject(error);
2024-06-12 16:14:55 +08:00
}
);
// 响应拦截器
2022-06-08 14:51:11 +08:00
axios.interceptors.response.use(
2024-06-12 21:18:39 +08:00
(response) => {
tryHideFullScreenLoading();
if (response.status === 200) {
return Promise.resolve(response);
} else {
// alert('进入了哪里444')
return Promise.reject(response);
}
},
// 服务器状态码不是2开头的的情况
// 这里可以跟你们的后台开发人员协商好统一的错误状态码
// 然后根据返回的状态码进行一些操作,例如登录过期提示,错误提示等等
// 下面列举几个常见的操作,其他需求可自行扩展
(error) => {
// tryHideFullScreenLoading()
if (error.response.status) {
console.log("----------", error.response);
let errorUrl = error.response.config.baseURL;
console.log("-----------------", errorUrl);
let screensUrl = sessionStorage.getItem("screens-baseURL");
2024-06-12 21:18:39 +08:00
const url = error.response.url;
2024-06-12 21:18:39 +08:00
if (
errorUrl == screensUrl ||
url === "26938512:7zS9LX2X8u4tW4Ps0ZjN/login"
) {
// alert('进入了哪里3')
2024-06-12 21:18:39 +08:00
return;
} else {
// alert('进入了哪里2')
switch (error.response.status) {
// 401: 未登录
// 未登录则跳转登录页面,并携带当前页面的路径
// 在登录成功后返回当前页面,这一步需要在登录页操作。
case 401:
// Message.error('会话已失效,请重新登录')
// logout()
let errorResponse = sessionStorage.getItem("errorResponse");
console.log("进入401", errorResponse);
if (errorResponse == null || errorResponse === "null") {
sessionStorage.setItem("errorResponse", "1");
MessageBox.confirm("会话已失效,请重新登录", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(() => {
logout();
})
.catch(() => {
MessageBox.message({
type: "info",
message: "已取消",
});
});
}
break;
2024-06-04 22:42:11 +08:00
2024-06-12 21:18:39 +08:00
// 403 token过期
// 登录过期对用户进行提示
// 清除本地token和清空vuex中token对象
// 跳转登录页面
case 403:
Message.error("登录过期,请重新登录");
logout();
break;
// 404请求不存在
case 404:
Message.error("网络请求不存在");
break;
case 500:
Message.error("服务器错误");
break;
2024-05-28 19:47:29 +08:00
}
2024-06-12 21:18:39 +08:00
return Promise.reject(error.response);
2024-05-28 19:47:29 +08:00
}
2022-06-08 14:51:11 +08:00
}
2024-06-12 16:14:55 +08:00
}
);
/**
* get方法对应get请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
2022-06-08 14:51:11 +08:00
function logout() {
2024-06-12 21:18:39 +08:00
store.commit("setUserInfo", null);
store.commit("setMapBackArr", []);
store.commit("setMoudle", null);
store.commit("setProDetail", null);
store.commit("setGroupTreeList", []);
store.commit("setSelectedGroupSn", null);
sessionStorage.clear();
sessionStorage.setItem("errorResponse", "1");
2024-05-28 19:47:29 +08:00
let json = {
2024-06-12 21:18:39 +08:00
name: "工作站",
2024-05-28 19:47:29 +08:00
id: 1,
2024-06-12 21:18:39 +08:00
url: "/projectV2/taskList",
2024-05-28 19:47:29 +08:00
menuList: [{
id: 100000,
2024-06-12 21:18:39 +08:00
menuName: "任务清单",
path: "/projectV2/taskList",
2024-05-28 19:47:29 +08:00
},
{
id: 100001,
2024-06-12 21:18:39 +08:00
menuName: "通知公告",
path: "/projectV2/noticeList",
2024-05-28 19:47:29 +08:00
},
{
id: 100002,
2024-06-12 21:18:39 +08:00
menuName: "工作日程",
path: "/projectV2/workerSchedule",
2024-05-28 19:47:29 +08:00
},
],
2024-06-12 21:18:39 +08:00
};
this.$store.commit("setCurModule", json);
2024-05-28 19:47:29 +08:00
setTimeout(() => {
2024-06-12 21:18:39 +08:00
window.location.href = "/index.html#/login";
}, 1000);
2022-06-08 14:51:11 +08:00
}
2024-06-07 19:58:48 +08:00
2022-06-08 14:51:11 +08:00
function startLoading() {
2024-05-28 19:47:29 +08:00
loading = Loading.service({
lock: true,
2024-06-12 21:18:39 +08:00
text: "加载中...",
spinner: "el-icon-loading",
background: "rgba(0, 0, 0, 0.5)",
});
2022-06-08 14:51:11 +08:00
}
function endLoading() {
2024-06-12 21:18:39 +08:00
loading.close();
2024-06-05 20:10:13 +08:00
}