Common.js 7.6 KB

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