base.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. var coreServer = $("#coreServer").val();
  2. var isPC = ($(window).width()>768);
  3. var globalUserData = '';
  4. var GLOBAL_TOKEN = 'X-Token';
  5. var GLOBAL_USER_ID = 0; // 用户Id
  6. var GLOBAL_USER_IDENTITY = 0;//2-会员机构;3-供应商;4-普通机构
  7. var GLOBAL_SHOP_ID = 0; // 供应商Id
  8. var GLOBAL_VIP_FLAG = 0; // 超级会员标记
  9. var GLOBAL_CLUB_TYPE = 0; // 会员类型 1 医美机构 2 胜美机构
  10. var IDENTITY = 0; // 用户标记
  11. var isForbidTabs = true; //控制菜单
  12. if(localStorage.getItem('userInfo')){
  13. globalUserData = JSON.parse(localStorage.getItem('userInfo'));
  14. GLOBAL_TOKEN = globalUserData.token;
  15. GLOBAL_USER_ID = globalUserData.userId*1;
  16. GLOBAL_USER_IDENTITY = globalUserData.userIdentity*1;
  17. IDENTITY = globalUserData.userIdentity*1;
  18. GLOBAL_VIP_FLAG = globalUserData.vipFlag;
  19. GLOBAL_SHOP_ID = globalUserData.shopId*1;
  20. GLOBAL_CLUB_TYPE = globalUserData.firstClubType ? globalUserData.firstClubType : null;
  21. if (GLOBAL_USER_ID === 5261 || GLOBAL_USER_ID === 10947 || GLOBAL_USER_ID === 11579) {
  22. GLOBAL_USER_IDENTITY = 1;
  23. GLOBAL_VIP_FLAG == 1;
  24. IDENTITY = 1;
  25. }else if(GLOBAL_CLUB_TYPE == 1){
  26. IDENTITY = 5;
  27. }
  28. if(globalUserData.listingFee === 1){
  29. isForbidTabs = false;
  30. }
  31. } else {
  32. var pathname = window.location.pathname;
  33. var weChatLoginFlag = (Number(getBaseCookie("weChatAutoLogin"))===1 || Number(getBaseCookie("weChatAutoLogin"))===2);
  34. if(pathname.indexOf('/product/auth/') === -1 && !weChatLoginFlag ){
  35. var userAgent = navigator.userAgent.toLowerCase();
  36. if (userAgent.match(/MicroMessenger/i)) {
  37. // 微信浏览器自动授权登录
  38. var urlForWeChat = 'https://www.caimei365.com/we_chat/redirect_uri.html';
  39. $.get(coreServer+'/user/login/auth/link?mode=1&redirectUri='+urlForWeChat, function(r){
  40. if(r.code===0 && r.data){
  41. setBaseCookie("weChatAutoLogin", 1);
  42. setBeforeUrl();
  43. window.location.href = r.data;
  44. }
  45. });
  46. }
  47. }
  48. }
  49. // 头部
  50. var globalHead = new Vue({
  51. el: '#globalHead',
  52. data: {
  53. loginStatus: false,
  54. userData: {},
  55. classify: [],
  56. headCart: {
  57. cartList: [],
  58. cartCount: 0,
  59. productCount: 0,
  60. priceTotal: 0
  61. },
  62. noticeNum:0,
  63. shopId:'',
  64. userIdentity:'',
  65. articleType: '',
  66. isFiexd:false,
  67. classifyIndex:1
  68. },
  69. watch:{
  70. isFiexd: function(nVal,oVal){
  71. // 防止跳动
  72. if(nVal && isPC){
  73. $("body").css('paddingTop', $("#globalHead").height() + "px");
  74. }else{
  75. $("body").css('paddingTop', "0px");
  76. }
  77. }
  78. },
  79. methods: {
  80. changeClassify: function(value){
  81. this.classifyIndex=value;
  82. },
  83. // 初始化分类菜单效果
  84. initClassufyTab: function(){
  85. if(isPC){
  86. setTimeout(function(){
  87. $('#mainClassify').slide({
  88. mainCell:".clsCon",
  89. titCell:".clsTab a",
  90. trigger: "mouseover"
  91. });
  92. $('#mainClassify_1').slide({
  93. mainCell:".tabItem_right",
  94. titCell:".tabItem_lift a",
  95. trigger: "mouseover"
  96. });
  97. $('#mainClassify_2').slide({
  98. mainCell:".tabItem_right",
  99. titCell:".tabItem_lift a",
  100. trigger: "mouseover"
  101. });
  102. $("#mainClassify_2 .tabItem_lift a").eq(0).trigger("mouseover");
  103. $("#mainClassify_1 .tabItem_lift a").eq(0).trigger("mouseover");
  104. },500);
  105. }else{
  106. setTimeout(function(){
  107. $('#mainClassify').slide({
  108. mainCell:".clsCon",
  109. titCell:".clsTab a",
  110. trigger: "click"
  111. });
  112. $('#mainClassify_1').slide({
  113. mainCell:".tabItem_right",
  114. titCell:".tabItem_lift a",
  115. trigger: "click"
  116. });
  117. $('#mainClassify_2').slide({
  118. mainCell:".tabItem_right",
  119. titCell:".tabItem_lift a",
  120. trigger: "click"
  121. });
  122. $("#mainClassify_2 .tabItem_lift a").eq(0).trigger("click");
  123. $("#mainClassify_1 .tabItem_lift a").eq(0).trigger("click");
  124. },500);
  125. }
  126. },
  127. // 头部购物车数据
  128. getHeadCart: function(userId) {
  129. var _self = this;
  130. ShoppingApi.getHeaderCartList({userId: userId},function(response){
  131. if (response.code === 0 && response.data) {
  132. var data = response.data;
  133. _self.headCart.cartList = data.list;
  134. _self.headCart.cartCount = data.list.length;
  135. _self.headCart.productCount = 0;
  136. _self.headCart.priceTotal = 0;
  137. data.list.map(function(item){
  138. _self.headCart.productCount += item.number;
  139. _self.headCart.priceTotal += item.number*item.price;
  140. });
  141. }else if(r.code== -99){
  142. CAIMEI.Storage.clear();
  143. location.href = "/login.html";
  144. }
  145. });
  146. },
  147. getAuthClubCount:function (clubId) {
  148. var _self = this;
  149. PublicApi.getAuthClubCount({commonId: clubId},function(response){
  150. if (response.code === 0 && response.data) {
  151. var data = response.data;
  152. if(data.count>99){
  153. _self.noticeNum = '99+'
  154. }else{
  155. _self.noticeNum = data.count
  156. }
  157. console.log('noticeNum',_self.noticeNum)
  158. }else{
  159. console.log('获取机构通知消息数量异常')
  160. }
  161. });
  162. },
  163. getAuthShopCount:function (shopId) {
  164. var _self = this;
  165. PublicApi.getAuthShopCount({commonId: shopId},function(response){
  166. if (response.code === 0 && response.data) {
  167. var data = response.data;
  168. if(data.count>99){
  169. _self.noticeNum = '99+'
  170. }else{
  171. _self.noticeNum = data.count
  172. }
  173. }else{
  174. console.log('获取供应商通知消息数量异常')
  175. }
  176. });
  177. },
  178. goMsgLinkFn:function (url) {
  179. var _self = this;
  180. if(_self.loginStatus){
  181. window.location.href= url;
  182. }else{
  183. window.location.href='/login.html';
  184. }
  185. },
  186. goCartsLinkFn:function(){
  187. var _self = this;
  188. if(_self.loginStatus){
  189. window.location.href='/shopping/cart.html';
  190. }else{
  191. window.location.href='/login.html';
  192. }
  193. },
  194. productDetailsFn:function(id){ // 跳转详情
  195. window.location.href='/product-'+id+'.html';
  196. },
  197. DeleteCartProducts:function(id){ // 删除购物车
  198. var _self = this;
  199. ShoppingApi.DeleteCartProducts({userId: this.userData.userId, productIds: id},function(response){
  200. if(response.code === 0){
  201. _self.getHeadCart(_self.userData.userId);
  202. }else{
  203. CAIMEI.Alert(response.msg,'确定',false);
  204. }
  205. });
  206. },
  207. pageLinkSupplier: function(){//预览商品
  208. window.open('/supplier-'+this.shopId+'.html');
  209. },
  210. // 退出登录
  211. userLogOut: function(){
  212. localStorage.removeItem('userInfo');
  213. delBaseCookie("loginBeforePath");
  214. this.loginStatus = false;
  215. window.location.href = "/index.html";
  216. },
  217. showGlobalMenu: function(){
  218. if(!isPC){
  219. $("#mGlobalMenu").show().find(".cShow").slideDown(500);
  220. }
  221. },
  222. hideGlobalMenu: function(){
  223. if(!isPC){
  224. $("#mGlobalMenu").hide().find(".cShow").hide();
  225. }
  226. }
  227. },
  228. created: function () {
  229. var _self = this;
  230. // 判断登录状态
  231. if (globalUserData.token) {
  232. this.loginStatus = true;
  233. this.userData = globalUserData;
  234. if(this.userData.userIdentity === 3){
  235. // 获取头部通知消息数量
  236. setTimeout(function(){
  237. _self.getAuthShopCount(_self.userData.shopId);
  238. }, 1000);
  239. // 获取头部通知消息数量
  240. var timerShopCount = setInterval(function(){
  241. _self.getAuthShopCount(_self.userData.shopId);
  242. }, 1000*120);
  243. }else{
  244. setTimeout(function(){
  245. // 获取头部购物车数据
  246. _self.getHeadCart(_self.userData.userId);
  247. _self.getAuthClubCount(_self.userData.clubId);
  248. }, 1000);
  249. // 获取头部通知消息数量
  250. var timerClubCount = setInterval(function(){
  251. _self.getAuthClubCount(_self.userData.clubId);
  252. }, 1000*120);
  253. }
  254. }
  255. // 信息中心
  256. this.articleType = getUrlParam("type");
  257. },
  258. mounted:function(){
  259. var _self = this;
  260. // 初始化分类菜单效果
  261. this.initClassufyTab();
  262. var userData = JSON.parse(window.localStorage.getItem('userInfo'));
  263. if(userData!=null) {
  264. this.userIdentity = userData.userIdentity;
  265. this.shopId = userData.shopId;
  266. }
  267. if($(window).height() > 750){
  268. $(window).on('scroll', function() {
  269. var scrollTop = $(this).scrollTop();
  270. if(scrollTop>200){
  271. _self.isFiexd = true;
  272. }else{
  273. _self.isFiexd = false;
  274. }
  275. })
  276. }
  277. // 判断导航菜单高亮-对比url
  278. setTimeout(function(){
  279. var path = window.location.pathname;
  280. var paramsArr = path.split(".")[0].split("-");
  281. var key = paramsArr[0].split("/")[1];
  282. var pageId = paramsArr.length>=1 ? paramsArr[1] : '';
  283. if(key && key.length>0) {
  284. $('.navBox li').each(function () {
  285. var href = $(this).find('a').attr("href");
  286. if (href.indexOf(path)>=0){
  287. $(this).find('a').addClass('on');
  288. return false;
  289. } else if (key != 'product' && key != 'info' && href.indexOf('/'+key)>=0) {
  290. $(this).find('a').addClass('on');
  291. return false;
  292. }
  293. });
  294. }
  295. },500);
  296. }
  297. });
  298. // 初始化效果
  299. $(function(){
  300. // PC,H5切换时刷新
  301. $(window).resize(function() {
  302. var flag = (isPC && $(window).width()<=768) || (!isPC && $(window).width()>768);
  303. if(flag){
  304. window.location.reload();
  305. }
  306. });
  307. // 导航分类
  308. if(isPC){
  309. jqClickShow(".baseHeadTop .cTab", ".baseHeadTop .cShow");
  310. jqSelect(".jqSelect");
  311. jqHoverShow(".classBtn", "#mainClassify");
  312. }else{
  313. jqMultipleShow("click", ".footLink .list", ".tab", ".con", true);
  314. $('body').on('click', '.baseHeadCenter .mClassBtn',function () {
  315. $('#mainClassify').show();
  316. fixedBody();
  317. }).on('click', '#mainClassify .close,#mainClassify .mClassBtn',function () {
  318. $('#mainClassify').hide();
  319. looseBody();
  320. }).on('click', '#mainClassify',function (e) {
  321. e.stopPropagation();
  322. });
  323. }
  324. // 登录弹窗
  325. function loginAert(content, btnTxt){
  326. $.confirm({
  327. useBootstrap: false,
  328. boxWidth: (isPC?'338px':'74.6vw'),
  329. title: false,
  330. content:'<div class="loginAlert">'+content+'</div>',
  331. closeIcon: true,
  332. animation: 'opacity',
  333. closeAnimation: 'opacity',
  334. animateFromElement: false,
  335. scrollToPreviousElement: false,
  336. buttons: {
  337. login: {
  338. text: btnTxt,
  339. btnClass: 'btn-confirm-login',
  340. action: function(){
  341. setBeforeUrl();
  342. window.location.href = '/login.html';
  343. }
  344. },
  345. close: {
  346. text: '取消',
  347. btnClass: 'btn-cancel-login'
  348. }
  349. }
  350. });
  351. }
  352. // 去登录弹窗
  353. $('body').on("click", '.toLogin',function () {
  354. //loginAert('<span>你还未登录</span><span>请登录后再进行购买</span>', '去登录');
  355. setBeforeUrl();
  356. window.location.href = '/login.html';
  357. });
  358. // 退出登录
  359. $('body').on("click", '.toLogOut',function () {
  360. localStorage.removeItem('userInfo');
  361. delBaseCookie("loginBeforePath");
  362. globalHead.loginStatus = false;
  363. window.location.href = "/index.html";
  364. });
  365. // 置顶
  366. $('#scrollTop').on("click", '.toTop',function () {
  367. $("html,body").animate({scrollTop:0},500);
  368. });
  369. // 热线
  370. if(!isPC){
  371. $('#scrollTop').on("click", '.phone',function () {
  372. $('#scrollTop').find('.phoneHover').show();
  373. fixedBody();
  374. }).on("click", '.phoneHover div',function (e) {
  375. e.stopPropagation();
  376. }).on("click", '.phoneHover,.phoneHover .close',function () {
  377. $('#scrollTop').find('.phoneHover').hide();
  378. looseBody();
  379. });
  380. $('body').on("click", '#centerNav .home',function () {
  381. if($('#centerNav').find('.home').hasClass("on")){
  382. $('#centerNav').find('.home').removeClass("on");
  383. $('#centerNav').find('.centerBox').slideUp();
  384. looseBody();
  385. }else{
  386. $('#centerNav').find('.home').addClass("on");
  387. $('#centerNav').find('.centerBox').slideDown();
  388. fixedBody();
  389. }
  390. }).on("click", '#centerNav .wrap',function (e) {
  391. e.stopPropagation();
  392. }).on("click", '#centerNav .centerBox',function () {
  393. $('#centerNav').find('.home').removeClass("on");
  394. $('#centerNav').find('.centerBox').slideUp();
  395. looseBody();
  396. });
  397. }
  398. // 侧边栏滚动判断
  399. $(window).scroll(function () {
  400. if($('body').attr("style") && $('body').attr("style").indexOf('fixed')>0){
  401. return false;
  402. }
  403. var scoll = Math.max($('html').scrollTop(), $('body').scrollTop());
  404. if(scoll >200){
  405. $('#scrollTop').show();
  406. }else {
  407. $('#scrollTop').hide();
  408. }
  409. });
  410. // 顶部搜索框
  411. $('#topSearch').on("click", '.searchBtn', function(){
  412. var type = isPC ? $('#topSearch').find('[data-select]').attr("data-select")*1 : $('#topSearch').find('select').val()*1;
  413. var keyword = $.trim($('#topSearch').find('.keyword').val());
  414. // 商品,文章
  415. if (keyword.length < 2 && (type === 0 || type > 2)) {
  416. $.confirm({
  417. useBootstrap: false,
  418. boxWidth: (isPC?'300px':'70%'),
  419. title:'提示',
  420. content:'请至少输入两个关键字!',
  421. closeIcon: true,
  422. animation: 'opacity',
  423. closeAnimation: 'opacity',
  424. animateFromElement: false,
  425. buttons: {
  426. close: {
  427. text: '确定',
  428. btnClass: 'btn-confirm'
  429. }
  430. }
  431. });
  432. return false;
  433. }
  434. if (type === 0) {
  435. window.location.href = '/product/list.html?keyword='+encodeURIComponent(keyword);
  436. } else if(type === 1) {
  437. // 供应商
  438. if( keyword === '' ){
  439. window.location.href = '/supplier/list.html';
  440. }else{
  441. window.location.href = '/supplier/list.html?keyword='+encodeURIComponent(keyword);
  442. }
  443. } else if (type === 2) {
  444. // 项目仪器
  445. if( keyword === '' ){
  446. window.location.href = '/equipment/list.html';
  447. }else{
  448. window.location.href = '/equipment/list.html?keyword=' + encodeURIComponent(keyword);
  449. }
  450. } else{
  451. if( keyword === '' ){
  452. $.confirm({
  453. useBootstrap: false,
  454. boxWidth: (isPC?'300px':'70%'),
  455. title:'提示',
  456. content:'请至少输入文章关键字!',
  457. closeIcon: true,
  458. animation: 'opacity',
  459. closeAnimation: 'opacity',
  460. animateFromElement: false,
  461. buttons: {
  462. close: {
  463. text: '确定',
  464. btnClass: 'btn-confirm'
  465. }
  466. }
  467. });
  468. return;
  469. }
  470. // 文章
  471. window.location.href = '/info/search-1.html?keyword=' + encodeURIComponent(keyword);
  472. }
  473. }).on('keyup', '.keyword', function(event) {
  474. // 搜索输入框 按回车键搜索
  475. var keyCode = event.keyCode || event.which;
  476. if(keyCode === 13){
  477. $(this).parents('#topSearch').find('.searchBtn').trigger("click");
  478. }
  479. }).on("click", '.hotKey .word', function(){
  480. // 点击热词 触发搜索
  481. $(this).parents('#topSearch').find('.keyword').val($(this).text());
  482. $(this).parents('#topSearch').find('.searchBtn').trigger("click");
  483. });
  484. // 支付弹框复制标识
  485. $('body').on("click", '.payAlert .copy',function () {
  486. var oInput = document.createElement('textarea');
  487. oInput.value = $(this).parents('.payAlert').find('.no').text();
  488. document.body.appendChild(oInput);
  489. oInput.select(); // 选择对象
  490. document.execCommand("Copy"); // 执行浏览器复制命令
  491. document.body.removeChild(oInput);
  492. CAIMEI.dialog('复制成功');
  493. });
  494. });
  495. /*function uuid() {
  496. var s = [];
  497. var hexDigits = "0123456789abcdef";
  498. for (var i = 0; i < 36; i++) {
  499. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  500. }
  501. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  502. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  503. s[8] = s[13] = s[18] = s[23] = "-";
  504. return s.join("");
  505. }*/
  506. function setBeforeUrl(){
  507. var baseUrl = window.location.href;
  508. var flag = baseUrl.indexOf('/login.html')>0 || baseUrl.indexOf('/register.html')>0 || baseUrl.indexOf('/we_chat/redirect_uri.html')>0;
  509. if(!flag){
  510. setBaseCookie("loginBeforePath", baseUrl);
  511. }
  512. }
  513. // 公共方法
  514. function setBaseCookie(cname,cvalue,exdays){
  515. if (exdays) {
  516. var d = new Date();
  517. d.setTime(d.getTime()+(exdays*24*60*60*1000));
  518. var expires = "expires="+d.toGMTString();
  519. document.cookie = cname+"="+cvalue+"; path=/; "+expires;
  520. } else {
  521. document.cookie = cname+"="+cvalue+"; path=/";
  522. }
  523. }
  524. function getBaseCookie(cname){
  525. if (document.cookie) {
  526. var name = cname + "=";
  527. var ca = document.cookie.split(';');
  528. for(var i=0; i<ca.length; i++) {
  529. var c = ca[i].trim();
  530. if (c && c.indexOf(name)===0) {
  531. return c.substring(name.length,c.length);
  532. }
  533. }
  534. }
  535. return "";
  536. }
  537. function delBaseCookie(cname){
  538. var exp = new Date();
  539. exp.setTime(exp.getTime() - 1);
  540. var cval=getBaseCookie(cname);
  541. if(cval!=null) {
  542. document.cookie= cname + "="+cval+";expires="+exp.toGMTString();
  543. }
  544. }
  545. function getDateStr(dd, addCount) {
  546. dd.setDate(dd.getDate() + addCount);//获取addCount天后的日期
  547. var y = dd.getFullYear();
  548. var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);//获取当前月份的日期,不足10补0
  549. var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  550. return y + "/" + m + "/" + d;
  551. }
  552. function jqClickShow(tab, con){
  553. $("body").on("click", tab, function(e){
  554. e.stopPropagation();
  555. if($(tab).hasClass("on")){
  556. $(tab).removeClass("on");
  557. $(con).slideUp("fast");
  558. }else{
  559. $(tab).addClass("on");
  560. $(con).slideDown("fast");
  561. }
  562. }).on("click", function(){
  563. $(tab).removeClass("on");
  564. $(con).slideUp("fast");
  565. });
  566. }
  567. function jqHoverShow(tab, con){
  568. $("body").on("mouseover", tab, function(e){
  569. e.stopPropagation();
  570. if($(tab).hasClass("on")){
  571. $(tab).removeClass("on");
  572. $(con).slideUp("fast");
  573. }else{
  574. $(tab).addClass("on");
  575. $(con).slideDown("fast");
  576. }
  577. }).on("mouseover", con, function(e){
  578. e.stopPropagation();
  579. }).on("mouseover", function(e){
  580. $(tab).removeClass("on");
  581. $(con).slideUp("fast");
  582. });
  583. }
  584. function jqMultipleShow(event, item, tab, con, stop){
  585. $("body").on(event, item+' '+tab, function(e){
  586. e.stopPropagation();
  587. $(this).parents(item).siblings(item).find(con).slideUp("fast");
  588. $(this).parents(item).siblings(item).removeClass("on");
  589. $(this).parents(item).siblings(item).find(tab).removeClass("on");
  590. if($(this).hasClass("on")){
  591. $(this).removeClass("on");
  592. $(this).parents(item).removeClass("on");
  593. $(this).parents(item).find(con).slideUp("fast");
  594. }else{
  595. $(this).addClass("on");
  596. $(this).parents(item).addClass("on");
  597. $(this).parents(item).find(con).slideDown("fast");
  598. }
  599. }).on(event, item+' '+con, function(e){
  600. e.stopPropagation();
  601. }).on(event, function(){
  602. if(stop){
  603. $(item+' '+tab).removeClass("on");
  604. $(item).removeClass("on");
  605. $(item+' '+con).slideUp("fast");
  606. }
  607. });
  608. }
  609. function jqSelect(selectBox){
  610. $("body").on("click", selectBox, function(e){
  611. e.stopPropagation();
  612. var select = $(this).find("select");
  613. var options = select.find("option");
  614. var list = "";
  615. for (var i=0; i<options.length; i++){
  616. list += '<li data-id="'+$(options[i]).val()+'">'+$(options[i]).text()+'</li>';
  617. if($(options[i]).val()===select.val()){
  618. $(this).find("[data-select]").text($(options[i]).text()).attr('data-select',$(options[i]).val());
  619. }
  620. }
  621. $(this).find("ul").html(list);
  622. $(this).find("ul").slideToggle("fast");
  623. }).on("click", selectBox + " li", function(e){
  624. e.stopPropagation();
  625. $(this).parent().prev().attr('data-select',$(this).attr("data-id")).text($(this).text());
  626. $(this).parents("pc").next().val($(this).attr("data-id"));
  627. $(this).parent().slideUp("fast");
  628. }).on("click", function(){
  629. $(selectBox).find("ul").slideUp("fast");
  630. });
  631. }
  632. //解决移动端滚动穿透
  633. function fixedBody(){
  634. var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  635. setTimeout(function(){
  636. document.body.style.cssText += 'position:fixed;top:-'+scrollTop+'px;';
  637. },10);
  638. }
  639. function looseBody() {
  640. var body = document.body;
  641. body.style.position = '';
  642. var top = body.style.top;
  643. document.body.scrollTop = document.documentElement.scrollTop = -parseInt(top);
  644. body.style.top = '';
  645. }
  646. //url参数
  647. function getUrlParam(name) {
  648. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  649. var r = decodeURIComponent(window.location.search).substr(1).match(reg);
  650. if (r != null) {
  651. return (r[2])
  652. }
  653. return null;
  654. }
  655. function updateUrlParam(params) {
  656. var loca = window.location;
  657. var baseUrl = loca.origin + loca.pathname + "?";
  658. var query = loca.search.substr(1).toLowerCase();
  659. var queryArr = query.split("&");
  660. var keys = Object.keys(params);
  661. if (keys.length > 0) {
  662. var obj = {};
  663. for (var i = 0; i < queryArr.length; i++) {
  664. queryArr[i] = queryArr[i].split("=");
  665. obj[queryArr[i][0]] = queryArr[i][1]
  666. }
  667. for (var k = 0; k < keys.length; k++) {
  668. var name = keys[k];
  669. var value = params[name];
  670. obj[name.toLowerCase()] = value;
  671. }
  672. var url = baseUrl + JSON.stringify(obj).replace(/["\{\}]/g, "").replace(/\:/g, "=").replace(/\,/g, "&");
  673. return url;
  674. }
  675. }
  676. function setSearchProductList(list, userId, callback) {//处理搜索
  677. var productIdArr = [];
  678. var resultData = [];
  679. list.map(function (item) {
  680. resultData.push({
  681. productId: item.productId,
  682. name: item.name,
  683. image: item.image,
  684. unit: item.unit,
  685. brandName: item.brandName,
  686. priceFlag: item.priceFlag,
  687. priceGrade: item.priceGrade,
  688. actStatus: 0,
  689. priceLoaded: false,
  690. beautyActFlag:item.beautyActFlag,
  691. productType:item.productType
  692. });
  693. // 0公开价格 1不公开价格 2仅对会员机构公开
  694. if (item.priceFlag !== 1) {
  695. productIdArr.push(item.productId)
  696. }
  697. });
  698. if (userId && userId > 0) {
  699. setProductPrice(resultData, productIdArr.join(","), userId, callback);
  700. }else {
  701. setProductPrice(resultData, productIdArr.join(","), 0, callback);
  702. }
  703. return resultData;
  704. }
  705. function setProductPrice(productList, productIds, userId, callback){
  706. $.getJSON(coreServer+"/commodity/price/list",{
  707. userId: userId,
  708. productIds: productIds,
  709. source: 1
  710. }).done(function (r) {
  711. if (r.code === 0 && r.data) {
  712. var priceList = r.data;
  713. productList.map(function(product){
  714. priceList.map(function(item){
  715. if(product.productId*1 === item.productId){
  716. product.shopId = item.shopId;
  717. product.userIdentity = item.userIdentity;
  718. product.price = item.price;
  719. product.originalPrice = item.originalPrice;
  720. product.priceLoaded = true;
  721. product.ladderPriceFlag = item.ladderPriceFlag;
  722. product.actStatus =item.actStatus;
  723. product.couponsLogo =item.couponsLogo;
  724. product.promotions =item.promotions;
  725. product.svipPriceTag =item.svipPriceTag;
  726. product.svipPriceType =item.svipPriceType;
  727. product.svipProductFlag =item.svipProductFlag;
  728. }
  729. });
  730. });
  731. }
  732. return callback();
  733. }).fail(function(){
  734. return callback();
  735. })
  736. }
  737. // 登录后返回登录前页面
  738. function toBeforePath(){
  739. var loginBeforePath = getBaseCookie("loginBeforePath");
  740. if (loginBeforePath && loginBeforePath!='undefined' && loginBeforePath.indexOf('.html')>0) {
  741. delBaseCookie("loginBeforePath");
  742. window.location.href = loginBeforePath;
  743. }
  744. window.location.href = '/index.html';
  745. }
  746. function toLogin() {//去登录
  747. setBeforeUrl();
  748. window.location.href = '/login.html';
  749. }
  750. function upgrade () { //升级会员
  751. window.location.href='';
  752. }
  753. function toLogOut() {//登出
  754. localStorage.removeItem('userInfo');
  755. delBaseCookie("loginBeforePath");
  756. this.loginStatus = false;
  757. window.location.href = "/index.html";
  758. }
  759. /**
  760. * 至少保留两位小数
  761. * 10 => "10.00"
  762. * @param {number} num
  763. */
  764. function toFloat(num) {
  765. var temp = String(num).split('.');
  766. if (temp.length === 1 || temp[1].length < 2) {
  767. return Number(num).toFixed(2);
  768. }else{
  769. return Number(num);
  770. }
  771. }
  772. /** 表单输入框校验 */
  773. function verifyHandle(obj){
  774. if ($(obj).is(":hidden")) { return false; }
  775. var v = $.trim($(obj).val()).replace(/\s+/g,"");
  776. var reg = $(obj).attr("rule");
  777. var errorTxt = $(obj).siblings('.errTips').attr("tips");
  778. if ($.trim(v) === '') {
  779. if ($(obj).attr("placeholder") && $(obj).attr("placeholder").indexOf("请输入")>=0) {
  780. errorTxt = $(obj).attr("placeholder").split("请输入")[1]+'不能为空';
  781. } else {
  782. errorTxt = '不能为空';
  783. }
  784. setErrorTxt(obj, errorTxt);
  785. return false;
  786. }else if (typeof reg != "undefined") {
  787. reg = new RegExp(reg);
  788. if (!reg.test(v)) {
  789. setErrorTxt(obj, errorTxt);
  790. return false;
  791. }
  792. }
  793. clearErrorTxt(obj);
  794. }
  795. function setErrorTxt(obj, txt) {
  796. $(obj).parent().addClass("error").find('.checked').removeClass("show");
  797. $(obj).siblings('.errTips').text(txt).addClass("show");
  798. }
  799. function clearErrorTxt(obj) {
  800. $(obj).siblings('.errTips').removeClass("show");
  801. $(obj).parent().removeClass("error").find('.checked').addClass("show");
  802. }
  803. // 提交时验证表单
  804. function verifyForm() {
  805. $("[needverify]:visible").each(function (index, ele) {
  806. if (!$(ele).siblings('.checked').hasClass("show")) {
  807. verifyHandle(ele);
  808. }
  809. });
  810. var len = $('.errTips.show:visible').length;
  811. return (len === 0);
  812. }
  813. // 单个验证表单
  814. function verifyCheack(ele) {
  815. if (!$(ele).siblings('.checked').hasClass("show")) {
  816. verifyHandle(ele);
  817. }
  818. var len = $(ele).siblings('.errTips.show:visible').length;
  819. return (len === 0);
  820. }
  821. // 公共信息提示弹框
  822. function alertInfo(txt,callback) {
  823. $.confirm({
  824. useBootstrap: false,
  825. boxWidth: (isPC?'300px':'70%'),
  826. title:'提示',
  827. content: txt,
  828. closeIcon: true,
  829. animation: 'opacity',
  830. closeAnimation: 'opacity',
  831. animateFromElement: false,
  832. buttons: {
  833. close: {
  834. text: '确定',
  835. btnClass: 'btn-confirm',
  836. action: function () {
  837. callback()
  838. }
  839. }
  840. }
  841. });
  842. }
  843. //公共吐司
  844. function dialog(txt,callback) {
  845. $.confirm({
  846. title: false,
  847. content: txt,
  848. boxWidth: (isPC?'300px':'70%'),
  849. autoClose: 'close|2000',
  850. useBootstrap:false,
  851. buttons: {
  852. close:{
  853. isHidden: true,
  854. action: function () {
  855. callback()
  856. }
  857. }
  858. }
  859. });
  860. }
  861. /** 时间格式化
  862. * @param {Date} date 标准时间格式 -> new Date()
  863. * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
  864. * @returns {string} 格式化后的时间 '2017-01-01 01:00:00'
  865. */
  866. function dateFormat(date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') {
  867. var o = {
  868. 'M+': date.getMonth() + 1, // month
  869. 'd+': date.getDate(), // day
  870. 'h+': date.getHours(), // hour
  871. 'm+': date.getMinutes(), // minute
  872. 's+': date.getSeconds(), // second
  873. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  874. S: date.getMilliseconds(), // millisecond
  875. };
  876. if (/(y+)/.test(format)) {
  877. format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  878. }
  879. for (var k in o) {
  880. if (new RegExp('(' + k + ')').test(format)) {
  881. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  882. }
  883. }
  884. return format
  885. }