Просмотр исходного кода

feat(admin): 添加零号线用户列表功能

- 新增 ZeroUserListValidate 验证器用于列表请求参数验证
- 创建 ZeroUserListRequestEntity 请求实体和 ZeroUserListResponseEntity 响应实体
- 在 NewUser 控制器中添加 getZeroUserList 方法处理列表请求
- 更新 ZeroUserRepository 以支持列表查询
- 优化了验证和响应处理逻辑,提高了代码复用性和可维护性
shichen месяцев назад: 11
Родитель
Сommit
49e35ed1bf

+ 13 - 0
app/common/dao/user/ZeroUserLineDao.php

@@ -14,4 +14,17 @@ class ZeroUserLineDao extends BaseDao
14 14
     {
15 15
         return ZeroUserLine::class;
16 16
     }
17
+
18
+    /**
19
+     * @param $where
20
+     * @param $page
21
+     * @param $limit
22
+     * @return mixed
23
+     * 获取列表
24
+     */
25
+    public function getList($where, $page, $limit)
26
+    {
27
+        $model = $this->getModel();
28
+        return $model::getDB()->where($where)->order('id','desc')->paginate(['page' => $page, 'list_rows' => $limit]);
29
+    }
17 30
 }

+ 41 - 0
app/common/repositories/user/ZeroUserRepository.php

@@ -70,4 +70,45 @@ class ZeroUserRepository extends BaseRepository
70 70
             return false;
71 71
         }
72 72
     }
73
+
74
+    /**
75
+     * @param $param
76
+     * @return array
77
+     * 获取列表
78
+     */
79
+    public function getList($param)
80
+    {
81
+        $where = $this->getWhere($param);
82
+        $list = $this->dao->getList($where,$param->page,$param->limit);
83
+        return $list->toArray();
84
+    }
85
+
86
+    /**
87
+     * @param $param
88
+     * @return array
89
+     * 拼接搜索条件
90
+     */
91
+    private function getWhere($param): array
92
+    {
93
+        $where = [];
94
+        
95
+        // 状态条件 - 精确搜索
96
+        if (!is_null($param->status)) {
97
+            $where[] = ['status', '=', $param->status];
98
+        }
99
+        
100
+        // 团队昵称条件 - 模糊搜索
101
+        if (!is_null($param->teamName)) {
102
+            $where[] = ['team_name', 'like', '%' . $param->teamName . '%'];
103
+        }
104
+        
105
+        // 团队长uid条件 - 精确搜索
106
+        if (!is_null($param->account)) {
107
+            $userDao = new UserDao();
108
+            $userWhere[] = ['account','like','%' . $param->account . '%'];
109
+            $userData = $userDao->getWhere($userWhere,'uid');
110
+            $where[] = ['team_uid', '=', $userData->uid ?? 0];
111
+        }
112
+        return $where;
113
+    }
73 114
 }

+ 57 - 0
app/controller/admin/BaseController.php

@@ -0,0 +1,57 @@
1
+<?php
2
+
3
+namespace app\controller\admin;
4
+
5
+use app\utils\EnumUtil;
6
+use think\Request;
7
+use think\response\Json;
8
+
9
+class BaseController
10
+{
11
+    /**
12
+     * 请求实例
13
+     * @var Request
14
+     */
15
+    protected $request;
16
+
17
+    /**
18
+     * 构造函数
19
+     */
20
+    public function __construct()
21
+    {
22
+        $this->request = app('request');
23
+    }
24
+
25
+    /**
26
+     * 验证请求数据
27
+     *
28
+     * @param  mixed  $validate  验证器实例
29
+     * @param  string  $entityClass  实体类名
30
+     * @return Json|mixed 验证失败返回Json响应,成功返回实体对象
31
+     */
32
+    protected function validateRequest($validate, string $entityClass)
33
+    {
34
+        $validationResult = $validate->validate($this->request->param(), $entityClass);
35
+        if ($validationResult['status'] === 'error') {
36
+            return app('json')->fail($validationResult['message']);
37
+        }
38
+
39
+        return $validationResult['data'];
40
+    }
41
+
42
+    /**
43
+     * 验证枚举数据
44
+     *
45
+     * @param  array  $requestData
46
+     * @return Json|bool 验证失败返回Json响应,成功返回true
47
+     */
48
+    protected function validateEnumData(array $requestData)
49
+    {
50
+        $validationResult = EnumUtil::autoValidate(\app\common\enum\user\ZeroUserEnum::class, $requestData);
51
+        if (!$validationResult['valid']) {
52
+            return app('json')->fail(implode('; ', $validationResult['errors']));
53
+        }
54
+
55
+        return true;
56
+    }
57
+}

