瀏覽代碼

feat(user): 添加零号线用户功能

- 新增零号线用户添加和更新功能
- 实现零号线用户数据验证和响应实体- 添加零号线用户相关模型和DAO
- 实现零号线用户插入或更新逻辑
shichen 11 月之前
父節點
當前提交
812279a8a0

+ 10 - 0
app/common/dao/user/UserDao.php

@@ -446,4 +446,14 @@ class UserDao extends BaseDao
446 446
             return [];
447 447
         }
448 448
     }
449
+
450
+    /**
451
+     * @param $userId
452
+     * @return array
453
+     * 获取直推用户ID
454
+     */
455
+    public function getDirectUserIds($userId)
456
+    {
457
+        return $this::getModel()::getDB()->where('level_id', '=', $userId)->column('uid');
458
+    }
449 459
 }

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

@@ -0,0 +1,17 @@
1
+<?php
2
+/**
3
+ * zero user line dao
4
+ */
5
+namespace app\common\dao\user;
6
+
7
+
8
+use app\common\dao\BaseDao;
9
+use app\common\model\user\ZeroUserLine;
10
+
11
+class ZeroUserLineDao extends BaseDao
12
+{
13
+    protected function getModel(): string
14
+    {
15
+        return ZeroUserLine::class;
16
+    }
17
+}

+ 28 - 0
app/common/model/user/ZeroUserLine.php

@@ -0,0 +1,28 @@
1
+<?php
2
+/**
3
+ * user_zero_line
4
+ */
5
+namespace app\common\model\user;
6
+
7
+
8
+use app\common\model\BaseModel;
9
+
10
+class ZeroUserLine extends BaseModel
11
+{
12
+
13
+    /**
14
+     * @return string
15
+     */
16
+    public static function tablePk(): string
17
+    {
18
+        return 'id';
19
+    }
20
+
21
+    /**
22
+     * @return string
23
+     */
24
+    public static function tableName(): string
25
+    {
26
+        return 'user_zero_line';
27
+    }
28
+}

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

@@ -0,0 +1,73 @@
1
+<?php
2
+
3
+namespace app\common\repositories\user;
4
+
5
+
6
+use app\common\dao\user\UserDao;
7
+use app\common\dao\user\ZeroUserLineDao;
8
+use app\common\repositories\BaseRepository;
9
+use think\db\exception\DbException;
10
+use think\facade\Db;
11
+
12
+class ZeroUserRepository extends BaseRepository
13
+{
14
+
15
+    private $systemUserId = 1;
16
+
17
+    /**
18
+     * @var ZeroUserLineDao
19
+     */
20
+    protected $dao;
21
+
22
+    /**
23
+     * UserGroupRepository constructor.
24
+     * @param  ZeroUserLineDao  $dao
25
+     */
26
+    public function __construct(ZeroUserLineDao $dao)
27
+    {
28
+        $this->dao = $dao;
29
+    }
30
+
31
+    /**
32
+     * 插入或更新零号线用户数据
33
+     *
34
+     * @param  int|null  $id  用户ID,为null时表示新增
35
+     * @param  array  $data  用户数据
36
+     * @return \app\common\dao\BaseDao|bool|\think\Model
37
+     * @throws DbException
38
+     */
39
+    public function insertOrUpdate(?int $id, array $data)
40
+    {
41
+        // 团队昵称为空时,取用户昵称
42
+        $userDao = new UserDao();
43
+        if (!$data['team_name']) {
44
+            $userData = $userDao->get($data['team_uid']);
45
+            $data['team_name'] = $userData->nickname;
46
+        }
47
+
48
+        // 判断是更新现有记录还是创建新记录
49
+        if ($data['id'] > 0) {
50
+            return $this->dao->update($id, $data);
51
+        }
52
+
53
+        try {
54
+            Db::startTrans();
55
+            // 0号线用户修改节点
56
+            $userDao->update($data['team_uid'], ['level_id' => $this->systemUserId]);
57
+
58
+            // 如果不带走团队,所有直推用户节点id修改成上级id
59
+            if ($data['is_remove'] != 1) {
60
+                $directUserIds = $userDao->getDirectUserIds($data['team_uid']);
61
+                $upLevelId = $userData->level_id;
62
+                $userDao->updates($directUserIds, ['level_id' => $upLevelId]);
63
+            }
64
+            $this->dao->create($data);
65
+
66
+            Db::commit();
67
+            return true;
68
+        } catch (\Exception $e) {
69
+            Db::rollback();
70
+            return false;
71
+        }
72
+    }
73
+}

+ 72 - 0
app/controller/admin/user/NewUser.php

