HouseAdmin.php
8.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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/7/10
* Time: 17:32
*/
namespace app\api\controller;
use app\common\controller\Api;
use think\Cache;
use think\Db;
use think\Validate;
use app\api\model\House;
use app\api\model\HouseAdmin as HouseAdminModel;
/**
* 社区管理员
*/
class HouseAdmin extends Api
{
protected $noNeedLogin = [''];
protected $noNeedRight = ['*'];
/**
* @ApiTitle (社区管理员-首页)
* @ApiSummary (社区管理员-首页)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="house_id", type="inter", required=false, description="社区ID")
* @ApiReturn({
"code": 1,
"msg": "success",
"time": "1598707632",
"data": {
"id": 4, //管理员ID
"house_id": 17, //小区ID
"is_direct": 1, //是否为主管理员 1是2否
"nickname": "", //管理员昵称
"avatar": "", //管理员头像
"house": { // 小区信息
"id": 17, //小区ID
"name": "碧海花园小区" //小区名称
}
}
})
*/
public function index()
{
$user = $this->auth->getUser();
$house_id = $this->request->param('house_id');
if($house_id){
$info = HouseAdminModel::get(['user_id'=>$user['id'],'house_id'=>$house_id]);
empty($info) && $this->error('您不是该小区的管理员');
$info->visible(['id','house_id','is_direct'])->toArray();
}else{
$info = HouseAdminModel::where(['user_id'=>$user['id']])
->order(['is_direct'=>'asc','createtime'=>'asc'])
->field('id,house_id,is_direct')
->find();
empty($info) && $this->error('您还不是管理员');
}
$data = [
'nickname' => $user['admin_nickname'],
'avatar' => cdnurl($user['admin_avatar'],true),
'house' => House::where('id',$info['house_id'])->field('id,name')->find(),
];
$data = array_merge($info->toArray(),$data);
$this->success('success',$data);
}
/**
* @ApiTitle (社区管理员-选择社区)
* @ApiSummary (社区管理员-选择社区)
* @ApiMethod (POST)
* @ApiRoute (/api/house_admin/houseList)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="keyword", type="string", required=false, description="关键字")
*
* @ApiReturn({
"code": 1,
"msg": "success",
"time": "1598688508",
"data": [{
"id": 16, //小区ID
"name": "兴安苑", //小区名称
}]
})
*/
public function houseList()
{
$user = $this->auth->getUser();
$keyword = $this->request->param('keyword');
$where = [];
if(!empty($keyword)){
$where['h.name'] = ['like',"%$keyword%"];
}
// 查出该管理员已绑定的小区
$list = HouseAdminModel::alias('ha')
->join('house h','h.id = ha.house_id')
->where('ha.user_id',$user['id'])
->where($where)
->field("h.id,h.name")
->order('ha.is_direct')
->select();
$this->success('success',$list);
}
/**
* @ApiTitle (社区管理员-个人信息)
* @ApiSummary (社区管理员-个人信息)
* @ApiMethod (POST)
* @ApiRoute (/api/house_admin/profile)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "success",
"time": "1598753244",
"data": {
"nickname": "", //昵称
"avatar": "http://community.qiniu.brotop.cn", //头像
"mobile": "" //手机号
}
})
*/
public function profile()
{
$user = $this->auth->getUser();
$data = [
'nickname' => $user['admin_nickname'],
'avatar' => cdnurl($user['admin_avatar'],true),
'mobile' => $user['admin_mobile'],
];
$this->success('success',$data);
}
/**
* 社区管理员-修改会员个人信息
*
* @param string $avatar 头像地址
* @param string $nickname 昵称
*/
public function profileEdit()
{
$user = $this->auth->getUser();
$nickname = $this->request->request('nickname');
$avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
$user->admin_nickname = $nickname;
$user->admin_avatar = $avatar;
$user->save();
$this->success();
}
/**
* 社区管理员-修改手机号
*
* @param string $mobile 手机号
* @param string $captcha 验证码
*/
public function changemobile()
{
$user = $this->auth->getUser();
$mobile = $this->request->request('mobile');
$captcha = $this->request->request('captcha');
if (!$mobile || !$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
if (\app\common\model\User::where('admin_mobile', $mobile)->where('id', '<>', $user->id)->find()) {
$this->error(__('Mobile already exists'));
}
$code = Cache::get($mobile);
if ($code != $captcha) {
$this->error(__('Captcha is incorrect'));
}
$user->admin_mobile = $mobile;
$user->save();
Cache::rm($mobile);
$this->success();
}
/**
* @ApiTitle (社区管理员-公告管理)
* @ApiSummary (社区管理员-公告管理)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="house_id", type=inter, required=true, description="社区id")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "success",
"time": "1598755666",
"data": {
"total_num": 1, //数据总条数
"info": [{
"id": 1, //公告id
"title": "周六交电费", //社区公告标题
"look_num": 2, //浏览人数
"createtime": "2020-08-19", //时间
"is_delete": 2, //是否下架:1=下架,2=未下架
"avatar": [{ //用户头像
"avatar": "",
"id": 1
}]
}]
}
})
*/
public function house_board_list()
{
$page = $this->request->param('page', 1, 'intval');
$pageNum = $this->request->param('pageNum', 10, 'intval');
$house_id = $this->request->param('house_id');
empty($house_id) && $this->error('社区id不能为空');
$data['total_num'] = Db::name('house_board')
->where('house_id',$house_id)
->count();
$data['info'] = Db::name('house_board')
->where('house_id',$house_id)
->field('id,title,look_num,createtime,deletetime')
->order('weigh desc')
->page($page,$pageNum)
->select();
foreach ($data['info'] as &$v){
$v['createtime'] = date('Y-m-d',$v['createtime']);
$v['is_delete'] = $v['deletetime'] > 0 ? 1 : 2;
$v['avatar'] = Db::name('house_board_detail')
->alias('a')
->join('user b','a.user_id = b.id')
->where('a.house_board_id',$v['id'])
->field('b.avatar,b.id')
->limit(6)
->select();
unset($v['deletetime']);
}
$this->success('success',$data);
}
}