+ 44 - 23
app/controller/admin/user/NewUser.php

@@ -3,17 +3,20 @@
3 3
 namespace app\controller\admin\user;
4 4
 
5 5
 use app\common\repositories\user\ZeroUserRepository;
6
+use app\controller\admin\BaseController;
7
+use app\entity\admin\request\user\ZeroUserListRequestEntity;
6 8
 use app\entity\admin\request\user\ZeroUserRequestEntity;
7 9
 use app\entity\admin\response\user\ZeroUserLineResponseEntity;
10
+use app\entity\admin\response\user\ZeroUserListResponseEntity;
8 11
 use app\validate\admin\user\AddZeroUserValidate;
9
-use app\utils\EnumUtil;
12
+use app\validate\admin\user\ZeroUserListValidate;
10 13
 use think\response\Json;
11 14
 
12 15
 /**
13 16
  * 零号线用户控制器
14 17
  * 负责零号线用户的添加和更新操作
15 18
  */
16
-class NewUser
19
+class NewUser extends BaseController
17 20
 {
18 21
     /**
19 22
      * 用户仓库实例
@@ -28,6 +31,7 @@ class NewUser
28 31
      */
29 32
     public function __construct(ZeroUserRepository $repository)
30 33
     {
34
+        parent::__construct();
31 35
         $this->repository = $repository;
32 36
     }
33 37
 
@@ -39,29 +43,22 @@ class NewUser
39 43
      */
40 44
     public function addZeroUser(AddZeroUserValidate $validate): Json
41 45
     {
42
-        // 执行验证
43
-        $validationResult = $validate->validate();
44
-        // 验证失败,返回错误信息
45
-        if ($validationResult['status'] === 'error') {
46
-            return app('json')->fail($validationResult['message']);
46
+        // 执行验证并获取实体
47
+        $validationResult = $this->validateRequest($validate, ZeroUserRequestEntity::class);
48
+        if ($validationResult instanceof Json) {
49
+            return $validationResult;
47 50
         }
48 51
 
49
-        /** @var ZeroUserRequestEntity $entity 请求实体 */
50
-        $entity = $validationResult['data'];
51
-
52
-        // 自动验证枚举参数
53
-        $requestData = $entity->toUnderlineArray();
54
-        
55
-        // 使用智能枚举验证器自动验证,直接传递枚举类名
56
-        $validationResult = EnumUtil::autoValidate(\app\common\enum\user\ZeroUserEnum::class, $requestData);
57
-        if (!$validationResult['valid']) {
58
-            return app('json')->fail(implode('; ', $validationResult['errors']));
52
+        // 验证枚举数据
53
+        $enumValidationResult = $this->validateEnumData($validationResult->toUnderlineArray());
54
+        if ($enumValidationResult instanceof Json) {
55
+            return $enumValidationResult;
59 56
         }
60 57
         try {
61 58
             // 执行插入或更新操作
62 59
             $result = $this->repository->insertOrUpdate(
63
-                $entity->getId(),
64
-                $requestData
60
+                $validationResult->getId(),
61
+                $validationResult->toUnderlineArray()
65 62
             );
66 63
             // 根据操作结果返回相应的响应
67 64
             if ($result) {
@@ -73,20 +70,44 @@ class NewUser
73 70
             }
74 71
         } catch (\Exception $e) {
75 72
             // 记录详细异常日志
76
-            $errorMessage = '零号线用户操作异常: ' . $e->getMessage() .
77
-                           ' File: ' . $e->getFile() .
78
-                           ' Line: ' . $e->getLine();
73
+            $errorMessage = '零号线用户操作异常: '.$e->getMessage().
74
+                ' File: '.$e->getFile().
75
+                ' Line: '.$e->getLine();
79 76
             \think\facade\Log::error($errorMessage);
80 77
 
81 78
             // 根据环境返回不同的错误信息
82 79
             $isDev = app()->isDebug();
83 80
             if ($isDev) {
84 81
                 // 开发环境返回详细错误信息
85
-                return app('json')->fail('操作失败: ' . $e->getMessage());
82
+                return app('json')->fail('操作失败: '.$e->getMessage());
86 83
             } else {
87 84
                 // 生产环境返回友好提示
88 85
                 return app('json')->fail('系统繁忙,请稍后重试');
89 86
             }
90 87
         }
91 88
     }
89
+
90
+    /**
91
+     * 0号线用户列表
92
+     * @param  ZeroUserListValidate  $validate
93
+     * @return Json
94
+     */
95
+    public function getZeroUserList(ZeroUserListValidate $validate): Json
96
+    {
97
+        // 执行验证并获取实体
98
+        $validationResult = $this->validateRequest($validate, ZeroUserListRequestEntity::class);
99
+        if ($validationResult instanceof Json) {
100
+            return $validationResult;
101
+        }
102
+
103
+        // 验证枚举数据
104
+        $enumValidationResult = $this->validateEnumData($validationResult->toUnderlineArray());
105
+        if ($enumValidationResult instanceof Json) {
106
+            return $enumValidationResult;
107
+        }
108
+
109
+        // 获取列表
110
+        $list = $this->repository->getList($validationResult);
111
+        return ZeroUserListResponseEntity::response($list);
112
+    }
92 113
 }

+ 88 - 0
app/entity/admin/request/user/ZeroUserListRequestEntity.php

@@ -0,0 +1,88 @@
1
+<?php
2
+
3
+namespace app\entity\admin\request\user;
4
+
5
+use app\entity\request\RequestCommonEntity;
6
+
7
+/**
8
+ * 0号线用户列表请求参数
9
+ */
10
+class ZeroUserListRequestEntity extends RequestCommonEntity
11
+{
12
+
13
+    // 状态
14
+    public $status;
15
+
16
+    // 团队昵称
17
+    public $teamName;
18
+
19
+    // 团队长uid
20
+    public $account;
21
+
22
+    // 分页
23
+    public $page = 1;
24
+
25
+    // 每页数量
26
+    public $limit = 20;
27
+
28
+    // 获取状态
29
+    public function getStatus()
30
+    {
31
+        return $this->status;
32
+    }
33
+
34
+    // 设置状态
35
+    public function setStatus($status)
36
+    {
37
+        $this->status = $status;
38
+        return $this;
39
+    }
40
+
41
+    // 获取团队昵称
42
+    public function getTeamName(): string
43
+    {
44
+        return $this->teamName ?? '';
45
+    }
46
+
47
+    // 设置团队昵称
48
+    public function setTeamName($teamName): ZeroUserListRequestEntity
49
+    {
50
+        $this->teamName = $teamName;
51
+        return $this;
52
+    }
53
+
54
+    // 获取团队长uid
55
+    public function getAccountUid()
56
+    {
57
+        return $this->account;
58
+    }
59
+
60
+    // 设置团队长uid
61
+    public function setAccount($account): ZeroUserListRequestEntity
62
+    {
63
+        $this->account = $account;
64
+        return $this;
65
+    }
66
+
67
+    public function getPage()
68
+    {
69
+        return $this->page;
70
+    }
71
+
72
+    public function setPage($page): ZeroUserListRequestEntity
73
+    {
74
+        $this->page = $page;
75
+        return $this;
76
+    }
77
+
78
+    public function getLimit()
79
+    {
80
+        return $this->limit;
81
+    }
82
+
83
+    public function setLimit($limit): ZeroUserListRequestEntity
84
+    {
85
+        $this->limit = $limit;
86
+        return $this;
87
+    }
88
+}

+ 27 - 0
app/entity/admin/response/AdminCommonResponseEntity.php

@@ -147,6 +147,33 @@ class AdminCommonResponseEntity extends CommonEntity
147 147
     }
148 148
 
149 149
     /**
150
+     * 创建分页数据响应
151
+     * @param  array  $list  数据列表
152
+     * @param  int  $total  总记录数
153
+     * @param  int  $page  当前页码
154
+     * @param  int  $limit  每页数量
155
+     * @param  string  $message  成功消息
156
+     * @param  int  $code  状态码
157
+     * @return AdminCommonResponseEntity
158
+     */
159
+    public static function paginate(array $list, int $total, int $page, int $limit, string $message = '获取成功', int $code = 200): AdminCommonResponseEntity
160
+    {
161
+        $paginationData = [
162
+            'list' => $list,
163
+            'total' => $total,
164
+            'page' => $page,
165
+            'limit' => $limit,
166
+            'total_page' => ceil($total / $limit)
167
+        ];
168
+
169
+        return (new self())
170
+            ->setCode($code)
171
+            ->setMessage($message)
172
+            ->setData($paginationData)
173
+            ->setTimestamp(time());
174
+    }
175
+
176
+    /**
150 177
      * 转换为数组格式
151 178
      * @return array
152 179
      */

+ 52 - 0
app/entity/admin/response/user/ZeroUserListResponseEntity.php

@@ -0,0 +1,52 @@
1
+<?php
2
+
3
+namespace app\entity\admin\response\user;
4
+
5
+use app\entity\admin\response\AdminCommonResponseEntity;
6
+use think\response\Json;
7
+
8
+/**
9
+ * 零号线用户响应实体
10
+ * 继承自 AdminCommonResponseEntity,用于零号线用户相关操作的响应
11
+ * 此接口只需要返回成功或失败状态
12
+ */
13
+class ZeroUserListResponseEntity extends AdminCommonResponseEntity
14
+{
15
+    /**
16
+     * 创建零号线用户操作成功响应
17
+     * @param  null  $data
18
+     * @param  string  $message
19
+     * @param  int  $code
20
+     * @return AdminCommonResponseEntity
21
+     */
22
+    public static function success(string $message = '获取列表成功', int $code = 200, $data = null): AdminCommonResponseEntity {
23
+        return parent::success($message);
24
+    }
25
+
26
+    /**
27
+     * 创建零号线用户操作失败响应
28
+     * @param  string  $message
29
+     * @param  int  $code
30
+     * @param  null  $data
31
+     * @return AdminCommonResponseEntity
32
+     */
33
+    public static function error(string $message = '获取列表失败', int $code = 400, $data = null): AdminCommonResponseEntity {
34
+        return parent::error($message, $code);
35
+    }
36
+
37
+    /**
38
+     * @param  bool  $isPage
39
+     * @param $data
40
+     */
41
+    public static function response(array $data, bool $isPage=true) : Json
42
+    {
43
+        // if (empty($data['data'])) {
44
+        //     return app('json')->fail(self::error()->toResponseArray());
45
+        // }
46
+
47
+        // 返回列表实体
48
+        $list = parent::paginate($data['data'],$data['total'],$data['current_page'],$data['per_page']);
49
+        return app('json')->success($list->toResponseArray());
50
+    }
51
+
52
+}

+ 27 - 0
app/validate/CommonValidate.php

@@ -21,4 +21,31 @@ class CommonValidate extends Validate
21 21
         json_decode($value);
22 22
         return (json_last_error() == JSON_ERROR_NONE);
23 23
     }