@@ -0,0 +1,72 @@
1
+<?php
2
+
3
+namespace app\controller\admin\user;
4
+
5
+use app\common\repositories\user\ZeroUserRepository;
6
+use app\entity\admin\request\user\ZeroUserRequestEntity;
7
+use app\entity\admin\response\user\ZeroUserLineResponseEntity;
8
+use app\validate\admin\user\AddZeroUserValidate;
9
+use think\response\Json;
10
+
11
+/**
12
+ * 零号线用户控制器
13
+ * 负责零号线用户的添加和更新操作
14
+ */
15
+class NewUser
16
+{
17
+    /**
18
+     * 用户仓库实例
19
+     * @var ZeroUserRepository
20
+     */
21
+    protected $repository;
22
+
23
+    /**
24
+     * 构造函数
25
+     *
26
+     * @param  ZeroUserRepository  $repository
27
+     */
28
+    public function __construct(ZeroUserRepository $repository)
29
+    {
30
+        $this->repository = $repository;
31
+    }
32
+
33
+    /**
34
+     * 添加或更新零号线用户
35
+     *
36
+     * @param  AddZeroUserValidate  $validate  验证器实例
37
+     * @return Json
38
+     */
39
+    public function addZeroUser(AddZeroUserValidate $validate): Json
40
+    {
41
+        // 执行验证
42
+        $validationResult = $validate->validate();
43
+        // 验证失败,返回错误信息
44
+        if ($validationResult['status'] === 'error') {
45
+            return app('json')->fail($validationResult['message']);
46
+        }
47
+
48
+        /** @var ZeroUserRequestEntity $entity 请求实体 */
49
+        $entity = $validationResult['data'];
50
+        try {
51
+            // 执行插入或更新操作
52
+            $result = $this->repository->insertOrUpdate(
53
+                $entity->getId(),
54
+                $entity->getArray()
55
+            );
56
+            // 根据操作结果返回相应的响应
57
+            if ($result) {
58
+                $response = ZeroUserLineResponseEntity::success();
59
+                return app('json')->success($response->toResponseArray());
60
+            } else {
61
+                $response = ZeroUserLineResponseEntity::error();
62
+                return app('json')->fail($response->toResponseArray());
63
+            }
64
+        } catch (\Exception $e) {
65
+            // 记录异常日志
66
+            \think\facade\Log::error('零号线用户操作异常: '.$e->getMessage());
67
+
68
+            // 返回友好的错误信息
69
+            return app('json')->fail('系统繁忙,请稍后重试');
70
+        }
71
+    }
72
+}

+ 140 - 0
app/entity/admin/request/user/ZeroUserRequestEntity.php

@@ -0,0 +1,140 @@
1
+<?php
2
+
3
+namespace app\entity\admin\request\user;
4
+
5
+use app\entity\request\RequestCommonEntity;
6
+
7
+/**
8
+ * 添加0号线用户请求参数
9
+ */
10
+class ZeroUserRequestEntity extends RequestCommonEntity
11
+{
12
+    // id
13
+    public $id;
14
+
15
+    // 万能验证码
16
+    public $customCode;
17
+
18
+    // 状态
19
+    public $status;
20
+
21
+    // 团队昵称
22
+    public $teamName;
23
+
24
+    // 团队长uid
25
+    public $teamUid;
26
+
27
+    // 是否移除团队
28
+    public $isRemove;
29
+
30
+    // 获取id
31
+    public function getId()
32
+    {
33
+        return $this->id;
34
+    }
35
+
36
+    // 设置id
37
+    public function setId($id)
38
+    {
39
+        $this->id = $id;
40
+        return $this;
41
+    }
42
+
43
+    // 获取万能验证码
44
+    public function getCustomCode()
45
+    {
46
+        return $this->customCode;
47
+    }
48
+
49
+    // 设置万能验证码
50
+    public function setCustomCode($customCode)
51
+    {
52
+        $this->customCode = $customCode ?? null;
53
+        return $this;
54
+    }
55
+
56
+    // 获取团队昵称
57
+    public function getStatus()
58
+    {
59
+        return $this->status;
60
+    }
61
+
62
+    public function setStatus($status)
63
+    {
64
+        $this->status = $status;
65
+        return $this;
66
+    }
67
+
68
+    // 获取团队昵称
69
+    public function getTeamName()
70
+    {
71
+        return $this->teamName ?? '';
72
+    }
73
+
74
+    public function setTeamName($teamName): ZeroUserRequestEntity
75
+    {
76
+        $this->teamName = $teamName;
77
+        return $this;
78
+    }
79
+
80
+    // 获取团队长uid
81
+    public function getTeamUid()
82
+    {
83
+        return $this->teamUid;
84
+    }
85
+
86
+    public function setTeamUid($teamUid): ZeroUserRequestEntity
87
+    {
88
+        $this->teamUid = $teamUid;
89
+        return $this;
90
+    }
91
+
92
+    // 获取是否移除
93
+    public function getIsRemove()
94
+    {
95
+        return $this->isRemove;
96
+    }
97
+
98
+    public function setIsRemove($isRemove): ZeroUserRequestEntity
99
+    {
100
+        $this->isRemove = $isRemove;
101
+        return $this;
102
+    }
103
+
104
+    /**
105
+     * 将请求参数转换为数组格式
106
+     * 用于数据库操作或API传输
107
+     * 动态获取当前类的所有公共属性,使用下划线分割的键名
108
+     *
109
+     * @return array
110
+     */
111
+    public function getArray(): array
112
+    {
113
+        $result = [];
114
+        $reflection = new \ReflectionClass($this);
115
+        $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
116
+        
117
+        foreach ($properties as $property) {
118
+            $propertyName = $property->getName();
119
+            // 跳过继承的属性
120
+            if ($property->getDeclaringClass()->getName() !== self::class) {
121
+                continue;
122
+            }
123
+            
124
+            // 将驼峰命名转换为下划线命名
125
+            $key = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $propertyName));
126
+            
127
+            // 获取对应的getter方法名
128
+            $getterMethod = 'get' . ucfirst($propertyName);
129
+            
130
+            if (method_exists($this, $getterMethod)) {
131
+                $result[$key] = $this->$getterMethod();
132
+            } else {
133
+                // 如果没有对应的getter方法,直接获取属性值
134
+                $result[$key] = $this->$propertyName;
135
+            }
136
+        }
137
+        
138
+        return $result;
139
+    }
140
+}

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

