fix
Helius
2021-02-20 45fb4b11ad51bb38306765b11a6747402e382cee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<template>
    <!-- 当前客户 -->
    <view class="container">
        <search-bar @confirm="search" placeholder="商品名称、编号、拼音" class="search-bar"></search-bar>
        <view class="list">
            <scroll-view class="list-left" scroll-y="true">
                <uni-collapse accordion="true" @change="toggleCollapse">
                    <uni-collapse-item :showAnimation="true" :open="item.id==cateId" :title="item.name" :isShowIcon="isShowIcon(item)" class="list-left-row"
                        v-if="item.parentId===0" v-for="(item, index) in goodsTypeList">
                        <view class="list-row-wrap">
                            <view class="list-row" :class="op.id==cateId?'blue':''"
                                v-for="op in goodsTypeList" 
                                v-if="op.parentId===item.id" 
                                @click="changeGoodsType(op.id)">
                                {{op.name}}
                            </view>
                        </view>
                    </uni-collapse-item>
                </uni-collapse>
            </scroll-view>
            <scroll-view class="list-right" scroll-y="true" @scrolltolower="scrolltolower()">
                <navigator :url="'../productDetail/index?goodsType='+item.goodsType+'&id='+item.id" hover-class="none" v-for="item in goodsList">
                    <view class="list-right-row">
                        <image class="product-img" :src="item.img?item.img:'../../../static/images/no-img.png'"></image>
                        <view class="flex-1">
                            <text>{{item.name}}</text>
                            <view class="flex justify-between mt-15">
                                <text class="price">¥{{item.price}}</text>
                                <view class="right">
                                    <template v-if="item.num">
                                        <text class="iconfont iconjian blue-outline-btn-circle mr-10" 
                                            @click.stop="decreaseGoods(item)"></text>
                                            <text>{{item.num}}</text>
                                    </template>
                                    <text class="iconfont iconjia blue-btn-circle ml-10" 
                                        @click.stop="addGoods(item)"></text>
                                </view>
                            </view>
                        </view>
                    </view>
                </navigator>
                <view v-if="goodsList.length">
                    <uni-load-more :status="loadStatus" color="#a5abaf"></uni-load-more>
                </view>
                <no-record :isShow="!goodsList.length" txt="暂无产品/项目"></no-record>
            </scroll-view>
        </view>
        <view class="footer">
            <view class="flex align-center" @click="openShoppingCart">
                <view class="icon-car-wrap">
                    <text class="iconfont iconicongouwuche icon-car gray"></text>
                    <text class="count" v-if="totalCount">{{totalCount}}</text>
                </view>
                <text class="font-16 blue">¥{{totalAmount}}</text>
            </view>
            <button class="blue-btn btn mr-0" @click="submit" :disabled="isEnableSumbit">提交订单</button>
        </view>
        <shopping-cart ref="shopCart" :list="selectItems" @freeChange="freeChange" @addGoods="addGoods" @decreaseGoods="decreaseGoods" @reset="clearShopCart"></shopping-cart>
    </view>
</template>
 