24
+    /**
25
+     * 通用验证方法
26
+     * 使用父类验证规则进行数据验证
27
+     *
28
+     * @param array $data 要验证的数据
29
+     * @param string|null $entityClass 数据实体类名,如果不传则返回原始数据
30
+     * @return array
31
+     */
32
+    public function validate(array $data, ?string $entityClass = null): array
33
+    {
34
+        if (!$this->check($data)) {
35
+            return [
36
+                'status' => 'error',
37
+                'message' => $this->getError()
38
+            ];
39
+        }
40
+        
41
+        $resultData = $data;
42
+        if ($entityClass && class_exists($entityClass)) {
43
+            $resultData = $entityClass::newInstance($data);
44
+        }
45
+        
46
+        return [
47
+            'status' => 'success',
48
+            'data' => $resultData
49
+        ];
50
+    }
24 51
 }

+ 0 - 21
app/validate/admin/user/AddZeroUserValidate.php

@@ -33,25 +33,4 @@ class AddZeroUserValidate extends CommonValidate
33 33
         'team_uid.integer' => '请输入团队长uid',
34 34
         'is_remove.require' => '请选择是否移除团队',
35 35
     ];
36
-
37
-    /**
38
-     * 验证添加零用户请求
39
-     * 使用父类通用验证方法进行数据验证
40
-     *
41
-     * @return array
42
-     */
43
-    public function validate(): array
44
-    {
45
-        $data = $this->request->param();
46
-        if (!$this->check($data)) {
47
-            return [
48
-                'status' => 'error',
49
-                'message' => $this->getError()
50
-            ];
51
-        }
52
-        return [
53
-            'status' => 'success',
54
-            'data' => ZeroUserRequestEntity::newInstance($data)
55
-        ];
56
-    }
57 36
 }

+ 24 - 0
app/validate/admin/user/ZeroUserListValidate.php

@@ -0,0 +1,24 @@
1
+<?php
2
+
3
+namespace app\validate\admin\user;
4
+
5
+use app\entity\admin\request\user\ZeroUserRequestEntity;
6
+use app\validate\CommonValidate;
7
+
8
+class ZeroUserListValidate extends CommonValidate
9
+{
10
+    protected $failException = true;
11
+
12
+    protected $rule = [
13
+        'status|状态' => 'integer',
14
+        'team_name|团队昵称' => 'max:20',
15
+        'account|账号' => 'max:20',
16
+        'page|页码' => 'integer|min:1',
17
+        'limit|每页数量' => 'integer|min:1|max:100',
18
+    ];
19
+
20
+    protected $message = [
21
+        'status.integer' => '非法状态',
22
+        'team_uid.integer' => '非法团队长uid',
23
+    ];
24
+}