Address.php
9.0 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
<?php
/**
* Created by PhpStorm.
* User: 86132
* Date: 2020/7/14
* Time: 8:59
*/
namespace app\api\controller;
use think\Db;
use app\common\controller\Api;
/**
* 地址接口
*/
class Address extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
/**
* @ApiTitle (地址接口-我的地址页面渲染)
* @ApiSummary (我的地址页面渲染)
* @ApiMethod (POST)
* @ApiRoute (/api/address/MyAddressPageRendering)
* @ApiParams (name="token", type="string", required=true, description="token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": [
{
"name": "收件人",
"address": "所在地址",
"address_con": "详细地址",
"mobile": "联系手机",
"id":"id",
"status":"是否默认地址:1=是,0=否"
}
]
})
*/
public function MyAddressPageRendering()
{
$user_id = $this->is_token($this->request->header());
$address_arr = Db::name('address')->where(['user_id' => $user_id])->select();
if (empty($address_arr)) {
$return = [];
$this->success('成功', $return);
}
foreach ($address_arr as $k => $v) {
$return[$k]['name'] = $v['name'];
$return[$k]['address'] = $v['address'];
$return[$k]['address_con'] = $v['address_con'];
$return[$k]['mobile'] = $v['mobile'];
$return[$k]['id'] = $v['id'];
$return[$k]['status'] = $v['status'];
}
$this->success('成功', $return);
}
/**
* @ApiTitle (地址接口-根据地址ID渲染地址)
* @ApiSummary (根据地址ID渲染地址)
* @ApiMethod (POST)
* @ApiRoute (/api/address/RenderAddressBasedOnAddressID)
* @ApiParams (name="token", type="string", required=true, description="token")
* @ApiParams (name="id", type="int", required=true, description="地址ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": {
"name": "王二蛋",
"address": "天津市",
"address_con": "天津市西青区",
"mobile": "13223332122",
"id": 1,
"status":"是否默认地址:1=是,0=否"
}
})
*/
public function RenderAddressBasedOnAddressID()
{
$user_id = $this->is_token($this->request->header());
$id = input('id');
$address_arr = Db::name('address')->where(['user_id' => $user_id])->where(['id' => $id])->find();
$return = [
'name' => $address_arr['name'],
'address' => $address_arr['address'],
'address_con' => $address_arr['address_con'],
'mobile' => $address_arr['mobile'],
'id' => $address_arr['id'],
'status' => $address_arr['status'],
];
$this->success('成功', $return);
}
/**
* @ApiTitle (地址接口-新增地址)
* @ApiSummary (新增地址)
* @ApiMethod (POST)
* @ApiRoute (/api/address/NewAddress)
* @ApiParams (name="token", type="string", required=true, description="token")
* @ApiParams (name="address", type="string", required=true, description="地址信息")
* @ApiParams (name="address_con", type="int", required=true, description="详细地址")
* @ApiParams (name="name", type="int", required=true, description="收货人姓名")
* @ApiParams (name="mobile", type="int", required=true, description="手机号")
* @ApiParams (name="type", type="int", required=true, description="是否设置为默认[1=是,0=否]")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": {
}
})
*/
public function NewAddress()
{
$user_id = $this->is_token($this->request->header());
$param = $this->request->param();
//判断是否有默认地址
if ($param['type'] == 1) {
$is_TypeAdress = Db::name('address')->where(['user_id' => $user_id])->where(['status' => 1])->find();
if ($is_TypeAdress) {
Db::name('address')->where(['id' => $is_TypeAdress['id']])->update(['status' => 0]);
}
}
$data = [
'user_id' => $user_id,
'address' => $param['address'],
'address_con' => $param['address_con'],
'name' => $param['name'],
'mobile' => $param['mobile'],
'status' => $param['type'],
'createtime' => time(),
'updatetime' => time(),
];
$res = Db::name('address')->insert($data);
if ($res) {
$this->success('成功', 1);
} else {
$this->error('失败', 0);
}
}
/**
* @ApiTitle (地址接口-修改地址)
* @ApiSummary (修改地址)
* @ApiMethod (POST)
* @ApiRoute (/api/address/ChangeAddress)
* @ApiParams (name="token", type="string", required=true, description="token")
* @ApiParams (name="address", type="string", required=true, description="地址信息")
* @ApiParams (name="address_con", type="int", required=true, description="详细地址")
* @ApiParams (name="name", type="int", required=true, description="收货人姓名")
* @ApiParams (name="mobile", type="int", required=true, description="手机号")
* @ApiParams (name="id", type="int", required=true, description="地址ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": {
}
})
*/
public function ChangeAddress()
{
$user_id = $this->is_token($this->request->header());
$param = $this->request->param();
$res = Db::name('address')->where(['user_id' => $user_id])->where(['id' => $param['id']])
->update([
'address' => $param['address'],
'address_con' => $param['address_con'],
'name' => $param['name'],
'mobile' => $param['mobile'],
'updatetime' => time()
]);
if ($res) {
$this->success('修改成功', 1);
} else {
$this->error('修改失败', 0);
}
}
/**
* @ApiTitle (地址接口-设为默认地址)
* @ApiSummary (设为默认地址)
* @ApiMethod (POST)
* @ApiRoute (/api/address/SetAsDefaultAddress)
* @ApiParams (name="token", type="string", required=true, description="token")
* @ApiParams (name="id", type="int", required=true, description="地址ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": {
}
})
*/
public function SetAsDefaultAddress()
{
$user_id = $this->is_token($this->request->header());
$id = input('id');
//检查是否已有默认地址
$is_TypeAdress = Db::name('address')->where(['user_id' => $user_id])->where(['status' => 1])->find();
if ($is_TypeAdress) {
Db::name('address')->where(['id' => $is_TypeAdress['id']])->update(['status' => 0]);
}
$res = Db::name('address')->where(['id' => $id])->where(['user_id' => $user_id])->update(['status' => 1]);
if ($res) {
$this->success('成功', 1);
} else {
$this->error('失败', 0);
}
}
/**
* @ApiTitle (地址接口-删除地址)
* @ApiSummary (删除地址)
* @ApiMethod (POST)
* @ApiRoute (/api/address/DeleteAddress)
* @ApiParams (name="token", type="string", required=true, description="token")
* @ApiParams (name="id", type="int", required=true, description="地址ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": {
}
})
*/
public function DeleteAddress()
{
$user_id = $this->is_token($this->request->header());
$id = input('id');
$res = Db::name('address')->where(['user_id' => $user_id])->where(['id' => $id])->delete();
if ($res) {
$this->success('成功', 1);
} else {
$this->error('失败', 0);
}
}
}