You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

549 lines
14 KiB

  1. (function(wd) {
  2. /**
  3. * @description js工具类,内置常用方法以及基本配置
  4. * */
  5. $$ = {
  6. base_api: "http://180.168.4.58:8081/", //接口地址
  7. v: "2.2", //版本号
  8. version: "version=2.2",
  9. logCount: 0, //打印的次数
  10. infoApiError: "服务器开小差了,请联系技术部相关人员",
  11. infoNotLogin: "您还没有登录,点击确定前往登录页面",
  12. /**
  13. * @param {Function} function
  14. * @param {Number} time
  15. * @description 延迟函数
  16. * */
  17. delay: (function() {
  18. var timer = 0;
  19. return function(callback, time) {
  20. clearTimeout(timer);
  21. timer = setTimeout(callback, time);
  22. };
  23. })(),
  24. /**
  25. * @description 得到uuid
  26. */
  27. getUUID: function() {
  28. var d = new Date().getTime();
  29. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  30. var r = (d + Math.random() * 16) % 16 | 0;
  31. d = Math.floor(d / 16);
  32. return(c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  33. });
  34. return uuid;
  35. },
  36. /**
  37. * @param {Number}
  38. * @description 得到随机字符串
  39. * */
  40. getRdStr: function(len) {
  41. len = len || 1;
  42. var rdmString = "";
  43. for(; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
  44. return rdmString.substr(0, len);
  45. },
  46. /**description 自定义打印日志*/
  47. ZZLog: function() {
  48. var isLog = true; //是否打印
  49. var isSign = false; //是否签名
  50. if(isLog) {
  51. this.logCount++;
  52. console.group("第" + this.logCount + "次打印");
  53. if(isSign) {
  54. //如果要加前缀
  55. var args = Array.prototype.slice.call(arguments);
  56. args.unshift('[ZZCX:DeBug:Log]');
  57. console.log.apply(console, args);
  58. } else {
  59. console.log.apply(console, arguments);
  60. }
  61. }
  62. console.groupEnd();
  63. //ES6
  64. /*
  65. * // 打印日志
  66. //function ZZLog(...type) {
  67. // var isLog = true; //是否打印
  68. // if(isLog) {
  69. // type.forEach(v => {
  70. // if(typeof(v) == 'object') {
  71. // console.log(v);
  72. // } else {
  73. // console.log('%c' + v, 'color:#666699');
  74. // }
  75. // })
  76. // }
  77. //}
  78. *
  79. */
  80. },
  81. /**
  82. * @param {Object} arrObj []
  83. * @description 自定义打印table arrObj必须是数组且成员是object
  84. */
  85. ZZLogTable: function(arrObj) {
  86. this.logCount++;
  87. console.group("第" + this.logCount + "次打印");
  88. console.table(arrObj);
  89. console.groupEnd();
  90. },
  91. /**
  92. * @param {String} par
  93. * @description 从URL上获得参数
  94. */
  95. getPar: function(par) {
  96. //获取当前URL
  97. var local_url = document.location.href;
  98. local_url = decodeURI(local_url);
  99. //获取要取得的get参数位置
  100. var get = local_url.indexOf(par + "=");
  101. if(get == -1) {
  102. return "";
  103. }
  104. //截取字符串
  105. var get_par = local_url.slice(par.length + get + 1);
  106. //判断截取后的字符串是否还有其他get参数
  107. var nextPar = get_par.indexOf("&");
  108. if(nextPar != -1) {
  109. get_par = get_par.slice(0, nextPar);
  110. }
  111. return get_par;
  112. },
  113. /**
  114. * @param {String} name
  115. * @param {String} value
  116. * @param {String} time
  117. * @description 设置cookie eg:time='d30|s30|h24'
  118. * */
  119. setCookie: function(name, value, time) {
  120. var strsec = getsec(time);
  121. var exp = new Date();
  122. exp.setTime(exp.getTime() + strsec * 1);
  123. document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
  124. function getsec(str) {
  125. var str1 = str.substring(1, str.length) * 1;
  126. var str2 = str.substring(0, 1);
  127. if(str2 == "s") {
  128. return str1 * 1000;
  129. } else if(str2 == "h") {
  130. return str1 * 60 * 60 * 1000;
  131. } else if(str2 == "d") {
  132. return str1 * 24 * 60 * 60 * 1000;
  133. }
  134. }
  135. },
  136. /**
  137. * @param {String} name
  138. * @description 删除指定cookie
  139. * */
  140. delCookie: function(name) {
  141. var exp = new Date();
  142. exp.setTime(exp.getTime() - 1);
  143. var cval = this.getCookie(name);
  144. if(cval != null)
  145. document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
  146. },
  147. /**
  148. * @param {String} name
  149. * @description 得到指定cookie
  150. * */
  151. getCookie: function(name) {
  152. var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  153. if(arr = document.cookie.match(reg))
  154. return unescape(arr[2]);
  155. else
  156. return null;
  157. },
  158. /**
  159. * @param {String} objName
  160. * @param {String} objValue
  161. * @description 设置字符串类型的本地缓存
  162. * */
  163. setStorage: function(objName, objValue) {
  164. var sto = window.localStorage;
  165. if(sto)
  166. sto.setItem(objName, objValue);
  167. },
  168. /**
  169. * @param {String} objName
  170. * @description 读取字符串类型的本地缓存
  171. * */
  172. getStorage: function(objName) {
  173. var ret = '';
  174. var sto = window.localStorage;
  175. if(sto)
  176. ret = sto.getItem(objName);
  177. return ret;
  178. },
  179. /**
  180. * @param {String} objName
  181. * @description 清除本地缓存,如没指定名称则为清空所有缓存
  182. * */
  183. clearStorage: function(objName) {
  184. var sto = window.localStorage;
  185. if(sto) {
  186. if(objName)
  187. sto.removeItem(objName);
  188. else
  189. sto.clear();
  190. }
  191. },
  192. /**
  193. * @param {String} objName
  194. * @param {Object} json
  195. * @description 设置Json类型的本地缓存
  196. * */
  197. setStorJson: function(objName, json) {
  198. if(json) setStorage(objName, JSON.stringify(json));
  199. },
  200. /**
  201. * @param {String} objName
  202. * @description 读取Json类型的本地缓存
  203. * */
  204. getStorJson: function(objName) {
  205. var ret = null;
  206. var str = getStorage(objName);
  207. if(str)
  208. ret = JSON.parse(str);
  209. return ret;
  210. },
  211. /**
  212. * @param {Date} day
  213. * @description js判断一个日期是星期几 传入 年/月/日
  214. * */
  215. getWeekDay: function(day) {
  216. var a = new Array("日", "一", "二", "三", "四", "五", "六");
  217. var week = new Date(day).getDay();
  218. var str = "星期" + a[week];
  219. return str;
  220. },
  221. /**
  222. * @param {Date} date
  223. * @description 获取前一天日期 传入指定时间
  224. * */
  225. before: function(date) {
  226. var d = date;
  227. d = new Date(d);
  228. d = +d - 10006060 / 3 * 24;
  229. d = new Date(d);
  230. //格式化
  231. var year = ";" + d.getFullYear() + ";";
  232. var month = ";" + (d.getMonth() + 0 + 1) + ";";
  233. var day = ";" + d.getDate() + ";";
  234. if(year.length < 4) {
  235. year = "0" + year;
  236. }
  237. if(month.length < 4) {
  238. month = "0" + month;
  239. }
  240. if(day.length < 4) {
  241. day = "0" + day;
  242. }
  243. var datestr = year + "-" + month + "-" + day;
  244. datestr = datestr.replace(/;/g, "");
  245. return datestr;
  246. },
  247. /**
  248. * @param {Number} addDayCount
  249. * @param {Date} curDate
  250. * @description 获取指定日期的前后几天
  251. * */
  252. getDateByDay: function(addDayCount, curDate) {
  253. var d = new Date(curDate);
  254. d.setDate(d.getDate() + addDayCount); //获取AddDayCount天后的日期
  255. //格式化
  256. var year = ";" + d.getFullYear() + ";";
  257. var month = ";" + (d.getMonth() + 0 + 1) + ";";
  258. var day = ";" + d.getDate() + ";";
  259. if(year.length < 4) {
  260. year = "0" + year;
  261. }
  262. if(month.length < 4) {
  263. month = "0" + month;
  264. }
  265. if(day.length < 4) {
  266. day = "0" + day;
  267. }
  268. var datestr = year + "-" + month + "-" + day;
  269. datestr = datestr.replace(/;/g, "");
  270. return datestr;
  271. },
  272. /**
  273. * @param {Date} date
  274. * @description 获取后一天日期 传入指定时间
  275. * */
  276. after: function(date) {
  277. var d = date;
  278. d = new Date(d);
  279. d = +d + 10006060 / 3 * 24;
  280. d = new Date(d);
  281. //格式化
  282. var year = ";" + d.getFullYear() + ";";
  283. var month = ";" + (d.getMonth() + 0 + 1) + ";";
  284. var day = ";" + d.getDate() + ";";
  285. if(year.length < 4) {
  286. year = "0" + year;
  287. }
  288. if(month.length < 4) {
  289. month = "0" + month;
  290. }
  291. if(day.length < 4) {
  292. day = "0" + day;
  293. }
  294. var datestr = year + "-" + month + "-" + day;
  295. datestr = datestr.replace(/;/g, "");
  296. return datestr;
  297. },
  298. /**
  299. * @description 解决冒泡
  300. * */
  301. solveBubbing: function() {
  302. var e = getEvent();
  303. if(window.event) {
  304. e.cancelBubble = true; //阻止冒泡
  305. } else if(e.preventDefault) {
  306. e.stopPropagation(); //阻止冒泡
  307. }
  308. //得到事件
  309. function getEvent() {
  310. if(window.event) {
  311. return window.event;
  312. }
  313. func = getEvent.caller;
  314. while(func != null) {
  315. var arg0 = func.arguments[0];
  316. if(arg0) {
  317. if((arg0.constructor == Event || arg0.constructor == MouseEvent ||
  318. arg0.constructor == KeyboardEvent) ||
  319. (typeof(arg0) == "object" && arg0.preventDefault &&
  320. arg0.stopPropagation)) {
  321. return arg0;
  322. }
  323. }
  324. func = func.caller;
  325. }
  326. return null;
  327. }
  328. },
  329. /**
  330. * @description 得到浏览器
  331. * */
  332. myBrowser: function() {
  333. var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  334. var isOpera = userAgent.indexOf("Opera") > -1;
  335. //判断是否Opera浏览器
  336. if(isOpera) {
  337. return "Opera"
  338. };
  339. //判断是否Firefox浏览器
  340. if(userAgent.indexOf("Firefox") > -1) {
  341. return "Firefox";
  342. }
  343. //判断是否Chrome浏览器
  344. if(userAgent.indexOf("Chrome") > -1) {
  345. return "Chrome";
  346. }
  347. //判断是否Safari浏览器
  348. if(userAgent.indexOf("Safari") > -1) {
  349. return "Safari";
  350. }
  351. if(userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
  352. return "IE";
  353. }; //判断是否IE浏览器
  354. },
  355. /**
  356. * @param {Number} nTypeFlag
  357. * @description 得到时间组合
  358. * */
  359. getDateTime: function(nTypeFlag) {
  360. var tNowTime = new Date();
  361. var myYear = ';' + tNowTime.getFullYear() + ';';
  362. var myMonth = ';' + (tNowTime.getMonth() + 1 - 0) + ';';
  363. var myDay = ';' + tNowTime.getDate() + ';';
  364. var myHour = ';' + tNowTime.getHours() + ';';
  365. var myMinu = ';' + tNowTime.getMinutes() + ';';
  366. var mySecond = ';' + tNowTime.getSeconds() + ';';
  367. if(myMonth.length < 4) myMonth = '0' + myMonth;
  368. if(myDay.length < 4) myDay = '0' + myDay;
  369. if(myHour.length < 4) myHour = '0' + myHour;
  370. if(myMinu.length < 4) myMinu = '0' + myMinu;
  371. if(mySecond.length < 4) mySecond = '0' + mySecond;
  372. var cNewTimeStr;
  373. //alert(tNowTime);
  374. switch(nTypeFlag + 1 - 1) {
  375. case 0:
  376. cNewTimeStr = myYear + '-' + myMonth + '-' + myDay;
  377. break;
  378. case 1:
  379. cNewTimeStr = myYear + myMonth + myDay;
  380. break;
  381. case 2:
  382. cNewTimeStr = myHour + ':' + myMinu + ':' + mySecond;
  383. break;
  384. case 3:
  385. cNewTimeStr = myHour + myMinu + mySecond;
  386. break;
  387. case 4:
  388. cNewTimeStr = myYear + myMonth + myDay + myHour + myMinu + mySecond;
  389. break;
  390. case 5:
  391. cNewTimeStr = myYear + '年' + myMonth + '月' + myDay + '日';
  392. break;
  393. case 6:
  394. cNewTimeStr = myYear;
  395. break;
  396. case 7:
  397. cNewTimeStr = myYear + '-' + myMonth;
  398. break;
  399. case 8: //得到上一个月的今天
  400. var date = getDateTime(0);
  401. var arr = date.split('-');
  402. var year = arr[0]; //获取当前日期的年份
  403. var month = arr[1]; //获取当前日期的月份
  404. var day = arr[2]; //获取当前日期的日
  405. var days = new Date(year, month, 0);
  406. days = days.getDate(); //获取当前日期中月的天数
  407. var year2 = year;
  408. var month2 = parseInt(month) - 1;
  409. if(month2 == 0) {
  410. year2 = parseInt(year2) - 1;
  411. month2 = 12;
  412. }
  413. var day2 = day;
  414. var days2 = new Date(year2, month2, 0);
  415. days2 = days2.getDate();
  416. if(day2 > days2) {
  417. day2 = days2;
  418. }
  419. if(month2 < 10) {
  420. month2 = '0' + month2;
  421. }
  422. cNewTimeStr = year2 + '-' + month2 + '-' + day2;
  423. break;
  424. case 9: //得到下一个月的今天
  425. var date = getDateTime(0);
  426. var arr = date.split('-');
  427. var year = arr[0]; //获取当前日期的年份
  428. var month = arr[1]; //获取当前日期的月份
  429. var day = arr[2]; //获取当前日期的日
  430. var days = new Date(year, month, 0);
  431. days = days.getDate(); //获取当前日期中的月的天数
  432. var year2 = year;
  433. var month2 = parseInt(month) + 1;
  434. if(month2 == 13) {
  435. year2 = parseInt(year2) + 1;
  436. month2 = 1;
  437. }
  438. var day2 = day;
  439. var days2 = new Date(year2, month2, 0);
  440. days2 = days2.getDate();
  441. if(day2 > days2) {
  442. day2 = days2;
  443. }
  444. if(month2 < 10) {
  445. month2 = '0' + month2;
  446. }
  447. var t2 = year2 + '-' + month2 + '-' + day2;
  448. return t2;
  449. break;
  450. default:
  451. cNewTimeStr = myYear + '-' + myMonth + '-' + myDay + ' ' + myHour + ':' + myMinu + ':' + mySecond;
  452. break;
  453. }
  454. cNewTimeStr = cNewTimeStr.replace(/;/g, "");
  455. return cNewTimeStr;
  456. },
  457. /**
  458. * @description ZZAlertInfo普通弹出框
  459. */
  460. alert: function(info, callok, callcancel, callclose) {
  461. window.wxc.xcConfirm(info, window.wxc.xcConfirm.typeEnum.info, {
  462. onOk: function() {
  463. if(callok) {
  464. callok();
  465. }
  466. },
  467. onCancel: function() {
  468. if(callcancel) {
  469. callcancel();
  470. }
  471. },
  472. onClose: function() {
  473. if(callclose) {
  474. callclose();
  475. }
  476. }
  477. });
  478. },
  479. /**
  480. * @description ZZConfirm确认框
  481. */
  482. confirm: function(info, callok, callcancel, callclose) {
  483. window.wxc.xcConfirm(info, window.wxc.xcConfirm.typeEnum.confirm, {
  484. onOk: function() {
  485. if(callok) {
  486. callok();
  487. }
  488. },
  489. onCancel: function() {
  490. if(callcancel) {
  491. callcancel();
  492. }
  493. },
  494. onClose: function() {
  495. if(callclose) {
  496. callclose();
  497. }
  498. }
  499. });
  500. },
  501. /**
  502. * @description ZZInput输入框
  503. */
  504. input: function(info, callok, callclose) {
  505. window.wxc.xcConfirm(info, window.wxc.xcConfirm.typeEnum.input, {
  506. onOk: function(ok) {
  507. callok(ok);
  508. },
  509. onClose: function(close) {
  510. callclose(close);
  511. }
  512. });
  513. },
  514. /**
  515. * @description loading动画
  516. */
  517. loading: function(time) {
  518. var newDiv = '<div id="screen-cover" class="loader-inner ball-beat" style="width: 100%; height: 100%; position: absolute; top: 0; left: 0; background-color: rgba(0,0,0,0.5); text-align: center; z-index: 9999;' +
  519. '"><div class="on"></div><div class="off"></div><div class="on"></div>' +
  520. '</div>';
  521. if(typeof $("#screen-cover") != 'undefined')
  522. $("#screen-cover").remove();
  523. $("<link>")
  524. .attr({
  525. rel: "stylesheet",
  526. type: "text/css",
  527. href: "../../css/loader.css"
  528. })
  529. .appendTo("head");
  530. $("html").append(newDiv);
  531. $("#screen-cover").css('padding-top', $(window).height() / 2 - 55);
  532. },
  533. /**
  534. * @description 关闭loading动画
  535. */
  536. closeLoading: function() {
  537. if($("#screen-cover").length > 0)
  538. $("#screen-cover").remove();
  539. }
  540. }
  541. })(window);