bc518174
王天杨
提交两个项目文件
|
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
|
<?php
declare(strict_types=1);
namespace app\controller\admin;
use app\BaseController;
use think\facade\Db;
/**
* 小程序 / 微信登录用户管理
*/
class MpUser extends BaseController
{
public function list()
{
$keyword = trim((string) $this->request->get('keyword', ''));
$page = max(1, (int) $this->request->get('page', 1));
$pageSize = min(50, max(10, (int) $this->request->get('page_size', 20)));
$q = Db::name('mp_user')->alias('u');
if ($keyword !== '') {
$kw = '%' . addcslashes($keyword, '%_\\') . '%';
$q->whereLike('u.nickname|u.phone|u.openid', $kw);
}
$paginator = $q->order('u.id', 'desc')->paginate([
'list_rows' => $pageSize,
'page' => $page,
]);
$list = [];
foreach ($paginator->items() as $row) {
$row = (array) $row;
$uid = (int) $row['id'];
$phone = trim((string) ($row['phone'] ?? ''));
$list[] = [
'id' => $uid,
'phone' => $phone,
'openid' => $this->maskOpenid((string) ($row['openid'] ?? '')),
'unionid' => $this->maskOpenid((string) ($row['unionid'] ?? '')),
'nickname' => $row['nickname'],
'avatar' => $row['avatar'],
'bio' => $row['bio'],
'gender' => (int) $row['gender'],
'status' => (int) ($row['status'] ?? 1),
'work_count' => Db::name('work')->where('user_id', $uid)->count(),
'create_time' => $row['create_time'],
];
}
return json([
'code' => 0,
'msg' => 'ok',
'data' => [
'list' => $list,
'total' => $paginator->total(),
'page' => $paginator->currentPage(),
'page_size' => $paginator->listRows(),
],
]);
}
public function save()
{
$id = (int) $this->request->post('id', 0);
$nickname = trim((string) $this->request->post('nickname', ''));
$avatar = trim((string) $this->request->post('avatar', ''));
$bio = trim((string) $this->request->post('bio', ''));
$gender = (int) $this->request->post('gender', 0);
$status = (int) $this->request->post('status', 1);
if ($id <= 0) {
return json(['code' => 400, 'msg' => 'id 必填', 'data' => []]);
}
if ($nickname === '') {
return json(['code' => 400, 'msg' => '名字不能为空', 'data' => []]);
}
if (mb_strlen($nickname) > 32) {
return json(['code' => 400, 'msg' => '名字过长', 'data' => []]);
}
if (mb_strlen($bio) > 200) {
return json(['code' => 400, 'msg' => '简介过长', 'data' => []]);
}
if (mb_strlen($avatar) > 512) {
return json(['code' => 400, 'msg' => '头像地址过长', 'data' => []]);
}
$data = [
'update_time' => date('Y-m-d H:i:s'),
'status' => $status === 0 ? 0 : 1,
'gender' => $gender,
'nickname' => $nickname,
'bio' => $bio,
'avatar' => $avatar,
];
Db::name('mp_user')->where('id', $id)->update($data);
return json(['code' => 0, 'msg' => '已保存', 'data' => []]);
}
/**
* 删除用户及其作品、作品关联表;并清除该用户对他人的点赞记录
*/
public function delete(int $id)
{
if ($id <= 0) {
return json(['code' => 400, 'msg' => '参数错误', 'data' => []]);
}
if (!Db::name('mp_user')->find($id)) {
return json(['code' => 404, 'msg' => '用户不存在', 'data' => []]);
}
Db::transaction(function () use ($id) {
$workIds = Db::name('work')->where('user_id', $id)->column('id');
if (!empty($workIds)) {
Db::name('work_like')->whereIn('work_id', $workIds)->delete();
Db::name('work_category')->whereIn('work_id', $workIds)->delete();
Db::name('work_image')->whereIn('work_id', $workIds)->delete();
Db::name('work')->where('user_id', $id)->delete();
}
Db::name('work_like')->where('user_id', $id)->delete();
Db::name('mp_user')->where('id', $id)->delete();
});
return json(['code' => 0, 'msg' => '已删除', 'data' => []]);
}
protected function maskOpenid(string $s): string
{
if ($s === '') {
return '—';
}
$len = strlen($s);
if ($len <= 8) {
return substr($s, 0, 2) . '****';
}
return substr($s, 0, 4) . '****' . substr($s, -4);
}
}
|