PlaceController.php
5.2 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
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/10/13
* Time: 11:44
*/
namespace app\address\controller;
use cmf\controller\PayBaseController;
use think\Db;
class PlaceController extends PayBaseController
{
function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求
// 指定允许其他域名访问
header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:POST');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,content-type');
}
public function add()
{
$request = request();
if ($request->isPost()) {
$post = $request->param();
$code = $post['code'];
$label = $post['label'];
$arr1 = explode(',', $code);
$post['province_id'] = $arr1[0];
$post['city_id'] = $arr1[1];
$post['county_id'] = $arr1[2];
$arr2 = explode(' ', $label);
$post['province'] = $arr2[0];
$post['city'] = $arr2[1];
$post['county'] = $arr2[2];
unset($post['code']);
unset($post['label']);
if (Db::name('zj_user_place')->insert($post)) {
echo json_encode(['msg' => 'insert ok', 'code' => 20000, 'id' => Db::name('zj_user_place')->getLastInsID()]);
exit();
} else {
echo json_encode(['msg' => 'insert failed', 'code' => 40000]);
exit();
}
} else {
echo '
<strong>接口说明:POST 添加地址接口传值方式(把省市区的id用逗号隔开参数名叫code,省市区用空格隔开参数名叫label,其他不变)</strong><br/>
name:收货人姓名<br/>
mobile:电话<br/>
code:省id,市id,区id<br/>
label:省 市 区<br/>
place:详细地址<br/>
';
}
}
public function edit()
{
$data = Db::name('zj_user_place')
->field('id,name,mobile,place,province_id,city_id,county_id,province,city,county')
->where(['id' => request()->param('id')])
->find();
if (request()->isPost()) {
echo json_encode(['data' => $data]);
} else {
echo '
<strong>接口说明:POST 编辑地址接口传值方式</strong><br/>
id:地址id<br/>
<strong>返回结果:</strong>
';
print_r($data);
}
}
public function editDo()
{
if (request()->isPost()) {
$request = request();
$post = $request->param();
$code = $post['code'];
$label = $post['label'];
$arr1 = explode(',', $code);
$post['province_id'] = $arr1[0];
$post['city_id'] = $arr1[1];
$post['county_id'] = $arr1[2];
$arr2 = explode(' ', $label);
$post['province'] = $arr2[0];
$post['city'] = $arr2[1];
$post['county'] = $arr2[2];
unset($post['code']);
unset($post['label']);
if (Db::name('zj_user_place')->update($post) >= 0) {
echo json_encode(['msg' => 'edit ok', 'code' => 20000]);
exit();
} else {
echo json_encode(['msg' => 'no changed', 'code' => 40000]);
exit();
}
} else {
echo '
<strong>接口说明:POST 更新地址接口传值方式(把省市区的id用逗号隔开参数名叫code,省市区用空格隔开参数名叫label,其他不变)</strong><br/>
id:地址id<br/>
name:收货人姓名<br/>
mobile:电话<br/>
code:省id,市id,区id<br/>
label:省 市 区<br/>
place:详细地址<br/>
';
}
}
public function del()
{
if (Db::name('zj_user_place')->delete(request()->param('id'))) {
echo json_encode(['msg' => 'del ok', 'code' => 20000]);
exit();
} else {
echo json_encode(['msg' => 'del failed', 'code' => 40000]);
exit();
}
}
public function setDefault()
{
if (request()->isPost()) {
//默认is_sta=2;
Db::name('zj_user_place')->where(['uid' => request()->param('uid')])->update(['is_sta' => 1]);
if (Db::name('zj_user_place')->where(['id' => request()->param('id')])->update(['is_sta' => request()->param('is_sta')])) {
echo json_encode(['msg' => 'set def ok', 'code' => 20000]);
exit();
} else {
echo json_encode(['msg' => 'set def failed', 'code' => 40000]);
exit();
}
} else {
echo '
<strong>接口说明:POST 设置默认地址接口传值方式</strong><br/>
id:地址id<br/>
is_sta:2
';
}
}
}