123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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("<?xml") == -1) {
- obj = JSON.parse(obj);
- }
- resolve(obj)
- }
- }
- }
- })
- }
- }
|