Pārlūkot izejas kodu

7-19个人中心数据修改

Home 4 gadi atpakaļ
vecāks
revīzija
28c57ab8e5

+ 24 - 0
app/api/controller/User.php

@@ -9,6 +9,7 @@
 
 namespace app\api\controller;
 
+use app\api\validate\User as UserValidate;
 use app\common\business\User as UserBusiness;
 
 class User extends AuthBase
@@ -23,4 +24,27 @@ class User extends AuthBase
         ];
         return show(config('status.success'), 'OK', $resultUser);
     }
+
+    public function update()
+    {
+        $username = $this->request->param('username', '', 'trim');
+        $sex = input('param.sex', 0, 'intval');
+        //
+        $data = [
+            'username' => $username,
+            'sex' => $sex,
+        ];
+        $validate = (new UserValidate)->scene('update_user');
+        if (!$validate->check($data)) {
+            return show(config('status.error'), $validate->getError());
+        }
+
+        $user = (new UserBusiness())->update($this->userId, $data);
+        if (!$user) {
+            return show(config('status.error'), '更新失败');
+        }
+        // 如果用户名被修改,Redis里面的数据也需要同步一下
+        // todo
+        return show(1, 'OK');
+    }
 }

+ 5 - 0
app/api/validate/User.php

@@ -20,6 +20,7 @@ class User extends Validate
         'phoneNumber' => 'require',
         'code' => 'require|number|min:4',
         'type' => 'require|in:1,2',
+        'sex' => 'require|in:0,1,2'
     ];
 
     protected $message = [
@@ -30,9 +31,13 @@ class User extends Validate
         'code.min' => '短信验证码长度不得低于4',
         'type.require' => '类型必须',
         'type.in' => '类型数值错误',
+        'sex.require' => '性别必须',
+        'sex.in' => '性别数值错误',
     ];
+
     protected $scene = [
         'send_code' => ['phoneNumber'],
         'login' => ['phoneNumber', 'code', 'type'],
+        'update_user' => ['username', 'sex']
     ];
 }

+ 33 - 0
app/common/business/User.php

@@ -24,6 +24,15 @@ class User
         $this->userObj = new UserModel();
     }
 
+    /**
+     * 用户登录
+     * @param array $data
+     * @return array|bool
+     * @throws \think\Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
     public function login(array $data)
     {
         $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']);
@@ -83,4 +92,28 @@ class User
         }
         return $user->toArray();
     }
+
+    public function getNormalUserByUsername(string $username): array
+    {
+        $user = $this->userObj->getUserByUsername($username);
+        if (!$user || $user->status !== config('status.mysql.table_normal')) {
+            return [];
+        }
+        return $user->toArray();
+    }
+
+    public function update(int $id, array $data)
+    {
+        $user = $this->getNormalUserById($id);
+        if (!$user) {
+            throw new \think\Exception('不存在该用户');
+        }
+
+        // 检查用户名是否存在
+        $userResult = $this->getNormalUserByUsername($data['username']);
+        if ($userResult && $userResult['id'] !== $id) {
+            throw new \think\Exception('该用户已经存在请重新设置');
+        }
+        return $this->userObj->updateById($id, $data);
+    }
 }

+ 19 - 0
app/common/model/mysql/User.php

@@ -56,6 +56,25 @@ class User extends Model
         return $this->find($id);
     }
 
+    /**
+     * 根据username获取用户数据
+     * @param string $username
+     * @return array|bool|Model|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getUserByUsername(string $username)
+    {
+        if (empty($username)) {
+            return false;
+        }
+        $where = [
+            'username' => $username
+        ];
+        return $this->where($where)->find();
+    }
+
     /**
      * 根据主键id更新数据表中的数据
      * @param $id