mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-05 19:41:44 +08:00
修改用户认证+增加异常处理
This commit is contained in:
parent
3b65ff7c9d
commit
9adeead422
@ -79,6 +79,7 @@ abstract class Auth extends Controller
|
||||
} catch (HttpResponseException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Exception $exception) {
|
||||
trace_file($exception);
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ class Login extends Controller
|
||||
if (empty($user)) $this->error('该手机号还没有注册哦!');
|
||||
if (empty($user['status'])) $this->error('该用户账号状态异常!');
|
||||
if (md5($data['password']) === $user['password']) {
|
||||
$this->success('手机登录成功!', UserAdminService::instance()->set($map, [], $this->type, true));
|
||||
$this->success('手机登录成功!', UserAdminService::set($map, [], $this->type, true));
|
||||
} else {
|
||||
$this->error('账号登录失败,请稍候再试!');
|
||||
}
|
||||
@ -72,22 +72,20 @@ class Login extends Controller
|
||||
'region_city.default' => '',
|
||||
'region_area.default' => '',
|
||||
'username.default' => '',
|
||||
'phone.mobile' => '手机号码格式错误!',
|
||||
'phone.require' => '手机号码不能为空!',
|
||||
// 'verify.require' => '验证码不能为空!',
|
||||
'phone.mobile' => '手机格式错误!',
|
||||
'phone.require' => '手机不能为空!',
|
||||
'verify.require' => '验证码不能为空!',
|
||||
'password.require' => '登录密码不能为空!',
|
||||
]);
|
||||
// if (MessageService::instance()->checkVerifyCode($data['verify'], $data['phone'])) {
|
||||
// @验证码验证能完
|
||||
// } else {
|
||||
// $this->error('验证失败!');
|
||||
// }
|
||||
if (!MessageService::instance()->checkVerifyCode($data['verify'], $data['phone'])) {
|
||||
$this->error('手机短信验证失败!');
|
||||
}
|
||||
$map = ['phone' => $data['phone'], 'deleted' => 0];
|
||||
if (DataUser::mk()->where($map)->count() > 0) {
|
||||
$this->error('手机号已注册,请使用其它手机号!');
|
||||
}
|
||||
$data['password'] = md5($data['password']);
|
||||
$user = UserAdminService::instance()->set($map, $data, $this->type, true);
|
||||
$user = UserAdminService::set($map, $data, $this->type, true);
|
||||
empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
|
||||
}
|
||||
|
||||
|
@ -85,8 +85,8 @@ class Wechat extends Controller
|
||||
$data['base_sex'] = ['未知', '男', '女'][$data['sex']] ?? '未知';
|
||||
if (isset($result['unionid'])) $data['unionid'] = $result['unionid'];
|
||||
if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl'];
|
||||
$map = UserAdminService::instance()->getUserUniMap($this->field, $data[$this->field], $data['unionid'] ?? '');
|
||||
$result['userinfo'] = UserAdminService::instance()->set($map, array_merge($map, $data), $this->type, true);
|
||||
$map = UserAdminService::getUserUniMap($this->field, $data[$this->field], $data['unionid'] ?? '');
|
||||
$result['userinfo'] = UserAdminService::set($map, array_merge($map, $data), $this->type, true);
|
||||
$script[] = "window.WeChatOpenid='{$result['openid']}'";
|
||||
$script[] = 'window.WeChatFansInfo=' . json_encode($result['fansinfo'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||||
$script[] = 'window.WeChatUserInfo=' . json_encode($result['userinfo'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||||
|
@ -33,7 +33,7 @@ class Wxapp extends Controller
|
||||
* 小程序配置参数
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
private $cfg;
|
||||
|
||||
/**
|
||||
* 接口服务初始化
|
||||
@ -43,7 +43,7 @@ class Wxapp extends Controller
|
||||
*/
|
||||
protected function initialize()
|
||||
{
|
||||
$this->config = [
|
||||
$this->cfg = [
|
||||
'appid' => sysconf('data.wxapp_appid'),
|
||||
'appsecret' => sysconf('data.wxapp_appkey'),
|
||||
'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat',
|
||||
@ -63,11 +63,11 @@ class Wxapp extends Controller
|
||||
public function session()
|
||||
{
|
||||
$input = $this->_vali(['code.require' => '登录凭证CODE不能为空!']);
|
||||
[$openid, $unionid, $session] = $this->_getSessionKey($input['code']);
|
||||
$map = UserAdminService::instance()->getUserUniMap($this->field, $openid, $unionid);
|
||||
[$openid, $unionid, $session] = $this->applySessionKey($input['code']);
|
||||
$map = UserAdminService::getUserUniMap($this->field, $openid, $unionid);
|
||||
$data = [$this->field => $openid, 'session_key' => $session];
|
||||
if (!empty($unionid)) $data['unionid'] = $unionid;
|
||||
$this->success('授权换取成功!', UserAdminService::instance()->set($map, $data, $this->type, true));
|
||||
$this->success('授权换取成功!', UserAdminService::set($map, $data, $this->type, true));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -81,14 +81,14 @@ class Wxapp extends Controller
|
||||
'code.require' => '授权CODE不能为空!',
|
||||
'encrypted.require' => '加密内容不能为空!',
|
||||
]);
|
||||
[$openid, $unionid, $input['session_key']] = $this->_getSessionKey($input['code']);
|
||||
$result = Crypt::instance($this->config)->decode($input['iv'], $input['session_key'], $input['encrypted']);
|
||||
[$openid, $unionid, $input['session_key']] = $this->applySessionKey($input['code']);
|
||||
$result = Crypt::instance($this->cfg)->decode($input['iv'], $input['session_key'], $input['encrypted']);
|
||||
if (is_array($result) && isset($result['avatarUrl']) && isset($result['nickName'])) {
|
||||
$data = [$this->field => $openid, 'nickname' => $result['nickName'], 'headimg' => $result['avatarUrl']];
|
||||
$data['base_sex'] = ['-', '男', '女'][$result['gender']] ?? '-';
|
||||
if (!empty($unionid)) $data['unionid'] = $unionid;
|
||||
$map = UserAdminService::instance()->getUserUniMap($this->field, $openid, $unionid);
|
||||
$this->success('数据解密成功!', UserAdminService::instance()->set($map, $data, $this->type, true));
|
||||
$map = UserAdminService::getUserUniMap($this->field, $openid, $unionid);
|
||||
$this->success('数据解密成功!', UserAdminService::set($map, $data, $this->type, true));
|
||||
} elseif (is_array($result)) {
|
||||
$this->success('数据解密成功!', $result);
|
||||
} else {
|
||||
@ -97,6 +97,7 @@ class Wxapp extends Controller
|
||||
} catch (HttpResponseException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Exception $exception) {
|
||||
trace_file($exception);
|
||||
$this->error("数据处理失败,{$exception->getMessage()}");
|
||||
}
|
||||
}
|
||||
@ -106,14 +107,14 @@ class Wxapp extends Controller
|
||||
* @param string $code 换取授权CODE
|
||||
* @return array [openid, sessionkey]
|
||||
*/
|
||||
private function _getSessionKey(string $code): array
|
||||
private function applySessionKey(string $code): array
|
||||
{
|
||||
try {
|
||||
$cache = $this->app->cache->get($code, []);
|
||||
if (isset($cache['openid']) && isset($cache['session_key'])) {
|
||||
return [$cache['openid'], $cache['unionid'] ?? '', $cache['session_key']];
|
||||
}
|
||||
$result = Crypt::instance($this->config)->session($code);
|
||||
$result = Crypt::instance($this->cfg)->session($code);
|
||||
if (isset($result['openid']) && isset($result['session_key'])) {
|
||||
$this->app->cache->set($code, $result, 60);
|
||||
return [$result['openid'], $result['unionid'] ?? '', $result['session_key']];
|
||||
@ -125,6 +126,7 @@ class Wxapp extends Controller
|
||||
} catch (HttpResponseException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Exception $exception) {
|
||||
trace_file($exception);
|
||||
$this->error("授权换取失败,{$exception->getMessage()}");
|
||||
}
|
||||
}
|
||||
@ -140,7 +142,7 @@ class Wxapp extends Controller
|
||||
'type.default' => 'base64',
|
||||
'path.require' => '跳转路径不能为空!',
|
||||
]);
|
||||
$result = Qrcode::instance($this->config)->createMiniPath($data['path'], $data['size']);
|
||||
$result = Qrcode::instance($this->cfg)->createMiniPath($data['path'], $data['size']);
|
||||
if ($data['type'] === 'base64') {
|
||||
$this->success('生成小程序码成功!', [
|
||||
'base64' => 'data:image/png;base64,' . base64_encode($result),
|
||||
@ -151,6 +153,7 @@ class Wxapp extends Controller
|
||||
} catch (HttpResponseException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Exception $exception) {
|
||||
trace_file($exception);
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
@ -162,11 +165,12 @@ class Wxapp extends Controller
|
||||
{
|
||||
try {
|
||||
$data = $this->_vali(['start.default' => 0, 'limit.default' => 10]);
|
||||
$list = Live::instance($this->config)->getLiveList($data['start'], $data['limit']);
|
||||
$list = Live::instance($this->cfg)->getLiveList($data['start'], $data['limit']);
|
||||
$this->success('获取直播列表成功!', $list);
|
||||
} catch (HttpResponseException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Exception $exception) {
|
||||
trace_file($exception);
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
@ -183,11 +187,12 @@ class Wxapp extends Controller
|
||||
'action.default' => 'get_replay',
|
||||
'room_id.require' => '直播间不能为空',
|
||||
]);
|
||||
$result = Live::instance($this->config)->getLiveInfo($data);
|
||||
$result = Live::instance($this->cfg)->getLiveInfo($data);
|
||||
$this->success('获取回放视频成功!', $result);
|
||||
} catch (HttpResponseException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Exception $exception) {
|
||||
trace_file($exception);
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
@ -55,12 +55,13 @@ class Center extends Auth
|
||||
{
|
||||
try {
|
||||
$data = $this->_vali(['base64.require' => '图片内容不为空!']);
|
||||
if (preg_match('|^data:image/(.*?);base64,|i', $data['base64'])) {
|
||||
[$ext, $img] = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $data['base64']));
|
||||
if (preg_match($preg = '|^data:image/(.*?);base64,|i', $data['base64'])) {
|
||||
[$ext, $img] = explode('|||', preg_replace($preg, '$1|||', $data['base64']));
|
||||
if (empty($ext) || !in_array(strtolower($ext), ['png', 'jpg', 'jpeg'])) {
|
||||
$this->error('图片格式异常!');
|
||||
}
|
||||
$info = Storage::instance()->set(Storage::name($img, $ext, 'image/'), base64_decode($img));
|
||||
$name = Storage::name($img, $ext, 'image/');
|
||||
$info = Storage::instance()->set($name, base64_decode($img));
|
||||
$this->success('图片上传成功!', ['url' => $info['url']]);
|
||||
} else {
|
||||
$this->error('解析内容失败!');
|
||||
@ -68,6 +69,7 @@ class Center extends Auth
|
||||
} catch (HttpResponseException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Exception $exception) {
|
||||
trace_file($exception);
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ class News extends Auth
|
||||
|
||||
/**
|
||||
* 用户评论内容
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function addComment()
|
||||
{
|
||||
|
@ -131,7 +131,7 @@ class Config extends Controller
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
private function __sysdata(string $template, $history = '')
|
||||
private function __sysdata(string $template, string $history = '')
|
||||
{
|
||||
if ($this->request->isGet()) {
|
||||
$this->data = sysdata($this->skey);
|
||||
|
@ -42,9 +42,6 @@ class Discount extends Controller
|
||||
/**
|
||||
* 添加折扣方案
|
||||
* @auth true
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
@ -54,9 +51,6 @@ class Discount extends Controller
|
||||
/**
|
||||
* 编辑折扣方案
|
||||
* @auth true
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
@ -88,7 +82,6 @@ class Discount extends Controller
|
||||
/**
|
||||
* 修改折扣方案状态
|
||||
* @auth true
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function state()
|
||||
{
|
||||
@ -98,7 +91,6 @@ class Discount extends Controller
|
||||
/**
|
||||
* 删除折扣方案配置
|
||||
* @auth true
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function remove()
|
||||
{
|
||||
|
@ -51,7 +51,7 @@ class UserAdminService extends Service
|
||||
|
||||
/**
|
||||
* 更新用户用户参数
|
||||
* @param array $map 查询条件
|
||||
* @param mixed $map 查询条件
|
||||
* @param array $data 更新数据
|
||||
* @param string $type 接口类型
|
||||
* @param boolean $force 强刷令牌
|
||||
@ -59,43 +59,40 @@ class UserAdminService extends Service
|
||||
* @throws \think\admin\Exception
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function set(array $map, array $data, string $type, bool $force = false): array
|
||||
public static function set($map, array $data, string $type, bool $force = false): array
|
||||
{
|
||||
$user = DataUser::mk()->where($map)->where(['deleted' => 0])->find();
|
||||
// 更新或写入用户数据
|
||||
unset($data['id'], $data['deleted'], $data['create_at']);
|
||||
if (empty($user)) ($user = DataUser::mk())->save($data);
|
||||
elseif (!empty($data)) $user->save($data);
|
||||
// 强行刷新用户认证令牌
|
||||
if ($force) UserTokenService::instance()->token($user['id'], $type);
|
||||
// 返回当前用户资料数据
|
||||
return $this->get($user['id'], $type);
|
||||
$user = DataUser::mk()->where($map)->where(['deleted' => 0])->findOrEmpty();
|
||||
if (!$user->save($data)) throw new Exception("更新用户资料失败!");
|
||||
// 刷新用户认证令牌
|
||||
if ($force) UserTokenService::token($user['id'], $type);
|
||||
// 返回当前用户资料
|
||||
return static::get($user['id'], $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户数据
|
||||
* @param integer $uuid 用户UID
|
||||
* @param ?string $type 接口类型
|
||||
* @param string $type 接口类型
|
||||
* @return array
|
||||
* @throws \think\admin\Exception
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function get(int $uuid, ?string $type = null): array
|
||||
public static function get(int $uuid, string $type): array
|
||||
{
|
||||
$user = DataUser::mk()->where(['id' => $uuid, 'deleted' => 0])->find();
|
||||
if (empty($user)) throw new Exception('用户还没有注册!');
|
||||
if (!is_null($type)) {
|
||||
$data = DataUserToken::mk()->where(['uuid' => $uuid, 'type' => $type])->find();
|
||||
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']];
|
||||
$map = ['id' => $uuid, 'deleted' => 0];
|
||||
$user = DataUser::mk()->where($map)->findOrEmpty();
|
||||
if ($user->isEmpty()) throw new Exception('用户还没有注册!');
|
||||
// 用户认证令牌处理
|
||||
$map = ['uuid' => $uuid, 'type' => $type];
|
||||
if (!($access = DataUserToken::mk()->where($map)->find())) {
|
||||
[$state, $message, $access] = UserTokenService::token($uuid, $type);
|
||||
if (empty($state) || empty($access)) throw new Exception($message);
|
||||
}
|
||||
unset($user['deleted'], $user['password']);
|
||||
return $user->toArray();
|
||||
$user['token'] = ['token' => $access['token'], 'expire' => $access['time']];
|
||||
return $user->hidden(['deleted', 'password'])->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,7 +112,7 @@ class UserAdminService extends Service
|
||||
* @param string $unionid 用户UNIONID值
|
||||
* @return array
|
||||
*/
|
||||
public function getUserUniMap(string $field, string $openid, string $unionid = ''): array
|
||||
public static function getUserUniMap(string $field, string $openid, string $unionid = ''): array
|
||||
{
|
||||
if (!empty($unionid)) {
|
||||
[$map1, $map2] = [[['unionid', '=', $unionid]], [[$field, '=', $openid]]];
|
||||
|
@ -17,7 +17,7 @@ class UserTokenService extends Service
|
||||
* 认证有效时间
|
||||
* @var integer
|
||||
*/
|
||||
private $expire = 7200;
|
||||
private static $expire = 7200;
|
||||
|
||||
/**
|
||||
* 检查 TOKEN 是否有效
|
||||
@ -29,7 +29,7 @@ class UserTokenService extends Service
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function check(string $type, string $token, array $data = []): array
|
||||
public static function check(string $type, string $token, array $data = []): array
|
||||
{
|
||||
if (empty($data)) {
|
||||
$map = ['type' => $type, 'token' => $token];
|
||||
@ -39,10 +39,10 @@ class UserTokenService extends Service
|
||||
return [0, '请重新登录,登录认证无效', 0, 0];
|
||||
} elseif ($token !== 'token' && $data['time'] < time()) {
|
||||
return [0, '请重新登录,登录认证失效', 0, 0];
|
||||
} elseif ($token !== 'token' && $data['tokenv'] !== $this->_buildTokenVerify()) {
|
||||
} elseif ($token !== 'token' && $data['tokenv'] !== static::buildVerify()) {
|
||||
return [0, '请重新登录,客户端已更换', 0, 0];
|
||||
} else {
|
||||
$this->expire($type, $token);
|
||||
static::expire($type, $token);
|
||||
return [1, '登录验证成功', $data['uuid'], $data['time']];
|
||||
}
|
||||
}
|
||||
@ -51,9 +51,10 @@ class UserTokenService extends Service
|
||||
* 获取令牌的认证值
|
||||
* @return string
|
||||
*/
|
||||
private function _buildTokenVerify(): string
|
||||
private static function buildVerify(): string
|
||||
{
|
||||
return md5($this->app->request->server('HTTP_USER_AGENT', '-'));
|
||||
return md5('-');
|
||||
// return md5(app()->request->server('HTTP_USER_AGENT', '-'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,11 +62,11 @@ class UserTokenService extends Service
|
||||
* @param string $type 接口类型
|
||||
* @param string $token 授权令牌
|
||||
*/
|
||||
public function expire(string $type, string $token)
|
||||
public static function expire(string $type, string $token)
|
||||
{
|
||||
$map = ['type' => $type, 'token' => $token];
|
||||
DataUserToken::mk()->where($map)->update([
|
||||
'time' => time() + $this->expire,
|
||||
'time' => time() + static::$expire,
|
||||
]);
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ class UserTokenService extends Service
|
||||
* @param string $type 接口类型
|
||||
* @return array [创建状态, 状态描述, 令牌数据]
|
||||
*/
|
||||
public function token(int $uuid, string $type): array
|
||||
public static function token(int $uuid, string $type): array
|
||||
{
|
||||
// 清理无效认证数据
|
||||
$time = time();
|
||||
@ -83,10 +84,14 @@ class UserTokenService extends Service
|
||||
$map2 = [['token', '<>', 'token'], ['type', '=', $type], ['uuid', '=', $uuid]];
|
||||
DataUserToken::mk()->whereOr([$map1, $map2])->delete();
|
||||
// 创建新的认证数据
|
||||
do $map = ['type' => $type, 'token' => md5(uniqid() . rand(100, 999))];
|
||||
do $map = ['type' => $type, 'token' => md5(uniqid(strval(rand(100, 999))))];
|
||||
while (DataUserToken::mk()->where($map)->count() > 0);
|
||||
// 写入用户认证数据
|
||||
$data = array_merge($map, ['uuid' => $uuid, 'time' => $time + $this->expire, 'tokenv' => $this->_buildTokenVerify()]);
|
||||
$data = array_merge($map, [
|
||||
'uuid' => $uuid,
|
||||
'time' => $time + static::$expire,
|
||||
'tokenv' => static::buildVerify()
|
||||
]);
|
||||
if (DataUserToken::mk()->insert($data) !== false) {
|
||||
return [1, '刷新认证成功', $data];
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user