Util.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. var Util = {
  2. /**
  3. * 获取url中"?"符后的字串
  4. * let code = getRequestParam()["code"]
  5. *
  6. * @returns {Object}
  7. */
  8. getRequestParam : function() {
  9. var url = location.search;
  10. var theRequest = new Object();
  11. if (url.indexOf("?") != -1) {
  12. var str = url.substr(1);
  13. var strs = str.split("&");
  14. for(var i = 0; i < strs.length; i ++) {
  15. theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
  16. }
  17. }
  18. return theRequest;
  19. },
  20. /**
  21. * 微信JSAPI分享
  22. * config = {
  23. * appId :
  24. * timestamp :
  25. * nonceStr :
  26. * signature :
  27. * logo :
  28. * url : window.location.href
  29. * title :
  30. * desc :
  31. * }
  32. */
  33. wxjsapiShare : function(config) {
  34. if(!wx) {
  35. return;
  36. }
  37. var logo = config.logo;
  38. var url = config.url;
  39. var title = config.title;
  40. var desc = config.desc;
  41. wx.config({
  42. debug: false,
  43. appId: config.appId,
  44. timestamp: config.timestamp,
  45. nonceStr: config.nonceStr,
  46. signature: config.signature,
  47. jsApiList: [
  48. 'onMenuShareTimeline',
  49. 'onMenuShareAppMessage',
  50. 'onMenuShareQQ',
  51. 'onMenuShareWeibo',
  52. 'onMenuShareQZone'
  53. ]
  54. });
  55. wx.error(function(res){});
  56. wx.ready(function () {
  57. share();
  58. });
  59. function share() {
  60. wx.onMenuShareTimeline({
  61. title: title,
  62. link: url,
  63. imgUrl: logo,
  64. success: function () {
  65. },
  66. cancel: function () {
  67. }
  68. });
  69. wx.onMenuShareAppMessage({
  70. title:title,
  71. desc: desc,
  72. link: url,
  73. imgUrl: logo,
  74. type: 'link',
  75. dataUrl: '',
  76. success: function () {},
  77. cancel: function () {}
  78. });
  79. wx.onMenuShareQQ({
  80. title: title,
  81. desc: desc,
  82. link: url,
  83. imgUrl: logo,
  84. success: function () {},
  85. cancel: function () {}
  86. });
  87. wx.onMenuShareWeibo({
  88. title: title,
  89. desc: desc,
  90. link: url,
  91. imgUrl: logo,
  92. success: function () {},
  93. cancel: function () {}
  94. });
  95. wx.onMenuShareQZone({
  96. title: title,
  97. desc: desc,
  98. link: url,
  99. imgUrl: logo,
  100. success: function () {},
  101. cancel: function () {}
  102. });
  103. }
  104. },
  105. /**
  106. * 跳转或打开APP
  107. * @param openUrl 打开APP自定义url
  108. * @param callback 是否打开APP回调, 入参 1-打开APP 2-没有打开APP
  109. */
  110. jumpOrDownload : function(openUrl, callback) {
  111. function checkOpen(cb){
  112. var _clickTime = +(new Date());
  113. function check(elsTime) {
  114. if ( elsTime > 3000 || document.hidden || document.webkitHidden) {
  115. cb(1);
  116. } else {
  117. cb(0);
  118. }
  119. }
  120. //启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束
  121. var _count = 0, intHandle;
  122. intHandle = setInterval(function(){
  123. _count++;
  124. var elsTime = +(new Date()) - _clickTime;
  125. if (_count>=100 || elsTime > 3000 ) {
  126. clearInterval(intHandle);
  127. check(elsTime);
  128. }
  129. }, 20);
  130. }
  131. //在iframe 中打开APP
  132. var ifr = document.createElement('iframe');
  133. ifr.src = openUrl;
  134. ifr.style.display = 'none';
  135. if (callback) {
  136. if (Util.isWechatBrowser()) {
  137. console.log("在微信浏览器");
  138. callback(0);
  139. }else{
  140. checkOpen(function(opened){
  141. callback && callback(opened);
  142. });
  143. }
  144. }
  145. document.body.appendChild(ifr);
  146. setTimeout(function() {
  147. document.body.removeChild(ifr);
  148. }, 2000);
  149. },
  150. /**
  151. * 身份证上提取性别
  152. * @param idcard
  153. * @returns {number} 1-男 0-女
  154. */
  155. gender : function(idcard) {
  156. if (parseInt(idcard.slice(-2, -1)) % 2 == 1) {
  157. return 1;
  158. } else {
  159. return 0;
  160. }
  161. },
  162. /**
  163. * 身份证提取出生日期
  164. * @param idcard
  165. * @returns {string}
  166. */
  167. birthDate : function(idcard) {
  168. var birthday = "";
  169. if(idcard != null && idcard != ""){
  170. if(idcard.length == 15){
  171. birthday = "19"+idcard.slice(6,12);
  172. } else if(idcard.length == 18){
  173. birthday = idcard.slice(6,14);
  174. }
  175. birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");
  176. //通过正则表达式来指定输出格式为:1990-01-01
  177. }
  178. return birthday;
  179. },
  180. /**
  181. * 页面加水印
  182. * @param settings
  183. */
  184. watermark : function(settings) {
  185. //默认设置
  186. var defaultSettings={
  187. watermark_txt:"水印",
  188. watermark_x:20,//水印起始位置x轴坐标
  189. watermark_y:20,//水印起始位置Y轴坐标
  190. watermark_rows:20,//水印行数
  191. watermark_cols:20,//水印列数
  192. watermark_x_space:100,//水印x轴间隔
  193. watermark_y_space:50,//水印y轴间隔
  194. watermark_color:'#aaa',//水印字体颜色
  195. watermark_alpha:0.4,//水印透明度
  196. watermark_fontsize:'15px',//水印字体大小
  197. watermark_font:'微软雅黑',//水印字体
  198. watermark_width:210,//水印宽度
  199. watermark_height:80,//水印长度
  200. watermark_angle:15//水印倾斜度数
  201. };
  202. //采用配置项替换默认值,作用类似jquery.extend
  203. if(arguments.length===1&&typeof arguments[0] ==="object" )
  204. {
  205. var src=arguments[0]||{};
  206. for(key in src)
  207. {
  208. if(src[key]&&defaultSettings[key]&&src[key]===defaultSettings[key])
  209. continue;
  210. else if(src[key])
  211. defaultSettings[key]=src[key];
  212. }
  213. }
  214. var oTemp = document.createDocumentFragment();
  215. //获取页面最大宽度
  216. var page_width = Math.max(document.body.clientWidth,document.body.clientWidth);
  217. var cutWidth = page_width*0.0150;
  218. var page_width=page_width-cutWidth;
  219. //获取页面最大高度
  220. var page_height = Math.max(document.body.scrollHeight,document.body.clientHeight)+450;
  221. // var page_height = document.body.scrollHeight+document.body.scrollTop;
  222. //如果将水印列数设置为0,或水印列数设置过大,超过页面最大宽度,则重新计算水印列数和水印x轴间隔
  223. if (defaultSettings.watermark_cols == 0 || (parseInt(defaultSettings.watermark_x + defaultSettings.watermark_width *defaultSettings.watermark_cols + defaultSettings.watermark_x_space * (defaultSettings.watermark_cols - 1)) > page_width)) {
  224. defaultSettings.watermark_cols = parseInt((page_width-defaultSettings.watermark_x+defaultSettings.watermark_x_space) / (defaultSettings.watermark_width + defaultSettings.watermark_x_space));
  225. defaultSettings.watermark_x_space = parseInt((page_width - defaultSettings.watermark_x - defaultSettings.watermark_width * defaultSettings.watermark_cols) / (defaultSettings.watermark_cols - 1));
  226. }
  227. //如果将水印行数设置为0,或水印行数设置过大,超过页面最大长度,则重新计算水印行数和水印y轴间隔
  228. if (defaultSettings.watermark_rows == 0 || (parseInt(defaultSettings.watermark_y + defaultSettings.watermark_height * defaultSettings.watermark_rows + defaultSettings.watermark_y_space * (defaultSettings.watermark_rows - 1)) > page_height)) {
  229. defaultSettings.watermark_rows = parseInt((defaultSettings.watermark_y_space + page_height - defaultSettings.watermark_y) / (defaultSettings.watermark_height + defaultSettings.watermark_y_space));
  230. defaultSettings.watermark_y_space = parseInt(((page_height - defaultSettings.watermark_y) - defaultSettings.watermark_height * defaultSettings.watermark_rows) / (defaultSettings.watermark_rows - 1));
  231. }
  232. var x;
  233. var y;
  234. for (var i = 0; i < defaultSettings.watermark_rows; i++) {
  235. y = defaultSettings.watermark_y + (defaultSettings.watermark_y_space + defaultSettings.watermark_height) * i;
  236. for (var j = 0; j < defaultSettings.watermark_cols; j++) {
  237. x = defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings.watermark_x_space) * j;
  238. var mask_div = document.createElement('div');
  239. mask_div.id = 'mask_div' + i + j;
  240. mask_div.className = 'mask_div';
  241. mask_div.appendChild(document.createTextNode(defaultSettings.watermark_txt));
  242. //设置水印div倾斜显示
  243. mask_div.style.webkitTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
  244. mask_div.style.MozTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
  245. mask_div.style.msTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
  246. mask_div.style.OTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
  247. mask_div.style.transform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
  248. mask_div.style.visibility = "";
  249. mask_div.style.position = "absolute";
  250. mask_div.style.left = x + 'px';
  251. mask_div.style.top = y + 'px';
  252. mask_div.style.overflow = "hidden";
  253. mask_div.style.zIndex = "9999";
  254. mask_div.style.pointerEvents='none';//pointer-events:none 让水印不遮挡页面的点击事件
  255. //mask_div.style.border="solid #eee 1px";
  256. mask_div.style.opacity = defaultSettings.watermark_alpha;
  257. mask_div.style.fontSize = defaultSettings.watermark_fontsize;
  258. mask_div.style.fontFamily = defaultSettings.watermark_font;
  259. mask_div.style.color = defaultSettings.watermark_color;
  260. mask_div.style.textAlign = "center";
  261. mask_div.style.width = defaultSettings.watermark_width + 'px';
  262. mask_div.style.height = defaultSettings.watermark_height + 'px';
  263. mask_div.style.display = "block";
  264. oTemp.appendChild(mask_div);
  265. };
  266. };
  267. document.body.appendChild(oTemp);
  268. },
  269. isAppWebView : function(appWebView) {
  270. try{
  271. var ua = window.navigator.userAgent.toLowerCase();
  272. if(ua.indexOf(appWebView)>-1){
  273. return true;
  274. }
  275. return false;
  276. }catch (e) {
  277. console.error(e);
  278. return false;
  279. }
  280. },
  281. /**
  282. * 是否在android设备
  283. * @returns {boolean}
  284. */
  285. isAndroid : function() {
  286. try{
  287. var ua = window.navigator.userAgent.toLowerCase();
  288. if(ua.match(/android/ig)){
  289. return true;
  290. }
  291. return false;
  292. }catch (e) {
  293. console.error(e);
  294. return false;
  295. }
  296. },
  297. /**
  298. * 是否在IOS设备
  299. * @returns {boolean}
  300. */
  301. isIOS : function() {
  302. try{
  303. var ua = window.navigator.userAgent.toLowerCase();
  304. if(ua.match(/iphone|ipod|ipad/ig)){
  305. return true;
  306. }
  307. return false;
  308. }catch (e) {
  309. console.error(e);
  310. return false;
  311. }
  312. },
  313. /**
  314. * 是否在iphone设备
  315. * @returns {boolean}
  316. */
  317. isIphone : function() {
  318. try{
  319. var ua = window.navigator.userAgent.toLowerCase();
  320. if(ua.match(/iphone|ipod/ig)){
  321. return true;
  322. }
  323. return false;
  324. }catch (e) {
  325. console.error(e);
  326. return false;
  327. }
  328. },
  329. /**
  330. * 是否在ipad设备
  331. * @returns {boolean}
  332. */
  333. isIpad : function() {
  334. try{
  335. var ua = window.navigator.userAgent.toLowerCase();
  336. if(ua.match(/ipad/ig)){
  337. return true;
  338. }
  339. return false;
  340. }catch (e) {
  341. console.error(e);
  342. return false;
  343. }
  344. },
  345. /**
  346. * 判断是否在微信浏览器
  347. * @returns {boolean}
  348. */
  349. isWechatBrowser : function(){
  350. try{
  351. var ua = window.navigator.userAgent.toLowerCase();
  352. if(ua.match(/MicroMessenger/i) == 'micromessenger'){
  353. return true;
  354. }else{
  355. return false;
  356. }
  357. }catch (e) {
  358. console.error(e);
  359. return false;
  360. }
  361. },
  362. /**
  363. * 是否在支付宝公众号webview里面
  364. * @returns {boolean}
  365. */
  366. isAliBrowser : function(){
  367. try{
  368. var ua = window.navigator.userAgent.toLowerCase();
  369. if(ua.match(/aliapp/i) == 'aliapp'){
  370. return true;
  371. }else if(ua.match(/alipayclient/i) == 'alipayclient') {
  372. return true;
  373. }
  374. return false;
  375. }catch (e) {
  376. console.error(e);
  377. return false;
  378. }
  379. },
  380. /**
  381. * 判断是否在支付宝小程序
  382. * @returns {boolean}
  383. */
  384. isAliProgramBrowser : function(){
  385. try{
  386. if(window.my && window.my.tradePay) {
  387. return true;
  388. }
  389. return false;
  390. }catch (e) {
  391. console.error(e);
  392. return false;
  393. }
  394. },
  395. /**
  396. * 是否Iphone全面屏手机
  397. * @returns {Window | boolean}
  398. */
  399. isNewIpPhone : function() {
  400. return window
  401. && navigator.userAgent.indexOf('iPhone')>-1
  402. && window.screen.height >= 812 &&
  403. window.devicePixelRatio >= 2;
  404. },
  405. /**
  406. * 站长统计
  407. * 页面PV自动统计:
  408. * var _czc = _czc || [];
  409. * Util.cnzz();
  410. * 页面PV自定义统计:
  411. * var _czc = _czc || [];
  412. * Util.cnzzAutoPageview(_czc, false);
  413. * Util.cnzz();
  414. * Util.cnzzTrackPageView(_czc, "/index.html");
  415. *
  416. * 事件统计:
  417. * var _czc = _czc || [];
  418. * Util.cnzzTrackEvent(_czc, category, action, label);
  419. */
  420. cnzz : function(cnzzId) {
  421. if(!document.getElementById("cnzz_stat_icon_"+cnzzId)) {
  422. try {
  423. var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
  424. document.write(unescape("%3Cspan id='cnzz_stat_icon_"+cnzzId+"'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D"+cnzzId+"' type='text/javascript'%3E%3C/script%3E"));
  425. } catch(e) {
  426. console.log(e);
  427. }
  428. }
  429. document.getElementById("cnzz_stat_icon_"+cnzzId).style.display="none";
  430. },
  431. /**
  432. * 设置是否自动PV统计
  433. * @param _czc
  434. * @param autopageview
  435. */
  436. cnzzAutoPageview : function(_czc, autopageview) {
  437. _czc = _czc || [];
  438. _czc.push(["_setAutoPageview",autopageview]);
  439. },
  440. /**
  441. * 页面访问统计
  442. * @param _czc
  443. * @param content_url 访问页面
  444. * @param referer_url 来源页面
  445. */
  446. cnzzTrackPageView : function(_czc, content_url, referer_url) {
  447. _czc = _czc || [];
  448. _czc.push(["_trackPageview",content_url,referer_url]);
  449. },
  450. /**
  451. * 事件统计
  452. * @param _czc
  453. * @param category 表示事件发生在谁身上
  454. * @param action 表示访客跟元素交互的行为动作
  455. * @param label 用于更详细的描述事件
  456. */
  457. cnzzTrackEvent : function(_czc, category, action, label) {
  458. _czc = _czc || [];
  459. _czc.push(["_trackEvent",category,action,label]);
  460. }
  461. };