Common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import { Notify, Uuid } 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('#accountId', localStorage.getItem('#accountId'));
  238. request.setRequestHeader('token', localStorage.getItem('#token'));
  239. },
  240. /**
  241. * 获取Token
  242. */
  243. getToken: function () {
  244. return localStorage.getItem('#token');
  245. },
  246. // 获取新建对象的Id
  247. getNewRecordId: function () {
  248. window.CRUDId++;
  249. return window.CRUDId;
  250. },
  251. // 清空 Cookie
  252. clearCookie: function () {
  253. // eslint-disable-next-line
  254. var keys = document.cookie.match('/[^ =;]+(?=\=)/g');
  255. if (keys) {
  256. for (var i = keys.length; i--;) {
  257. // 清除当前域名路径的有限日期
  258. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
  259. // Domain Name域名 清除当前域名的
  260. document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
  261. // 清除一级域名下的或指定的
  262. document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString();
  263. }
  264. }
  265. },
  266. // 清空 Cookie
  267. clearAppCookie: function (loginInfo) {
  268. document.cookie.split(';').forEach(function (c) {
  269. document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
  270. });
  271. $.removeCookie('token', { path: '/' });
  272. $.removeCookie('token', { path: '/app' });
  273. $.cookie('token', loginInfo.token, {
  274. expires: 7,
  275. path: '/',
  276. secure: true,
  277. sameSite: 'Strict',
  278. });
  279. },
  280. clearLocalStorage: function () {
  281. // 清理localStorage时需要保留的参数列表
  282. var reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
  283. 'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL'];
  284. //存放的信息
  285. var reserveParamValues = [];
  286. //获取参数信息
  287. var len = reserveParams.length;
  288. for (let i = 0; i < len; i++) {
  289. var reserveParam = reserveParams[i];
  290. var reserveParamValue = '';
  291. if (localStorage.getItem(reserveParam) != undefined) {
  292. reserveParamValue = localStorage.getItem(reserveParam);
  293. }
  294. reserveParamValues.push(reserveParamValue);
  295. }
  296. //清理localStorage
  297. window.localStorage.clear();
  298. //还原参数信息
  299. for (let i = 0; i < len; i++) {
  300. localStorage.setItem(reserveParams[i], reserveParamValues[i]);
  301. }
  302. },
  303. showDialog: function (title, content, type) {
  304. if (type == 'success') {
  305. Notify.success(title, content, 4000);
  306. }
  307. else if (type == 'error') {
  308. Notify.error(title, content, -1);
  309. }
  310. else if (type == 'info') {
  311. Notify.info(title, content, 2000);
  312. }
  313. else if (type == 'notice') {
  314. Notify.notice(title, content, 2000);
  315. }
  316. },
  317. // 获取路由中的参数
  318. getRouteParam: function (name) {
  319. var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
  320. if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
  321. return '';
  322. },
  323. /**
  324. * 获取跳转的路径
  325. * @param {*} url
  326. */
  327. getRedirectUrl: function (url) {
  328. var href = window.location.href;
  329. if (href.indexOf('pcapp') >= 0) {
  330. return this.getRootPath() + '/pcapp/' + url;
  331. } else {
  332. return this.getRootPath() + '/' + url;
  333. }
  334. },
  335. };