From 21324a74dabfaa7210c4e2639e4c873cc4097186 Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 14 Sep 2020 11:21:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/controller/api/Auth.php | 6 +-- app/data/controller/api/Login.php | 10 ++--- app/data/controller/api/Wxapp.php | 11 ++--- app/data/service/NewsService.php | 2 +- app/data/service/UserService.php | 70 ++++++++++++++++++------------- 5 files changed, 51 insertions(+), 48 deletions(-) diff --git a/app/data/controller/api/Auth.php b/app/data/controller/api/Auth.php index 94d57ddd5..292ac001f 100644 --- a/app/data/controller/api/Auth.php +++ b/app/data/controller/api/Auth.php @@ -48,10 +48,8 @@ abstract class Auth extends Controller protected function getMember(): array { try { - if (empty($this->token)) { - $this->error('接口授权TOKEN无效'); - } - return UserService::instance()->get($this->token); + if (empty($this->token)) $this->error('接口授权TOKEN无效'); + return UserService::instance()->get(['token' => $this->token]); } catch (HttpResponseException $exception) { throw $exception; } catch (\Exception $exception) { diff --git a/app/data/controller/api/Login.php b/app/data/controller/api/Login.php index 044f8b5f1..4f7ffcc49 100644 --- a/app/data/controller/api/Login.php +++ b/app/data/controller/api/Login.php @@ -37,7 +37,7 @@ class Login extends Controller if (empty($user)) $this->error('该手机号还没有注册哦!'); if (empty($user['status'])) $this->error('该会员账号状态异常!'); if (md5($data['password']) === $user['password']) { - $this->success('手机登录成功!', UserService::instance()->token($user['id'])); + $this->success('手机登录成功!', UserService::instance()->get($map, true)); } else { $this->error('账号登录失败,请稍候再试!'); } @@ -45,7 +45,6 @@ class Login extends Controller /** * 会员统一注册入口 - * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException @@ -66,11 +65,8 @@ class Login extends Controller $this->error('手机号已注册,请使用其它手机号!'); } $data['password'] = md5($data['password']); - if (($mid = $this->app->db->name($this->table)->insertGetId($data)) !== false) { - $this->success('会员注册成功!', UserService::instance()->token($mid)); - } else { - $this->error('手机注册失败!'); - } + $user = UserService::instance()->save(['phone' => $data['phone']], $data, true); + empty($user) ? $this->success('会员注册成功!', $user) : $this->error('手机注册失败!'); } } \ No newline at end of file diff --git a/app/data/controller/api/Wxapp.php b/app/data/controller/api/Wxapp.php index 80292dd03..083315ccf 100644 --- a/app/data/controller/api/Wxapp.php +++ b/app/data/controller/api/Wxapp.php @@ -43,7 +43,6 @@ class Wxapp extends Controller /** * 授权Code换取会话信息 - * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException @@ -52,8 +51,8 @@ class Wxapp extends Controller { $input = $this->_vali(['code.require' => '登录凭证code不能为空!']); [$openid, $sessionKey] = $this->_exchangeSessionKey($input['code']); - $result = UserService::instance()->token($openid, ['session_key' => $sessionKey]); - $this->success('授权换取成功!', $result); + $data = array_merge($map = ['openid' => $openid], ['session_key' => $sessionKey]); + $this->success('授权换取成功!', UserService::instance()->save($map, $data, true)); } /** @@ -75,8 +74,8 @@ class Wxapp extends Controller } $result = Crypt::instance($this->config)->decode($input['iv'], $input['session_key'], $input['encrypted']); if (is_array($result) && isset($result['openId']) && isset($result['avatarUrl']) && isset($result['nickName'])) { - data_save('DataMember', ['openid' => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName']], 'openid'); - $this->success('数据解密成功!', UserService::instance()->token($result['openId'])); + $data = ['openid' => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName']]; + $this->success('数据解密成功!', UserService::instance()->save(['openid' => $result['openId']], $data, true)); } elseif (is_array($result) && isset($result['phoneNumber'])) { $this->success('数据解密成功!', $result); } else { @@ -100,13 +99,11 @@ class Wxapp extends Controller try { $cache = $this->app->cache->get($code, []); if (isset($cache['openid']) && isset($cache['session_key'])) { - data_save('DataMember', ['openid' => $cache['openid']], 'openid'); return [$cache['openid'], $cache['session_key']]; } $result = Crypt::instance($this->config)->session($code); if (isset($result['openid']) && isset($result['session_key'])) { $this->app->cache->set($code, $result, 3600); - data_save('DataMember', ['openid' => $result['openid']], 'openid'); return [$result['openid'], $result['session_key']]; } elseif (isset($result['errmsg'])) { $this->error($result['errmsg']); diff --git a/app/data/service/NewsService.php b/app/data/service/NewsService.php index 6b2028de9..a90e22551 100644 --- a/app/data/service/NewsService.php +++ b/app/data/service/NewsService.php @@ -23,7 +23,7 @@ class NewsService extends Service $query->where($map)->group('type')->select()->map(function ($item) use (&$total) { $total[$item['type']] = $item['count']; }); - $this->app->db->name('DataNewsItem')->where($map)->update([ + $this->app->db->name('DataNewsItem')->where(['id' => $cid])->update([ 'num_collect' => $total[2] ?? 0, 'num_like' => $total[1] ?? 0, 'num_comment' => $this->app->db->name('DataNewsXComment')->where($map)->count(), ]); diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index 3836c3c8d..dccccf284 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -19,49 +19,50 @@ class UserService extends Service /** * 获取会员资料 - * @param string $token 接口认证 - * @param array $data 额外数据 + * @param array $map 查询条件 + * @param bool $force 强制令牌 * @return array * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function get(string $token, array $data = []): array + public function get(array $map, bool $force = false): array { - $map = ['token' => $token, 'deleted' => 0]; - $query = $this->app->db->name($this->table)->where($map); - $member = $query->withoutField('tokenv,deleted')->find(); - if (empty($member)) { - throw new \think\Exception('登录授权失败'); - } -// if ($member['tokenv'] !== $this->buildTokenVerify()) { -// throw new \think\Exception('请重新登录授权'); -// } - return array_merge($member, $data); + $member = $this->save($map, [], $force); + if (empty($member)) throw new \think\Exception('登录授权失败'); + // if ($member['tokenv'] !== $this->buildTokenVerify()) { + // throw new \think\Exception('请重新登录授权'); + // } + return $member; } /** - * 刷新会员授权 TOKEN - * @param mixed $mkey 会员标识 - * @param array $data 额外数据 + * 更新会员用户参数 + * @param array $map 查询条件 + * @param array $data 更新数据 + * @param boolean $force 强刷令牌 * @return array - * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function token($mkey, array $data = []): array + public function save(array $map, array $data = [], bool $force = false): array { - // 生成新的接口令牌 - do $set = ['token' => md5(uniqid("{$mkey}#", true) . rand(100, 999))]; - while ($this->app->db->name($this->table)->where($set)->count() > 0); - // 更新账号授权令牌 - $this->app->db->name($this->table)->where(['id|token' => $mkey, 'deleted' => 0])->update([ - 'token' => $set['token'], 'tokenv' => $this->buildTokenVerify(), - ]); - // 获取新的会员数据 - return $this->get($set['token'], $data); + $user = $this->app->db->name($this->table)->where($map)->where(['deleted' => 0])->find() ?: []; + unset($data['id'], $data['token'], $data['tokenv'], $data['status'], $data['deleted'], $data['create_at']); + if ($force) $data = array_merge($data, $this->_buildUserToken()); + if (empty($data)) { + unset($user['deleted'], $user['password']); + return $user; + } elseif (empty($user['id'])) { + $user['id'] = $this->app->db->name($this->table)->strict(false)->insertGetId($data); + } else { + $this->app->db->name($this->table)->strict(false)->where(['id' => $user['id']])->update($data); + } + $map = ['id' => $user['id'], 'deleted' => 0]; + $query = $this->app->db->name($this->table)->where($map); + return $query->withoutField('deleted,password')->find() ?: []; } /** @@ -76,10 +77,21 @@ class UserService extends Service } /** - * 获取认证信息编码 + * 生成新的用户令牌 + * @return array + */ + private function _buildUserToken(): array + { + do $map = ['token' => md5(uniqid('', true) . rand(100, 999))]; + while ($this->app->db->name($this->table)->where($map)->count() > 0); + return ['token' => $map['token'], 'tokenv' => $this->_buildTokenVerify()]; + } + + /** + * 获取令牌的认证值 * @return string */ - private function buildTokenVerify(): string + private function _buildTokenVerify(): string { return md5($this->app->request->server('HTTP_USER_AGENT', '-')); }