@@ -0,0 +1,171 @@
1
+<?php
2
+
3
+namespace app\entity\admin\response;
4
+
5
+use app\entity\CommonEntity;
6
+
7
+/**
8
+ * 管理员公共响应实体
9
+ * 用于统一接口返回格式
10
+ */
11
+class AdminCommonResponseEntity extends CommonEntity
12
+{
13
+    /**
14
+     * 状态码
15
+     * @var int
16
+     */
17
+    public $code;
18
+
19
+    /**
20
+     * 消息内容
21
+     * @var string
22
+     */
23
+    public $message;
24
+
25
+    /**
26
+     * 响应数据
27
+     * @var mixed
28
+     */
29
+    public $data;
30
+
31
+    /**
32
+     * 时间戳
33
+     * @var int
34
+     */
35
+    public $timestamp;
36
+
37
+    /**
38
+     * 获取状态码
39
+     * @return int
40
+     */
41
+    public function getCode(): int
42
+    {
43
+        return $this->code;
44
+    }
45
+
46
+    /**
47
+     * 设置状态码
48
+     * @param int $code
49
+     * @return AdminCommonResponseEntity
50
+     */
51
+    public function setCode(int $code): AdminCommonResponseEntity
52
+    {
53
+        $this->code = $code;
54
+        return $this;
55
+    }
56
+
57
+    /**
58
+     * 获取消息内容
59
+     * @return string
60
+     */
61
+    public function getMessage(): string
62
+    {
63
+        return $this->message;
64
+    }
65
+
66
+    /**
67
+     * 设置消息内容
68
+     * @param string $message
69
+     * @return AdminCommonResponseEntity
70
+     */
71
+    public function setMessage(string $message): AdminCommonResponseEntity
72
+    {
73
+        $this->message = $message;
74
+        return $this;
75
+    }
76
+
77
+    /**
78
+     * 获取响应数据
79
+     * @return mixed
80
+     */
81
+    public function getData()
82
+    {
83
+        return $this->data;
84
+    }
85
+
86
+    /**
87
+     * 设置响应数据
88
+     * @param mixed $data
89
+     * @return AdminCommonResponseEntity
90
+     */
91
+    public function setData($data): AdminCommonResponseEntity
92
+    {
93
+        $this->data = $data;
94
+        return $this;
95
+    }
96
+
97
+    /**
98
+     * 获取时间戳
99
+     * @return int
100
+     */
101
+    public function getTimestamp(): int
102
+    {
103
+        return $this->timestamp;
104
+    }
105
+
106
+    /**
107
+     * 设置时间戳
108
+     * @param int $timestamp
109
+     * @return AdminCommonResponseEntity
110
+     */
111
+    public function setTimestamp(int $timestamp): AdminCommonResponseEntity
112
+    {
113
+        $this->timestamp = $timestamp;
114
+        return $this;
115
+    }
116
+
117
+    /**
118
+     * 创建成功响应
119
+     * @param mixed $data 响应数据
120
+     * @param string $message 成功消息
121
+     * @param int $code 状态码
122
+     * @return AdminCommonResponseEntity
123
+     */
124
+    public static function success(string $message = '操作成功', int $code = 200, $data = null): AdminCommonResponseEntity
125
+    {
126
+        return (new self())
127
+            ->setCode($code)
128
+            ->setMessage($message)
129
+            ->setData($data)
130
+            ->setTimestamp(time());
131
+    }
132
+
133
+    /**
134
+     * 创建失败响应
135
+     * @param string $message 错误消息
136
+     * @param int $code 状态码
137
+     * @param mixed $data 额外数据
138
+     * @return AdminCommonResponseEntity
139
+     */
140
+    public static function error(string $message = '操作失败', int $code = 400, $data = null): AdminCommonResponseEntity
141
+    {
142
+        return (new self())
143
+            ->setCode($code)
144
+            ->setMessage($message)
145
+            ->setData($data)
146
+            ->setTimestamp(time());
147
+    }
148
+
149
+    /**
150
+     * 转换为数组格式
151
+     * @return array
152
+     */
153
+    public function toResponseArray(): array
154
+    {
155
+        return [
156
+            'code' => $this->code,
157
+            'message' => $this->message,
158
+            'data' => $this->data,
159
+            'timestamp' => $this->timestamp
160
+        ];
161
+    }
162
+
163
+    /**
164
+     * 转换为JSON字符串
165
+     * @return string
166
+     */
167
+    public function toJson(): string
168
+    {
169
+        return json_encode($this->toResponseArray(), JSON_UNESCAPED_UNICODE);
170
+    }
171
+}

