浏览代码

h5商城完成

yuhao 2 年之前
父节点
当前提交
7f6bccd388
共有 3 个文件被更改,包括 107 次插入47 次删除
  1. 1 1
      src/api/index.js
  2. 105 46
      src/views/home/index.vue
  3. 1 0
      src/views/swiper/index.vue

+ 1 - 1
src/api/index.js

@@ -25,7 +25,7 @@ export const getGoodList = (params) => {
25 25
     })
26 26
 }
27 27
 // 获取商品详情
28
-export const getGoodInfo = (params) => {
28
+export const getGoodInfoList = (params) => {
29 29
     return request({
30 30
         url: '/v2/api-h5/goods/info',
31 31
         method: 'get',

+ 105 - 46
src/views/home/index.vue

@@ -8,6 +8,7 @@
8 8
           center-active
9 9
           color="#FA7D22"
10 10
           height="120"
11
+          v-model="categoryIndex"
11 12
         >
12 13
           <v-tab
13 14
             @click="selectTab(item)"
@@ -22,7 +23,6 @@
22 23
     <div class="swiper-container">
23 24
       <div class="swiper-wrapper">
24 25
         <div
25
-          @click="selectGood(item)"
26 26
           v-for="(item, index) in goodList"
27 27
           :key="index"
28 28
           class="swiper-slide"
@@ -106,7 +106,7 @@
106 106
         <div class="content">嫩滑细腻 紧实肌肤</div>
107 107
       </div> -->
108 108
       <div class="ingredient-content">
109
-        <div class="content">{{ goodInfo.component }}</div>
109
+        <div class="content" v-html="goodInfo.component"></div>
110 110
       </div>
111 111
     </div>
112 112
   </div>
@@ -114,7 +114,12 @@
114 114
 
115 115
 <script>
116 116
 var Swiper = require("../../utils/swiper-bundle.min");
117
-import { getBanner, getCategory, getGoodList, getGoodInfo } from "../../api";
117
+import {
118
+  getBanner,
119
+  getCategory,
120
+  getGoodList,
121
+  getGoodInfoList,
122
+} from "../../api";
118 123
 import QRCode from "qrcodejs2";
119 124
 export default {
120 125
   components: {
@@ -130,16 +135,23 @@ export default {
130 135
       isOver: false,
131 136
       isSkinOver: false,
132 137
       goods_id: "", //商品ID
138
+      page: 1,
139
+      total: 0,
140
+      category_id: "", //分类ID
141
+      Myswiper: "", //swiper实例
142
+      categoryIndex: 0,
143
+      category_id_act: "",
133 144
     };
134 145
   },
135 146
   created() {
136 147
     // 获取banner传过来的商品ID
137 148
     this.goods_id = this.$route.query.goods_id;
149
+    // 获取banner传过来的商品分类
150
+    this.category_id_act = this.$route.query.category_id;
138 151
     // 获取商品分类列表
139 152
     this.getCategory();
140 153
   },
141 154
   mounted() {
142
-    this.swiper();
143 155
     this.init();
144 156
   },
145 157
   methods: {
@@ -172,10 +184,13 @@ export default {
172 184
         .replace(/&amp;nbsp;/g, "\u3000");
173 185
     },
174 186
     // swiper轮播图
175
-    swiper() {
187
+    swiper(list) {
176 188
       let effect = 1;
177 189
       let that = this;
178
-      var swiper = new Swiper(".swiper-container", {
190
+      if(that.Myswiper){
191
+        return that.Myswiper
192
+      }
193
+      that.Myswiper = new Swiper(".swiper-container", {
179 194
         speed: 2500,
180 195
         slidesPerView: 5,
181 196
         spaceBetween: 30,
@@ -183,6 +198,12 @@ export default {
183 198
         watchSlidesProgress: true,
184 199
         slideToClickedSlide: true,
185 200
         preloadImages: true,
201
+        observer: true,
202
+        onSlideChangeEnd: function (swiper) {
203
+          swiper.update();
204
+          swiper.startAutoplay();
205
+          swiper.reLoop();
206
+        },
186 207
         on: {
187 208
           setTranslate: function () {
188 209
             let slides = this.slides;
@@ -203,30 +224,39 @@ export default {
203 224
               slide.transition(transition);
204 225
             }
205 226
           },
206
-          transitionStart() {
207
-            // 如果是第一张或者第二张就不居中
208
-            if (this.activeIndex == 0 || this.activeIndex == 1) {
209
-              this.setTranslate(0);
210
-            }
211
-            // 最后一张或者两张不动
212
-            if (
213
-              this.activeIndex == this.slides.length - 2 ||
214
-              this.activeIndex == this.slides.length - 1
215
-            ) {
216
-              let slideW = (this.width / 5).toFixed(5); //每个轮播的宽度
217
-              let keepW = slideW * (this.slides.length - 5); //需要设置的setTranslate的宽度
218
-              this.setTranslate(-keepW);
219
-            }
220
-          },
227
+          // transitionStart() {
228
+          //   // 如果是第一张或者第二张就不居中
229
+          //   if (this.activeIndex == 0 || this.activeIndex == 1) {
230
+          //     this.setTranslate(0);
231
+          //   }
232
+          //   // 最后一张或者两张不动
233
+          //   if (
234
+          //     this.activeIndex == this.slides.length - 2 ||
235
+          //     this.activeIndex == this.slides.length - 1
236
+          //   ) {
237
+          //     let slideW = (this.width / 5).toFixed(5); //每个轮播的宽度
238
+          //     let keepW = slideW * (this.slides.length - 5); //需要设置的setTranslate的宽度
239
+          //     this.setTranslate(-keepW);
240
+          //   }
241
+          // },
221 242
 
222 243
           // 滑动切换商品详情
223
-          touchEnd: function (swiper, event) {
244
+          touchStart(e) {},
245
+          slideChange: function (swiper) {
246
+            that.goods_id = "";
224 247
             //你的事件
225 248
             let id = that.goodList[swiper.activeIndex].id;
226 249
             that.getGoodInfo(id);
227 250
           },
251
+          // 滑动商品切换到最后一个加载
252
+          reachEnd(swiper) {
253
+            if (that.goodList.length < that.total) {
254
+              that.getGoodList(++that.page);
255
+            }
256
+          },
228 257
         },
229 258
       });
259
+      return this.Myswiper;
230 260
     },
231 261
     // 二维码
232 262
     qrcodeScan() {
@@ -245,22 +275,57 @@ export default {
245 275
     getCategory() {
246 276
       getCategory().then((res) => {
247 277
         if (res.status == 200) {
278
+          res.data.data.list.forEach((item, index) => {
279
+            if (item.id == this.category_id_act) {
280
+              this.categoryIndex = index;
281
+            }
282
+          });
248 283
           this.categoryList = res.data.data.list;
249 284
           this.getGoodList();
250 285
         }
251 286
       });
252 287
     },
253 288
     // 获取商品列表
254
-    getGoodList(id) {
289
+    getGoodList(page) {
255 290
       let params = {
256
-        page: 1,
257
-        limit: 10,
258
-        category_id: id ? id : this.categoryList[0].id, //分类ID
291
+        page: page || this.page,
292
+        limit: 50,
293
+        category_id: this.category_id_act
294
+          ? this.category_id_act
295
+          : this.category_id
296
+          ? this.category_id
297
+          : this.categoryList[0].id, //分类ID
259 298
       };
260 299
       getGoodList(params).then((res) => {
261 300
         if (res.status == 200) {
262
-          this.goodList = res.data.data.list;
263
-          this.getGoodInfo();
301
+          // 滑动下一页数据
302
+          this.total = res.data.data.total;
303
+          let list = this.goodList;
304
+          if (page == 1 || this.page == 1) {
305
+            list = [];
306
+          }
307
+          this.goodList = list;
308
+          let mySwiper = this.swiper(list);
309
+          let selectID = ''
310
+          res.data.data.list.forEach((item, index) => {
311
+            if(index==0){
312
+              selectID=item.id
313
+            }
314
+            list.push(item);
315
+            if (item.id == this.goods_id) {
316
+              setTimeout(() => {
317
+                mySwiper.slideTo(index, 1000, false);
318
+              }, 100);
319
+              this.goods_id=''
320
+              selectID=''
321
+            }
322
+          });
323
+          if(selectID){
324
+            this.getGoodInfo(selectID);
325
+            setTimeout(() => {
326
+              mySwiper.slideTo(0, 1000, false);
327
+            }, 100);
328
+          }
264 329
         }
265 330
       });
266 331
     },
@@ -268,14 +333,8 @@ export default {
268 333
     getGoodInfo(id) {
269 334
       this.isOver = false;
270 335
       this.isSkinOver = false;
271
-      getGoodInfo({
272
-        id: this.goods_id
273
-          ? this.goods_id
274
-          : id
275
-          ? id
276
-          : this.goodList[0]
277
-          ? this.goodList[0].id
278
-          : "",
336
+      getGoodInfoList({
337
+        id
279 338
       }).then((res) => {
280 339
         if (res.status == 200) {
281 340
           if (this.goodInfo.id == res.data.data.id) {
@@ -290,6 +349,7 @@ export default {
290 349
               let a = c.replace(c, "...");
291 350
               res.data.data.goods_name = b + a;
292 351
             }
352
+            // 处理价格
293 353
             if (res.data.data.price) {
294 354
               let priceA = res.data.data.price.indexOf(".");
295 355
               res.data.data.price = res.data.data.price.slice(0, priceA);
@@ -300,6 +360,8 @@ export default {
300 360
             if (res.data.data.skin_type > 7) {
301 361
               this.isSkinOver = true;
302 362
             }
363
+            // 处理富文本
364
+            res.data.data.component = this.showHtml(res.data.data.component);
303 365
             this.goodInfo = res.data.data;
304 366
             this.qrcodeScan();
305 367
           }
@@ -308,17 +370,14 @@ export default {
308 370
     },
309 371
     // 选择商品分类
310 372
     selectTab(e) {
373
+      this.page = 1;
311 374
       // 重置banner传过来的商品
312 375
       this.goods_id = "";
313
-      let id = e.id;
314
-      this.getGoodList(id);
315
-    },
316
-    // 选择商品
317
-    selectGood(e) {
318
-      // 重置banner传过来的商品
319
-      this.goods_id = "";
320
-      let id = e.id;
321
-      this.getGoodInfo(id);
376
+      this.category_id_act = "";
377
+      this.category_id = e.id;
378
+      this.getGoodList();
379
+      let mySwiper = this.swiper();
380
+      
322 381
     },
323 382
   },
324 383
 };
@@ -574,7 +633,7 @@ body {
574 633
     .content {
575 634
       margin-top: 30px;
576 635
       font-size: 30px;
577
-      color: #666666;
636
+      // color: #666666;
578 637
       margin-left: 32px;
579 638
     }
580 639
   }

+ 1 - 0
src/views/swiper/index.vue

@@ -33,6 +33,7 @@ export default {
33 33
         path: "/home",
34 34
         query: {
35 35
           goods_id: e.goods_id,
36
+          category_id:e.category_id
36 37
         },
37 38
       });
38 39
     },