修改代码

This commit is contained in:
Anyon 2020-09-14 11:21:51 +08:00
parent ca216e1e8a
commit 21324a74da
5 changed files with 51 additions and 48 deletions

View File

@ -48,10 +48,8 @@ abstract class Auth extends Controller
protected function getMember(): array protected function getMember(): array
{ {
try { try {
if (empty($this->token)) { if (empty($this->token)) $this->error('接口授权TOKEN无效');
$this->error('接口授权TOKEN无效'); return UserService::instance()->get(['token' => $this->token]);
}
return UserService::instance()->get($this->token);
} catch (HttpResponseException $exception) { } catch (HttpResponseException $exception) {
throw $exception; throw $exception;
} catch (\Exception $exception) { } catch (\Exception $exception) {

View File

@ -37,7 +37,7 @@ class Login extends Controller
if (empty($user)) $this->error('该手机号还没有注册哦!'); if (empty($user)) $this->error('该手机号还没有注册哦!');
if (empty($user['status'])) $this->error('该会员账号状态异常!'); if (empty($user['status'])) $this->error('该会员账号状态异常!');
if (md5($data['password']) === $user['password']) { if (md5($data['password']) === $user['password']) {
$this->success('手机登录成功!', UserService::instance()->token($user['id'])); $this->success('手机登录成功!', UserService::instance()->get($map, true));
} else { } else {
$this->error('账号登录失败,请稍候再试!'); $this->error('账号登录失败,请稍候再试!');
} }
@ -45,7 +45,6 @@ class Login extends Controller
/** /**
* 会员统一注册入口 * 会员统一注册入口
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
@ -66,11 +65,8 @@ class Login extends Controller
$this->error('手机号已注册,请使用其它手机号!'); $this->error('手机号已注册,请使用其它手机号!');
} }
$data['password'] = md5($data['password']); $data['password'] = md5($data['password']);
if (($mid = $this->app->db->name($this->table)->insertGetId($data)) !== false) { $user = UserService::instance()->save(['phone' => $data['phone']], $data, true);
$this->success('会员注册成功!', UserService::instance()->token($mid)); empty($user) ? $this->success('会员注册成功!', $user) : $this->error('手机注册失败!');
} else {
$this->error('手机注册失败!');
}
} }
} }

View File

@ -43,7 +43,6 @@ class Wxapp extends Controller
/** /**
* 授权Code换取会话信息 * 授权Code换取会话信息
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
@ -52,8 +51,8 @@ class Wxapp extends Controller
{ {
$input = $this->_vali(['code.require' => '登录凭证code不能为空']); $input = $this->_vali(['code.require' => '登录凭证code不能为空']);
[$openid, $sessionKey] = $this->_exchangeSessionKey($input['code']); [$openid, $sessionKey] = $this->_exchangeSessionKey($input['code']);
$result = UserService::instance()->token($openid, ['session_key' => $sessionKey]); $data = array_merge($map = ['openid' => $openid], ['session_key' => $sessionKey]);
$this->success('授权换取成功!', $result); $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']); $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'])) { 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'); $data = ['openid' => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName']];
$this->success('数据解密成功!', UserService::instance()->token($result['openId'])); $this->success('数据解密成功!', UserService::instance()->save(['openid' => $result['openId']], $data, true));
} elseif (is_array($result) && isset($result['phoneNumber'])) { } elseif (is_array($result) && isset($result['phoneNumber'])) {
$this->success('数据解密成功!', $result); $this->success('数据解密成功!', $result);
} else { } else {
@ -100,13 +99,11 @@ class Wxapp extends Controller
try { try {
$cache = $this->app->cache->get($code, []); $cache = $this->app->cache->get($code, []);
if (isset($cache['openid']) && isset($cache['session_key'])) { if (isset($cache['openid']) && isset($cache['session_key'])) {
data_save('DataMember', ['openid' => $cache['openid']], 'openid');
return [$cache['openid'], $cache['session_key']]; return [$cache['openid'], $cache['session_key']];
} }
$result = Crypt::instance($this->config)->session($code); $result = Crypt::instance($this->config)->session($code);
if (isset($result['openid']) && isset($result['session_key'])) { if (isset($result['openid']) && isset($result['session_key'])) {
$this->app->cache->set($code, $result, 3600); $this->app->cache->set($code, $result, 3600);
data_save('DataMember', ['openid' => $result['openid']], 'openid');
return [$result['openid'], $result['session_key']]; return [$result['openid'], $result['session_key']];
} elseif (isset($result['errmsg'])) { } elseif (isset($result['errmsg'])) {
$this->error($result['errmsg']); $this->error($result['errmsg']);

View File

@ -23,7 +23,7 @@ class NewsService extends Service
$query->where($map)->group('type')->select()->map(function ($item) use (&$total) { $query->where($map)->group('type')->select()->map(function ($item) use (&$total) {
$total[$item['type']] = $item['count']; $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_collect' => $total[2] ?? 0, 'num_like' => $total[1] ?? 0,
'num_comment' => $this->app->db->name('DataNewsXComment')->where($map)->count(), 'num_comment' => $this->app->db->name('DataNewsXComment')->where($map)->count(),
]); ]);

View File

@ -19,49 +19,50 @@ class UserService extends Service
/** /**
* 获取会员资料 * 获取会员资料
* @param string $token 接口认证 * @param array $map 查询条件
* @param array $data 额外数据 * @param bool $force 强制令牌
* @return array * @return array
* @throws \think\Exception * @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @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]; $member = $this->save($map, [], $force);
$query = $this->app->db->name($this->table)->where($map); if (empty($member)) throw new \think\Exception('登录授权失败');
$member = $query->withoutField('tokenv,deleted')->find(); // if ($member['tokenv'] !== $this->buildTokenVerify()) {
if (empty($member)) { // throw new \think\Exception('请重新登录授权');
throw new \think\Exception('登录授权失败'); // }
} return $member;
// if ($member['tokenv'] !== $this->buildTokenVerify()) {
// throw new \think\Exception('请重新登录授权');
// }
return array_merge($member, $data);
} }
/** /**
* 刷新会员授权 TOKEN * 更新会员用户参数
* @param mixed $mkey 会员标识 * @param array $map 查询条件
* @param array $data 额外数据 * @param array $data 更新数据
* @param boolean $force 强刷令牌
* @return array * @return array
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function token($mkey, array $data = []): array public function save(array $map, array $data = [], bool $force = false): array
{ {
// 生成新的接口令牌 $user = $this->app->db->name($this->table)->where($map)->where(['deleted' => 0])->find() ?: [];
do $set = ['token' => md5(uniqid("{$mkey}#", true) . rand(100, 999))]; unset($data['id'], $data['token'], $data['tokenv'], $data['status'], $data['deleted'], $data['create_at']);
while ($this->app->db->name($this->table)->where($set)->count() > 0); if ($force) $data = array_merge($data, $this->_buildUserToken());
// 更新账号授权令牌 if (empty($data)) {
$this->app->db->name($this->table)->where(['id|token' => $mkey, 'deleted' => 0])->update([ unset($user['deleted'], $user['password']);
'token' => $set['token'], 'tokenv' => $this->buildTokenVerify(), return $user;
]); } elseif (empty($user['id'])) {
// 获取新的会员数据 $user['id'] = $this->app->db->name($this->table)->strict(false)->insertGetId($data);
return $this->get($set['token'], $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 * @return string
*/ */
private function buildTokenVerify(): string private function _buildTokenVerify(): string
{ {
return md5($this->app->request->server('HTTP_USER_AGENT', '-')); return md5($this->app->request->server('HTTP_USER_AGENT', '-'));
} }