Common.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import Notify from './Notify.js';
  2. import { notification } from 'ant-design-vue';
  3. export default {
  4. pageSize: 20,
  5. /**
  6. * 使用 BootstarpDialog 处理Ajax请求异常
  7. * @param {*} XMLHttpRequest
  8. * @param {*} textStatus
  9. * @param {*} errorThrown
  10. */
  11. processException: function (XMLHttpRequest, textStatus, errorThrown) {
  12. var _self = this;
  13. console.log(XMLHttpRequest);
  14. if (XMLHttpRequest.status == 400) {
  15. // 400 Bad Request
  16. Notify.error('400', XMLHttpRequest.responseText, true);
  17. } else if (XMLHttpRequest.status == 401) {
  18. var currentUrl = window.location;
  19. var href = window.location.href;
  20. // 当前未处于登陆的界面
  21. // 系统未登录
  22. if (href.indexOf('login') < 0 && href.indexOf('redirectUrl=') < 0) {
  23. // 处理钉钉免登陆
  24. const clientId = this.getRouteParam('clientId');
  25. const appName = this.getRouteParam('appName');
  26. const corpId = this.getRouteParam('corpId');
  27. let newUrl;
  28. if (clientId != null && clientId.length > 0 && appName != null && appName.length > 0 && corpId != null && corpId.length > 0) {
  29. newUrl = _self.getRedirectUrl('#/login?clientId=' + clientId + '&appName=' + appName + '&corpId=' + corpId + '&redirectUrl=' + encodeURIComponent(currentUrl));
  30. } else {
  31. newUrl = _self.getRedirectUrl('#/login?redirectUrl=' + encodeURIComponent(currentUrl));
  32. }
  33. window.location = newUrl;
  34. }
  35. } else if (XMLHttpRequest.status == 500) {
  36. // 500 Internal Server Error
  37. Notify.error('500', XMLHttpRequest.responseText, true);
  38. if (XMLHttpRequest.responseText.indexOf('登录超时') > 0) {
  39. // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面
  40. setTimeout(function () {
  41. window.location = _self.getRedirectUrl('#/login');
  42. }, 2 * 1000);
  43. }
  44. } else {
  45. Notify.error('服务器异常', XMLHttpRequest.responseText, true);
  46. }
  47. },
  48. /**
  49. * 使用 ant-design-vue 处理Ajax请求异常
  50. * @param {*} XMLHttpRequest
  51. */
  52. processExceptionWithAntV: function (XMLHttpRequest) {
  53. var _self = this;
  54. console.log(XMLHttpRequest);
  55. notification.error({
  56. message: XMLHttpRequest.status,
  57. description: XMLHttpRequest.responseText,
  58. });
  59. if (XMLHttpRequest.status == 401) {
  60. var currentUrl = window.location;
  61. var href = window.location.href;
  62. // 当前未处于登陆的界面
  63. // 系统未登录
  64. if (href.indexOf('login') < 0 && href.indexOf('redirectUrl=') < 0) {
  65. // 处理钉钉免登陆
  66. const clientId = this.getRouteParam('clientId');
  67. const appName = this.getRouteParam('appName');
  68. const corpId = this.getRouteParam('corpId');
  69. let newUrl;
  70. if (clientId != null && clientId.length > 0 && appName != null && appName.length > 0 && corpId != null && corpId.length > 0) {
  71. newUrl = _self.getRedirectUrl('index.html#/login?clientId=' + clientId + '&appName=' + appName + '&corpId=' + corpId + '&redirectUrl=' + encodeURIComponent(currentUrl));
  72. } else {
  73. newUrl = _self.getRedirectUrl('index.html#/login?redirectUrl=' + encodeURIComponent(currentUrl));
  74. }
  75. window.location = newUrl;
  76. }
  77. } else if (XMLHttpRequest.status == 500) {
  78. // 500 Internal Server Error
  79. if (XMLHttpRequest.responseText.indexOf('登录超时') >= 0) {
  80. // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面
  81. setTimeout(function () {
  82. window.location = _self.getRedirectUrl('index.html#/login');
  83. }, 2 * 1000);
  84. }
  85. }
  86. },
  87. /**
  88. * 获取主机地址
  89. */
  90. getRootPath: function () {
  91. var protocol = window.location.protocol;
  92. //console.log("protocol:" + protocol);
  93. var host = window.location.host;
  94. //console.log("host:" + host);
  95. var localhostPaht = protocol + '//' + host;
  96. //console.log("localhostPaht:" + localhostPaht);
  97. return localhostPaht;
  98. },
  99. getHostPageBaseURL: function () {
  100. //return this.getRootPath() + '/';
  101. // changed by jack 增加代理的配置。
  102. if(typeof REMOTE_DEV_PORT !== 'undefined' && REMOTE_DEV_PORT != null){
  103. return this.getRootPath() + '/proxy/' + REMOTE_DEV_PORT;
  104. }else{
  105. return this.getRootPath();
  106. }
  107. },
  108. // 获取图片路径url
  109. getFileServerUrl: function () {
  110. return this.getHostPageBaseURL() + '/';
  111. },
  112. // 获取API的地址
  113. getApiURL: function (apiName) {
  114. if(apiName.startsWith('/')){
  115. return this.getHostPageBaseURL() + '/api' + apiName;
  116. }else{
  117. return this.getHostPageBaseURL() + '/api/' + apiName;
  118. }
  119. },
  120. /**
  121. * 获取API的地址
  122. * @param {*} apiName
  123. * @returns
  124. */
  125. getApiUrl2: function (apiName) {
  126. if (apiName === undefined || apiName === null || apiName.length === 0) {
  127. return this.getHostPageBaseURL();
  128. }
  129. if (apiName[0] === '/') {
  130. return this.getHostPageBaseURL() + apiName;
  131. } else {
  132. return this.getHostPageBaseURL() + '/' + apiName;
  133. }
  134. },
  135. // 获取测试API的地址
  136. getTestApiURL: function (apiName) {
  137. return 'http://xxx/' + 'api/' + apiName;
  138. },
  139. // 获取图片路径
  140. getImageUrl: function (imageName) {
  141. if (imageName == null || imageName == '') {
  142. return this.getFileServerUrl() + 'notFound.png';
  143. } else {
  144. return this.getFileServerUrl() + imageName;
  145. }
  146. },
  147. // 获取图片路径
  148. getImageSrc: function (className, imageName) {
  149. var accountId = localStorage.getItem('#accountId');
  150. if (imageName == null) {
  151. return null;
  152. }
  153. return '/api/file/imageDownload?className=' + className + '&fileName=' + imageName;
  154. },
  155. // 获取略缩图图片路径
  156. getThumbnailImageSrc: function (className, imageName) {
  157. var accountId = localStorage.getItem('#accountId');
  158. if (imageName == null) {
  159. return null;
  160. }
  161. return '/api/file/thumbnailImageDownload?className=' + className + '&fileName=' + imageName;
  162. },
  163. /**
  164. * 获取附件的路径
  165. * @param {[type]} className [description]
  166. * @param {[type]} imageName [description]
  167. * @return {[type]} [description]
  168. */
  169. getAttachmentsSrc: function (className, imageName) {
  170. var accountId = localStorage.getItem('#accountId');
  171. return this.getFileServerUrl() + 'Files/' + accountId + '/Attachments/' + className + '/' + imageName;
  172. },
  173. // 获取图片路径
  174. getVideoSrc: function (className, imageName) {
  175. var accountId = localStorage.getItem('#accountId');
  176. if (imageName == undefined || imageName == '') {
  177. return this.getHostPageBaseURL() + 'static/image/noImage.jpg';
  178. }
  179. return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + imageName;
  180. },
  181. // 给请求头中加上account和token信息
  182. addTokenToRequest: function (request) {
  183. var token = localStorage.getItem('#token');
  184. var account = localStorage.getItem('#accountId');
  185. request.setRequestHeader('account', account);
  186. request.setRequestHeader('token', token);
  187. },
  188. /**
  189. * 获取Token
  190. */
  191. getToken: function () {
  192. return localStorage.getItem('#token');
  193. },
  194. // 清空 Cookie
  195. clearCookie: function () {
  196. // eslint-disable-next-line
  197. var keys = document.cookie.match('/[^ =;]+(?=\=)/g');
  198. if (keys) {
  199. for (var i = keys.length; i--;) {
  200. // 清除当前域名路径的有限日期
  201. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
  202. // Domain Name域名 清除当前域名的
  203. document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
  204. // 清除一级域名下的或指定的
  205. document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString();
  206. }
  207. }
  208. },
  209. // 获取路由中的参数
  210. getRouteParam: function (name) {
  211. var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
  212. if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
  213. return '';
  214. },
  215. /**
  216. * 获取跳转的路径
  217. * @param {*} url
  218. */
  219. getRedirectUrl: function (url) {
  220. return this.getHostPageBaseURL() + '/' + url;
  221. },
  222. clearLocalStorage: function () {
  223. // 清理localStorage时需要保留的参数列表
  224. var reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
  225. 'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL'];
  226. //存放的信息
  227. var reserveParamValues = [];
  228. //获取参数信息
  229. var len = reserveParams.length;
  230. for (let i = 0; i < len; i++) {
  231. var reserveParam = reserveParams[i];
  232. var reserveParamValue = '';
  233. if (localStorage.getItem(reserveParam) != undefined) {
  234. reserveParamValue = localStorage.getItem(reserveParam);
  235. }
  236. reserveParamValues.push(reserveParamValue);
  237. }
  238. //清理localStorage
  239. window.localStorage.clear();
  240. //还原参数信息
  241. for (let i = 0; i < len; i++) {
  242. localStorage.setItem(reserveParams[i], reserveParamValues[i]);
  243. }
  244. },
  245. };