base.js 29 KB

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