Commit 47dd5d0e authored by hujun's avatar hujun

权限缓存

parent 9f9d6df6
...@@ -30,10 +30,12 @@ class RedisCacheService ...@@ -30,10 +30,12 @@ class RedisCacheService
public function __construct() public function __construct()
{ {
$this->redis_ext = RedisExt::getRedis(); $this->redis_ext = RedisExt::getRedis();
$this->time = 604800; //7 $this->time = 172800; //2
} }
/** /**
* 设置缓存
*
* @param $key * @param $key
* @param $value * @param $value
* @param int $time * @param int $time
...@@ -42,7 +44,6 @@ class RedisCacheService ...@@ -42,7 +44,6 @@ class RedisCacheService
public function setRedisCache($key, $value, $time = 0) public function setRedisCache($key, $value, $time = 0)
{ {
$value = json_encode($value); $value = json_encode($value);
$key = $this->key_string.$key;
if (!$time) { if (!$time) {
$time = $this->time; $time = $this->time;
} }
...@@ -50,6 +51,8 @@ class RedisCacheService ...@@ -50,6 +51,8 @@ class RedisCacheService
} }
/** /**
* 获取缓存
*
* @param $key * @param $key
* @return mixed * @return mixed
*/ */
...@@ -65,6 +68,8 @@ class RedisCacheService ...@@ -65,6 +68,8 @@ class RedisCacheService
} }
/** /**
* 设置缓存
*
* @param $key * @param $key
* @param $value * @param $value
* @return bool * @return bool
...@@ -78,6 +83,7 @@ class RedisCacheService ...@@ -78,6 +83,7 @@ class RedisCacheService
} }
/** /**
* 设置hash缓存
* @param $key * @param $key
* @param $value * @param $value
* @param int $time * @param int $time
...@@ -92,6 +98,33 @@ class RedisCacheService ...@@ -92,6 +98,33 @@ class RedisCacheService
return $this->redis_ext->set($key, $value, $time); return $this->redis_ext->set($key, $value, $time);
} }
/**
* 删除缓存
*
* @param $type
* @param $id
* @return bool
*/
public function delRedisCache($type, $id)
{
$result = 0;
switch ($type) {
case 1:
$result = $this->redis_ext->del($this->user_key.$id);
break;
case 2 :
$result = $this->redis_ext->del($this->agent_key.$id);
break;
case 3 :
$result = $this->redis_ext->del($this->group_key.$id);
break;
case 4 :
$result = $this->redis_ext->del($this->rule_key.$id);
}
return $result;
}
/** /**
* 缓存用户信息 * 缓存用户信息
* *
...@@ -149,6 +182,15 @@ class RedisCacheService ...@@ -149,6 +182,15 @@ class RedisCacheService
return $data; return $data;
} }
/**
* 缓存角色
*
* @param $group_id
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function groupCache($group_id) public function groupCache($group_id)
{ {
$m_auth_group = new AuthGroup(); $m_auth_group = new AuthGroup();
...@@ -160,19 +202,28 @@ class RedisCacheService ...@@ -160,19 +202,28 @@ class RedisCacheService
return $data; return $data;
} }
public function authCache($rule_id) /**
* 根据规则名缓存角色权限规则
*
* @param $name
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function authCache($name)
{ {
$m_auth_rule = new AuthRule(); $m_auth_rule = new AuthRule();
$field = 'id,name,title,pid,is_menu'; $field = 'id,name,title,pid,is_menu';
$data = $m_auth_rule->getRule($field, ['id'=>$rule_id,'status'=>0]); $data = $m_auth_rule->getRule($field, ['name'=>$name,'status'=>0]);
if (!empty($data)) { if (!empty($data)) {
$this->setRedisCache($this->rule_key . $rule_id, $data[0]); $this->setRedisCache($this->rule_key . $name, $data[0]);
} }
return $data; return $data;
} }
/** /**
* @param $type 1:客户 2:经纪人 3:角色组 4:权限 * @param $type 1:客户 2:经纪人 3:角色组 4:权限规则
* @param $id * @param $id
* @return array|bool|false|mixed|\PDOStatement|string|\think\Model * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
*/ */
......
...@@ -18,11 +18,13 @@ class VipService ...@@ -18,11 +18,13 @@ class VipService
private $agentModel; private $agentModel;
private $groupModel; private $groupModel;
private $rulesModel; private $rulesModel;
private $redisCache;
public function __construct() public function __construct()
{ {
$this->agentModel = new AAgents(); $this->agentModel = new AAgents();
$this->groupModel = new AuthGroup(); $this->groupModel = new AuthGroup();
$this->rulesModel = new AuthRule(); $this->rulesModel = new AuthRule();
$this->redisCache = new RedisCacheService();
} }
/** /**
...@@ -39,13 +41,31 @@ class VipService ...@@ -39,13 +41,31 @@ class VipService
return 0; return 0;
} }
$fields = 'auth_group_id'; $agent_data = $this->redisCache->getRedisCache(2, $id);
$auth_group_id = $this->agentModel->getAgentsById($id, $fields);
$fields = 'rules'; if (empty($agent_data['auth_group_id'])) {
$rules = $this->groupModel->getAuthGroupById($auth_group_id, $fields); $fields = 'auth_group_id';
$auth_group_id = $this->agentModel->getAgentsById($id, $fields);
} else {
$auth_group_id = $agent_data['auth_group_id'];
}
$group_data = $this->redisCache->getRedisCache(3, $auth_group_id);
if (empty($group_data)) {
$fields = 'rules';
$rules = $this->groupModel->getAuthGroupById($auth_group_id, $fields);
} else {
$rules = $group_data['rules'];
}
$rule_data = $this->redisCache->getRedisCache(4, $name);
if (empty($auth_group_id)) {
$fields = 'id';
$auth_rule_id = $this->rulesModel->getAuthRuleByName($name, $fields);
} else {
$auth_rule_id = $rule_data['id'];
}
$fields = 'id';
$auth_rule_id = $this->rulesModel->getAuthRuleByName($name, $fields);
$rules_arr = explode(',', $rules); $rules_arr = explode(',', $rules);
if (in_array($auth_rule_id, $rules_arr)) { if (in_array($auth_rule_id, $rules_arr)) {
...@@ -91,9 +111,27 @@ class VipService ...@@ -91,9 +111,27 @@ class VipService
} }
try { try {
$auth_group_id = $this->agentModel->getAgentsById($id, 'auth_group_id'); $agent_data = $this->redisCache->getRedisCache(2, $id);
$rules = $this->groupModel->getAuthGroupById($auth_group_id, 'rules'); if (empty($agent_data['auth_group_id'])) {
$auth_rule_id = $this->rulesModel->getAuthRuleByName($name, 'id'); $auth_group_id = $this->agentModel->getAgentsById($id, 'auth_group_id');
} else {
$auth_group_id = $agent_data['auth_group_id'];
}
$group_data = $this->redisCache->getRedisCache(3, $auth_group_id);
if (empty($group_data)) {
$rules = $this->groupModel->getAuthGroupById($auth_group_id, 'rules');
} else {
$rules = $group_data['rules'];
}
$rule_data = $this->redisCache->getRedisCache(4, $name);
if (empty($auth_group_id)) {
$auth_rule_id = $this->rulesModel->getAuthRuleByName($name, 'id');
} else {
$auth_rule_id = $rule_data['id'];
}
} catch (Exception $e) { } catch (Exception $e) {
return -1; return -1;
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace app\index\controller; namespace app\index\controller;
use app\api_broker\service\RedisCacheService;
use app\index\extend\Basic; use app\index\extend\Basic;
use app\index\service\BrokerService; use app\index\service\BrokerService;
use app\model\AuthGroup; use app\model\AuthGroup;
...@@ -269,6 +270,8 @@ class Auth extends Basic ...@@ -269,6 +270,8 @@ class Auth extends Basic
} }
if ($auth->saveStatus('rules', $this->params['rules'], $this->params['id'])) { if ($auth->saveStatus('rules', $this->params['rules'], $this->params['id'])) {
$redis_service = new RedisCacheService();
$redis_service->groupCache($this->params['id']);
return $this->response(200, '成功'); return $this->response(200, '成功');
}else{ }else{
return $this->response(100, '失败'); return $this->response(100, '失败');
...@@ -279,12 +282,23 @@ class Auth extends Basic ...@@ -279,12 +282,23 @@ class Auth extends Basic
* 设置权限状态 * 设置权限状态
*/ */
public function updateRoleStatus(){ public function updateRoleStatus(){
$table=New AuthRule; $table = new AuthRule;
$data=$this->request->param(); $data = $this->request->param();
$ids = trim($data['ids'],',');
$rule_data = $table->getAuthRule('id,name,status', ['id'=>$ids]);
if ($rule_data['status'] == $data['status']) {
return $this->response(101, '无更新内容');
}
$ids=trim($data['ids'],',');
if($table->saveStatus('status',$data['status'],$ids)){ if($table->saveStatus('status',$data['status'],$ids)){
$redis_service = new RedisCacheService();
if ($data['status']) {
$redis_service->delRedisCache(4, $rule_data['name']);
} else {
$redis_service->getRedisCache(4, $rule_data['name']);
}
return $this->response(200,'成功',$data); return $this->response(200,'成功',$data);
}else{ }else{
return $this->response(100,'失败',$data); return $this->response(100,'失败',$data);
...@@ -302,6 +316,14 @@ class Auth extends Basic ...@@ -302,6 +316,14 @@ class Auth extends Basic
$ids=trim($data['ids'],','); $ids=trim($data['ids'],',');
if($table->saveStatus('status',$data['status'],$ids)){ if($table->saveStatus('status',$data['status'],$ids)){
$redis_service = new RedisCacheService();
if ($data['status']) {
$redis_service->delRedisCache(3, $ids);
} else {
$redis_service->getRedisCache(3, $ids);
}
return $this->response(200,'成功',$data); return $this->response(200,'成功',$data);
}else{ }else{
return $this->response(100,'失败',$data); return $this->response(100,'失败',$data);
...@@ -395,6 +417,8 @@ class Auth extends Basic ...@@ -395,6 +417,8 @@ class Auth extends Basic
$data = input('post.'); $data = input('post.');
$data['name'] = trim($data['name']);
$data['title'] = trim($data['title']);
if($table->repetition('name',$data['name'])&& empty($data['id'])){ if($table->repetition('name',$data['name'])&& empty($data['id'])){
return $this->response(100, 'url存在重复值'); return $this->response(100, 'url存在重复值');
} }
...@@ -413,8 +437,9 @@ class Auth extends Basic ...@@ -413,8 +437,9 @@ class Auth extends Basic
//新增或者编辑数据 //新增或者编辑数据
//prt($data) //prt($data)
if ($table->editData($data,$id)) { if ($table->editData($data,$id)) {
$redis_service = new RedisCacheService();
$redis_service->getRedisCache(4, $data['name']);
return $this->response(200, '成功'); return $this->response(200, '成功');
} else { } else {
return $this->response(100, '无修改'); return $this->response(100, '无修改');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment