Common.js 13 KB

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