<script>
    import uniCollapse from '../../../components/uni-collapse/uni-collapse.vue'
    import uniCollapseItem from '../../../components/uni-collapse-item/uni-collapse-item.vue'
    import searchBar from '../../../components/searchBar/index.vue';
    import shoppingCart from './shoppingCart.vue'
    import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
    export default {
        components:{
            uniCollapse,
            uniCollapseItem,
            searchBar,
            shoppingCart,
            uniLoadMore
        },
        data(){
            return{
                id: '',
                tabIndex: '0',
                goodsTypeList: [], 
                goodsList: [],
                cateId: null,//当前产品类别id
                selectItems: [],
                queryKey: '',
                loadStatus: 'more',
                pageNum: 1
            }
        },
        computed:{
            // 购物车总数量
            totalCount(){
                let count = 0;
                this.selectItems.forEach((item) => {
                    count += item.num;
                })
                return count;
            },
            // 购物车总金额
            totalAmount(){
                let amount = 0;
                this.selectItems.forEach((item) => {
                    if(!item.isFree){
                        amount += item.price * item.num;
                    }
                })
                return amount;
            },
            isEnableSumbit(){
                return !this.selectItems.length
            }
        },
        onLoad(options) {
            this.id = options.id;
            uni.setNavigationBarTitle({
                title: '当前客户:' + options.vipName
            });
            this.loadGoodsTypeList()
        },
        onPullDownRefresh(){
            this.reloadData();
            let timer = setTimeout(function () {
                uni.stopPullDownRefresh();
                clearTimeout(timer);
                timer = null;
            }, 800);
        },
        methods:{
            reloadData(){
                this.goodsList = [];
                this.pageNum = 1;
                this.loadStatus = 'more';
                this.loadGoodsList();
            },
            scrolltolower(){
                this.loadGoodsList()
            },
            isShowIcon(item){
                let count = 0;
                this.goodsTypeList.forEach((op) => {
                    if(op.parentId==item.id){
                        count ++ 
                    }
                });
                return count?true:false;
            },
            openShoppingCart(){
                this.$refs.shopCart.toggle()
            },
            loadGoodsTypeList(){
                this.$httpUtils.request('/api/order/findShoppingGoodsType').then((res) => {
                    if(res.status == 200){
                        let result = res.rows;
                        this.goodsTypeList = result;
                        if(result.length){
                            this.cateId = result[0].id;
                            this.loadGoodsList();
                        }
                    }
                })
            },
            loadGoodsList(){
                if(this.loadStatus!=='more'){
                    return;
                }
                this.$httpUtils.request('/api/order/findShoppingGoods',{
                    cateId: this.cateId,
                    pageNum: this.pageNum,
                    pageSize: 10,
                    queryKey: this.queryKey
                },'POST').then((res) => {
                    if(res.status == 200){
                        let result = res.rows.map((item) => {
                            return Object.assign(item, {num: 0});
                        });
                        if(result.length < 10){
                            this.loadStatus = 'noMore';
                        } else {
                            this.pageNum ++ ;
                            this.loadStatus = 'more';
                        }
                        this.goodsList = this.goodsList.concat(result);
                        this.goodsList.forEach((item) => {
                            this.selectItems.forEach((op) => {
                                if(item.id == op.id){
                                    item.num = op.num;
                                }
                            })
                        });
                    }
                })
            },
            // 搜索
            search(val){
                this.queryKey = val;
                this.reloadData();
            },
            getParentId(){
                let arr = this.goodsTypeList.filter((item) => {
                    return item.id===this.cateId
                })
                return arr.length?arr[0].parentId:'';
            },
            toggleAll(arr){
                this.changeGoodsType('');
            },
            // 打开关闭面板
            toggleCollapse(arr){
                if(!arr.length){
                    let parentId = this.getParentId();
                    if(parentId){
                        this.changeGoodsType();
                    }
                    return;
                }
                let index = arr[0];
                let result = this.goodsTypeList.filter((item) => {
                    return item.parentId===0
                })
                this.changeGoodsType(result[index].id);
            },
            // 切换商品类型
            changeGoodsType(id){
                this.cateId = id;
                this.reloadData();
            },
            freeChange(data){
                data.goods.isFree = !data.goods.isFree;
                this.$set(this.selectItems, data.index, data.goods)
            },
            // 添加商品
            addGoods(goods){
                goods.num ++ 
                // 所有商品列表的下标
                let ids = this.goodsList.map((item) => {
                    return item.id;
                })
                let index = ids.indexOf(goods.id);
                if(index>-1){
                    this.goodsList[index].num = goods.num;
                }
                
                // 选中商品的下标
                let selectIds = this.selectItems.map((item) => {
                    return item.id;
                })
                let selectIndex = selectIds.indexOf(goods.id);
                
                // 该商品若已在购物车,则购物车数量上+1,否则加入购物车
                if(selectIndex > -1){
                    this.selectItems[selectIndex].num = goods.num;
                }else{
                    this.selectItems.push(Object.assign(goods, {isFree: false}));
                }
                
            },
            decreaseGoods(goods){
                goods.num = goods.num <= 0 ? 0 : goods.num - 1;
                // 所有商品列表的下标
                let ids = this.goodsList.map((item) => {
                    return item.id;
                })
                let index = ids.indexOf(goods.id);
                if(index>-1){
                    this.goodsList[index].num = goods.num;
                }
                
                // 选中商品的下标
                let selectIds = this.selectItems.map((item) => {
                    return item.id;
                })
                let selectIndex = selectIds.indexOf(goods.id);
                
                // 该商品若已在购物车,则购物车数量上-1,否则从购物车删除
                if(goods.num >= 1){
                    this.selectItems[selectIndex].num = goods.num;
                }else{
                    this.selectItems.splice(selectIndex, 1);
                }
            },
            clearShopCart(){
                this.selectItems = [];
                this.goodsList.forEach((item) => {
                    item.num = 0;
                });
            },
            submit(){
                let str = (encodeURIComponent(JSON.stringify(this.selectItems))).replace(/%/g, '%25');
                uni.navigateTo({
                    url: '../confirmOrder?id='+this.id+'&list='+str
                });
            }
        }
    }
