import '../static/encry.min.js' //接口加密工具 import Vue from 'vue' function initData(params) { var URLSearchParams = require('url-search-params'); //360浏览器不支持URLSearchParams if (typeof URLSearchParams === "function") { var formMap = new URLSearchParams(); for (var key in params) { if (typeof (params[key]) == "undefined") { params[key] = ''; } formMap.append(key, params[key]); } return formMap; } else { return params; } } let project = host.PROJECT; export default async (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET', pathArr = [], method = 'fetch') => { type = type.toUpperCase(); if (process.env.NODE_ENV === 'development') { baseUrl = baseUrl; } else if (process.env.NODE_ENV === 'production') { if (baseUrl) { // let buildUrl = "https://test.hajwy.com/" let buildUrl = "https://pms.hajwy.com/" switch (baseUrl) { case '/landcrm': // baseUrl = 'https://test.hajwy.com/landcrm'; baseUrl = buildUrl + 'landcrm'; break; case '/purchase': // // baseUrl = "https://test.hajwy.com/purchase"; baseUrl = buildUrl + 'purchase'; break; case '/hajwy': // // baseUrl = "https://test.hajwy.com/qpi/rest"; baseUrl = buildUrl + 'qpi/rest'; break; case '/mkf': // // baseUrl = "https://test.hajwy.com/landcrm"; baseUrl = buildUrl + 'landcrm'; break; default: baseUrl = ""; break; } } if (host.PROJECT === 'chuangxinfang') baseUrl = `/wg${baseUrl}`; } url = baseUrl + url; if (pathArr && pathArr.length > 0) { url += "/" + pathArr.join("/"); } if (type == 'GET' || type == 'DELETE') { let dataStr = ''; //数据拼接字符串 Object.keys(data).forEach(key => { if (typeof (data[key]) == "undefined") { data[key] = ""; } dataStr += key + '=' + encodeURIComponent(data[key]) + '&'; }) if (dataStr !== '') { dataStr = dataStr.substr(0, dataStr.lastIndexOf('&')); url = url + '?' + dataStr + '&_t=' + new Date().getTime(); } else { url = url + '?_t=' + new Date().getTime(); } } if (window.fetch && method == 'fetch') { var header_content = { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', 'client_token': '', 'token': '' }; if (dataType && dataType === "XML") { header_content = { 'Accept': 'application/xml', 'Content-Type': 'application/x-www-form-urlencoded' }; } else if (dataType && dataType === "JSON2") { header_content = { 'Accept': 'application/json', 'Content-Type': 'application/json' }; } else if (dataType && dataType === "FORMDATA") { header_content = { dataType: "json", cache: false, //上传文件无需缓存 processData: false, //用于对data参数进行序列化处理 这里必须false // contentType: "application/x-www-form-urlencoded", //必须 ContentType: "application/x-www-form-urlencoded" }; } if (localStorage.token != "" && typeof (localStorage.token) !== "undefined") { header_content.client_token = localStorage.token; header_content.token = localStorage.token; } header_content.sign = key_gen.encode(key_gen.handlerData(type == 'GET' ? url.substring(url.indexOf("?") + 1) : data)); if (localStorage.user_id) { header_content["userId"] = localStorage.user_id; } let requestConfig = { credentials: 'include', method: type, headers: header_content, mode: "cors", cache: "force-cache" } if ((type == 'POST' || type == 'PUT' || type == 'DELETE') && dataType != "JSON2" && dataType != "FORMDATA") { Object.defineProperty(requestConfig, 'body', { value: initData(data) }) } else if ((type == 'POST' || type == 'PUT' || type == 'DELETE') && dataType == "JSON2") { Object.defineProperty(requestConfig, 'body', { value: JSON.stringify(data) }) } else if ((type == 'POST') && dataType == "FORMDATA") { Object.defineProperty(requestConfig, 'body', { value: data }) } try { const response = await fetch(url, requestConfig); if (response.headers.get("token_code") == -2 || response.headers.get("token_code") == -1) { Vue.prototype.$Message.config({ top: 80, duration: 3 }); console.log("=====账号过期======"); console.log(url); console.log(requestConfig); console.log("------end-------"); Vue.prototype.$Message.info("当前账号已过期,请重新登录!"); Vue.prototype.$Message = function (str) { return; } setTimeout(function () { localStorage.clear(); if (host.PROJECT && host.PROJECT == "xingfujiye") { window.location.href = "http://csms.cfldcn.com:5902/qpi/logoutFilter?returnUrl=" + Util.filterUrl(window.location.href); } else { location.reload(); } return; }, 3000); } else { if (dataType && dataType === "XML") { const responseJson = await response.text(); return responseJson } else { const responseJson = await response.json(); return responseJson } } } catch (error) { Vue.prototype.$Spin.hide(); const responseJson = { errCode: 500, msg: "系统异常" }; return responseJson; } } else { return new Promise((resolve, reject) => { let requestObj; if (window.XMLHttpRequest) { requestObj = new XMLHttpRequest(); } else { requestObj = new ActiveXObject; } let sendData = ''; if ((type == 'POST' || type == 'PUT' || type == 'DELETE') && dataType != "JSON2" && dataType != "FORMDATA") { sendData = initData(data); } else if ((type == 'POST' || type == 'PUT' || type == 'DELETE') && dataType == "JSON2") { sendData = JSON.stringify(data); } else if ((type == 'POST') && dataType == "FORMDATA") { sendData = data; } requestObj.open(type, url, true); requestObj.setRequestHeader("Accept", "application/json"); if (dataType && dataType === "XML") { requestObj.setRequestHeader("Accept", "application/xml"); requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } else if (dataType && dataType === "JSON2") { requestObj.setRequestHeader("Accept", "application/json"); requestObj.setRequestHeader("Content-type", "application/json"); } else if (dataType && dataType === "FORMDATA") { requestObj.setRequestHeader("Accept", "application/json"); requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); requestObj.setRequestHeader("dataType", "json"); requestObj.setRequestHeader("cache", false); //上传文件无需缓存 requestObj.setRequestHeader("processData", false); //用于对data参数进行序列化处理 这里必须false requestObj.setRequestHeader("contentType", false); //必须 } else { requestObj.setRequestHeader("Accept", "application/json"); requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } requestObj.setRequestHeader("client_token", localStorage.token || ""); requestObj.setRequestHeader("token", localStorage.token || ""); requestObj.setRequestHeader("sign", key_gen.encode(key_gen.handlerData(type == 'GET' ? url.substring(url.indexOf("?") + 1) : data))); if (localStorage.user_id) { requestObj.setRequestHeader("x-user-id-header", localStorage.user_id); } requestObj.send(sendData); requestObj.onreadystatechange = () => { if (requestObj.readyState == 4) { if (requestObj.status == 200) { let obj = requestObj.response || requestObj.responseT if (typeof obj !== 'object' && obj.indexOf("