Common.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. if(typeof REMOTE_DEV_PORT !== 'undefined' && REMOTE_DEV_PORT != null){
  56. return this.getRootPath() + '/proxy/' + REMOTE_DEV_PORT;
  57. }else{
  58. return this.getRootPath();
  59. }
  60. },
  61. // 获取API的地址
  62. getApiURL: function (apiName) {
  63. if(apiName.startsWith('/')){
  64. return this.getHostPageBaseURL() + '/api' + apiName;
  65. }else{
  66. return this.getHostPageBaseURL() + '/api/' + apiName;
  67. }
  68. },
  69. /**
  70. * 获取API的地址 直接拼接
  71. * @param {*} apiName
  72. * @returns
  73. */
  74. getApiUrl2: function (apiName) {
  75. if(apiName.startsWith('/')){
  76. return this.getHostPageBaseURL() + apiName;
  77. }else{
  78. return this.getHostPageBaseURL() + '/' + apiName;
  79. }
  80. },
  81. // 获取图片路径
  82. getImageUrl: function (imageName) {
  83. if (imageName == null || imageName == '') {
  84. return this.getHostPageBaseURL() + 'notFound.png';
  85. } else {
  86. return this.getHostPageBaseURL() + imageName;
  87. }
  88. },
  89. // 获取图片路径
  90. getImageSrc: function (className, imageName) {
  91. if (imageName == null) {
  92. return null;
  93. }
  94. return this.getHostPageBaseURL() + '/api/file/imageDownload?className=' + className + '&fileName=' + imageName;
  95. },
  96. // 获取略缩图图片路径
  97. getThumbnailImageSrc: function (className, imageName) {
  98. if (imageName == null) {
  99. return null;
  100. }
  101. return this.getHostPageBaseURL() + '/api/file/thumbnailImageDownload?className=' + className + '&fileName=' + imageName;
  102. },
  103. /**
  104. * 获取附件的路径
  105. * @param {[type]} className [description]
  106. * @param {[type]} imageName [description]
  107. * @return {[type]} [description]
  108. */
  109. getAttachmentsSrc: function (className, imageName) {
  110. var accountId = localStorage.getItem('#accountId');
  111. return this.getHostPageBaseURL() + 'Files/' + accountId + '/Attachments/' + className + '/' + imageName;
  112. },
  113. // 获取图片路径
  114. getVideoSrc: function (className, imageName) {
  115. var accountId = localStorage.getItem('#accountId');
  116. if (imageName == undefined || imageName == '') {
  117. return this.getHostPageBaseURL() + 'static/image/noImage.jpg';
  118. }
  119. return this.getHostPageBaseURL() + 'Files/' + accountId + '/Video/' + className + '/' + imageName;
  120. },
  121. //获取资源路径 type: 图片image,视频video,文件file,
  122. getResourceUrl: function (type, className, resourceName) {
  123. var accountId = localStorage.getItem('#accountId');
  124. if (resourceName == undefined || className == undefined || type == undefined || resourceName == '' || className == '' || type == '') {
  125. return;
  126. }
  127. if (type == 'image') {
  128. return this.getHostPageBaseURL() + 'Files/' + accountId + '/Images/' + className + '/' + resourceName;
  129. }
  130. if (type == 'video') {
  131. return this.getHostPageBaseURL() + 'Files/' + accountId + '/Video/' + className + '/' + resourceName;
  132. }
  133. if (type == 'file') {
  134. return this.getHostPageBaseURL() + 'Files/' + accountId + '/Files/' + className + '/' + resourceName;
  135. }
  136. },
  137. //设置路径到localStorage
  138. setHref: function () {
  139. var href = window.location.href;
  140. if (href.indexOf('http') == 0) {
  141. href = href.substring(0, href.indexOf('#') + 2);
  142. localStorage.setItem('href', href);
  143. } else {
  144. var hostPageBaseURL = localStorage.getItem('hostPageBaseURL');
  145. if (hostPageBaseURL == undefined) {
  146. href = href.substring(0, href.indexOf('#') + 2);
  147. localStorage.setItem('href', href);
  148. }
  149. }
  150. },
  151. //加载系统参数到localStorage
  152. loadSystemParam: function (systemParamName, success) {
  153. var that = this;
  154. $.ajax({
  155. url: that.getApiURL('SystemParamResource/loadSystemParam'),
  156. type: 'GET',
  157. dataType: 'text',
  158. data: {
  159. 'systemParamName': systemParamName,
  160. },
  161. beforeSend: function (request) {
  162. that.addTokenToRequest(request);
  163. },
  164. success: function (data) {
  165. localStorage.setItem(systemParamName, data);
  166. success();
  167. },
  168. error: function (XMLHttpRequest, textStatus, errorThrown) {
  169. that.processException(XMLHttpRequest, textStatus, errorThrown);
  170. },
  171. });
  172. },
  173. // 给请求头中加上account和token信息
  174. addTokenToRequest: function (request) {
  175. // request.setRequestHeader('#accountId', localStorage.getItem('#accountId'));
  176. request.setRequestHeader('token', localStorage.getItem('#token'));
  177. },
  178. /**
  179. * 获取Token
  180. */
  181. getToken: function () {
  182. return localStorage.getItem('#token');
  183. },
  184. // 获取新建对象的Id
  185. getNewRecordId: function () {
  186. window.CRUDId++;
  187. return window.CRUDId;
  188. },
  189. // 清空 Cookie
  190. clearCookie: function () {
  191. // eslint-disable-next-line
  192. var keys = document.cookie.match('/[^ =;]+(?=\=)/g');
  193. if (keys) {
  194. for (var i = keys.length; i--;) {
  195. // 清除当前域名路径的有限日期
  196. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
  197. // Domain Name域名 清除当前域名的
  198. document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
  199. // 清除一级域名下的或指定的
  200. document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString();
  201. }
  202. }
  203. },
  204. // 清空 Cookie
  205. clearAppCookie: function (loginInfo) {
  206. document.cookie.split(';').forEach(function (c) {
  207. document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
  208. });
  209. $.removeCookie('token', { path: '/' });
  210. $.removeCookie('token', { path: '/app' });
  211. $.cookie('token', loginInfo.token, {
  212. expires: 7,
  213. path: '/',
  214. secure: true,
  215. sameSite: 'Strict',
  216. });
  217. },
  218. clearLocalStorage: function () {
  219. // 清理localStorage时需要保留的参数列表
  220. var reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
  221. 'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL','#rememberPassword','#userName','#password','#languageSelected'];
  222. //存放的信息
  223. var reserveParamValues = [];
  224. //获取参数信息
  225. var len = reserveParams.length;
  226. for (let i = 0; i < len; i++) {
  227. var reserveParam = reserveParams[i];
  228. var reserveParamValue = '';
  229. if (localStorage.getItem(reserveParam) != undefined) {
  230. reserveParamValue = localStorage.getItem(reserveParam);
  231. }
  232. reserveParamValues.push(reserveParamValue);
  233. }
  234. //清理localStorage
  235. window.localStorage.clear();
  236. //还原参数信息
  237. for (let i = 0; i < len; i++) {
  238. localStorage.setItem(reserveParams[i], reserveParamValues[i]);
  239. }
  240. },
  241. showDialog: function (title, content, type) {
  242. if (type == 'success') {
  243. Notify.success(title, content, 4000);
  244. }
  245. else if (type == 'error') {
  246. Notify.error(title, content, -1);
  247. }
  248. else if (type == 'info') {
  249. Notify.info(title, content, 2000);
  250. }
  251. else if (type == 'notice') {
  252. Notify.notice(title, content, 2000);
  253. }
  254. },
  255. // 获取路由中的参数
  256. getRouteParam: function (name) {
  257. var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
  258. if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
  259. return '';
  260. },
  261. /**
  262. * 获取跳转的路径
  263. * @param {*} url
  264. */
  265. getRedirectUrl: function (url) {
  266. var href = window.location.href;
  267. if (href.indexOf('pcapp') >= 0) {
  268. return this.getHostPageBaseURL() + '/pcapp/' + url;
  269. } else {
  270. return this.getHostPageBaseURL() + '/' + url;
  271. }
  272. },
  273. };