Delivery.php
9.33 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
<?php
/**
* Public 收货地址类
* User: elkan
* Date: 2014-7-30
* Time: PM1:08
*/
class YHMApi_App_V1_Delivery extends YHMApi_App_V1_Base {
/**
* 获取用户收货地址
* @param array $params
* @param string $fileds
* @return array
*/
static function getListByUid(array $params, $fileds = '*') {
if (empty($params['uid'])) {
return self::result(400, '用户id不能为空');
}
$data = YHMDelivery_Models_Delivery_Client::getUserDelivery($params['uid']);
$getData = array();
foreach ($data as $k => $v) {
$Province = YHMPassport_Models_Area_Client::getAreaByCode(substr($v['area_code'], 0, 2));
$City = YHMPassport_Models_Area_Client::getAreaByCode(substr($v['area_code'], 0, 4));
$District = YHMPassport_Models_Area_Client::getAreaByCode($v['area_code']);
$list = array(
'id' => $v['id'],
'delivery_name' => $v['delivery_name'],
'mobile' => $v['mobile'],
'area_code' => $v['area_code'],
'address' => $v['address'],
'is_default' => $v['is_default'],
'zip_code' => $v['zip_code']
);
if ($Province == $City) {
$list['area_text'] = $City . $District;
} else {
$list['area_text'] = $Province . $City . $District;
}
$getData[] = $list;
}
if (empty($data)) {
return self::result(200, '暂无地址',array());
}
return self::result(200, '收获地址列表', $getData);
}
/**
* 添加用户收货地址
* @param array $params
* @param type $fileds
* @return type
*/
static function add(array $params, $fileds = '*') {
if (empty($params['uid'])) {
return self::result(400, '用户id不能为空');
}
if (empty($params['delivery_name'])) {
return self::result(400, '收获人姓名不能为空');
}
if (empty($params['area_code'])) {
return self::result(400, '地区不能为空');
}
if (empty($params['address'])) {
return self::result(400, '详细地址不能为空');
}
if (empty($params['zip_code'])) {
return self::result(400, '邮编不能为空');
}
if (empty($params['mobile'])) {
return self::result(400, '手机号不能为空');
}
$is_default = !empty($params['is_default']) ? $params['is_default'] : 'N';
if ($is_default == 'Y') {
//先把默认地址清零
YHMDelivery_Models_Delivery_Client::updateEmptyDefaultAddress($params['uid']);
}
$data = YHMDelivery_Models_Delivery_Client::getUserDelivery($params['uid']);
if (count($data)<1){
$is_default='Y';
}
$id = YHMDelivery_Models_Delivery_Client::add($params['uid'], $params['delivery_name'], $params['area_code'], $params['address'], $params['zip_code'], $params['mobile'], $is_default);
if ($id < 1) {
return self::result(400, '添加失败');
}
return self::result(200, '添加成功', array("id" => $id));
}
/**
* 设为默认收获地址
* @param array $params
* @param type $fileds
* @return array
*/
static function setDefault(array $params, $fileds = '*') {
if (empty($params['id'])) {
return self::result(400, '收获地址id不能为空');
}
if (empty($params['uid'])) {
return self::result(400, '用户id不能为空');
}
//先把默认地址清零
YHMDelivery_Models_Delivery_Client::updateEmptyDefaultAddress($params['uid']);
//设置默认地址
YHMDelivery_Models_Delivery_Client::updateUserDefaultAddress($params['id'], $params['uid']);
return self::result(200, '设置成功');
}
/**
* 修改地址信息
* @param array $params
* @param type $fileds
* @return array
*/
static function update(array $params, $fileds = '*') {
if (empty($params['id'])) {
return self::result(400, 'id不能为空');
}
if (empty($params['uid'])) {
return self::result(400, '用户ID不能为空');
}
if (empty($params['delivery_name'])) {
return self::result(400, '收货人姓名不能为空');
}
if (empty($params['area_code'])) {
return self::result(400, '地区不能为空');
}
if (empty($params['address'])) {
return self::result(400, '详细地址不能为空');
}
if (empty($params['zip_code'])) {
return self::result(400, '邮编不能为空');
}
if (empty($params['mobile'])) {
return self::result(400, '手机号不能为空');
}
$is_default = !empty($params['is_default']) ? $params['is_default'] : 'N';
if($is_default == 'Y') {
//先把默认地址清零
YHMDelivery_Models_Delivery_Client::updateEmptyDefaultAddress($params['uid']);
}
YHMDelivery_Models_Delivery_Client::updateDeliveryAddress($params['id'], $params['uid'], $params['delivery_name'], $params['area_code'], $params['address'], $params['zip_code'], $params['mobile'], $is_default);
return self::result(200, "修改成功");
}
/**
* 删除修改地址
* @param array $params
* @param type $fileds
* @return array
*/
static function delete(array $params, $fileds = '*') {
if (empty($params['id'])) {
return self::result(400, 'id不能为空');
}
if (empty($params['uid'])) {
return self::result(400, '用户id不能为空');
}
$id = YHMDelivery_Models_Delivery_Client::deleteDefaultAddress($params['id'], $params['uid']);
if ($id < 1) {
return self::result(400, '删除失败');
}
return self::result(200, '删除成功');
}
/**
* 获取地址信息
* @param array $params
* @param string $fileds
*/
public static function getSubAreaList(array $params, $fileds = '*') {
$parent_id = intval($params['area_id']);
$retData = array();
$areaList = YHMPassport_Models_Area_Client::getSubAreaList($parent_id);
foreach ($areaList as $area) {
$retData[] = array(
'id' => $area['id'],
'code' => $area['code'],
'caption' => $area['caption']
);
}
return self::result(200, '地址列表', $retData);
}
/**
* 通过经纬度坐标获取地区
* @param array $params
* @param string $fields
*/
public static function getAreaByCoord(array $params, $fields = '*') {
$lng = $params['lng'];
$lat= $params['lat'];
$addrData = YHMUtils_Lbs_Client::getAddrFromLatlng($lat, $lng);
$retData = array();
if(!empty($addrData)) {
$area_code = YHMPassport_Models_Area_Client::getIdByCaption($addrData['area']);
if(!empty($area_code)) {
$retData = array(
'area_code' => $area_code,
'province' => $addrData['province'],
'city' => $addrData['city'],
'area' => $addrData['area']
);
return self::result(200, '获取成功', $retData);
}
}
return self::result(412, '获取失败');
}
/**
* 获取默认送货地址
* @param array $params
* @param string $fields
*/
public static function getDefaultDelivery(array $params, $fields = '*') {
$retData = YHMDelivery_Models_Delivery_Client::getDefaultDeliveryByUid($params['uid']);
//如没有默认地址,取最新的地址
if (empty($retData)) {
$retData = YHMDelivery_Models_Delivery_Client::getDeliveryByUid($params['uid']);
}
//没有地址则返回暂无数据
if(empty($retData)){
return self::result(400,'暂无数据');
}
$retData = array(
'id' => $retData['id'],
'delivery_name' => $retData['delivery_name'],
'mobile' => $retData['mobile'],
'area_code' => $retData['area_code'],
'address' => $retData['address'],
'is_default' => $retData['is_default'],
'zip_code' => $retData['zip_code']
);
$Province = YHMPassport_Models_Area_Client::getAreaByCode(substr($retData['area_code'], 0, 2));
$City = YHMPassport_Models_Area_Client::getAreaByCode(substr($retData['area_code'], 0, 4));
$District = YHMPassport_Models_Area_Client::getAreaByCode($retData['area_code']);
if ($Province == $City) {
$retData['area_text'] = $City . $District;
} else {
$retData['area_text'] = $Province . $City . $District;
}
return self::result(200, '成功', $retData);
}
/**
* 通过id获取地区
* @param int $id
* @return type
*/
public static function getDeliveryById(array $params, $fields = '*') {
if ($params['id'] < 1) {
return self::result(400, '用户id不能为空');
}
$retData = YHMDelivery_Models_Delivery_Client::getDeliveryById($params['id']);
return self::result(200, '成功', $retData);
}
}