base.js 32 KB

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