Common.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import { Notify } from 'pc-component-v3';
  2. import { notification } from 'ant-design-vue';
  3. export default {
  4. pageSize: 20,
  5. // 异常处理
  6. processException: function (XMLHttpRequest, textStatus, errorThrown) {
  7. var _self = this;
  8. console.log(XMLHttpRequest);
  9. if (XMLHttpRequest.status == 400) {
  10. // 400 Bad Request
  11. Notify.error('400', XMLHttpRequest.responseText, true);
  12. } else if (XMLHttpRequest.status == 401) {
  13. var currentUrl = window.location;
  14. var href = window.location.href;
  15. // 当前未处于登陆的界面
  16. // 系统未登录
  17. if (href.indexOf('login') < 0 && href.indexOf('redirectUrl=') < 0) {
  18. // 处理钉钉免登陆
  19. const clientId = this.getRouteParam('clientId');
  20. const appName = this.getRouteParam('appName');
  21. const corpId = this.getRouteParam('corpId');
  22. let newUrl;
  23. if(clientId != null && clientId.length > 0 && appName != null && appName.length > 0 && corpId != null && corpId.length > 0){
  24. newUrl = _self.getRedirectUrl('#/login?clientId=' + clientId + '&appName=' + appName + '&corpId=' + corpId + '&redirectUrl=' + encodeURIComponent(currentUrl));
  25. }else{
  26. newUrl = _self.getRedirectUrl('#/login?redirectUrl=' + encodeURIComponent(currentUrl));
  27. }
  28. window.location = newUrl;
  29. }
  30. } else if (XMLHttpRequest.status == 500) {
  31. // 500 Internal Server Error
  32. Notify.error('500', XMLHttpRequest.responseText, true);
  33. if (XMLHttpRequest.responseText.indexOf('登录超时') > 0) {
  34. // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面
  35. setTimeout(function () {
  36. window.location = _self.getRedirectUrl('#/login');
  37. }, 2 * 1000);
  38. }
  39. } else {
  40. Notify.error('服务器异常', XMLHttpRequest.responseText, true);
  41. }
  42. },
  43. /**
  44. * 获取主机地址
  45. */
  46. getRootPath: function () {
  47. //获取当前网址,如: http://localhost:8083/myproj/view/my.jsp
  48. var curWwwPath = window.document.location.href;
  49. //获取主机地址之后的目录,如: myproj/view/my.jsp
  50. var pathName = window.document.location.pathname;
  51. var pos = curWwwPath.indexOf(pathName);
  52. //获取主机地址,如: http://localhost:8083
  53. var localhostPaht = curWwwPath.substring(0, pos);
  54. return localhostPaht;
  55. },
  56. getHostPageBaseURL: function () {
  57. return this.getRootPath() + '/';
  58. },
  59. // 获取图片路径url
  60. getFileServerUrl: function () {
  61. return this.getRootPath() + '/';
  62. },
  63. // 获取API的地址
  64. getApiURL: function (apiName) {
  65. return this.getHostPageBaseURL() + 'api/' + apiName;
  66. },
  67. // 获取datacenterAPI的地址
  68. getDataCenterURL: function (apiName) {
  69. return this.getHostPageBaseURL() + apiName;
  70. },
  71. // 获取测试API的地址
  72. getTestApiURL: function (apiName) {
  73. return 'http://xxx/' + 'api/' + apiName;
  74. },
  75. // 获取中间件API的地址
  76. getMidURL: function (apiName) {
  77. var that = this;
  78. if (window.middlewareUrl == undefined) {
  79. $.ajax({
  80. url: that.getApiURL('MiddleareResource/getApiUrl'),
  81. type: 'GET',
  82. dataType: 'text',
  83. async: false,
  84. beforeSend: function (request) {
  85. that.addTokenToRequest(request);
  86. },
  87. success: function (data) {
  88. if (data.indexOf('http') == 0) {
  89. window.middlewareUrl = data;
  90. } else {
  91. window.middlewareUrl = that.getHostPageBaseURL() + 'mid';
  92. }
  93. if (window.middlewareUrl.indexOf(window.middlewareUrl.length() - 1) == '/') {
  94. window.middlewareUrl = window.middlewareUrl.substring(0, window.middlewareUrl.length() - 2);
  95. }
  96. },
  97. error: function (XMLHttpRequest, textStatus, errorThrown) {
  98. window.middlewareUrl = that.getHostPageBaseURL() + 'mid';
  99. },
  100. });
  101. }
  102. if (apiName.indexOf(0) == '/') {
  103. apiName = apiName.substring(1, apiName.length() - 2);
  104. }
  105. return window.middlewareUrl + apiName;
  106. },
  107. // 获取微信测试api地址
  108. getWeixinApiURL: function (apiName) {
  109. return this.getHostPageBaseURL() + '/api/' + apiName;
  110. },
  111. // 获取图片路径
  112. getImageUrl: function (imageName) {
  113. if (imageName == null || imageName == '') {
  114. return this.getFileServerUrl() + 'notFound.png';
  115. } else {
  116. return this.getFileServerUrl() + imageName;
  117. }
  118. },
  119. // 获取图片路径
  120. getImageSrc: function (className, imageName) {
  121. if (imageName == null) {
  122. return null;
  123. }
  124. return '/api/file/imageDownload?className=' + className + '&fileName=' + encodeURIComponent(imageName);
  125. },
  126. // 获取略缩图图片路径
  127. getThumbnailImageSrc: function (className, imageName) {
  128. if (imageName == null) {
  129. return null;
  130. }
  131. return '/api/file/thumbnailImageDownload?className=' + className + '&fileName=' + encodeURIComponent(imageName);
  132. },
  133. /**
  134. * 获取附件的路径
  135. * @param {[type]} className [description]
  136. * @param {[type]} imageName [description]
  137. * @return {[type]} [description]
  138. */
  139. getAttachmentsSrc: function (className, imageName) {
  140. var accountId = localStorage.getItem('#accountId');
  141. return this.getFileServerUrl() + 'Files/' + accountId + '/Attachments/' + className + '/' + imageName;
  142. },
  143. // 获取图片路径
  144. getVideoSrc: function (className, imageName) {
  145. var accountId = localStorage.getItem('#accountId');
  146. if (imageName == undefined || imageName == '') {
  147. return this.getHostPageBaseURL() + 'static/image/noImage.jpg';
  148. }
  149. return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + imageName;
  150. },
  151. //获取资源路径 type: 图片image,视频video,文件file,
  152. getResourceUrl: function (type, className, resourceName) {
  153. var accountId = localStorage.getItem('#accountId');
  154. if (resourceName == undefined || className == undefined || type == undefined || resourceName == '' || className == '' || type == '') {
  155. return;
  156. }
  157. if (type == 'image') {
  158. return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/' + resourceName;
  159. }
  160. if (type == 'video') {
  161. return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + resourceName;
  162. }
  163. if (type == 'file') {
  164. return this.getFileServerUrl() + 'Files/' + accountId + '/Files/' + className + '/' + resourceName;
  165. }
  166. },
  167. getApsURL: function (apiName) {
  168. var apsBaseUrl = localStorage.getItem('apsBaseUrl');
  169. if (apsBaseUrl == undefined) {
  170. Notify.error('错误', '系统参数"apsBaseUrl"未设置,请联系系统管理员设置参数', false);
  171. return;
  172. }
  173. return apsBaseUrl + apiName;
  174. },
  175. getActivitiURL: function (apiName) {
  176. var apsBaseUrl = localStorage.getItem('activitiUrl');
  177. if (apsBaseUrl == undefined) {
  178. Notify.error('错误', '系统参数"activitiUrl"未设置,请联系系统管理员设置参数', false);
  179. return;
  180. }
  181. return apsBaseUrl + apiName;
  182. },
  183. getSchedulingURL: function (apiName) {
  184. var _self = this;
  185. var apsBaseUrl = localStorage.getItem('schedulingUrl');
  186. if (apsBaseUrl == undefined) {
  187. _self.loadSystemParam('schedulingUrl', function () {
  188. apsBaseUrl = localStorage.getItem('schedulingUrl');
  189. if (apsBaseUrl == undefined) {
  190. Notify.error('错误', '系统参数"schedulingUrl"未设置,请联系系统管理员设置参数', false);
  191. return;
  192. }
  193. });
  194. }
  195. return apsBaseUrl + apiName;
  196. },
  197. getCameraURL: function (apiName) {
  198. var apsBaseUrl = localStorage.getItem('cameraBaseURL');
  199. if (apsBaseUrl == undefined) {
  200. Notify.error('错误', '系统参数"cameraBaseURL"未设置,请联系系统管理员设置参数', false);
  201. return;
  202. }
  203. return apsBaseUrl + apiName;
  204. },
  205. //设置路径到localStorage
  206. setHref: function () {
  207. var href = window.location.href;
  208. if (href.indexOf('http') == 0) {
  209. href = href.substring(0, href.indexOf('#') + 2);
  210. localStorage.setItem('href', href);
  211. } else {
  212. var hostPageBaseURL = localStorage.getItem('hostPageBaseURL');
  213. if (hostPageBaseURL == undefined) {
  214. var href1 = window.location.href;
  215. href1 = href1.substring(0, href1.indexOf('#') + 2);
  216. localStorage.setItem('href', href1);
  217. }
  218. }
  219. },
  220. //加载系统参数到localStorage
  221. loadSystemParam: function (systemParamName, success) {
  222. var that = this;
  223. $.ajax({
  224. url: that.getApiURL('SystemParamResource/loadSystemParam'),
  225. type: 'GET',
  226. dataType: 'text',
  227. data: {
  228. 'systemParamName': systemParamName,
  229. },
  230. beforeSend: function (request) {
  231. that.addTokenToRequest(request);
  232. },
  233. success: function (data) {
  234. localStorage.setItem(systemParamName, data);
  235. success();
  236. },
  237. error: function (XMLHttpRequest, textStatus, errorThrown) {
  238. that.processException(XMLHttpRequest, textStatus, errorThrown);
  239. },
  240. });
  241. },
  242. // 给请求头中加上account和token信息
  243. addTokenToRequest: function (request) {
  244. request.setRequestHeader('token', localStorage.getItem('#token'));
  245. },
  246. /**
  247. * 获取Token
  248. */
  249. getToken: function () {
  250. return localStorage.getItem('#token');
  251. },
  252. // 获取新建对象的Id
  253. getNewRecordId: function () {
  254. window.CRUDId++;
  255. return window.CRUDId;
  256. },
  257. // 清空 Cookie
  258. clearCookie: function () {
  259. var keys = document.cookie.match(/[^ =;]+(?=\\=)/g);
  260. if (keys) {
  261. for (var i = keys.length; i--;) {
  262. // 清除当前域名路径的有限日期
  263. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
  264. // Domain Name域名 清除当前域名的
  265. document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
  266. // 清除一级域名下的或指定的
  267. document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString();
  268. }
  269. }
  270. },
  271. clearLocalStorage: function () {
  272. // 清理localStorage时需要保留的参数列表
  273. var reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
  274. 'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL'];
  275. //存放的信息
  276. var reserveParamValues = [];
  277. //获取参数信息
  278. var len = reserveParams.length;
  279. for (var i = 0; i < len; i++) {
  280. var reserveParam = reserveParams[i];
  281. var reserveParamValue = '';
  282. if (localStorage.getItem(reserveParam) != undefined) {
  283. reserveParamValue = localStorage.getItem(reserveParam);
  284. }
  285. reserveParamValues.push(reserveParamValue);
  286. }
  287. //清理localStorage
  288. window.localStorage.clear();
  289. //还原参数信息
  290. for (var j = 0; j < len; j++) {
  291. localStorage.setItem(reserveParams[j], reserveParamValues[j]);
  292. }
  293. },
  294. showDialog: function (title, content, type) {
  295. if (type == 'success') {
  296. Notify.success(title, content, 4000);
  297. }
  298. else if (type == 'error') {
  299. Notify.error(title, content, -1);
  300. }
  301. else if (type == 'info') {
  302. Notify.info(title, content, 2000);
  303. }
  304. else if (type == 'notice') {
  305. Notify.notice(title, content, 2000);
  306. }
  307. },
  308. //合并路由数组
  309. mergeArray: function (arr1, arr2) {
  310. var arr = [];
  311. for (var k = 0, len = arr1.length; k < len; k++) {
  312. arr.push(arr1[k]);
  313. }
  314. for (var i = 0, len2 = arr2.length; i < len2; i++) {
  315. //是否添加该元素
  316. var flag = true;
  317. for (var j = 0, len1 = arr.length; j < len1; j++) {
  318. //如果path相同,则合并children;如果children不存在,则不合并;
  319. if (arr[j].path == arr2[i].path) {
  320. flag = false;
  321. if (arr[j].children instanceof Array && arr2[i].children instanceof Array) {
  322. arr[j].children = this.mergeArray(arr[j].children, arr2[i].children);
  323. }
  324. }
  325. }
  326. if (flag) {
  327. arr.push(arr2[i]);
  328. }
  329. }
  330. //将path为"*"的项移到最后
  331. var temp;
  332. var index;
  333. for (var n = 0, len3 = arr.length; n < len3; n++) {
  334. if (arr[n].path == '*' || arr[n].path == '/*') {
  335. index = n;
  336. }
  337. }
  338. if (index != undefined) {
  339. temp = arr[index];
  340. arr[index] = arr[len3 - 1];
  341. arr[len3 - 1] = temp;
  342. }
  343. return arr;
  344. },
  345. // 获取路由中的参数
  346. getRouteParam: function (name) {
  347. var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
  348. if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
  349. return '';
  350. },
  351. /**
  352. * 获取跳转的路径
  353. * @param {*} url
  354. */
  355. getRedirectUrl: function (url) {
  356. var href = window.location.href;
  357. if (href.indexOf('pcapp') >= 0) {
  358. return this.getRootPath() + '/pcapp/' + url;
  359. } else {
  360. return this.getRootPath() +'/'+ url;
  361. }
  362. },
  363. /**
  364. * 可以修改url的参数
  365. * 例如将
  366. * www.baidu.com
  367. * 修改为
  368. * www.baidu.com?name=123
  369. * 操作为
  370. * window.location.href = changeURLArg(window.location.href,'name',123)
  371. */
  372. changeURLArg: function (url, arg, value) {
  373. var pattern = arg + '=([^&]*)';
  374. var replaceText = arg + '=' + value;
  375. if (url.match(pattern)) {
  376. var tmp = '/(' + arg + '=)([^&]*)/gi';
  377. tmp = url.replace(eval(tmp), replaceText);
  378. return tmp;
  379. } else {
  380. if (url.match('[\\?]')) {
  381. return url + '&' + replaceText;
  382. } else {
  383. return url + '?' + replaceText;
  384. }
  385. }
  386. },
  387. };
  388. /**
  389. * 请求错误
  390. * @param {} err
  391. */
  392. export function requestFailed(err) {
  393. console.error(err);
  394. notification['error']({
  395. message: '错误',
  396. description: ((err.response || {}).data || {}).message || '请求出现错误,请稍后再试',
  397. duration: 8,
  398. });
  399. }
  400. /**
  401. * 请求正常
  402. * @param {} response
  403. */
  404. export function requestSuccess(response) {
  405. if (response.errorCode !== 0) {
  406. notification['error']({
  407. message: '错误',
  408. description: response.errorMessage,
  409. duration: 8,
  410. });
  411. }
  412. }
  413. /**
  414. * 错误提示
  415. * @param {} response
  416. */
  417. export function notificationError(message, title) {
  418. notification['error']({
  419. message: title || '操作失败',
  420. description: message,
  421. duration: 8,
  422. });
  423. }
  424. /**
  425. * 错误提示
  426. * @param {} response
  427. */
  428. export function notificationSuccess(message, title) {
  429. notification['success']({
  430. message: title || '操作成功',
  431. description: message,
  432. duration: 8,
  433. });
  434. }
  435. // 获取IP
  436. export function getRootPath() {
  437. var protocol = window.location.protocol;
  438. var host = window.location.host;
  439. var localhostPath = `${protocol}//${host}`;
  440. return localhostPath;
  441. }
  442. // 格式化时间为 YYYY-MM-DD HH:mm:ss
  443. export function getFormattedDateTime() {
  444. const now = new Date();
  445. const year = now.getFullYear();
  446. const month = String(now.getMonth() + 1).padStart(2, '0');
  447. const day = String(now.getDate()).padStart(2, '0');
  448. const hours = String(now.getHours()).padStart(2, '0');
  449. const minutes = String(now.getMinutes()).padStart(2, '0');
  450. const seconds = String(now.getSeconds()).padStart(2, '0');
  451. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  452. }