Common.js 13 KB

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