123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import '../static/encry.min.js' //接口加密工具
- import Vue from 'vue'
- import Util from "../src/assets/js/util";
- import {
- router
- } from '../src/router'
- 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;
- }
- }
- export default async (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET', method = 'fetch') => {
- type = type.toUpperCase();
- if (process.env.NODE_ENV === 'development') {
- baseUrl = baseUrl;
- } else if (process.env.NODE_ENV === 'production') {
- if (baseUrl) {
- switch (baseUrl) {
- case '/landcrm':
- baseUrl = '/landcrm'; //其他环境
- break;
- case '/rcrm':
- baseUrl = '/landcrm'; //报表地址
- break;
- case '/decorationManage': //装修
- baseUrl = '/decorationManage';
- break;
- default:
- baseUrl = "";
- break;
- }
- }
- }
- if (host.PROJECT === 'chuangxinfang') {
- const host = window.location.hostname;
- switch (host) {
- case 'www.novotown.com.cn':
- baseUrl = `/wg${baseUrl}`;
- break
- }
- }
- url = baseUrl + url;
- if (type == 'GET') {
- let dataStr = ''; //数据拼接字符串
- Object.keys(data).forEach(key => {
- if (typeof (data[key]) == "undefined") {
- data[key] = "";
- }
- dataStr += key + '=' + data[key] + '&';
- })
- if (dataStr !== '') {
- dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
- url = url + '?' + dataStr + '&_t=' + new Date().getTime();
- } else {
- url = url + (url.indexOf('?') > -1 ? '&_t=' + new Date().getTime() : '?_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 (localStorage.token != "" && typeof (localStorage.token) !== "undefined") {
- header_content.client_token = localStorage.token;
- header_content.token = localStorage.token;
- }
- // if (localStorage.companyId != "" && typeof (localStorage.companyId) !== "undefined") {
- // header_content.companyId = localStorage.companyId;
- // }
- // if (localStorage.user_id != "" && typeof (localStorage.user_id) !== "undefined") {
- // header_content.userId = localStorage.user_id;
- // }
- header_content.sign = key_gen.encode(key_gen.handlerData(type == 'GET' ? url.substring(url.indexOf("?") + 1) : data));
- 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',
- 'token': localStorage.token,
- };
- } else if (dataType && dataType === "FORMDATA") {
- header_content = {
- dataType: "json",
- cache: false, //上传文件无需缓存
- processData: false, //用于对data参数进行序列化处理 这里必须false
- contentType: false, //必须
- };
- }
- // 空置房管理接口 头部必须含有userId以及userName
- if (baseUrl == '/vacant') {
- if (localStorage.user_id && localStorage.user_id != "") {
- header_content.userId = localStorage.user_id;
- header_content.userName = encodeURI(localStorage.user_name);
- }
- }
- if (localStorage.companyId != "" && typeof (localStorage.companyId) !== "undefined") {
- header_content.companyId = localStorage.companyId;
- }
- if (localStorage.user_id != "" && typeof (localStorage.user_id) !== "undefined") {
- 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
- })
- }
- // if (type == 'POST') {
- // Object.defineProperty(requestConfig, 'body', {
- // value: initData(data)
- // })
- // }
- //if (type == 'POST') {
- // Object.defineProperty(requestConfig, 'body', {
- // value: JSON.stringify(data)
- // })
- //}
- try {
- const response = await fetch(url, requestConfig);
- if (response.headers.get("token_code") == -2 || response.headers.get("token_code") == -1) {
- // alert("当前账号已过期,请重新登录!");
- // window.alert=function(str){return;}
- Vue.prototype.$Message.config({
- top: 80,
- duration: 3
- });
- 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 (response.headers.get("securitycode") && response.headers.get("securitycode") == 0) {
- //0.未通过; -1.不验证; 1.通过;
- Vue.prototype.$Message.config({
- top: 80,
- duration: 3
- });
- Vue.prototype.$Message.info("非法请求");
- 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();
- if (responseJson.status == '401') {
- localStorage.clear();
- sessionStorage.clear();
- location.reload();
- }
- return responseJson
- }
- }
- } catch (error) {
- Vue.prototype.$Spin.hide();
- const responseJson = {
- errCode: 500,
- msg: "系统异常"
- };
- return responseJson;
- // throw new Error(error)
- }
- } else {
- return new Promise((resolve, reject) => {
- let requestObj;
- if (window.XMLHttpRequest) {
- requestObj = new XMLHttpRequest();
- } else {
- requestObj = new ActiveXObject;
- }
- let sendData = '';
- // if (type == 'POST') {
- // console.log(data)
- // Object.keys(data).forEach(key => {
- // sendData += key + '=' + data[key] + '&';
- // })
- // sendData = sendData.substr(0, sendData.lastIndexOf('&'));
- // }
- 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);
- 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); //必须
- }
- requestObj.setRequestHeader("sign", key_gen.encode(key_gen.handlerData(type == 'GET' ? url.substring(url.indexOf("?") + 1) : data)));
- if (localStorage.token != "" && typeof (localStorage.token) !== "undefined") {
- requestObj.setRequestHeader("token", localStorage.token);
- requestObj.setRequestHeader("client_token", localStorage.token);
- }
- if (localStorage.companyId != "" && typeof (localStorage.companyId) !== "undefined") {
- requestObj.setRequestHeader("companyId", localStorage.companyId);
- }
- if (localStorage.user_id != "" && typeof (localStorage.user_id) !== "undefined") {
- requestObj.setRequestHeader("user_id", 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)
- }
- }
- }
- })
- }
- }
|