Common.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import Notify from './Notify.js';
  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. // changed by jack 增加代理的配置。
  57. if(typeof REMOTE_DEV_PORT !== 'undefined' && REMOTE_DEV_PORT != null){
  58. return this.getRootPath() + '/proxy/' + REMOTE_DEV_PORT;
  59. }else{
  60. return this.getRootPath();
  61. }
  62. },
  63. // 获取图片路径url
  64. getFileServerUrl: function () {
  65. return this.getHostPageBaseURL() + '/';
  66. },
  67. // 获取API的地址
  68. getApiURL: function (apiName) {
  69. if(apiName.startsWith('/')){
  70. return this.getHostPageBaseURL() + '/api' + apiName;
  71. }else{
  72. return this.getHostPageBaseURL() + '/api/' + apiName;
  73. }
  74. },
  75. /**
  76. * 获取API的地址
  77. * @param {*} apiName
  78. * @returns
  79. */
  80. getApiUrl2: function (apiName) {
  81. if (apiName === undefined || apiName === null || apiName.length === 0) {
  82. return this.getHostPageBaseURL();
  83. }
  84. if (apiName[0] === '/') {
  85. return this.getHostPageBaseURL() + apiName;
  86. } else {
  87. return this.getHostPageBaseURL() + '/' + apiName;
  88. }
  89. },
  90. // 获取测试API的地址
  91. getTestApiURL: function (apiName) {
  92. return 'http://xxx/' + 'api/' + apiName;
  93. },
  94. // 获取图片路径
  95. getImageUrl: function (imageName) {
  96. if (imageName == null || imageName == '') {
  97. return this.getFileServerUrl() + 'notFound.png';
  98. } else {
  99. return this.getFileServerUrl() + imageName;
  100. }
  101. },
  102. // 获取图片路径
  103. getImageSrc: function (className, imageName) {
  104. var accountId = localStorage.getItem('#accountId');
  105. if (imageName == null) {
  106. return null;
  107. }
  108. return '/api/file/imageDownload?className=' + className + '&fileName=' + imageName;
  109. },
  110. // 获取略缩图图片路径
  111. getThumbnailImageSrc: function (className, imageName) {
  112. var accountId = localStorage.getItem('#accountId');
  113. if (imageName == null) {
  114. return null;
  115. }
  116. return '/api/file/thumbnailImageDownload?className=' + className + '&fileName=' + imageName;
  117. },
  118. /**
  119. * 获取附件的路径
  120. * @param {[type]} className [description]
  121. * @param {[type]} imageName [description]
  122. * @return {[type]} [description]
  123. */
  124. getAttachmentsSrc: function (className, imageName) {
  125. var accountId = localStorage.getItem('#accountId');
  126. return this.getFileServerUrl() + 'Files/' + accountId + '/Attachments/' + className + '/' + imageName;
  127. },
  128. // 获取图片路径
  129. getVideoSrc: function (className, imageName) {
  130. var accountId = localStorage.getItem('#accountId');
  131. if (imageName == undefined || imageName == '') {
  132. return this.getHostPageBaseURL() + 'static/image/noImage.jpg';
  133. }
  134. return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + imageName;
  135. },
  136. // 给请求头中加上account和token信息
  137. addTokenToRequest: function (request) {
  138. var token = $.cookie('token');
  139. var account = $.cookie('account');
  140. if (token == undefined) {
  141. var localStorageToken = localStorage.getItem('#token');
  142. if (localStorageToken != undefined) {
  143. token = localStorageToken;
  144. }
  145. }
  146. if (account == undefined) {
  147. var localStorageAccount = localStorage.getItem('#accountId');
  148. if (localStorageAccount != undefined) {
  149. account = localStorageAccount;
  150. }
  151. }
  152. request.setRequestHeader('account', account);
  153. request.setRequestHeader('token', token);
  154. },
  155. /**
  156. * 获取Token
  157. */
  158. getToken: function () {
  159. return localStorage.getItem('#token');
  160. },
  161. // 清空 Cookie
  162. clearCookie: function () {
  163. // eslint-disable-next-line
  164. var keys = document.cookie.match('/[^ =;]+(?=\=)/g');
  165. if (keys) {
  166. for (var i = keys.length; i--;) {
  167. // 清除当前域名路径的有限日期
  168. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
  169. // Domain Name域名 清除当前域名的
  170. document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
  171. // 清除一级域名下的或指定的
  172. document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString();
  173. }
  174. }
  175. },
  176. // 获取路由中的参数
  177. getRouteParam: function (name) {
  178. var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
  179. if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
  180. return '';
  181. },
  182. /**
  183. * 获取跳转的路径
  184. * @param {*} url
  185. */
  186. getRedirectUrl: function (url) {
  187. var href = window.location.href;
  188. if (href.indexOf('pcapp') >= 0) {
  189. return this.getHostPageBaseURL() + '/pcapp/' + url;
  190. } else {
  191. return this.getHostPageBaseURL() + '/' + url;
  192. }
  193. },
  194. clearLocalStorage: function () {
  195. // 清理localStorage时需要保留的参数列表
  196. var reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
  197. 'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL'];
  198. //存放的信息
  199. var reserveParamValues = [];
  200. //获取参数信息
  201. var len = reserveParams.length;
  202. for (let i = 0; i < len; i++) {
  203. var reserveParam = reserveParams[i];
  204. var reserveParamValue = '';
  205. if (localStorage.getItem(reserveParam) != undefined) {
  206. reserveParamValue = localStorage.getItem(reserveParam);
  207. }
  208. reserveParamValues.push(reserveParamValue);
  209. }
  210. //清理localStorage
  211. window.localStorage.clear();
  212. //还原参数信息
  213. for (let i = 0; i < len; i++) {
  214. localStorage.setItem(reserveParams[i], reserveParamValues[i]);
  215. }
  216. },
  217. };