+ 40 - 0
app/entity/admin/response/user/ZeroUserLineResponseEntity.php

@@ -0,0 +1,40 @@
1
+<?php
2
+
3
+namespace app\entity\admin\response\user;
4
+
5
+use app\entity\admin\response\AdminCommonResponseEntity;
6
+
7
+/**
8
+ * 零号线用户响应实体
9
+ * 继承自 AdminCommonResponseEntity,用于零号线用户相关操作的响应
10
+ * 此接口只需要返回成功或失败状态
11
+ */
12
+class ZeroUserLineResponseEntity extends AdminCommonResponseEntity
13
+{
14
+    // 直接使用父类的 success() 和 error() 方法
15
+    // 不需要额外字段,只需要返回操作状态
16
+    
17
+    /**
18
+     * 创建零号线用户操作成功响应
19
+     * @param  null  $data
20
+     * @param  string  $message
21
+     * @param  int  $code
22
+     * @return AdminCommonResponseEntity
23
+     */
24
+    public static function success(string $message = '零号线用户操作成功', int $code = 200, $data = null): AdminCommonResponseEntity
25
+    {
26
+        return parent::success($message);
27
+    }
28
+
29
+    /**
30
+     * 创建零号线用户操作失败响应
31
+     * @param  string  $message
32
+     * @param  int  $code
33
+     * @param  null  $data
34
+     * @return AdminCommonResponseEntity
35
+     */
36
+    public static function error(string $message = '零号线用户操作失败', int $code = 400, $data = null): AdminCommonResponseEntity
37
+    {
38
+        return parent::error($message, $code);
39
+    }
40
+}

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

@@ -0,0 +1,57 @@
1
+<?php
2
+/**
3
+ * @package merchant
4
+ *
5
+ * @author xaboy
6
+ * @day 2020-05-09
7
+ *
8
+ *
9
+ */
10
+
11
+namespace app\validate\admin\user;
12
+
13
+use app\entity\admin\request\user\ZeroUserRequestEntity;
14
+use app\validate\CommonValidate;
15
+
16
+class AddZeroUserValidate extends CommonValidate
17
+{
18
+    protected $failException = true;
19
+
20
+    protected $rule = [
21
+        'custom_code|万能验证码' => 'in:4,6',
22
+        'status|状态' => 'require|integer',
23
+        'team_name|团队昵称' => 'max:20',
24
+        'team_uid|团队长' => 'require|integer',
25
+        'is_remove|是否移除团队' => 'require|in:0,1'
26
+    ];
27
+
28
+    protected $message = [
29
+        'custom_code.in' => '万能验证码错误',
30
+        'status.require' => '请选择状态',
31
+        'status.integer' => '请选择状态',
32
+        'team_uid.require' => '请输入团队长uid',
33
+        'team_uid.integer' => '请输入团队长uid',
34
+        'is_remove.in' => '请选择是否移除团队'
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
+}

+ 5 - 0
route/admin.php

@@ -471,6 +471,11 @@ Route::group(config('admin.api_admin_prefix') . '/', function () {
471 471
 
472 472
         })->prefix('admin.user.User');
473 473
 
474
+
475
+        Route::group('newUser', function () {
476
+            Route::any('addZeroUser', '/addZeroUser')->name('addZeroUser');
477
+        })->prefix('admin.user.NewUser');
478
+
474 479
         Route::group('warehouse',function (){
475 480
             Route::get('getProvince', '/getProvince');//省
476 481
             Route::get('getCity/:id', '/getCity');//市