| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- import { Notify } from 'pc-component-v3';
- export default{
- pageSize: 20,
- // 异常处理
- processException: function (XMLHttpRequest, textStatus, errorThrown) {
- var _self = this;
- console.log(XMLHttpRequest);
- if (XMLHttpRequest.status == 400) {
- // 400 Bad Request
- Notify.error('400', XMLHttpRequest.responseText, true);
- } else if (XMLHttpRequest.status == 401) {
- var currentUrl = window.location;
- var href = window.location.href;
- // 当前未处于登陆的界面
- // 系统未登录
- if (href.indexOf('login') < 0 && href.indexOf('redirectUrl=') < 0) {
- // 处理钉钉免登陆
- const clientId = this.getRouteParam('clientId');
- const appName = this.getRouteParam('appName');
- const corpId = this.getRouteParam('corpId');
- let newUrl;
- if (clientId != null && clientId.length > 0 && appName != null && appName.length > 0 && corpId != null && corpId.length > 0) {
- newUrl = _self.getRedirectUrl('#/login?clientId=' + clientId + '&appName=' + appName + '&corpId=' + corpId + '&redirectUrl=' + encodeURIComponent(currentUrl));
- } else {
- newUrl = _self.getRedirectUrl('#/login?redirectUrl=' + encodeURIComponent(currentUrl));
- }
- window.location = newUrl;
- }
- } else if (XMLHttpRequest.status == 500) {
- // 500 Internal Server Error
- Notify.error('500', XMLHttpRequest.responseText, true);
- if (XMLHttpRequest.responseText.indexOf('登录超时') > 0) {
- // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面
- setTimeout(function () {
- window.location = _self.getRedirectUrl('#/login');
- }, 2 * 1000);
- }
- } else {
- Notify.error('服务器异常', XMLHttpRequest.responseText, true);
- }
- },
- /**
- * 获取主机地址
- */
- getRootPath: function () {
- var protocol = window.location.protocol;
- // console.log('protocol:' + protocol);
- var host = window.location.host;
- // console.log('host:' + host);
- var localhostPaht = protocol + '//' + host;
- // console.log('localhostPaht:' + localhostPaht);
- return localhostPaht;
- },
- getHostPageBaseURL: function () {
- return this.getRootPath() + '/';
- },
- // 获取图片路径url
- getFileServerUrl: function () {
- return this.getRootPath() + '/';
- },
- // 获取API的地址
- getApiURL: function (apiName) {
- return this.getHostPageBaseURL() + 'api/' + apiName;
- },
- // 获取测试API的地址
- getTestApiURL: function (apiName) {
- return 'http://xxx/' + 'api/' + apiName;
- },
- // 获取中间件API的地址
- getMidURL: function (apiName) {
- var that = this;
- if (window.middlewareUrl == undefined) {
- $.ajax({
- url: that.getApiURL('MiddleareResource/getApiUrl'),
- type: 'GET',
- dataType: 'text',
- async: false,
- beforeSend: function (request) {
- that.addTokenToRequest(request);
- },
- success: function (data) {
- if (data.indexOf('http') == 0) {
- window.middlewareUrl = data;
- } else {
- window.middlewareUrl = that.getHostPageBaseURL() + 'mid';
- }
- if (window.middlewareUrl.indexOf(window.middlewareUrl.length() - 1) == '/') {
- window.middlewareUrl = window.middlewareUrl.substring(0, window.middlewareUrl.length() - 2);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- window.middlewareUrl = that.getHostPageBaseURL() + 'mid';
- },
- });
- }
- if (apiName.indexOf(0) == '/') {
- apiName = apiName.substring(1, apiName.length() - 2);
- }
- return window.middlewareUrl + apiName;
- },
- // 获取微信测试api地址
- getWeixinApiURL: function (apiName) {
- return this.getHostPageBaseURL() + '/api/' + apiName;
- },
- // 获取图片路径
- getImageUrl: function (imageName) {
- if (imageName == null || imageName == '') {
- return this.getFileServerUrl() + 'notFound.png';
- } else {
- return this.getFileServerUrl() + imageName;
- }
- },
- // 获取图片路径
- getImageSrc: function (className, imageName) {
- if (imageName == null) {
- return null;
- }
- return '/api/file/imageDownload?className=' + className + '&fileName=' + imageName;
- },
- // 获取略缩图图片路径
- getThumbnailImageSrc: function (className, imageName) {
- if (imageName == null) {
- return null;
- }
- return '/api/file/thumbnailImageDownload?className=' + className + '&fileName=' + imageName;
- },
-
- /**
- * 获取附件的路径
- * @param {[type]} className [description]
- * @param {[type]} imageName [description]
- * @return {[type]} [description]
- */
- getAttachmentsSrc: function (className, imageName) {
- var accountId = localStorage.getItem('#accountId');
- return this.getFileServerUrl() + 'Files/' + accountId + '/Attachments/' + className + '/' + imageName;
- },
- // 获取图片路径
- getVideoSrc: function (className, imageName) {
- var accountId = localStorage.getItem('#accountId');
- if (imageName == undefined || imageName == '') {
- return this.getHostPageBaseURL() + 'static/image/noImage.jpg';
- }
- return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + imageName;
- },
- //获取资源路径 type: 图片image,视频video,文件file,
- getResourceUrl: function (type, className, resourceName) {
- var accountId = localStorage.getItem('#accountId');
- if (resourceName == undefined || className == undefined || type == undefined || resourceName == '' || className == '' || type == '') {
- return;
- }
- if (type == 'image') {
- return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/' + resourceName;
- }
- if (type == 'video') {
- return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + resourceName;
- }
- if (type == 'file') {
- return this.getFileServerUrl() + 'Files/' + accountId + '/Files/' + className + '/' + resourceName;
- }
- },
- getApsURL: function (apiName) {
- var apsBaseUrl = localStorage.getItem('apsBaseUrl');
- if (apsBaseUrl == undefined) {
- Notify.error('错误', '系统参数"apsBaseUrl"未设置,请联系系统管理员设置参数', false);
- return;
- }
- return apsBaseUrl + apiName;
- },
- getActivitiURL: function (apiName) {
- var apsBaseUrl = localStorage.getItem('activitiUrl');
- if (apsBaseUrl == undefined) {
- Notify.error('错误', '系统参数"activitiUrl"未设置,请联系系统管理员设置参数', false);
- return;
- }
- return apsBaseUrl + apiName;
- },
- getSchedulingURL: function (apiName) {
- var _self = this;
- var apsBaseUrl = localStorage.getItem('schedulingUrl');
- if (apsBaseUrl == undefined) {
- _self.loadSystemParam('schedulingUrl', function () {
- apsBaseUrl = localStorage.getItem('schedulingUrl');
- if (apsBaseUrl == undefined) {
- Notify.error('错误', '系统参数"schedulingUrl"未设置,请联系系统管理员设置参数', false);
- return;
- }
- });
- }
- return apsBaseUrl + apiName;
- },
- getCameraURL: function (apiName) {
- var apsBaseUrl = localStorage.getItem('cameraBaseURL');
- if (apsBaseUrl == undefined) {
- Notify.error('错误', '系统参数"cameraBaseURL"未设置,请联系系统管理员设置参数', false);
- return;
- }
- return apsBaseUrl + apiName;
- },
- //设置路径到localStorage
- setHref: function () {
- var href = window.location.href;
- if (href.indexOf('http') == 0) {
- href = href.substring(0, href.indexOf('#') + 2);
- localStorage.setItem('href', href);
- } else {
- var hostPageBaseURL = localStorage.getItem('hostPageBaseURL');
- if (hostPageBaseURL == undefined) {
- href = href.substring(0, href.indexOf('#') + 2);
- localStorage.setItem('href', href);
- }
- }
- },
- //加载系统参数到localStorage
- loadSystemParam: function (systemParamName, success) {
- var that = this;
- $.ajax({
- url: that.getApiURL('SystemParamResource/loadSystemParam'),
- type: 'GET',
- dataType: 'text',
- data: {
- 'systemParamName': systemParamName,
- },
- beforeSend: function (request) {
- that.addTokenToRequest(request);
- },
- success: function (data) {
- localStorage.setItem(systemParamName, data);
- success();
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- that.processException(XMLHttpRequest, textStatus, errorThrown);
- },
- });
- },
- // 给请求头中加上account和token信息
- addTokenToRequest: function (request) {
- request.setRequestHeader('token', localStorage.getItem('#token'));
- },
- /**
- * 获取Token
- */
- getToken: function () {
- return localStorage.getItem('#token');
- },
- // 获取新建对象的Id
- getNewRecordId: function () {
- window.CRUDId++;
- return window.CRUDId;
- },
- // 清空 Cookie
- clearCookie: function () {
- var keys = document.cookie.match(/[^ =;]+(?=\\=)/g);
- if (keys) {
- for (var i = keys.length; i--;) {
- // 清除当前域名路径的有限日期
- document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
- // Domain Name域名 清除当前域名的
- document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
- // 清除一级域名下的或指定的
- document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString();
- }
- }
- },
- clearLocalStorage: function () {
- // 清理localStorage时需要保留的参数列表
- var reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
- 'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL'];
- //存放的信息
- var reserveParamValues = [];
- //获取参数信息
- var len = reserveParams.length;
- for (var i = 0; i < len; i++) {
- var reserveParam = reserveParams[i];
- var reserveParamValue = '';
- if (localStorage.getItem(reserveParam) != undefined) {
- reserveParamValue = localStorage.getItem(reserveParam);
- }
- reserveParamValues.push(reserveParamValue);
- }
- //清理localStorage
- window.localStorage.clear();
- //还原参数信息
- for (var j = 0; j < len; j++) {
- localStorage.setItem(reserveParams[j], reserveParamValues[j]);
- }
- },
- showDialog: function (title, content, type) {
- if (type == 'success') {
- Notify.success(title, content, 4000);
- }
- else if (type == 'error') {
- Notify.error(title, content, -1);
- }
- else if (type == 'info') {
- Notify.info(title, content, 2000);
- }
- else if (type == 'notice') {
- Notify.notice(title, content, 2000);
- }
- },
- //合并路由数组
- mergeArray: function (arr1, arr2) {
- var arr = [];
- for (var k = 0, len = arr1.length; k < len; k++) {
- arr.push(arr1[k]);
- }
- for (var i = 0, len2 = arr2.length; i < len2; i++) {
- //是否添加该元素
- var flag = true;
- for (var j = 0, len1 = arr.length; j < len1; j++) {
- //如果path相同,则合并children;如果children不存在,则不合并;
- if (arr[j].path == arr2[i].path) {
- flag = false;
- if (arr[j].children instanceof Array && arr2[i].children instanceof Array) {
- arr[j].children = this.mergeArray(arr[j].children, arr2[i].children);
- }
- }
- }
- if (flag) {
- arr.push(arr2[i]);
- }
- }
- //将path为"*"的项移到最后
- var temp;
- var index;
- for (var n = 0, len3 = arr.length; n < len3; n++) {
- if (arr[n].path == '*' || arr[n].path == '/*') {
- index = n;
- }
- }
- if (index != undefined) {
- temp = arr[index];
- arr[index] = arr[len3 - 1];
- arr[len3 - 1] = temp;
- }
- return arr;
- },
- // 获取路由中的参数
- getRouteParam: function (name) {
- var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
- if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
- return '';
- },
- /**
- * 获取跳转的路径
- * @param {*} url
- */
- getRedirectUrl: function (url) {
- var href = window.location.href;
- if (href.indexOf('pcapp') >= 0) {
- return this.getRootPath() + '/pcapp/' + url;
- } else {
- return this.getRootPath() + '/' + url;
- }
- },
- /**
- * 可以修改url的参数
- * 例如将
- * www.baidu.com
- * 修改为
- * www.baidu.com?name=123
- * 操作为
- * window.location.href = changeURLArg(window.location.href,'name',123)
- */
- changeURLArg: function (url, arg, value) {
- var pattern = arg + '=([^&]*)';
- var replaceText = arg + '=' + value;
- if (url.match(pattern)) {
- var tmp = '/(' + arg + '=)([^&]*)/gi';
- tmp = url.replace(eval(tmp), replaceText);
- return tmp;
- } else {
- if (url.match('[\\?]')) {
- return url + '&' + replaceText;
- } else {
- return url + '?' + replaceText;
- }
- }
- },
- };
|