common.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. var priceshow = false;
  2. var timeshow = false;
  3. function priceTime(){
  4. $(".pricefat").mouseover(function(){
  5. if(!$(".pricefat").hasClass("active")){
  6. $("#minprice").css({"border-color":"#2cb7ca"});
  7. $("#maxprice").css({"border-color":"#2cb7ca"});
  8. $(".pricebut").show();
  9. $(".pricefat").addClass("customtime-active");
  10. }
  11. }).mouseout(function(){
  12. if(!$(".pricefat").hasClass("active")){
  13. if(!priceshow){
  14. $("#minprice").css({"border-color":""});
  15. $("#maxprice").css({"border-color":""});
  16. $(".pricebut").hide();
  17. $(".pricefat").removeClass("customtime-active");
  18. }
  19. }
  20. })
  21. $("#minprice,#maxprice").blur(function(){
  22. if(!priceshow&&!$(".pricefat").hasClass("active")){
  23. $("#minprice").css({"border-color":""});
  24. $("#maxprice").css({"border-color":""});
  25. $(".pricebut").hide();
  26. $(".pricefat").removeClass("customtime-active");
  27. }
  28. })
  29. $("#minprice").click(function(){
  30. priceshow=true;
  31. $(".pricebut").show();
  32. $(".pricefat").addClass("customtime-active");
  33. })
  34. $("#maxprice").click(function(){
  35. priceshow=true;
  36. $(".pricebut").show();
  37. $(".pricefat").addClass("customtime-active");
  38. })
  39. //
  40. $(".timerInput").mouseover(function(){
  41. if(!$(".timerInput").hasClass("active")){
  42. $("#starttime").css({"border-color":"#2cb7ca"});
  43. $("#endtime").css({"border-color":"#2cb7ca"});
  44. $("#timebut").show();
  45. $(".timerInput").addClass("customtime-active");
  46. }
  47. }).mouseout(function(){
  48. if(!$(".timerInput").hasClass("active")){
  49. if(!timeshow){
  50. $("#starttime").css({"border-color":""});
  51. $("#endtime").css({"border-color":""});
  52. $("#timebut").hide();
  53. $(".timerInput").removeClass("customtime-active");
  54. }
  55. }
  56. })
  57. //
  58. $("#starttime").click(function(){
  59. timeshow = true;
  60. $("#timebut").show();
  61. $(".timerInput").addClass("customtime-active");
  62. })
  63. $("#endtime").click(function(){
  64. timeshow = true;
  65. $("#timebut").show();
  66. $(".timerInput").addClass("customtime-active");
  67. })
  68. }
  69. //
  70. var EasyAlert = {
  71. timeout: null,
  72. waitTime: 1000,
  73. show: function(text,css,waitTime){
  74. if(this.timeout != null){
  75. clearTimeout(this.timeout);
  76. this.hide();
  77. this.timeout = null;
  78. }
  79. var thisClass = this;
  80. this.timeout = setTimeout(function(){
  81. thisClass.hide();
  82. thisClass.timeout = null;
  83. },waitTime?waitTime:this.waitTime);
  84. $("body").append('<div class="easyalert" id="easyAlert">'+text+'</div>');
  85. if(typeof(css) != "undefined"&&css!=""){
  86. $("#easyAlert").css(css);
  87. }
  88. $("#easyAlert").css({"left":"50%","margin-top":-($("#easyAlert").outerHeight()/2),"margin-left":-($("#easyAlert").outerWidth()/2)}).show();
  89. },
  90. hide: function(){
  91. $("#easyAlert").remove();
  92. }
  93. }
  94. var EasyPopup = function(id){
  95. this.id = id;
  96. var thisClass = this;
  97. document.getElementById(id).ontap = function(e){
  98. if(e.target.id == id){
  99. thisClass.hide();
  100. }
  101. }
  102. this.show = function(){
  103. $("#"+this.id).fadeIn();
  104. var mainObj = $("#"+id+">div:first");
  105. mainObj.css({"margin-top":-(mainObj.outerHeight()/2 + 35)});
  106. },
  107. this.hide = function(){
  108. $("#"+this.id).fadeOut();
  109. }
  110. }
  111. //计算时差
  112. function timeDiff(date){
  113. var date1 = date;//开始时间
  114. var date2 = new Date();//结束时间
  115. var date3 = date2.getTime()-date1.getTime();//时间差的毫秒数
  116. //计算出相差天数
  117. var days = Math.floor(date3/(24*3600*1000));
  118. //计算出小时数
  119. var leave1 = date3%(24*3600*1000);//计算天数后剩余的毫秒数
  120. var hours = Math.floor(leave1/(3600*1000));
  121. //计算相差分钟数
  122. var leave2 = leave1%(3600*1000);//计算小时数后剩余的毫秒数
  123. var minutes = Math.floor(leave2/(60*1000));
  124. //计算相差秒数
  125. var td = "30秒前";
  126. if(days > 0){
  127. if (days > 10) {
  128. var date1year = date1.getFullYear();
  129. var date2year = date2.getFullYear();
  130. var date1month = date1.getMonth()+1;
  131. var date1day = date1.getDate();
  132. if(date1month < 10){
  133. date1month ="0"+date1month;
  134. }
  135. if(date1day < 10){
  136. date1day ="0"+date1day;
  137. }
  138. if (date1year<date2year){
  139. td = date1.getFullYear()+"-"+date1month+"-"+date1day;
  140. }else{
  141. td = date1month+"-"+date1day;
  142. }
  143. } else {
  144. td = days+"天前"
  145. }
  146. }else if(hours > 0){
  147. td = hours+"小时前";
  148. }else if(minutes > 0){
  149. td = minutes+"分钟前";
  150. }
  151. return td;
  152. }
  153. function redirect(zbadd,link,sid,sds){
  154. if(link != null && typeof(link) != "undefined"){
  155. link = link.replace(/\n/g,"");
  156. if(!/^http/.test(link)){
  157. link="http://"+link
  158. }
  159. }
  160. if(sds){
  161. window.location.href="/article/content/"+sid+".html?keywords="+encodeURIComponent(sds);
  162. }else{
  163. window.location.href="/article/content/"+sid+".html";
  164. }
  165. }
  166. function newredirect(zbadd,link,sid,sds,index){
  167. if(link != null && typeof(link) != "undefined"){
  168. link = link.replace(/\n/g,"");
  169. if(!/^http/.test(link)){
  170. link="http://"+link
  171. }
  172. }
  173. var pt = ""
  174. if (index==1){
  175. pt="projectMatch=项目匹配"
  176. }
  177. if(sds){
  178. pt = "&"+pt
  179. window.location.href="/article/content/"+sid+".html?keywords="+encodeURIComponent(sds)+pt;
  180. }else{
  181. window.location.href="/article/content/"+sid+".html"+pt;
  182. }
  183. }
  184. function pcredirect(link,sid){
  185. window.open("/pcdetail/"+sid+".html");
  186. }
  187. var isNumber = /^[0-9]+$/
  188. var isLetter = /^[a-zA-Z]+$/
  189. function keyWordHighlight(value,oldChars,newChar){
  190. if(typeof(oldChars) == "undefined" || oldChars == null || typeof(newChar) == "undefined" || newChar == null || newChar == ""){
  191. return value;
  192. }
  193. var array = [];
  194. if(oldChars instanceof Array){
  195. array = oldChars.concat();
  196. }else{
  197. array.push(oldChars);
  198. }
  199. try{
  200. var map = {};
  201. for(var i=0;i<array.length;i++){
  202. var oldChar = array[i];
  203. if(oldChar.replace(/\s+/g,"") == "" || map[oldChar] || isNumber.test(oldChar) || isLetter.test(oldChar)){
  204. continue;
  205. }
  206. map[oldChar] = true;
  207. oldChar = oldChar.replace(/\$/g,"\\$");
  208. oldChar = oldChar.replace(/\(/g,"\\(");
  209. oldChar = oldChar.replace(/\)/g,"\\)");
  210. oldChar = oldChar.replace(/\*/g,"\\*");
  211. oldChar = oldChar.replace(/\+/g,"\\+");
  212. oldChar = oldChar.replace(/\./g,"\\.");
  213. oldChar = oldChar.replace(/\[/g,"\\[");
  214. oldChar = oldChar.replace(/\]/g,"\\]");
  215. oldChar = oldChar.replace(/\?/g,"\\?");
  216. oldChar = oldChar.replace(/\\/g,"\\");
  217. oldChar = oldChar.replace(/\//g,"\\/");
  218. oldChar = oldChar.replace(/\^/g,"\\^");
  219. oldChar = oldChar.replace(/\{/g,"\\{");
  220. oldChar = oldChar.replace(/\}/g,"\\}");
  221. oldChar = oldChar.replace(/\|/g,"\\|");
  222. value = value.replace(new RegExp("("+oldChar+")",'gmi'),newChar);
  223. }
  224. }catch(e){}
  225. return value;
  226. }
  227. function getWxVersion(){
  228. var wechatInfo = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i);
  229. if(!wechatInfo) {
  230. return null;
  231. }
  232. return wechatInfo[1];
  233. }
  234. /**
  235. * 设置光标在短连接输入框中的位置
  236. * @param inpObj 输入框
  237. * @param pos
  238. */
  239. function setCursorPos(inpObj, pos){
  240. if(navigator.userAgent.indexOf("MSIE") > -1){
  241. var range = document.selection.createRange();
  242. var textRange = inpObj.createTextRange();
  243. textRange.moveStart('character',pos);
  244. textRange.collapse();
  245. textRange.select();
  246. }else{
  247. inpObj.setSelectionRange(pos,pos);
  248. }
  249. }
  250. /**
  251. * 获取光标在短连接输入框中的位置
  252. * @param inpObj 输入框
  253. */
  254. function getCursorPos(inpObj){
  255. if(navigator.userAgent.indexOf("MSIE") > -1) { // IE
  256. var range = document.selection.createRange();
  257. range.text = '';
  258. range.setEndPoint('StartToStart',inpObj.createTextRange());
  259. return range.text.length;
  260. } else {
  261. return inpObj.selectionStart;
  262. }
  263. }
  264. //获取url中参数
  265. function getUrlParam(name){
  266. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  267. var r = window.location.search.substr(1).match(reg);
  268. if(r != null)
  269. return unescape(r[2]);
  270. return null;
  271. }
  272. /******* 获取url参数(正则)********/
  273. function getParam(name) {
  274. var search = document.location.search;
  275. // alert(search);
  276. var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
  277. var matcher = pattern.exec(search);
  278. var items = null;
  279. if (null != matcher) {
  280. try {
  281. items = decodeURIComponent(decodeURIComponent(matcher[1]));
  282. } catch (e) {
  283. try {
  284. items = decodeURIComponent(matcher[1]);
  285. } catch (e) {
  286. items = matcher[1];
  287. }
  288. }
  289. }
  290. return items;
  291. };
  292. //获取滚动条宽度
  293. function getScrollWidth() {
  294. var noScroll, scroll, oDiv = document.createElement("DIV");
  295. oDiv.style.cssText = "position:absolute; top:-1000px; width:100px; height:100px; overflow:hidden;";
  296. noScroll = document.body.appendChild(oDiv).clientWidth;
  297. oDiv.style.overflowY = "scroll";
  298. scroll = oDiv.clientWidth;
  299. document.body.removeChild(oDiv);
  300. return noScroll-scroll;
  301. }
  302. //表头固定
  303. var TableHeadFixed = function(className,isminus,flag,pageName){
  304. if(typeof(className) == "undefined"){
  305. className = "tabContainer-2";
  306. }
  307. if(typeof(isminus) == "undefined"){
  308. isminus = true;
  309. }
  310. if(typeof(flag) == "undefined"){
  311. flag = true;
  312. }
  313. var cHeight = document.body.clientHeight;
  314. if(cHeight <= 0){
  315. cHeight = 500;
  316. }
  317. var thisFlag = false;
  318. var prevSelectType = null;
  319. $(window).scroll(function(event){
  320. if(!$("#right-table").hasClass("active") && flag){
  321. return;
  322. }
  323. //超级搜索页面增加处理逻辑
  324. if(pageName == "supsearch"){
  325. if(prevSelectType != selectType){
  326. thisFlag = false;
  327. }
  328. prevSelectType = selectType;
  329. if(selectType == "all"){
  330. className = "tabContainer"
  331. }else{
  332. className = "tabContainer-2";
  333. }
  334. }
  335. var tableHeight = $("."+className).outerHeight();
  336. if(tableHeight <= cHeight){
  337. $("."+className+" .lucene-table").removeClass("tabfixed tababsolute");
  338. $("."+className+" .lucene-table>table:first").css("top",'64px');
  339. thisFlag = false;
  340. return;
  341. }
  342. var offsetTop = $("."+className).offset().top;
  343. var scrollTop = $(this).scrollTop();
  344. var oTop = offsetTop;
  345. var ooTop = offsetTop;
  346. if(isminus){
  347. oTop+=20;
  348. ooTop-=20;
  349. }
  350. if(scrollTop >= (oTop-64)){
  351. if(ooTop + tableHeight - scrollTop -20 <= cHeight){
  352. if(!thisFlag){
  353. $("."+className+" .lucene-table").addClass("tababsolute");
  354. $("."+className+" .lucene-table>table:first").css("top",scrollTop);
  355. }
  356. thisFlag = true;
  357. }else{
  358. if(thisFlag){
  359. $("."+className+" .lucene-table").removeClass("tababsolute");
  360. $("."+className+" .lucene-table>table:first").css("top",'64px');
  361. }
  362. thisFlag = false;
  363. }
  364. $("."+className+" .lucene-table").addClass("tabfixed");
  365. }else{
  366. $("."+className+" .lucene-table").removeClass("tabfixed tababsolute");
  367. $("."+className+" .lucene-table>table:first").css("top",'64px');
  368. thisFlag = false;
  369. }
  370. });
  371. }
  372. //页面跳转输入框光标位置
  373. function moveEnd(obj) {
  374. obj.focus();
  375. var len = obj.value.length;
  376. if (document.selection) {
  377. var sel = obj.createTextRange();
  378. sel.moveStart('character', len);
  379. sel.collapse();
  380. sel.select();
  381. } else if (typeof obj.selectionStart == 'number'
  382. && typeof obj.selectionEnd == 'number') {
  383. obj.selectionStart = obj.selectionEnd = len;
  384. }
  385. }
  386. function mySysIsIos(){
  387. return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  388. }
  389. // 金额转换
  390. function moneyUnit(num, type, lv) {
  391. if (num === '' || num == null || num == undefined || isNaN(num)) return ''
  392. var levelArr = ['元', '万元', '亿元', '万亿元']
  393. if (type === void 0) {
  394. type = 'string';
  395. }
  396. if (lv === void 0) {
  397. lv = 0;
  398. }
  399. if (num === 0) {
  400. if (type === 'string') {
  401. return '0';
  402. }
  403. if (type === 'lv') {
  404. return levelArr[lv];
  405. }
  406. if (type === 'number') {
  407. return 0;
  408. }
  409. if (type === 'index') {
  410. return lv;
  411. }
  412. }
  413. var result = num / Math.pow(10000, lv);
  414. if (result > 10000 && lv < 2) {
  415. return this.moneyUnit(num, type, lv + 1)
  416. } else {
  417. if (type === 'string') {
  418. return String(Math.floor(result * 100) / 100).replace('.00', '') + levelArr[lv];
  419. }
  420. if (type === 'lv') {
  421. return levelArr[lv];
  422. }
  423. if (type === 'index') {
  424. return lv;
  425. }
  426. if (type === 'number') {
  427. return String(Math.floor(result * 100) / 100).replace('.00', '');
  428. }
  429. }
  430. }
  431. /*
  432. * 时间格式化函数(将时间格式化为,2019年08月12日,2019-08-12,2019/08/12的形式)
  433. *
  434. *
  435. * pattern参数(想要什么格式的数据就传入什么格式的数据)
  436. * · 'yyyy-MM-dd' ---> 输出如2019-09-20
  437. * · 'yyyy-MM-dd hh:mm' ---> 输出如2019-09-20 08:20
  438. * · 'yyyy-MM-dd HH:mm:ss' ---> 输出如2019-09-20 18:20:23
  439. * · 'yyyy/MM/dd' ---> 输出如2019/09/20
  440. * · 'yyyy年MM月dd日' ---> 输出如2019年09月20日
  441. * · 'yyyy年MM月dd日 hh时mm分' ---> 输出如2019年09月20日 08时20分
  442. * · 'yyyy年MM月dd日 hh时mm分ss秒' ---> 输出如2019年09月20日 08时20分23秒
  443. * · 'yyyy年MM月dd日 hh时mm分ss秒 EE' ---> 输出如2019年09月20日 08时20分23秒 周二
  444. * · 'yyyy年MM月dd日 hh时mm分ss秒 EEE' ---> 输出如2019年09月20日 08时20分23秒 星期二
  445. *
  446. * 参考: https://www.cnblogs.com/mr-wuxiansheng/p/6296646.html
  447. */
  448. Date.prototype.pattern = function (fmt) {
  449. if (!fmt) return ''
  450. var o = {
  451. 'y+': this.getFullYear(),
  452. 'M+': this.getMonth() + 1, // 月份
  453. 'd+': this.getDate(), // 日
  454. // 12小时制
  455. 'h+': this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时
  456. // 24小时制
  457. 'H+': this.getHours(), // 小时
  458. 'm+': this.getMinutes(), // 分
  459. 's+': this.getSeconds(), // 秒
  460. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  461. 'S': this.getMilliseconds(), // 毫秒
  462. 'E+': this.getDay(), // 周
  463. };
  464. var week = {
  465. '0': '日',
  466. '1': '一',
  467. '2': '二',
  468. '3': '三',
  469. '4': '四',
  470. '5': '五',
  471. '6': '六'
  472. };
  473. if (/(y+)/.test(fmt)) {
  474. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  475. }
  476. if (/(E+)/.test(fmt)) {
  477. fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[
  478. this.getDay() + '']);
  479. }
  480. for (var k in o) {
  481. if (new RegExp('(' + k + ')').test(fmt)) {
  482. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k])
  483. .length)));
  484. }
  485. }
  486. return fmt;
  487. }
  488. $(function(){
  489. //自定义tap
  490. if("ontouchend" in document){
  491. $(document).on("touchstart", function(e) {
  492. var $target = $(e.target);
  493. $target.data("isMoved", 0);
  494. $target.data("startTime", Date.now());
  495. });
  496. $(document).on("touchmove", function(e) {
  497. var $target = $(e.target);
  498. $target.data("isMoved", 1);
  499. });
  500. $(document).on("touchend", function(e) {
  501. var $target = $(e.target);
  502. var startTime = $target.data("startTime");
  503. if(Date.now()-startTime>200){
  504. return;
  505. }
  506. if($target.data("isMoved") == 1){
  507. return;
  508. }
  509. $target.trigger("tap");
  510. });
  511. }else{
  512. $(document).on("click", function(e) {
  513. var $target = $(e.target);
  514. $target.trigger("tap");
  515. });
  516. }
  517. });