-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
checkout.vue
245 lines (218 loc) · 6.8 KB
/
checkout.vue
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
<template>
<div class="order">
<van-cell-group>
<van-cell v-if="checkedAddress" isLink @click="goAddressList()" title="收货地址">
<div slot="label">
<div>
<span>{{ checkedAddress.name }} </span>
<span>{{ checkedAddress.tel }} </span>
</div>
<div>
{{ checkedAddress.addressDetail }}
</div>
</div>
</van-cell>
</van-cell-group>
<van-cell-group>
<van-cell class="order-coupon" title="优惠券" is-link :value="getCouponValue()" @click="getCoupons" />
</van-cell-group>
<!-- 优惠券列表 -->
<van-popup v-model="showList" position="bottom">
<van-coupon-list
:coupons="coupons"
:chosen-coupon="chosenCoupon"
:disabled-coupons="disabledCoupons"
@change="onChange"
@exchange="onExchange"
/>
</van-popup>
<van-card
v-for="item in checkedGoodsList"
:key="item.id"
:title="item.goodsName"
:num="item.number"
:price="item.price +'.00'"
:thumb="item.picUrl"
>
<div slot="desc">
<div class="van-card__desc">
<van-tag plain style="margin-right:6px;" v-for="(spec, index) in item.specifications" :key="index">
{{spec}}
</van-tag>
</div>
</div>
</van-card>
<van-cell-group>
<van-cell title="商品金额">
<span class="red">{{goodsTotalPrice * 100 | yuan}}</span>
</van-cell>
<van-cell title="邮费">
<span class="red">{{ freightPrice * 100| yuan}}</span>
</van-cell>
<van-cell title="优惠券">
<span class="red">-{{ couponPrice * 100| yuan}}</span>
</van-cell>
<van-field v-model="message" placeholder="请输入备注" label="订单备注">
<template slot="icon">{{message.length}}/50</template>
</van-field>
</van-cell-group>
<van-submit-bar
:price="actualPrice*100"
label="总计:"
buttonText="提交订单"
:disabled="isDisabled"
@submit="onSubmit"
/>
</div>
</template>
<script>
import { Card, Tag, ard, Field, SubmitBar, Toast } from 'vant';
import { CouponCell, CouponList, Popup } from 'vant';
import { cartCheckout, orderSubmit, couponSelectList} from '@/api/api';
import { getLocalStorage, setLocalStorage } from '@/utils/local-storage';
import dayjs from 'dayjs';
export default {
data() {
return {
checkedGoodsList: [],
checkedAddress: {},
availableCouponLength: 0, // 可用的优惠券数量
goodsTotalPrice: 0, //商品总价
freightPrice: 0, //快递费
couponPrice: 0, //优惠券的价格
grouponPrice: 0, //团购优惠价格
orderTotalPrice: 0, //订单总价
actualPrice: 0, //实际需要支付的总价
message: '',
isDisabled: false,
showList: false,
chosenCoupon: -1,
coupons: [],
disabledCoupons: []
};
},
created() {
this.init();
},
methods: {
onSubmit() {
const {AddressId, CartId, CouponId, UserCouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId', 'UserCouponId');
if (AddressId === null) {
Toast.fail('请设置收货地址');
return;
}
this.isDisabled = true;
orderSubmit({
addressId: AddressId,
cartId: CartId,
couponId: CouponId,
userCouponId: UserCouponId,
grouponLinkId: 0,
grouponRulesId: 0,
message: this.message
}).then(res => {
// 下单成功,重置下单参数。
setLocalStorage({AddressId: 0, CartId: 0, CouponId: 0});
let orderId = res.data.data.orderId;
this.$router.push({
name: 'payment',
params: { orderId: orderId }
});
}).catch(error => {
this.isDisabled = false;
this.$toast("下单失败");
})
},
goAddressList() {
this.$router.push({
path: '/user/address'
});
},
getCouponValue() {
if(this.couponPrice !== 0 ){
return "-¥" + this.couponPrice + ".00元"
}
if(this.availableCouponLength !== 0){
return this.availableCouponLength + "张可用"
}
return '没有可用优惠券'
},
getCoupons() {
const {AddressId, CartId, CouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId');
couponSelectList({cartId: CartId, grouponRulesId: 0}).then(res => {
var cList = res.data.data.list;
this.coupons = []
this.disabledCoupons = [];
for(var i = 0; i < cList.length; i++){
var c = cList[i]
var coupon = {
id: c.id,
cid: c.cid,
name: c.name,
condition: '满' + c.min + '元可用',
value: c.discount * 100,
description: c.desc,
startAt: new Date(c.startTime).getTime()/1000,
endAt: new Date(c.endTime).getTime()/1000,
valueDesc: c.discount,
unitDesc: '元'
}
if (c.available) {
this.coupons.push(coupon);
} else {
this.disabledCoupons.push(coupon);
}
}
this.showList = true
})
},
init() {
const {AddressId, CartId, CouponId, UserCouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId', 'UserCouponId');
cartCheckout({cartId: CartId, addressId: AddressId, couponId: CouponId, userCouponId: UserCouponId, grouponRulesId: 0}).then(res => {
var data = res.data.data
this.checkedGoodsList = data.checkedGoodsList;
this.checkedAddress= data.checkedAddress;
this.availableCouponLength= data.availableCouponLength;
this.actualPrice= data.actualPrice;
this.couponPrice= data.couponPrice;
this.grouponPrice= data.grouponPrice;
this.freightPrice= data.freightPrice;
this.goodsTotalPrice= data.goodsTotalPrice;
this.orderTotalPrice= data.orderTotalPrice;
setLocalStorage({AddressId: data.addressId, CartId: data.cartId, CouponId: data.couponId, UserCouponId: data.userCouponId});
});
},
onChange(index) {
this.showList = false;
this.chosenCoupon = index;
if(index === -1 ){
setLocalStorage({CouponId: -1, UserCouponId: -1});
}
else{
const couponId = this.coupons[index].cid;
const userCouponId = this.coupons[index].id;
setLocalStorage({CouponId: couponId, UserCouponId: userCouponId});
}
this.init()
},
onExchange() {
this.$toast("兑换暂不支持");
}
},
components: {
[Toast.name]: Toast ,
[SubmitBar.name]: SubmitBar,
[Card.name]: Card,
[Field.name]: Field,
[Tag.name]: Tag,
[CouponCell.name]: CouponCell,
[CouponList.name]: CouponList,
[Popup.name]: Popup
}
};
</script>
<style lang="scss" scoped>
.order-coupon {
margin-top: 10px;
}
</style>