-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemberController.class.php
316 lines (290 loc) · 9.06 KB
/
MemberController.class.php
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
<?php
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller {
/**
* 会员 订单 页面
*/
public function member_order(){
if(checkSession()==false){
redirect("../Login/login", 0.1, ' ');
}else{
//当前第几页
$ye = I('page')?I('page'):1;
$op = $this->orderPage($ye,$mc=8);
$page=$op['page'];
$this->assign('page',$page);
$order=$op['order'];
$this->assign('order',$order);
$this->display('/Common/header');
$this->display('/Common/search_nav');
$this->display();
$this->display('/Common/footer');
}
}
/*
返回 分页 和 分页内容数组
$ye[当前第几页],$mc[每页订单数量]
*/
function orderPage($ye,$mc=''){
$user_id = getUser(); //用户id
$orderName ='order_page'.$user_id; //订单内容缓存名字
$cache = useCache($orderName,$user_id,'file'); //将订单取出缓存
$countName ='order_count'.$user_id; //订单数量缓存名字
$cacheCount = useCache($countName,$user_id,'file'); //获取缓存订单数量
$ordCount = $this->orderGlCount(); //查询订单数量
if($cache == ""||$cacheCount!==$ordCount){ //如果订单缓存为空或者数量不相等
$order = $this->restructureOrder(); //获取订单内容数组
useCache($orderName,$user_id,'file',$order,900); //订单内容存入缓存
$count = count($order);
useCache($countName,$user_id,'file',$count,900); //订单条数存入缓存
}else{
$order = $cache;
}
$count = count($order); //总共订单数量
$pc = ceil($count/$mc); //总共需要多少页
$n=$mc*($ye-1); //每页从第几个订单开始取
$order = ($order,$n,$mc); //每页显示
for ($i=1; $i <=$pc ; $i++) { //生成页面数组
$page[] .="<a href='./member_order?page=".$i."' class='order_page_div_a'>".$i."</a>  ";
}
if($ye>1){ //显示向回按钮
$i=$ye-1;
$back = "<a href='./member_order?page=".$i."'> << </a>  ";
array_unshift($page,$back);
}
if($ye<$pc&&$pc>1){ //显示向前按钮
$i=$ye+1;
$front = "<a href='./member_order?page=".$i."'> >> </a>  ";
array_push($page,$front);
}
return array('order'=>$order,'page'=>$page); //返回订单内容和分页
}
/*
同时查询两个order表
*/
public function selectOrderBoth(){
$user_id = getUser();
$OrderG=D('Order_glancing');
// $OrderD=D('Order_detail');
return $OrderG->join("Order_detail on order_glancing.order_sn = order_detail.order_sn")->order('create_time desc')->where("user_id='$user_id'")->select();
}
/*
查询订单glancing数量
*/
public function orderGlCount(){
$user_id = getUser();
$OrderG=D('Order_glancing');
return $OrderG->where("user_id='$user_id'")->count();
}
/*
调整订单数组的结构
*/
public function restructureOrder(){
$order = $this->selectOrderBoth();
$gla =array();
$det = array();
for ($i=0; $i <count($order); $i++) {
//统一个订单号的detail部分放入 det 数组储存
$part = array_slice($order[$i], 6);
$part['order_sn']=$order[$i]['order_sn'];
$det[]=$part;
//去掉订单detail部分
array_splice($order[$i], 6);
//该订单数组 sn 号 不等于 下一个订单数组 sn 号,表示该订单查找完毕
if($order[$i]['order_sn'] !== $order[$i+1]['order_sn']){
//时间戳转换成时间
$time = $order[$i]['create_time'];
$order[$i]['create_time']=date('Y-m-d G:i:s',$time);
//将detail 部分放入 order_detail元素
$order[$i]['order_detail']=$det;
//该订单有多少个商品
$order[$i]['goods_count']=count($det);
//清空 det 数组
$det=array();
//放入 gla 数组储存
$gla[] = $order[$i];
}
}
return $gla;
}
/**
* 会员 地址 页面
*/
public function member_address(){
if(checkSession()==false){
redirect("../Login/login", 0.1, ' ');
}else{
$address=$this->getAddress();
$this->assign('address',$address);
$count=$this->addressCount();
$this->assign('count',$count);
$this->display('/Common/header');
$this->display('/Common/search_nav');
$this->display();
$this->display('/Common/footer');
}
}
/**
* 增加地址
* ajax
*/
public function appendAddress(){
if(IS_POST){
//如果地址数小于10
if($this->addressCount()<10){
$data=array();
$data['ad_address']=I('ad_address');
$data['ad_consignee']=I('ad_consignee');
$data['ad_phone']=I('ad_phone');
$data['ad_default']=I('ad_default');
$data['user_id']=getUser();
$Address = D('Address');
//如果要默认地址数大于零
// var_dump($this->defAddressCount());
if(I('ad_default')=='1'){
if($this->defAddressCount()!=='0'){
//先删除默认标识再增加地址
if($this->cancelDefAddress()){
if($data['ad_address']!==''&&$data['ad_consignee']!==''&&$data['ad_phone']!==''){
print_r($Address->add($data)?true:false);
}
}
}else{
if($data['ad_address']!==''&&$data['ad_consignee']!==''&&$data['ad_phone']!==''){
print_r($Address->add($data)?true:false);
}
}
}else{
if($data['ad_address']!==''&&$data['ad_consignee']!==''&&$data['ad_phone']!==''){
print_r($Address->add($data)?true:false);
}
}
}
}
}
/**
* 删除地址
* ajax
*/
public function deleteAddress(){
if(IS_POST){
$address_id=I('address_id');
$Address = D('Address');
if($Address->where("address_id='$address_id'")->delete()){
//默认地址数
// var_dump($this->addressCount());
if($this->addressCount()=='0'){
echo 1;
}else{
if ($this->defAddressCount()=='0') {
echo $this->setLastAddressDef()?1:0;
}else{
echo 1;
}
}
}
}
}
/**
* 更新地址
* ajax
*/
public function updateAddress(){
if(IS_POST){
$data=array();
$data['ad_address']=I('ad_address');
$data['ad_consignee']=I('ad_consignee');
$data['ad_phone']=I('ad_phone');
$address_id=I('address_id');
$Address = D('Address');
print_r($Address->where("address_id='$address_id'")->save($data));
}
}
/**
*设置默认地址
*ajax
*/
public function setDefAddress(){
$Address = D('Address');
$address_id=I('address_id');
if(IS_POST){
//如果找不到默认地址
if($this->getDefAddress()==false){
$data=array();
$data['ad_default']=1;
print_r($Address->where("address_id='$address_id'")->save($data));
}else{
//如果找到,先取消标识,再设定
if($this->cancelDefAddress()){
$data=array();
$data['ad_default']=1;
print_r($Address->where("address_id='$address_id'")->save($data));
}
}
}
}
/**
* 查找该用户所有地址
*/
public function getAddress(){
$Address = D('Address');
$user_id=getUser();
return $Address->where("user_id='$user_id'")->select();
}
/**
* 用户地址数
*/
public function addressCount(){
$Address = D('Address');
$user_id=getUser();
return $Address->where("user_id='$user_id'")->count();
}
/**
*默认地址数
*/
public function defAddressCount(){
$Address = D('Address');
$user_id=getUser();
return $Address->where("user_id='$user_id' AND ad_default=1")->count();
}
/**
* 查找默认地址
*/
public function getDefAddress(){
$Address = D('Address');
$user_id=getUser();
return $Address->where("user_id='$user_id' AND ad_default=1")->find();
}
/**
*取消默认地址的默认标识
*/
public function cancelDefAddress(){
$cancel=$this->getDefAddress();
$address_id = $cancel['address_id'];
$data=array();
$data['ad_default']='';
$Address = D('Address');
return $Address->where("address_id='$address_id'")->save($data);
}
/*
* 设定最后一条地址为默认地址
*/
public function setLastAddressDef(){
$d=$this->getAddress();
$a=end($d);
$address_id=$a['address_id'];
$data=array();
$data['ad_default'] = '1';
$Address=D('Address');
return $Address->where("address_id='$address_id'")->save($data);
}
/**
* 查找最新增加的三个地址
*/
public function getThreeAddress(){
$Address = D('Address');
$user_id=getUser();
return $Address->where("user_id='$user_id'")->order("address_id desc")->limit(3)->select();
}
}