Address.php
3.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
<?php
namespace app\api\controller\wanlshop;
use app\common\controller\Api;
/**
* WanlShop地址接口
*/
class Address extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
/**
* 获取地址列表
*
* @ApiSummary (WanlShop 地址接口获取地址列表)
* @ApiMethod (GET)
*
*/
public function getaddress()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$list = model('app\api\model\wanlshop\Address')
->where('user_id', $this->auth->id)
->field('id,user_id,adcode,address,address_name,city,citycode,country,default,district,formatted_address,location,mobile,name,province')
->order('updatetime desc')
->paginate();
$this->success('返回成功', $list);
}
/**
* 修改/新增地址
*
* @ApiSummary (WanlShop 地址接口修改/新增地址)
* @ApiMethod (POST)
*
* @param string $user_id 用户ID
*/
public function address()
{
if ($this->request->isPost()) {
//设置过滤方法
$this->request->filter(['strip_tags']);
$request = $this->request->post();
$address = new \app\api\model\wanlshop\Address();
$data = $request['data'];
$data['user_id'] = $this->auth->id;
$count = $address->where(['user_id'=>$data['user_id']])->count();
// 操作
switch ($request['type']) {
case "edit":
if($count <= 1){
$data['default'] = 1;
$address->allowField(true)->save($data,['id' => $data['id']]);
$this->success('ok','成功(仅一个不许修改默认)');
}else{
// 更新
$address->allowField(true)->save($data,['id' => $data['id']]);
// 单独设置默认,避免非默认消耗资源
if($data['default'] == 1){
$list = \app\api\model\wanlshop\Address::all(['user_id'=>$data['user_id']]);
$list = collection($list)->toArray();
$itemdata = [];
foreach($list as $item){
if($item['id'] == $data['id']){
$item['default'] = 1;
}else{
$item['default'] = 0;
}
$itemdata[] = $item;
}
$address->allowField(true)->saveAll($itemdata);
}
$this->success('ok');
}
break;
case "add":
if($count == 0){
// 新增
$data['default'] = 1;
$address->data($data);
$address->save();
$this->success('地址回调',$address);
}else{
$address->data($data);
$address->save();
if($data['default'] == 1){
$list = \app\api\model\wanlshop\Address::all(['user_id'=>$data['user_id']]);
$list = collection($list)->toArray();
$itemdata = [];
foreach($list as $item){
if($item['id'] == $address->id){
$item['default'] = 1;
}else{
$item['default'] = 0;
}
$itemdata[] = $item;
}
$address->saveAll($itemdata);
}
$this->success('新增成功',[]);
}
break;
}
} else {
$this->error(__('非法请求'));
}
}
/**
* 删除地址
*
* @ApiSummary (WanlShop 地址接口删除地址)
* @ApiMethod (POST)
*
* @param string $id 地址ID
*/
public function deladdress()
{
if ($this->request->isPost()) {
//设置过滤方法
$this->request->filter(['strip_tags']);
$id = $this->request->post('id');
if (!$id) {
$this->error(__('Invalid parameters'));
}
if(model('app\api\model\wanlshop\Address')->where(['id'=>$id,'user_id'=>$this->auth->id])->delete()){
$this->success(__('删除成功',[]));
}else{
$this->error(__('删除失败'));
}
} else {
$this->error(__('非法请求'));
}
}
}