Common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. var accountId = localStorage.getItem('#accountId');
  116. if (imageName == null) {
  117. return null;
  118. }
  119. if (imageName != null && imageName[0] == '/') {
  120. return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + imageName;
  121. } else {
  122. return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/' + imageName;
  123. }
  124. },
  125. // 获取略缩图图片路径
  126. getThumbnailImageSrc: function (className, imageName) {
  127. var accountId = localStorage.getItem('#accountId');
  128. if (imageName == null) {
  129. return null;
  130. }
  131. if (imageName != null && imageName[0] == '/') {
  132. return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/thumbnail' + imageName;
  133. } else {
  134. return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/thumbnail/' + imageName;
  135. }
  136. },
  137. /**
  138. * 获取附件的路径
  139. * @param {[type]} className [description]
  140. * @param {[type]} imageName [description]
  141. * @return {[type]} [description]
  142. */
  143. getAttachmentsSrc: function (className, imageName) {
  144. var accountId = localStorage.getItem('#accountId');
  145. return this.getFileServerUrl() + 'Files/' + accountId + '/Attachments/' + className + '/' + imageName;
  146. },
  147. // 获取图片路径
  148. getVideoSrc: function (className, imageName) {
  149. var accountId = localStorage.getItem('#accountId');
  150. if (imageName == undefined || imageName == '') {
  151. return this.getHostPageBaseURL() + 'static/image/noImage.jpg';
  152. }
  153. return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + imageName;
  154. },
  155. //获取资源路径 type: 图片image,视频video,文件file,
  156. getResourceUrl: function (type, className, resourceName) {
  157. var accountId = localStorage.getItem('#accountId');
  158. if (resourceName == undefined || className == undefined || type == undefined || resourceName == '' || className == '' || type == '') {
  159. return;
  160. }
  161. if (type == 'image') {
  162. return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/' + resourceName;
  163. }
  164. if (type == 'video') {
  165. return this.getFileServerUrl() + 'Files/' + accountId + '/Video/' + className + '/' + resourceName;
  166. }
  167. if (type == 'file') {
  168. return this.getFileServerUrl() + 'Files/' + accountId + '/Files/' + className + '/' + resourceName;
  169. }
  170. },
  171. getApsURL: function (apiName) {
  172. var apsBaseUrl = localStorage.getItem('apsBaseUrl');
  173. if (apsBaseUrl == undefined) {
  174. Notify.error('错误', '系统参数"apsBaseUrl"未设置,请联系系统管理员设置参数', false);
  175. return;
  176. }
  177. return apsBaseUrl + apiName;
  178. },
  179. getActivitiURL: function (apiName) {
  180. var apsBaseUrl = localStorage.getItem('activitiUrl');
  181. if (apsBaseUrl == undefined) {
  182. Notify.error('错误', '系统参数"activitiUrl"未设置,请联系系统管理员设置参数', false);
  183. return;
  184. }
  185. return apsBaseUrl + apiName;
  186. },
  187. getSchedulingURL: function (apiName) {
  188. var _self = this;
  189. var apsBaseUrl = localStorage.getItem('schedulingUrl');
  190. if (apsBaseUrl == undefined) {
  191. _self.loadSystemParam('schedulingUrl', function () {
  192. apsBaseUrl = localStorage.getItem('schedulingUrl');
  193. if (apsBaseUrl == undefined) {
  194. Notify.error('错误', '系统参数"schedulingUrl"未设置,请联系系统管理员设置参数', false);
  195. return;
  196. }
  197. });
  198. }
  199. return apsBaseUrl + apiName;
  200. },
  201. getCameraURL: function (apiName) {
  202. var apsBaseUrl = localStorage.getItem('cameraBaseURL');
  203. if (apsBaseUrl == undefined) {
  204. Notify.error('错误', '系统参数"cameraBaseURL"未设置,请联系系统管理员设置参数', false);
  205. return;
  206. }
  207. return apsBaseUrl + apiName;
  208. },
  209. //设置路径到localStorage
  210. setHref: function () {
  211. var href = window.location.href;
  212. if (href.indexOf('http') == 0) {
  213. href = href.substring(0, href.indexOf('#') + 2);
  214. localStorage.setItem('href', href);
  215. } else {
  216. var hostPageBaseURL = localStorage.getItem('hostPageBaseURL');
  217. if (hostPageBaseURL == undefined) {
  218. href = href.substring(0, href.indexOf('#') + 2);
  219. localStorage.setItem('href', href);
  220. }
  221. }
  222. },
  223. //加载系统参数到localStorage
  224. loadSystemParam: function (systemParamName, success) {
  225. var that = this;
  226. $.ajax({
  227. url: that.getApiURL('SystemParamResource/loadSystemParam'),
  228. type: 'GET',
  229. dataType: 'text',
  230. data: {
  231. 'systemParamName': systemParamName,
  232. },
  233. beforeSend: function (request) {
  234. that.addTokenToRequest(request);
  235. },
  236. success: function (data) {
  237. localStorage.setItem(systemParamName, data);
  238. success();
  239. },
  240. error: function (XMLHttpRequest, textStatus, errorThrown) {
  241. that.processException(XMLHttpRequest, textStatus, errorThrown);
  242. },
  243. });
  244. },
  245. // 给请求头中加上account和token信息
  246. addTokenToRequest: function (request) {
  247. // request.setRequestHeader('#accountId', localStorage.getItem('#accountId'));
  248. request.setRequestHeader('token', localStorage.getItem('#token'));
  249. },
  250. /**
  251. * 获取Token
  252. */
  253. getToken: function () {
  254. return $.cookie('token');
  255. },
  256. // 获取新建对象的Id
  257. getNewRecordId: function () {
  258. window.CRUDId++;
  259. return window.CRUDId;
  260. },
  261. // 清空 Cookie
  262. clearCookie: function () {
  263. // eslint-disable-next-line
  264. var keys = document.cookie.match('/[^ =;]+(?=\=)/g');
  265. if (keys) {
  266. for (var i = keys.length; i--;) {
  267. // 清除当前域名路径的有限日期
  268. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
  269. // Domain Name域名 清除当前域名的
  270. document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
  271. // 清除一级域名下的或指定的
  272. document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString();
  273. }
  274. }
  275. },
  276. clearLocalStorage: function () {
  277. // 清理localStorage时需要保留的参数列表
  278. var reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
  279. 'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL'];
  280. //存放的信息
  281. var reserveParamValues = [];
  282. //获取参数信息
  283. var len = reserveParams.length;
  284. for (let i = 0; i < len; i++) {
  285. var reserveParam = reserveParams[i];
  286. var reserveParamValue = '';
  287. if (localStorage.getItem(reserveParam) != undefined) {
  288. reserveParamValue = localStorage.getItem(reserveParam);
  289. }
  290. reserveParamValues.push(reserveParamValue);
  291. }
  292. //清理localStorage
  293. window.localStorage.clear();
  294. //还原参数信息
  295. for (let i = 0; i < len; i++) {
  296. localStorage.setItem(reserveParams[i], reserveParamValues[i]);
  297. }
  298. },
  299. showDialog: function (title, content, type) {
  300. if (type == 'success') {
  301. Notify.success(title, content, 4000);
  302. }
  303. else if (type == 'error') {
  304. Notify.error(title, content, -1);
  305. }
  306. else if (type == 'info') {
  307. Notify.info(title, content, 2000);
  308. }
  309. else if (type == 'notice') {
  310. Notify.notice(title, content, 2000);
  311. }
  312. },
  313. // 获取路由中的参数
  314. getRouteParam: function (name) {
  315. var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
  316. if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
  317. return '';
  318. },
  319. /**
  320. * 获取跳转的路径
  321. * @param {*} url
  322. */
  323. getRedirectUrl: function (url) {
  324. var href = window.location.href;
  325. if (href.indexOf('pcapp') >= 0) {
  326. return this.getRootPath() + '/pcapp/' + url;
  327. } else {
  328. return this.getRootPath() + '/' + url;
  329. }
  330. },
  331. };