From 57780a39b48381deaa5a605e71caa6707550d24c Mon Sep 17 00:00:00 2001 From: Anyon Date: Tue, 24 Nov 2020 18:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/controller/api/Auth.php | 3 ++- app/data/controller/api/Login.php | 2 +- app/data/service/UserService.php | 16 ++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/data/controller/api/Auth.php b/app/data/controller/api/Auth.php index 99958efad..cc121e508 100644 --- a/app/data/controller/api/Auth.php +++ b/app/data/controller/api/Auth.php @@ -36,6 +36,7 @@ abstract class Auth extends Controller */ protected function initialize() { + $this->type = input('api', 'web'); $this->user = $this->getUser(); $this->uuid = $this->user['id']; } @@ -51,7 +52,7 @@ abstract class Auth extends Controller $token = input('token') ?: $this->request->header('token'); if (empty($token)) $this->error('接口认证令牌不能为空!'); [$state, $message, $this->uuid] = UserService::instance()->checkUserToken($this->type, $token); - if ($state) $this->error($message); + if (empty($state)) $this->error($message); } return UserService::instance()->get($this->type, $this->uuid); } catch (HttpResponseException $exception) { diff --git a/app/data/controller/api/Login.php b/app/data/controller/api/Login.php index aad7def7e..ea8b8c7e8 100644 --- a/app/data/controller/api/Login.php +++ b/app/data/controller/api/Login.php @@ -74,7 +74,7 @@ class Login extends Controller } $data['password'] = md5($data['password']); $user = UserService::instance()->save($map, $data, 'web', true); - empty($user) ? $this->success('用户注册成功!', $user) : $this->error('手机注册失败!'); + empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user); } /** diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index 3dfd85582..84f668d40 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -53,16 +53,16 @@ class UserService extends Service public function save(array $map, array $data, string $type, bool $force = false): array { unset($data['id'], $data['deleted'], $data['create_at']); - if ($uid = $this->app->db->name('DataUser')->where($map)->where(['deleted' => 0])->value('id')) { + if ($uuid = $this->app->db->name('DataUser')->where($map)->where(['deleted' => 0])->value('id')) { if (!empty($data)) { - $map = ['id' => $uid, 'deleted' => 0]; + $map = ['id' => $uuid, 'deleted' => 0]; $this->app->db->name('DataUser')->strict(false)->where($map)->update($data); } } else { - $uid = $this->app->db->name('DataUser')->strict(false)->insertGetId($data); + $uuid = $this->app->db->name('DataUser')->strict(false)->insertGetId($data); } - if ($force) $this->buildUserToken($uid, $type); - return $this->get($type, $uid); + if ($force) $this->buildUserToken(intval($uuid), $type); + return $this->get($type, $uuid); } /** @@ -92,9 +92,9 @@ class UserService extends Service // 创建用户新的用户认证数据 do $map = ['type' => $type, 'token' => md5(uniqid('', true) . rand(100, 999))]; while ($this->app->db->name('DataUserToken')->where($map)->count() > 0); - $token = array_merge($map, ['time' => $time + $this->expire, 'tokenv' => $this->_buildTokenVerify()]); - if ($this->app->db->name('DataUserToken')->insert($token) !== false) { - return [1, '刷新用户认证成功', $token]; + $data = array_merge($map, ['uid' => $uid, 'time' => $time + $this->expire, 'tokenv' => $this->_buildTokenVerify()]); + if ($this->app->db->name('DataUserToken')->insert($data) !== false) { + return [1, '刷新用户认证成功', $data]; } else { return [0, '刷新用户认证失败', []]; }