Skip to content

Commit

Permalink
5.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
funadmin committed Jul 12, 2023
1 parent 903d492 commit 74c111f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 106 deletions.
6 changes: 3 additions & 3 deletions src/auth/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Api
* 不需要鉴权方法
*/
protected $noAuth = [];

protected $member_id = '';

protected $type ='simple';
Expand All @@ -50,7 +50,7 @@ class Api
*/
public function __construct(App $app)
{
$this->type = Config::get('api.type','simple');
$this->type = Config::get('api.type')?:"simple";
$this->request = Request::instance();
$this->request->filter('trim,strip_tags,htmlspecialchars');
$this->group = $this->request->param('group')?$this->request->param('group'):'api';
Expand Down Expand Up @@ -81,4 +81,4 @@ public function _empty()
{
$this->error('empty method!');
}
}
}
63 changes: 59 additions & 4 deletions src/auth/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ trait Send

/**
* redis 对象
* @var
* @var
*/
public $redis ;
/**
* 客户端对象
* @var
* @var
*/
public $client ;

Expand All @@ -69,10 +69,10 @@ trait Send
protected $appsecret = '';
/**
* JWT key
* @var string
* @var string
*/
public $key = '';

public $group = 'api';

/**
Expand Down Expand Up @@ -198,6 +198,61 @@ public function match($arr = [])
return false;
}

/**
* token
* @param $memberInfo
* @param $expires
* @return string
*/
protected function buildAccessToken($memberInfo,$expires)
{
$time = time(); //签发时间
$expire = $time + $expires; //过期时间
$scopes = 'role_access';
if($expires==$this->refreshExpires) $scopes = 'role_refresh';
$token = array(
"member_id" => $memberInfo['member_id'],
'appid'=>$this->appid,
'appsecret'=>$this->appsecret,
"iss" => "funadmin.com",//签发组织
"aud" => "funadmin", //签发作者
"scopes" => $scopes, //刷新
"iat" => $time,
"nbf" => $time,
"exp" => $expire, //过期时间时间戳
);
return JWT::encode($token, $this->key, 'HS256');
}

/**
* @param $membername
* @param $password
* @return array|mixed|void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function getMember($membername, $password)
{
$member = Db::name($this->tableName)
->where('status',1)
->where('username', $membername)
->whereOr('mobile', $membername)
->whereOr('email', $membername)
->field('id as member_id,password')
->limit(1)
->find();
if ($member) {
if (password_verify($password, $member['password'])) {
unset($member['password']);
return $member;
} else {
$this->error(lang('Password is not right'), [], 401);
}
} else {
$this->error(lang('Account is not exist'), [], 401);
}
}

}

49 changes: 0 additions & 49 deletions src/auth/SimpleToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
namespace fun\auth;

use app\common\service\PredisService;
use Firebase\JWT\Key;
use think\facade\Config;
use think\facade\Request;
use fun\auth\Send;
use think\facade\Db;
use think\Lang;
use Firebase\JWT\JWT;
/**
* 生成token
*/
Expand Down Expand Up @@ -96,51 +94,4 @@ public function refresh()
$accessToken = $this->buildAccessToken($memberInfo,$this->expires);
$this->success(lang('success'), ['access_token'=>$accessToken]);
}

/**
* 生成AccessToken
* @return string
*/
protected function buildAccessToken($memberInfo,$expires)
{
$time = time(); //签发时间
$expire = $time + $expires; //过期时间
$scopes = 'role_access';
if($expires==$this->refreshExpires) $scopes = 'role_refresh';
$token = array(
"member_id" => $memberInfo['member_id'],
'appid'=>$this->appid,
'appsecret'=>$this->appsecret,
"iss" => "https://www.funadmin.com",//签发组织
"aud" => "https://www.funadmin.com", //签发作者
"scopes" => $scopes, //刷新
"iat" => $time,
"nbf" => $time,
"exp" => $expire, //过期时间时间戳
);
return JWT::encode($token, $this->key, 'HS256');
}


protected function getMember($membername, $password)
{
$member = Db::name($this->tableName)
->where('status',1)
->where('username', $membername)
->whereOr('mobile', $membername)
->whereOr('email', $membername)
->field('id as member_id,password')
// ->cache($this->appid.$membername,3600)
->find();
if ($member) {
if (password_verify($password, $member['password'])) {
unset($member['password']);
return $member;
} else {
$this->error(lang('Password is not right'), [], 401);
}
} else {
$this->error(lang('Account is not exist'), [], 401);
}
}
}
48 changes: 0 additions & 48 deletions src/auth/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use fun\auth\Send;
use think\facade\Db;
use think\Lang;
use Firebase\JWT\JWT;
/**
* 生成token
*/
Expand Down Expand Up @@ -206,30 +205,6 @@ public function checkParams($params = [])
}
}

/**
* 生成AccessToken
* @return string
*/
protected function buildAccessToken($memberInfo,$expires)
{
$time = time(); //签发时间
$expire = $time + $expires; //过期时间
$scopes = 'role_access';
if($expires==$this->refreshExpires) $scopes = 'role_refresh';
$token = array(
"member_id" => $memberInfo['member_id'],
'appid'=>$this->appid,
'appsecret'=>$this->appsecret,
"iss" => "https://www.funadmin.com",//签发组织
"aud" => "https://www.funadmin.com", //签发作者
"scopes" => $scopes, //刷新
"iat" => $time,
"nbf" => $time,
"exp" => $expire, //过期时间时间戳
);
return JWT::encode($token, $this->key, 'HS256');
}

/**
* 获取刷新用的token检测是否还有效
*/
Expand Down Expand Up @@ -279,29 +254,6 @@ protected function saveToken($accessTokenInfo)
return true;
}

protected function getMember($membername, $password)
{
$member = Db::name($this->tableName)
->where('status',1)
->where('username', $membername)
->whereOr('mobile', $membername)
->whereOr('email', $membername)
->field('id as member_id,password')
->limit(1)
->find();
if ($member) {
if (password_verify($password, $member['password'])) {
unset($member['password']);
return $member;
} else {
$this->error(lang('Password is not right'), [], 401);
}
} else {
$this->error(lang('Account is not exist'), [], 401);
}
}


/**
* 生成签名
* 字符开头的变量不参与签名
Expand Down
4 changes: 2 additions & 2 deletions src/curd/service/CurdService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class CurdService
'fields' => [],//显示的字段
'ignoreFields' => ['create_time', 'status', 'update_time', 'delete_time'],//忽略字段
'tagsSuffix' => ['tags', 'tag'],//识别为tag类型
'urlSuffix' => ['url', 'urls'],//识别为tag类型
'urlSuffix' => ['url', 'urls'],//识别为url类型
'fileSuffix' => ['file', 'files', 'path', 'paths'],//识别为文件字段
'priSuffix' => ['_id', '_ids'],//识别为别的表的主键
'sortSuffix' => ['sort'],//排序
'sortSuffix' => ['sort','orderby','weight'],//排序
'imageSuffix' => ['image', 'images', 'thumb', 'thumbs', 'avatar', 'avatars','picture', 'pictures',''],//识别为图片字段
'editorSuffix' => ['editor', 'content', 'detail', 'details', 'description'],//识别为编辑器字段
'iconSuffix' => ['icon'],//识别为图标字段
Expand Down

0 comments on commit 74c111f

Please sign in to comment.