shopcar.js
7.8 KB
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
/*获取*/
//$(document).ready(function(){
new Vue({
el: '#shopcar',
data: {
carlist: [],
checktotalmoney:[],
cartnum:{
num:0
},
//总价钱
totalmoney:{
money:0
},
checkgood:false,
checklist:[],
cartnum:0
},
created: function() {
this.details();
this.num();
},
methods: {
//获取信息
details: function() {
var that = this;
var url = '/Cart/index'
var params = {
unique_id: localStorage.getItem('unique_id')
}
ajaxsend(url, params, function(res) {
/*console.log(JSON.stringify(res))*/
console.log(res);
var data=res.data;
var carlist=data.cartList;
that.carlist=carlist;
console.log(that.carlist);
for(obj of that.carlist){
obj.checked=true;
}
that.checklist.length=that.carlist.length;
console.log(that.checklist)
//全选按钮
that.checkgood=true;
that.countPrice();
})
},
addnum:function(obj,index){
var that=this;
var adnum="";
console.log(obj);
var cartid=obj.cart_id;
adnum=parseInt(that.carlist[index].goods_num);
that.carlist[index].goods_num++
adnum+=1;
var itemmoney=adnum*obj.goods_price;
itemmoney=Math.floor(itemmoney * 100) / 100;
console.log(itemmoney);
//更改购物车里的数量
var url = '/Cart/editNum';
var params = {
cart_id:cartid,
goods_num:adnum
}
ajaxsend(url, params, function(res) {
/*console.log(JSON.stringify(res))*/
console.log(res);
if(res.status==true){
//alert("更改成功");
//window.location.reload();
that.details();
that.num();
load();
}else if(res.status==false){
unstock()
}
})
},
reducenum:function(obj,index){
var that=this;
console.log(obj);
var rednum="";
var cartid=obj.cart_id;
rednum=parseInt(that.carlist[index].goods_num);
//剩一个商品的时候
if(rednum==1){
var that=this;
var con=confirm("是否删除该商品");
if(con==true){
//var carid=cart_id;
var url='/Cart/del';
var params = {
cart_id:cartid
}
ajaxsend(url, params, function(res) {
/*console.log(JSON.stringify(res))*/
console.log(res);
if(res.status==true){
alert("删除成功");
window.location.reload();
}
})
}
}else{
that.carlist[index].goods_num--;
rednum-=1;
console.log(rednum)
var itemmoney=rednum*obj.goods_price;
itemmoney=Math.floor(itemmoney * 100) / 100;
console.log(itemmoney);
var url = '/Cart/editNum';
var params = {
cart_id:cartid,
goods_num:rednum
}
ajaxsend(url, params, function(res) {
/*console.log(JSON.stringify(res))*/
console.log(res);
if(res.status==true){
//alert("更改成功");
//window.location.reload();
that.details();
that.num();
load();
}else if(res.status==false){
//alert("库存不足")
unstock();
}
})
}
},
check:function(obj,index){
var that=this;
// console.log(obj);
// var checkprice=obj.goods_price;
// var checknum=obj.goods_num;
// var checkmoney=checkprice*checknum;
// checkmoney=Math.floor(checkmoney * 100) / 100;
// console.log(checkmoney);
if(that.carlist[index].checked==false){
that.carlist[index].checked=true;
that.checklist.push(1);
console.log(that.checklist)
}else if(that.carlist[index].checked==true){
that.carlist[index].checked=false;
that.checklist.pop(1);
console.log(that.checklist)
}
console.log(that.checklist);
console.log(that.carlist);
//var j=0;
//for(var obj of that.carlist){
// //console.log(obj.goods_num)
// if(obj.goods_num>0){
// j++
// }
//}
if(that.checklist.length==that.carlist.length){
that.checkgood=true;
}else{
that.checkgood=false;
}
that.countPrice();
// if(that.carlist[index].checked==true){
// that.checktotalmoney.push(checkmoney);
//
// }else if(that.carlist[index].checked==false){
// that.checktotalmoney.pop(checkmoney);
//
// }
// console.log(that.checktotalmoney);
// var s=0;
// for(var i=0;i<that.checktotalmoney.length;i++){
// s=s+that.checktotalmoney[i]
// }
// console.log(s);
// that.totalmoney.money=s;
},
choseall:function(){
var that=this;
that.checklist.length=that.carlist.length;
console.log(that.checklist.length)
if(that.checkgood==false){
that.checkgood=true;
var cartList=that.carlist;
for(obj of cartList){
obj.checked=true;
}
that.countPrice();
}else if(that.checkgood==true){
that.checkgood=false;
var cartList=that.carlist;
for(obj of cartList){
obj.checked=false;
}
that.countPrice();
that.checklist.length=0;
//window.location.reload();
}
},
//购物车数量
num:function(){
var that=this;
var url='/CountNum/countCart';
var params = {
unique_id: localStorage.getItem('unique_id')
}
ajaxsend(url, params, function(res) {
/*console.log(JSON.stringify(res))*/
console.log(res);
var data=res.data;
console.log(data[0].count)
that.cartnum=data[0].count;
console.log(that.cartnum)
})
},
//删除购物车
delcart:function(cart_id){
var that=this;
var con=confirm("是否删除该商品");
if(con==true){
var carid=cart_id;
var url='/Cart/del';
var params = {
cart_id: carid
}
ajaxsend(url, params, function(res) {
/*console.log(JSON.stringify(res))*/
console.log(res);
if(res.status==true){
alert("删除成功");
window.location.reload();
}
})
}
},
//计算价钱的回调函数
countPrice(){
var that=this;
let countPrice = 0
let cartList = that.carlist;
console.log(cartList);
for(obj of cartList){
console.log(obj)
if(obj.checked==true){
var checkmoney=obj.goods_num*parseInt(obj.goods_price*1000);
countPrice +=checkmoney/1000;
console.log(countPrice);
}
}
that.totalmoney.money=countPrice;
},
//结算
accounts:function(){
var that=this;
console.log(that.checklist.length)
if(that.checklist.length==0){
alert("请选择商品");
return false;
}else{
var cartList=that.carlist;
var cartdid=[];
console.log(cartList);
for(obj of cartList){
console.log(obj);
if(obj.checked==true){
var cartid=obj.cart_id;
cartdid.push(cartid);
console.log(cartdid);
window.location.href="/index.php?g=&m=template&a=order&id="+cartdid;
}
//else if(that.checklist.length==0){
// alert("请选择商品");
// return false;
//}
}
}
}
}
})
//});