Bladeren bron

7-18获取登录用户基本信息

Home 4 jaren geleden
bovenliggende
commit
fb263f648d
4 gewijzigde bestanden met toevoegingen van 43 en 2 verwijderingen
  1. 8 1
      app/api/controller/User.php
  2. 2 1
      app/api/route/api.php
  3. 17 0
      app/common/business/User.php
  4. 16 0
      app/common/model/mysql/User.php

+ 8 - 1
app/api/controller/User.php

@@ -9,11 +9,18 @@
 
 namespace app\api\controller;
 
+use app\common\business\User as UserBusiness;
 
 class User extends AuthBase
 {
     public function index()
     {
-
+        $user = (new UserBusiness)->getNormalUserById($this->userId);
+        $resultUser = [
+            'id' => $this->userId,
+            'username' => $user['username'],
+            'sex' => $user['sex'],
+        ];
+        return show(config('status.success'), 'OK', $resultUser);
     }
 }

+ 2 - 1
app/api/route/api.php

@@ -8,4 +8,5 @@
 
 use think\facade\Route;
 
-Route::rule('smscode', 'Sms/code', 'POST');
+Route::rule('smscode', 'Sms/code', 'POST');
+Route::resource('user', 'User');

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

@@ -66,4 +66,21 @@ class User
 
         return $res ? ['token' => $token, 'username' => $username] : false;
     }
+
+    /**
+     * 返回正常用户数据
+     * @param int $id
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getNormalUserById(int $id): array
+    {
+        $user = $this->userObj->getUserById($id);
+        if (!$user || $user->status !== config('status.mysql.table_normal')) {
+            return [];
+        }
+        return $user->toArray();
+    }
 }

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

@@ -40,6 +40,22 @@ class User extends Model
         return $this->where($where)->find();
     }
 
+    /**
+     * 通过id获取用户数据
+     * @param int $id
+     * @return array|bool|Model|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getUserById(int $id)
+    {
+        if (!$id) {
+            return false;
+        }
+        return $this->find($id);
+    }
+
     /**
      * 根据主键id更新数据表中的数据
      * @param $id