index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. var homeData = new Vue({
  2. el: '#container',
  3. data: {
  4. userId:0,
  5. asideNav: [],
  6. liveList:[],//右侧直播模块
  7. cmImageList:[],//右侧活动模块
  8. infoList:[],//右侧文章模块
  9. popupFlag:false
  10. },
  11. filters: {
  12. NumFormat: function(value) {//处理金额
  13. return Number(value).toFixed(2);
  14. },
  15. },
  16. methods: {
  17. // 美博会弹窗链接
  18. popupPage:function(){
  19. this.popupFlag=false;
  20. window.location.href='/activity/activityTopic.html?id=306&name=美博会';
  21. localStorage.setItem('lockTime',Date.now());
  22. window.localStorage.setItem('isActivityStatus',true);
  23. },
  24. // 关闭美博会弹窗
  25. closePopup:function(){
  26. this.popupFlag=false;
  27. $('.Popup').slideUp(100);
  28. localStorage.setItem('lockTime',Date.now());
  29. localStorage.setItem('isActivityStatus',true);
  30. },
  31. diffTime:function(time){
  32. var date = Date.now();
  33. console.log('缓存时间戳',time);
  34. console.log('当前时间戳',date);
  35. console.log('计算时间戳',(date-time));
  36. return (date -time) < 2*60*1000 ? false : true
  37. },
  38. // 页面主图轮播
  39. swiperBanner: function(){
  40. setTimeout(function(){
  41. if (isPC) {
  42. $('#swiper-container').slide({
  43. mainCell:".swiper-wrapper-banner",
  44. titCell:".swiper-pagination-banner span",
  45. effect: "leftLoop",
  46. prevCell:".swiper-button-prev",
  47. nextCell:".swiper-button-next",
  48. interTime: 3000,
  49. autoPlay: true,
  50. autoPage: false,
  51. trigger: "mouseover"
  52. });
  53. } else {
  54. var swiper = new Swiper('#swiper-container', {
  55. loop : true,
  56. autoplay: {
  57. delay: 3000,
  58. disableOnInteraction: false
  59. },
  60. navigation: {
  61. nextEl: '.swiper-button-next',
  62. prevEl: '.swiper-button-prev'
  63. },
  64. pagination: {
  65. el: '.swiper-pagination',
  66. clickable :true
  67. }
  68. });
  69. }
  70. },300);
  71. },
  72. GetHomeRightData: function(){
  73. var _self = this;
  74. PublicApi.GetHomeRightData({ source: 1 },function(response){
  75. if(response.code == 0){
  76. var data = response.data;
  77. _self.liveList = data.liveList;
  78. _self.cmImageList = data.cmImageList;
  79. _self.infoList = data.infoList;
  80. }else{
  81. CAIMEI.Alert(response.msg, '确定');
  82. }
  83. });
  84. },
  85. setAsideNav: function(){
  86. var _self = this;
  87. if (isPC) {
  88. var titArr = $('.section_page_title').find('h1');
  89. _self.asideNav = [];
  90. for (var i=0; i<titArr.length; i++){
  91. _self.asideNav.push({
  92. id: $(titArr[i]).attr("data-id"),
  93. value: $(titArr[i]).text()
  94. })
  95. }
  96. }
  97. },
  98. // 商品楼层轮播
  99. swiperFloor: function(){
  100. setTimeout(function(){
  101. // 图片懒加载
  102. $("img[data-original]").lazyload();
  103. // 商品轮播
  104. if (isPC) {
  105. var cell = $(".swiper-pagination-floor");
  106. var size = Math.ceil(cell.attr("data-id")/5);
  107. var span = '';
  108. for (var i = 0; i < size; i++) {
  109. span += '<span></span>';
  110. }
  111. cell.html(span);
  112. $('#recommendBox').slide({
  113. mainCell:".recommendBox-wrapper",
  114. titCell:".swiper-pagination-floor span",
  115. effect: "leftLoop",
  116. autoPlay: false,
  117. scroll:5,
  118. vis:5
  119. });
  120. } else {
  121. var swiper = new Swiper('#recommendBox', {
  122. slidesPerView: 2,
  123. slidesPerColumn: 2,
  124. spaceBetween:0,
  125. slidesPerGroup: 2,
  126. autoplay: {
  127. delay: 3000,
  128. disableOnInteraction: false
  129. },
  130. pagination: {
  131. el: '.swiper-pagination'
  132. }
  133. });
  134. }
  135. },500);
  136. },
  137. showSubNav: function(event){
  138. var el = event.currentTarget;
  139. var parent = $(el).parent();
  140. parent.siblings().removeAttr("style").removeClass("on").find(".navItem").hide();
  141. if (parent.hasClass("on")) {
  142. parent.removeAttr("style").removeClass("on").find(".navItem").slideUp(200);
  143. } else {
  144. var height = parent.height();
  145. parent.addClass("on").find(".navItem").slideDown(200);
  146. if (parent.find(".navItem").length>0) {
  147. setTimeout(function(){
  148. height += parent.find(".navItem").height();
  149. parent.css("height",height);
  150. },200);
  151. }
  152. }
  153. },
  154. // 设置页面查看更多按钮显示
  155. setReadeMore: function(){
  156. $(".section_page_more").each(function(){
  157. var id = $(this).attr('data-id')*1;
  158. var size = $(this).attr('data-size')*1;
  159. var el = $(this);
  160. var readMore = false;
  161. if (isPC) {
  162. if([1,3].indexOf(id) !== -1 && size>7) {
  163. readMore = true;
  164. }
  165. if([2,4,7].indexOf(id) !== -1 && size>5) {
  166. readMore = true;
  167. }
  168. if(5 === id && size>10) {
  169. readMore = true;
  170. }
  171. } else {
  172. if([1,3].indexOf(id) !== -1 && size>3) {
  173. readMore = true;
  174. }
  175. if([2,4].indexOf(id) !== -1 && size>2) {
  176. readMore = true;
  177. }
  178. if(5 === id && size>4) {
  179. readMore = true;
  180. }
  181. }
  182. if (readMore) {
  183. el.addClass("show");
  184. } else {
  185. el.parents(".section_page").find(".section_page_main").addClass("max");
  186. }
  187. });
  188. },
  189. // 点击查看更多
  190. showMoreItem: function(e){
  191. var el = e.currentTarget;
  192. $(el).parents(".section_page").find(".section_page_main").addClass("max");
  193. $(el).parents(".section_page_more").hide();
  194. },
  195. // 设置页面商品价格
  196. setPriceShown: function(){
  197. var productIds = [];
  198. var userId = GLOBAL_USER_ID;
  199. var shopId = GLOBAL_SHOP_ID;
  200. var userIdentity = GLOBAL_USER_IDENTITY;
  201. $(".main_price_text").each(function(){
  202. var id = $(this).attr('data-id');
  203. if (productIds.indexOf(id) === -1) {
  204. productIds .push(id);
  205. }
  206. });
  207. // 获取商品价格列表
  208. $.getJSON(coreServer+"/commodity/price/list",{
  209. userId: GLOBAL_USER_ID,
  210. productIds: productIds.join(',')
  211. }).done(function (res) {
  212. if (res.code === 0 && res.data) {
  213. var priceList = res.data;
  214. $(".main_price_none").hide();
  215. $(".price_grade_club").hide();
  216. $(".price_grade_shop").hide();
  217. $(".main_price_login").show();
  218. priceList.map(function(price){
  219. // 用户身份 0、个人 1、协销 2、会员机构 3、供应商 4,普通机构
  220. // priceFlag: 0公开价格 1不公开价格 2仅对会员机构公开
  221. var parent = $(".main_price_text.product_"+ price.productId);
  222. var priceFlag = parent.find(".main_price_login").attr('data-id')*1;
  223. if (userIdentity===2 || (priceFlag === 0 && userIdentity===4 ) || (userIdentity===3 && shopId === price.shopId*1)) {
  224. parent.find(".price_o_num").text(Number(price.originalPrice).toFixed(2));
  225. parent.find(".price_num").text(Number(price.price).toFixed(2));
  226. if(price.promotions && price.promotions.type*1===1 && price.promotions.mode*1===1){
  227. parent.find(".main_price_show").hide();
  228. parent.find(".main_price_show.none").show();
  229. }
  230. } else if (userIdentity===4 && priceFlag === 2){
  231. parent.find(".main_price_show").hide();
  232. parent.find(".price_grade_club").show();
  233. } else {
  234. parent.find(".main_price_show").hide();
  235. parent.find(".price_grade_shop").show();
  236. }
  237. });
  238. }
  239. });
  240. }
  241. },
  242. created: function() {
  243. if(GLOBAL_TOKEN){
  244. this.userId = GLOBAL_USER_ID;
  245. }
  246. },
  247. mounted: function() {
  248. var _this = this;
  249. // 页面主图轮播
  250. this.swiperBanner();
  251. // 设置页面查看更多
  252. this.setReadeMore();
  253. // 推荐专区商品轮播
  254. this.swiperFloor();
  255. // 设置右侧导航
  256. this.setAsideNav();
  257. // 获取设置商品价格
  258. if (GLOBAL_USER_ID) {
  259. this.setPriceShown();
  260. }
  261. // 侧边栏滚动判断
  262. var distance = 0;
  263. $(window).scroll(function () {
  264. if($('body').attr("style") && $('body').attr("style").indexOf('fixed')>0){
  265. return false;
  266. }
  267. var scrollSize = Math.max($('html').scrollTop(), $('body').scrollTop());
  268. if(scrollSize >300){
  269. $('#sideNav').show();
  270. if(isPC){
  271. // $('#advertising').show();
  272. // 鼠标滚动楼层
  273. setTimeout(function () {
  274. distance = scrollSize;
  275. },0);
  276. // 侧边导航栏高亮
  277. $('.section_page_title').each(function (i,e) {
  278. //每个元素距离顶部的距离
  279. var top = $(e).offset().top;
  280. if(scrollSize > distance){
  281. if(scrollSize >= (top-300)){
  282. $('#sideNav').find('a').eq(i).addClass('on').siblings().removeClass("on");
  283. }
  284. }else {
  285. if(scrollSize >= (top-600)){
  286. $('#sideNav').find('a').eq(i).addClass('on').siblings().removeClass("on");
  287. }
  288. }
  289. });
  290. // 侧边模块固定 fixed
  291. if($('#fixed-wrap').length>0) {
  292. var tocWrap = $('#fixed-wrap'),
  293. changeSize = $("#globalHead").outerHeight() + $("#swiper-container").outerHeight() + $("#sidebar").outerHeight();
  294. //changeSize = document.getElementById("header").offsetHeight + document.getElementById("sidebar").offsetHeight;
  295. scrollSize >= changeSize ? tocWrap.addClass("fixed") : tocWrap.removeClass("fixed");
  296. }
  297. }
  298. }else {
  299. $('#sideNav').hide();
  300. $('#advertising').hide();
  301. }
  302. });
  303. // 侧边导航
  304. setTimeout(function(){
  305. $('#sideNav').on("click", 'a[data-id]',function () {
  306. var id = $(this).attr('data-id');
  307. var floorScroll = $('.section_page_title [data-id='+id+']').offset().top-200;
  308. $('html,body').animate({scrollTop: floorScroll},600);
  309. });
  310. },2000);
  311. //关闭广告图
  312. $('#advertising').on('click','.close',function () {
  313. $(this).parents('.item').hide();
  314. });
  315. var isActivityStatus = localStorage.getItem('isActivityStatus');
  316. //新加优惠券弹窗
  317. if(isActivityStatus){
  318. var lockTime = localStorage.getItem('lockTime');
  319. _this.popupFlag = this.diffTime(lockTime);
  320. }else{
  321. setTimeout(function () {
  322. _this.popupFlag = true;
  323. },1000);
  324. }
  325. }
  326. });