</script>
 
<style>
    page{
        height: 100%;
    }
    .container{
        display: flex;
        flex-direction: column;
        height: 100%;
        padding-top: 10px;
        box-sizing: border-box;
    }
    .search-bar{
        margin-left: 10px;
        margin-right: 10px;
    }
    .list{
        flex: 1;
        display: flex;
        overflow: hidden;
    }
    .list-left{
        width: 200rpx;
        background: #F6F6F8;
        border-radius: 4px;
    }
    .list-right{
        flex: 1;
    }
    .list-left-row{
        color: #8c9fad;
        font-size: 13px;
        border-bottom: 0;
    }
    .uni-collapse{
        background-color: #F6F6F8!important;
    }
    .uni-collapse-cell{
        border-color: #F6F6F8!important;
    }
    .uni-collapse-cell__title:active, .uni-collapse-cell--hover, .uni-collapse-cell--open, .uni-collapse-cell--disabled{
        background-color: #FFFFFF!important;
    }
    
    .list-row-wrap{
        padding: 0 15px;
        margin-top: -8px;
    }
    .list-row{
        font-size: 13px;
        margin: 8px 0;
    }
    .list-right-row{
        display: flex;
        align-items: center;
        padding: 10px;
        margin: 0 10px 10px;
        border: 1px solid #EDEAF4;
        box-shadow:0 6px 6px rgba(237,234,244,0.4);
        border-radius: 4px;
        font-size: 13px;
    }
    .list-row .list-right-row:nth-last-of-type(1){
        margin-bottom: 0;
    }
    .list-right-row .product-img{
        width: 66px;
        height: 66px;
        margin-right: 5px;
    }
    .list-right-row .price{
        display: block;
        color: #FA5151;
        font-size: 15px;
    }
    .footer{
        position: relative;
        z-index: 999;
        display: flex;
        justify-content: space-between;
        background: #FFFFFF;
        padding: 10px;
        padding-bottom: 10px;
        padding-bottom: calc(10px + constant(safe-area-inset-bottom));  
        padding-bottom: calc(10px + env(safe-area-inset-bottom)); 
        box-shadow: 0 -6px 6px rgba(237,234,244,0.2);
    }
    .footer .icon-car-wrap{
        position: relative;
        margin-right: 15px;
    }
    .footer .icon-car{
        font-size: 28px;
    }
    .footer .count{
        position: absolute;
        right: -10px;
        top: -10px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        background: #518EFF;
        border-radius: 50%;
        color: #FFFFFF;
        font-size: 12px;
        text-align: center;
    }
    .footer .btn{
        border-radius: 20px;
        line-height: 36px;
        padding: 0 26px;
        font-size: 14px;
    }
</style>