index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. var homeData = new Vue({
  2. el: '#container',
  3. data: {
  4. userId:0,
  5. images: [],
  6. recommends: [],
  7. recommendPage: 1,
  8. pageFloors: [],
  9. advertising: [],
  10. asideNav: [],
  11. productIdArr:[],
  12. },
  13. methods: {
  14. getBanners: function(){
  15. var _self = this;
  16. $.getJSON("/home/banner").done(function (r) {
  17. if (r.code === 0 && r.data) {
  18. _self.images = r.data;
  19. setTimeout(function(){
  20. if (isPC) {
  21. $('#swiper-container').slide({
  22. mainCell:".swiper-wrapper"
  23. ,titCell:".swiper-pagination span"
  24. ,effect: "leftLoop"
  25. ,prevCell:".swiper-button-prev"
  26. ,nextCell:".swiper-button-next"
  27. ,interTime: 2000
  28. ,autoPlay: true
  29. ,autoPage: false
  30. ,trigger: "mouseover"
  31. });
  32. } else {
  33. var swiper = new Swiper('#swiper-container', {
  34. loop : true,
  35. autoplay: {
  36. delay: 2000,
  37. disableOnInteraction: false
  38. },
  39. navigation: {
  40. nextEl: '.swiper-button-next',
  41. prevEl: '.swiper-button-prev'
  42. },
  43. pagination: {
  44. el: '.swiper-pagination',
  45. clickable :true
  46. }
  47. });
  48. }
  49. },500);
  50. }
  51. });
  52. },
  53. getRecommends: function(){
  54. var _self = this;
  55. $.getJSON("/home/recommend").done(function (r) {
  56. if (r.code === 0 && r.data) {
  57. _self.recommends = r.data;
  58. _self.recommendPage = Math.ceil(r.data.length / 4);
  59. setTimeout(function(){
  60. if (isPC) {
  61. $('#recommendBox').slide({
  62. mainCell:".swiper-wrapper"
  63. ,titCell:".swiper-pagination span"
  64. ,effect: "leftLoop"
  65. ,interTime: 3000
  66. ,autoPlay: true
  67. ,scroll:4
  68. ,vis:4
  69. ,trigger: "mouseover"
  70. });
  71. } else {
  72. var swiper = new Swiper('#recommendBox', {
  73. slidesPerView: 2,
  74. slidesPerColumn: 2,
  75. spaceBetween: 0,
  76. slidesPerGroup: 2,
  77. autoplay: {
  78. delay: 3000,
  79. disableOnInteraction: false
  80. },
  81. pagination: {
  82. el: '.swiper-pagination'
  83. }
  84. });
  85. }
  86. },500);
  87. // 获取价格
  88. // if(_self.userId && _self.userId>0){
  89. var productIdArr = [];
  90. _self.recommends.map(function (item) {
  91. // 0公开价格 1不公开价格 2仅对会员机构公开
  92. if (item.priceFlag !== 1) {
  93. productIdArr.push(item.id)
  94. }
  95. });
  96. setProductPrice(_self.recommends, productIdArr.join(","), _self.userId, function () {});
  97. console.log(_self.recommends)
  98. // }
  99. }
  100. });
  101. },
  102. getFloorData: function(){
  103. var _self = this;
  104. $.getJSON("/home/floor").done(function (r) {
  105. if (r.code === 0 && r.data) {
  106. _self.pageFloors = r.data;
  107. setTimeout(function(){
  108. // 设置侧边导航数据
  109. _self.setAsideNav();
  110. // 商品专题
  111. if (isPC) {
  112. $('#goodsTopic').slide({
  113. mainCell:".swiper-wrapper"
  114. ,titCell:".swiper-pagination span"
  115. ,effect: "leftLoop"
  116. ,prevCell:".swiper-button-prev"
  117. ,nextCell:".swiper-button-next"
  118. ,interTime: 2000
  119. ,autoPlay: true
  120. ,autoPage: false
  121. ,trigger: "mouseover"
  122. });
  123. } else {
  124. var swiper = new Swiper('#goodsTopic', {
  125. loop : true,
  126. autoplay: {
  127. delay: 2000,
  128. disableOnInteraction: false
  129. },
  130. navigation: {
  131. nextEl: '.swiper-button-next',
  132. prevEl: '.swiper-button-prev'
  133. },
  134. pagination: {
  135. el: '.swiper-pagination',
  136. clickable :true
  137. }
  138. });
  139. }
  140. },500)
  141. }
  142. });
  143. },
  144. getAdvertising: function(){
  145. var _self = this;
  146. $.getJSON("/home/advertising").done(function (r) {
  147. if (r.code === 0 && r.data) {
  148. _self.advertising = r.data;
  149. }
  150. });
  151. },
  152. setAsideNav: function(){
  153. var _self = this;
  154. var titArr = $('.floorTit').find('h2,h3');
  155. for (var i=0; i<titArr.length; i++){
  156. _self.asideNav.push({
  157. id: $(titArr[i]).attr("data-id"),
  158. value: $(titArr[i]).text()
  159. })
  160. }
  161. },
  162. showSubNav: function(event){
  163. var el = event.currentTarget;
  164. var parent = $(el).parent();
  165. parent.siblings().removeAttr("style").removeClass("on").find(".navItem").hide();
  166. if (parent.hasClass("on")) {
  167. parent.removeAttr("style").removeClass("on").find(".navItem").slideUp(200);
  168. } else {
  169. var height = parent.height();
  170. parent.addClass("on").find(".navItem").slideDown(200);
  171. if (parent.find(".navItem").length>0) {
  172. setTimeout(function(){
  173. height += parent.find(".navItem").height();
  174. parent.css("height",height);
  175. },200);
  176. }
  177. }
  178. }
  179. },
  180. created: function() {
  181. var userInfo = localStorage.getItem('userInfo');
  182. if(userInfo){
  183. this.userId = JSON.parse(userInfo).userId;
  184. }
  185. this.getBanners();
  186. this.getRecommends();
  187. this.getFloorData();
  188. },
  189. mounted: function() {
  190. this.getAdvertising();
  191. // 侧边栏滚动判断
  192. var distance = 0;
  193. $(window).scroll(function () {
  194. if($('body').attr("style") && $('body').attr("style").indexOf('fixed')>0){
  195. return false;
  196. }
  197. var scoll = Math.max($('html').scrollTop(), $('body').scrollTop());
  198. if(scoll >200){
  199. $('#sideNav').show();
  200. if(isPC){
  201. $('#advertising').show();
  202. // 鼠标滚动楼层
  203. setTimeout(function () {
  204. distance = scoll;
  205. },0);
  206. $('.floorTit').each(function (i,e) {
  207. //每个元素距离顶部的距离
  208. var top = $(e).offset().top;
  209. if(scoll > distance){
  210. if(scoll >= (top-300)){
  211. $('#sideNav').find('a').eq(i).addClass('on').siblings().removeClass("on");
  212. }
  213. }else {
  214. if(scoll >= (top-600)){
  215. $('#sideNav').find('a').eq(i).addClass('on').siblings().removeClass("on");
  216. }
  217. }
  218. });
  219. }
  220. }else {
  221. $('#sideNav').hide();
  222. $('#advertising').hide();
  223. }
  224. });
  225. // 置顶
  226. $('#sideNav').on("click", '.toTop',function () {
  227. $("html,body").animate({scrollTop:0},500);
  228. });
  229. // 侧边导航
  230. $('#sideNav').on("click", 'a[data-id]',function () {
  231. var id = $(this).attr('data-id');
  232. var floorScroll = $('.floorTit [data-id='+id+']').offset().top;
  233. $('html,body').animate({scrollTop: floorScroll},600);
  234. });
  235. //关闭广告图
  236. $('#advertising').on('click','.close',function () {
  237. $(this).parents('.item').hide();
  238. })
  239. }
  240. });