浏览代码

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
     return request({
29
     return request({
30
         url: '/v2/api-h5/goods/info',
30
         url: '/v2/api-h5/goods/info',
31
         method: 'get',
31
         method: 'get',

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

@@ -8,6 +8,7 @@
8
           center-active
8
           center-active
9
           color="#FA7D22"
9
           color="#FA7D22"
10
           height="120"
10
           height="120"
11
+          v-model="categoryIndex"
11
         >
12
         >
12
           <v-tab
13
           <v-tab
13
             @click="selectTab(item)"
14
             @click="selectTab(item)"
@@ -22,7 +23,6 @@
22
     <div class="swiper-container">
23
     <div class="swiper-container">
23
       <div class="swiper-wrapper">
24
       <div class="swiper-wrapper">
24
         <div
25
         <div
25
-          @click="selectGood(item)"
26
           v-for="(item, index) in goodList"
26
           v-for="(item, index) in goodList"
27
           :key="index"
27
           :key="index"
28
           class="swiper-slide"
28
           class="swiper-slide"
@@ -106,7 +106,7 @@
106
         <div class="content">嫩滑细腻 紧实肌肤</div>
106
         <div class="content">嫩滑细腻 紧实肌肤</div>
107
       </div> -->
107
       </div> -->
108
       <div class="ingredient-content">
108
       <div class="ingredient-content">
109
-        <div class="content">{{ goodInfo.component }}</div>
109
+        <div class="content" v-html="goodInfo.component"></div>
110
       </div>
110
       </div>
111
     </div>
111
     </div>
112
   </div>
112
   </div>
@@ -114,7 +114,12 @@
114
 
114
 
115
 <script>
115
 <script>
116
 var Swiper = require("../../utils/swiper-bundle.min");
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
 import QRCode from "qrcodejs2";
123
 import QRCode from "qrcodejs2";
119
 export default {
124
 export default {
120
   components: {
125
   components: {
@@ -130,16 +135,23 @@ export default {
130
       isOver: false,
135
       isOver: false,
131
       isSkinOver: false,
136
       isSkinOver: false,
132
       goods_id: "", //商品ID
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
   created() {
146
   created() {
136
     // 获取banner传过来的商品ID
147
     // 获取banner传过来的商品ID
137
     this.goods_id = this.$route.query.goods_id;
148
     this.goods_id = this.$route.query.goods_id;
149
+    // 获取banner传过来的商品分类
150
+    this.category_id_act = this.$route.query.category_id;
138
     // 获取商品分类列表
151
     // 获取商品分类列表
139
     this.getCategory();
152
     this.getCategory();
140
   },
153
   },
141
   mounted() {
154
   mounted() {
142
-    this.swiper();
143
     this.init();
155
     this.init();
144
   },
156
   },
145
   methods: {
157
   methods: {
@@ -172,10 +184,13 @@ export default {
172
         .replace(/&amp;nbsp;/g, "\u3000");
184
         .replace(/&amp;nbsp;/g, "\u3000");
173
     },
185
     },
174
     // swiper轮播图
186
     // swiper轮播图
175
-    swiper() {
187
+    swiper(list) {
176
       let effect = 1;
188
       let effect = 1;
177
       let that = this;
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
         speed: 2500,
194
         speed: 2500,
180
         slidesPerView: 5,
195
         slidesPerView: 5,
181
         spaceBetween: 30,
196
         spaceBetween: 30,
@@ -183,6 +198,12 @@ export default {
183
         watchSlidesProgress: true,
198
         watchSlidesProgress: true,
184
         slideToClickedSlide: true,
199
         slideToClickedSlide: true,
185
         preloadImages: true,
200
         preloadImages: true,
201
+        observer: true,
202
+        onSlideChangeEnd: function (swiper) {
203
+          swiper.update();
204
+          swiper.startAutoplay();
205
+          swiper.reLoop();
206
+        },
186
         on: {
207
         on: {
187
           setTranslate: function () {
208
           setTranslate: function () {
188
             let slides = this.slides;
209
             let slides = this.slides;
@@ -203,30 +224,39 @@ export default {
203
               slide.transition(transition);
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
             let id = that.goodList[swiper.activeIndex].id;
248
             let id = that.goodList[swiper.activeIndex].id;
226
             that.getGoodInfo(id);
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
     qrcodeScan() {
262
     qrcodeScan() {
@@ -245,22 +275,57 @@ export default {
245
     getCategory() {
275
     getCategory() {
246
       getCategory().then((res) => {
276
       getCategory().then((res) => {
247
         if (res.status == 200) {
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
           this.categoryList = res.data.data.list;
283
           this.categoryList = res.data.data.list;
249
           this.getGoodList();
284
           this.getGoodList();
250
         }
285
         }
251
       });
286
       });
252
     },
287
     },
253
     // 获取商品列表
288
     // 获取商品列表
254
-    getGoodList(id) {
289
+    getGoodList(page) {
255
       let params = {
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
       getGoodList(params).then((res) => {
299
       getGoodList(params).then((res) => {
261
         if (res.status == 200) {
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
     getGoodInfo(id) {
333
     getGoodInfo(id) {
269
       this.isOver = false;
334
       this.isOver = false;
270
       this.isSkinOver = false;
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
       }).then((res) => {
338
       }).then((res) => {
280
         if (res.status == 200) {
339
         if (res.status == 200) {
281
           if (this.goodInfo.id == res.data.data.id) {
340
           if (this.goodInfo.id == res.data.data.id) {
@@ -290,6 +349,7 @@ export default {
290
               let a = c.replace(c, "...");
349
               let a = c.replace(c, "...");
291
               res.data.data.goods_name = b + a;
350
               res.data.data.goods_name = b + a;
292
             }
351
             }
352
+            // 处理价格
293
             if (res.data.data.price) {
353
             if (res.data.data.price) {
294
               let priceA = res.data.data.price.indexOf(".");
354
               let priceA = res.data.data.price.indexOf(".");
295
               res.data.data.price = res.data.data.price.slice(0, priceA);
355
               res.data.data.price = res.data.data.price.slice(0, priceA);
@@ -300,6 +360,8 @@ export default {
300
             if (res.data.data.skin_type > 7) {
360
             if (res.data.data.skin_type > 7) {
301
               this.isSkinOver = true;
361
               this.isSkinOver = true;
302
             }
362
             }
363
+            // 处理富文本
364
+            res.data.data.component = this.showHtml(res.data.data.component);
303
             this.goodInfo = res.data.data;
365
             this.goodInfo = res.data.data;
304
             this.qrcodeScan();
366
             this.qrcodeScan();
305
           }
367
           }
@@ -308,17 +370,14 @@ export default {
308
     },
370
     },
309
     // 选择商品分类
371
     // 选择商品分类
310
     selectTab(e) {
372
     selectTab(e) {
373
+      this.page = 1;
311
       // 重置banner传过来的商品
374
       // 重置banner传过来的商品
312
       this.goods_id = "";
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
     .content {
633
     .content {
575
       margin-top: 30px;
634
       margin-top: 30px;
576
       font-size: 30px;
635
       font-size: 30px;
577
-      color: #666666;
636
+      // color: #666666;
578
       margin-left: 32px;
637
       margin-left: 32px;
579
     }
638
     }
580
   }
639
   }

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

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