Browse Source

修改用户服务

邹景立 4 years ago
parent
commit
03a755acd9
2 changed files with 12 additions and 10 deletions
  1. 2 2
      app/data/controller/api/Auth.php
  2. 10 8
      app/data/service/UserService.php

+ 2 - 2
app/data/controller/api/Auth.php

@@ -8,7 +8,7 @@ use think\admin\Controller;
 use think\exception\HttpResponseException;
 
 /**
- * 授权认证基类
+ * 接口授权认证基类
  * Class Auth
  * @package app\store\controller\api
  */
@@ -70,7 +70,7 @@ abstract class Auth extends Controller
                 [$state, $info, $this->uuid] = UserTokenService::instance()->check($this->type, $token);
                 if (empty($state)) $this->error($info, '{-null-}', 401);
             }
-            return UserService::instance()->get($this->type, $this->uuid);
+            return UserService::instance()->get($this->uuid, $this->type);
         } catch (HttpResponseException $exception) {
             throw $exception;
         } catch (\Exception $exception) {

+ 10 - 8
app/data/service/UserService.php

@@ -50,22 +50,24 @@ class UserService extends Service
 
     /**
      * 获取用户数据
-     * @param string $type 接口类型
      * @param integer $uuid 用户UID
+     * @param ?string $type 接口类型
      * @return array
      * @throws DbException
      * @throws Exception
      */
-    public function get(string $type, int $uuid): array
+    public function get(int $uuid, ?string $type = null): array
     {
         $user = $this->app->db->name('DataUser')->where(['id' => $uuid, 'deleted' => 0])->findOrEmpty();
         if (empty($user)) throw new Exception('指定UID用户不存在');
-        $data = $this->app->db->name('DataUserToken')->where(['uid' => $uuid, 'type' => $type])->findOrEmpty();
-        if (empty($data)) {
-            [$state, $info, $data] = UserTokenService::instance()->token($uuid, $type);
-            if (empty($state) || empty($data)) throw new Exception($info);
+        if (!is_null($type)) {
+            $data = $this->app->db->name('DataUserToken')->where(['uid' => $uuid, 'type' => $type])->findOrEmpty();
+            if (empty($data)) {
+                [$state, $info, $data] = UserTokenService::instance()->token($uuid, $type);
+                if (empty($state) || empty($data)) throw new Exception($info);
+            }
+            $user['token'] = ['token' => $data['token'], 'expire' => $data['time']];
         }
-        $user['token'] = ['token' => $data['token'], 'expire' => $data['time']];
         unset($user['deleted'], $user['password']);
         return $user;
     }
@@ -94,7 +96,7 @@ class UserService extends Service
         if ($force) {
             UserTokenService::instance()->token(intval($uuid), $type);
         }
-        return $this->get($type, $uuid);
+        return $this->get($uuid, $type);
     }
 
     /**