common.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * common.js
  3. * @param {Object} val
  4. */
  5. var Prodog = {
  6. version: "3.4",
  7. // 动态
  8. setting: {
  9. // 是否显示注册按钮
  10. allowRegister: false
  11. }
  12. };
  13. Array.prototype.myIndexOf = function (val) {
  14. for (var i = 0; i < this.length; i++) {
  15. if (this[i] == val) return i;
  16. }
  17. return -1;
  18. };
  19. Array.prototype.myIndexOf2 = function (val) {
  20. for (var i = 0; i < this.length; i++) {
  21. if (this[i].name == val.name) return i;
  22. }
  23. return -1;
  24. };
  25. Array.prototype.myIndexOf3 = function (val) {
  26. for (var i = 0; i < this.length; i++) {
  27. if (JSON.stringify(this[i]) == JSON.stringify(val)) return i;
  28. }
  29. return -1;
  30. };
  31. Array.prototype.indexOfProperty = function (val, propertyName) {
  32. for (var i = 0; i < this.length; i++) {
  33. if (this[i][propertyName] == val) return i;
  34. }
  35. return -1;
  36. };
  37. Array.prototype.remove = function (val) {
  38. var index = this.myIndexOf(val);
  39. if (index > -1) {
  40. this.splice(index, 1);
  41. }
  42. };
  43. String.prototype.startsWith = function (str) {
  44. var reg = new RegExp("^" + str);
  45. return reg.test(this);
  46. }
  47. String.prototype.endWith = function (str) {
  48. var reg = new RegExp(str + "$");
  49. return reg.test(this);
  50. }
  51. Date.prototype.format = function (fmt) {
  52. var o = {
  53. "M+": this.getMonth() + 1, //月份
  54. "d+": this.getDate(), //日
  55. "h+": this.getHours(), //小时
  56. "m+": this.getMinutes(), //分
  57. "s+": this.getSeconds(), //秒
  58. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  59. "S": this.getMilliseconds() //毫秒
  60. };
  61. if (/(y+)/.test(fmt)) {
  62. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  63. }
  64. for (var k in o) {
  65. if (new RegExp("(" + k + ")").test(fmt)) {
  66. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  67. }
  68. }
  69. return fmt;
  70. }
  71. // 给请求头中加上account和token信息
  72. function addTokenToRequest(request) {
  73. // var token = Cookies.get('token');
  74. // var account = Cookies.get('account');
  75. // 修改在Android上Cookies无效
  76. var token = localStorage.getItem("token");
  77. var account = localStorage.getItem("account");
  78. console.log("token" + token);
  79. request.setRequestHeader("account", account);
  80. request.setRequestHeader("token", token);
  81. }
  82. // 时间格式转换
  83. function dateFormat(date) {
  84. var year = date.getFullYear();
  85. var m = date.getMonth() + 1;
  86. var d = date.getDate();
  87. if (m < 10) {
  88. m = "0" + m;
  89. }
  90. if (d < 10) {
  91. d = "0" + d;
  92. }
  93. return year + "-" + m + "-" + d;
  94. }
  95. /**
  96. * 获取ECharts饼图数据
  97. * @param {Object} data
  98. */
  99. function getEChartsPieOption(data, title) {
  100. var option = {
  101. title: {
  102. text: title,
  103. x: 'center',
  104. },
  105. legend: {
  106. orient: 'horizontal',
  107. x: 'left',
  108. data: data,
  109. top: '20'
  110. },
  111. series: [{
  112. name: '资产统计',
  113. type: 'pie',
  114. radius: '45%',
  115. center: ['50%', '60%'],
  116. data: data,
  117. itemStyle: {
  118. emphasis: {
  119. shadowBlur: 10,
  120. shadowOffsetX: 0,
  121. shadowColor: 'rgba(0, 0, 0, 0.5)'
  122. },
  123. normal: {
  124. label: {
  125. show: true,
  126. textStyle: {
  127. fontSize: '0.2em',
  128. fontWeight: 'bolder'
  129. },
  130. formatter: '{b}({c}) '
  131. },
  132. labelLine: {
  133. show: true,
  134. length: 12
  135. }
  136. }
  137. }
  138. }]
  139. };
  140. return option;
  141. }
  142. /**
  143. * 获取ECharts柱状图数据
  144. * @param {Object} data
  145. */
  146. function getEChartsBarOption(data, title) {
  147. var xData = new Array();
  148. var yData = new Array();
  149. data.forEach(function (item, index, arr) {
  150. xData.push(item.name);
  151. yData.push(item.value);
  152. });
  153. var option = {
  154. tooltip: {
  155. trigger: 'axis',
  156. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  157. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
  158. }
  159. },
  160. title: {
  161. text: title,
  162. x: 'center',
  163. },
  164. legend: {
  165. x: 'left',
  166. top: '20',
  167. data: ['数量'],
  168. },
  169. grid: {
  170. left: '3%',
  171. right: '4%',
  172. bottom: '3%',
  173. containLabel: true
  174. },
  175. xAxis: [{
  176. type: 'value'
  177. }],
  178. yAxis: [{
  179. type: 'category',
  180. axisTick: {
  181. show: false
  182. },
  183. data: xData
  184. }],
  185. series: [{
  186. name: '数量',
  187. type: 'bar',
  188. label: {
  189. normal: {
  190. show: true,
  191. position: 'inside'
  192. }
  193. },
  194. data: yData
  195. }]
  196. };
  197. return option;
  198. }
  199. // 未找到图片
  200. function imgNotFound(event) {
  201. var img = event;
  202. console.log("imgNotFound。。。");
  203. img.src = "../../img/null.jpg";
  204. img.onerror = null;
  205. }
  206. var Common = {};
  207. /**
  208. * 从URL里面获取UUID
  209. */
  210. Common.getUuidFromUrl = function (param) {
  211. var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");
  212. var r = window.location.search.substr(1).match(reg);
  213. if (r != null) {
  214. return unescape(r[2]);
  215. } else {
  216. return null;
  217. }
  218. }
  219. /**
  220. * 从URL里面获取UUID
  221. */
  222. Common.getParamFromUrl = function (param) {
  223. var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");
  224. var r = window.location.search.substr(1).match(reg);
  225. if (r != null) {
  226. return unescape(r[2]);
  227. } else {
  228. return null;
  229. }
  230. }
  231. /**
  232. * 获取主机地址
  233. */
  234. Common.getRootPath = function () {
  235. //获取当前网址,如: http://localhost:8083/myproj/view/my.jsp
  236. var curWwwPath = window.document.location.href;
  237. //获取主机地址之后的目录,如: myproj/view/my.jsp
  238. var pathName = window.document.location.pathname;
  239. var pos = curWwwPath.indexOf(pathName);
  240. //获取主机地址,如: http://localhost:8083
  241. var localhostPaht = curWwwPath.substring(0, pos);
  242. return localhostPaht;
  243. }
  244. /**
  245. * 获取IP地址
  246. */
  247. Common.getIp = function () {
  248. return localStorage.getItem("ipServer");
  249. }
  250. // 文件服务器
  251. Common.getFileServerUrl = function (api) {
  252. return this.getRootPath() + api;
  253. },
  254. /**
  255. * 获取API的地址
  256. * @param {Object} api
  257. */
  258. Common.getApi = function (api) {
  259. // return 'http://192.168.1.107:10026' + api;
  260. return localStorage.getItem("ipServer") + api;
  261. }
  262. Common.getUrlApi = function (api) {
  263. return Common.getIp() + api;
  264. }
  265. Common.getApiURL = function (api) {
  266. if (api.indexOf("/") == 0) {
  267. return Common.getIp() + "/api" + api;
  268. } else {
  269. return Common.getIp() + "/api/" + api
  270. }
  271. }
  272. /**
  273. * 获取URL中的参数
  274. * 适应以下两种模式,来获取url参数值:
  275. * /User/vip_card_manager/useless/219/id/18
  276. * /User/vip_card_manager?useless=219&id=18
  277. * @param {Object} name
  278. */
  279. Common.getQueryString = function (name) {
  280. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  281. var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
  282. var r = window.location.search.substr(1).match(reg);
  283. var q = window.location.pathname.substr(1).match(reg_rewrite);
  284. if (r != null) {
  285. return unescape(r[2]);
  286. } else if (q != null) {
  287. return unescape(q[2]);
  288. } else {
  289. return null;
  290. }
  291. }
  292. // 获取图片相对路径
  293. Common.getimgUrl = function (imageUrl) {
  294. return imageUrl;
  295. }
  296. // 获取图片路径
  297. Common.getImageSrc = function (className, imageName) {
  298. var accountId = localStorage.getItem("account");
  299. //return "http://prodog.leanwo.com/Files/Images/" + className + "/" + imageName;
  300. return Common.getRootPath() + "/Files/" + accountId + "/Images/" + className + "/thumbnail/" + imageName; //本地测试用
  301. //return Common.getRootPath() + "/Files/Images/" + className + "/" + imageName; //本地测试用
  302. }
  303. // 获取大图片路径
  304. Common.getBigImageSrc = function (className, imageName) {
  305. var accountId = localStorage.getItem("account");
  306. //return "http://prodog.leanwo.com/Files/Images/" + className + "/" + imageName;
  307. //return Common.getRootPath() + "/Files/Images/" + className + "/thumbnail/" + imageName; //本地测试用
  308. return Common.getRootPath() + "/Files/" + accountId + "/Images/" + className + "/" + imageName; //本地测试用
  309. }
  310. Common.addTokenToRequest = addTokenToRequest;
  311. // 异常处理
  312. Common.processException = function (XMLHttpRequest) {
  313. var _self = this;
  314. console.log(XMLHttpRequest);
  315. if (XMLHttpRequest.status == 400) {
  316. // 400 Bad Request
  317. mui.toast("400:" + XMLHttpRequest.responseText);
  318. } else if (XMLHttpRequest.status == 401) {
  319. var currentUrl = window.location;
  320. // 系统未登录
  321. if (window.location.href.indexOf('redirectUrl=') < 0) {
  322. window.location = Common.getRedirectUrl("index.html?redirectUrl=" + currentUrl);
  323. }
  324. } else if (XMLHttpRequest.status == 500) {
  325. // 500 Internal Server Error
  326. mui.toast("500:" + XMLHttpRequest.responseText);
  327. if (XMLHttpRequest.responseText.indexOf("登录超时") > 0) {
  328. // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面
  329. setTimeout(function () {
  330. window.location = Common.getRedirectUrl("index.html");
  331. }, 2 * 1000);
  332. }
  333. } else {
  334. mui.toast("服务器异常:" + XMLHttpRequest.responseText);
  335. }
  336. }
  337. // 异常处理
  338. Common.processExceptionVant = function (XMLHttpRequest) {
  339. var _self = this;
  340. console.log(XMLHttpRequest);
  341. if (XMLHttpRequest.status == 400) {
  342. vant.Dialog.alert({
  343. title: 400,
  344. message: XMLHttpRequest.responseText,
  345. theme: 'round-button',
  346. });
  347. } else if (XMLHttpRequest.status == 401) {
  348. var currentUrl = window.location;
  349. // 系统未登录
  350. if (window.location.href.indexOf('redirectUrl=') < 0) {
  351. window.location = Common.getRedirectUrl("index.html?redirectUrl=" + currentUrl);
  352. }
  353. } else if (XMLHttpRequest.status == 500) {
  354. // 500 Internal Server Error
  355. vant.Dialog.alert({
  356. title: 500,
  357. message: XMLHttpRequest.responseText,
  358. theme: 'round-button',
  359. });
  360. if (XMLHttpRequest.responseText.indexOf("登录超时") > 0) {
  361. // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面
  362. setTimeout(function () {
  363. window.location = Common.getRedirectUrl("index.html");
  364. }, 2 * 1000);
  365. }
  366. } else {
  367. vant.Dialog.alert({
  368. title: "服务器异常:",
  369. message: XMLHttpRequest.responseText,
  370. theme: 'round-button',
  371. });
  372. }
  373. }
  374. /**
  375. * 获取跳转的路径
  376. * @param {*} url
  377. */
  378. Common.getRedirectUrl = function (url) {
  379. const href = window.location.href;
  380. if (href.indexOf('wmsapp') >= 0 || href.indexOf('static-eam-app') >= 0) {
  381. return this.getRootPath() + '/wmsapp/' + url;
  382. } else {
  383. return this.getRootPath() + url;
  384. }
  385. }
  386. var IS_TESTING_CONNECTION = false;
  387. // 测试网络连接
  388. function testConnection() {
  389. if (IS_TESTING_CONNECTION == true) {
  390. return;
  391. }
  392. IS_TESTING_CONNECTION = true;
  393. $.ajax({
  394. type: "get",
  395. url: Common.getApi("/authApi/LoginResource/heartBeat"),
  396. data: {
  397. currentTime: new Date().getTime()
  398. },
  399. success: function (data) {
  400. IS_TESTING_CONNECTION = false;
  401. var delay = new Date().getTime() - data;
  402. $("[name=testConnection]").remove();
  403. $("body").append("<div onclick='showDelay(" + delay +
  404. ")' name='testConnection' style='width:100%;height:6px;background-color:#2AC845;position:fixed;bottom:0px;line-height:6px;font-size:2px;'></div>"
  405. )
  406. },
  407. error: function () {
  408. IS_TESTING_CONNECTION = false;
  409. $("[name=testConnection]").remove();
  410. $("body").append("<div onclick='showDelay(" + -1 +
  411. ")' name='testConnection' style='width:100%;height:6px;background-color:#FF0000;position:fixed;bottom:0px;line-height:6px;font-size:2px;'></div>"
  412. )
  413. }
  414. });
  415. }
  416. // 测试网络是否连接正常
  417. // (function () {
  418. // testConnection();
  419. // setInterval(testConnection, 5000);
  420. // })();
  421. function showDelay(delay) {
  422. if (delay < 0) {
  423. mui.toast("网络连接失败");
  424. } else {
  425. mui.toast("当前延时: " + delay + "ms");
  426. }
  427. }
  428. // ajax的cache默认值为true:使用缓存,这个时候读取的数据是缓存中的数据而不是最新的数据。
  429. // eg:界面返回的时候,ajax使用了缓存中的数据,并没有向服务器发起请求。造成界面数据不刷新。
  430. // 设置Ajax请求的默认值,不允许缓存
  431. $.ajaxSetup({
  432. cache: false
  433. });