123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- import Vue from 'vue'
- import util from '../src/assets/js/util';
- import '../src/assets/js/encry.min.js' //接口加密工具
- 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', requestType = 'FORMDATA', pathArr = [], method = 'fetch') => {
- type = type.toUpperCase();
- // 清除空参数
- //if(type == "GET") {
- // data = util.filter_param(data);
- //}
- if (process.env.NODE_ENV === 'development') {
- baseUrl = baseUrl
- } else if (process.env.NODE_ENV === 'production') {
- if (baseUrl) {
- switch (baseUrl) {
- case '/wuyecrm':
- baseUrl = '/wuyecrm'; // 物业crm云环境测试收费需要改成wuyecrm6093
- break;
- case '/iot':
- baseUrl = '/device'; // iot首页
- break;
- case '/datacenter':
- baseUrl = '/datacenter'; // 数据中心
- break;
- case '/datashow':
- baseUrl = '/datashow'; //综合门户
- break;
- case '/Edevice':
- baseUrl = '/device'; // 设备管理
- break;
- case '/fullscreen':
- baseUrl = '/datashow'; // 住总大屏、中启云大屏
- break;
- case '/warning':
- baseUrl = '/warning'; //预警管理
- break;
- case '/groupcontrol':
- baseUrl = '/groupcontrol'; //会议、机房进出等管理
- break;
- case '/dining':
- baseUrl = '/dining'; //订餐
- break;
- case '/cesesynchron':
- baseUrl = '/cesesynchron'; //消费管理
- break;
- case '/energy-charging':
- baseUrl = '/energy-charging'; //能源
- break;
- case '/activiti':
- baseUrl = '/activiti'; //流程
- break;
- case '/neikong':
- baseUrl = '/neikong'; //访问主数据
- break;
- case '/leasing':
- baseUrl = '/leasing'; //租赁系统
- break;
- case '/landcrm':
- baseUrl = '/landcrm';
- break;
- case '/wxmanage':
- baseUrl = '/wxmanage';
- break;
- default:
- baseUrl = "";
- break;
- }
- }
- }
- // 将wuyecrm替换成landcrm
- if (baseUrl === '/wuyecrm') {
- if (process.env.Url.indexOf('114.55.208.86') >= 0) {
- // 新收费云环境测试环境
- baseUrl = '/wuyecrm6903'
- } else {
- baseUrl = '/landcrm'
- }
- }
- url = baseUrl + url;
- if (pathArr && pathArr.length > 0) {
- url += "/" + pathArr.join("/");
- }
- if (type == 'GET') {
- let dataStr = ''; //数据拼接字符串
- Object.keys(data).forEach(key => {
- dataStr += key + '=' + 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',
- 'token': '',
- 'x-user-header': ''
- };
- if (dataType && dataType === "XML") {
- header_content = {
- 'Accept': 'application/xml',
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'token': ''
- };
- } else {
- if (requestType === "JSON") {
- header_content = {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- 'token': '',
- 'x-user-header': localStorage.user_account ? localStorage.user_account : '',
- 'x-userId-header': localStorage.user_id ? localStorage.user_id : '',
- 'x-userAccount-header': localStorage.userAccount ? encodeURI(localStorage.userAccount) : (localStorage.user_account ? encodeURI(localStorage.user_account) : ''),
- 'x-userName-header': localStorage.userName ? encodeURI(localStorage.userName) : (localStorage.user_name ? encodeURI(localStorage.user_name) : ''),
- 'x-companyId-header': localStorage.companyId ? localStorage.companyId : '',
- };
- } else if (requestType === "FORMDATA") {
- header_content = {
- 'Accept': 'application/json',
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Authorization': "Basic ZWJlaTplYmVp",
- 'x-user-header': localStorage.user_account ? localStorage.user_account : '',
- 'x-userId-header': localStorage.user_id ? localStorage.user_id : '',
- 'x-userAccount-header': localStorage.userAccount ? encodeURI(localStorage.userAccount) : (localStorage.user_account ? encodeURI(localStorage.user_account) : ''),
- 'x-userName-header': localStorage.userName ? encodeURI(localStorage.userName) : (localStorage.user_name ? encodeURI(localStorage.user_name) : ''),
- 'x-companyId-header': localStorage.companyId ? localStorage.companyId : '',
- };
- }
- }
- if (localStorage.token != "" && typeof (localStorage.token) !== "undefined") {
- header_content.token = localStorage.token;
- }
- header_content.sign = key_gen.encode(key_gen.handlerData(type == 'GET' ? url.substring(url.indexOf("?") + 1) : data));
- let requestConfig = {
- credentials: 'include',
- method: type,
- headers: header_content,
- mode: "cors",
- cache: "force-cache"
- }
- if (type == 'POST') {
- if (requestType === "FORMDATA") {
- Object.defineProperty(requestConfig, 'body', {
- value: initData(data)
- })
- } else if (requestType === "JSON") {
- 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) {
- Vue.prototype.$Message.config({
- top: 80,
- duration: 3
- });
- Vue.prototype.$Message.info("当前账号已过期,请重新登录!");
- router.push({
- name: 'login',
- params: {
- timeout: true
- }
- });
- Vue.prototype.$Message = function (str) {
- return;
- };
- setTimeout(function () {
- localStorage.clear();
- 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();
- //throw new Error(error)
- 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') {
- console.log(data)
- Object.keys(data).forEach(key => {
- sendData += key + '=' + data[key] + '&';
- })
- sendData = sendData.substr(0, sendData.lastIndexOf('&'));
- console.log(sendData)
- }
- requestObj.open(type, url, true);
- requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- requestObj.setRequestHeader("Accept", "application/json");
- requestObj.send(sendData);
- requestObj.onreadystatechange = () => {
- if (requestObj.readyState == 4) {
- if (requestObj.status == 200) {
- let obj = requestObj.response
- if (typeof obj !== 'object' && obj.indexOf("<?xml") == -1) {
- obj = JSON.parse(obj);
- }
- resolve(obj)
- }
- }
- }
- })
- }
- }
|