Commit 47dd5d0e authored by hujun's avatar hujun

权限缓存

parent 9f9d6df6
......@@ -30,10 +30,12 @@ class RedisCacheService
public function __construct()
{
$this->redis_ext = RedisExt::getRedis();
$this->time = 604800; //7
$this->time = 172800; //2
}
/**
* 设置缓存
*
* @param $key
* @param $value
* @param int $time
......@@ -42,7 +44,6 @@ class RedisCacheService
public function setRedisCache($key, $value, $time = 0)
{
$value = json_encode($value);
$key = $this->key_string.$key;
if (!$time) {
$time = $this->time;
}
......@@ -50,6 +51,8 @@ class RedisCacheService
}
/**
* 获取缓存
*
* @param $key
* @return mixed
*/
......@@ -65,6 +68,8 @@ class RedisCacheService
}
/**
* 设置缓存
*
* @param $key
* @param $value
* @return bool
......@@ -78,6 +83,7 @@ class RedisCacheService
}
/**
* 设置hash缓存
* @param $key
* @param $value
* @param int $time
......@@ -92,6 +98,33 @@ class RedisCacheService
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
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)
{
$m_auth_group = new AuthGroup();
......@@ -160,19 +202,28 @@ class RedisCacheService
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();
$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)) {
$this->setRedisCache($this->rule_key . $rule_id, $data[0]);
$this->setRedisCache($this->rule_key . $name, $data[0]);
}
return $data;
}
/**
* @param $type 1:客户 2:经纪人 3:角色组 4:权限
* @param $type 1:客户 2:经纪人 3:角色组 4:权限规则
* @param $id
* @return array|bool|false|mixed|\PDOStatement|string|\think\Model
*/
......
......@@ -18,11 +18,13 @@ class VipService
private $agentModel;
private $groupModel;
private $rulesModel;
private $redisCache;
public function __construct()
{
$this->agentModel = new AAgents();
$this->groupModel = new AuthGroup();
$this->rulesModel = new AuthRule();
$this->redisCache = new RedisCacheService();
}
/**
......@@ -39,13 +41,31 @@ class VipService
return 0;
}
$fields = 'auth_group_id';
$auth_group_id = $this->agentModel->getAgentsById($id, $fields);
$fields = 'rules';
$rules = $this->groupModel->getAuthGroupById($auth_group_id, $fields);
$agent_data = $this->redisCache->getRedisCache(2, $id);
if (empty($agent_data['auth_group_id'])) {
$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);
if (in_array($auth_rule_id, $rules_arr)) {
......@@ -91,9 +111,27 @@ class VipService
}
try {
$auth_group_id = $this->agentModel->getAgentsById($id, 'auth_group_id');
$rules = $this->groupModel->getAuthGroupById($auth_group_id, 'rules');
$auth_rule_id = $this->rulesModel->getAuthRuleByName($name, 'id');
$agent_data = $this->redisCache->getRedisCache(2, $id);
if (empty($agent_data['auth_group_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) {
return -1;
}
......
......@@ -8,6 +8,7 @@
namespace app\index\controller;
use app\api_broker\service\RedisCacheService;
use app\index\extend\Basic;
use app\index\service\BrokerService;
use app\model\AuthGroup;
......@@ -269,6 +270,8 @@ class Auth extends Basic
}
if ($auth->saveStatus('rules', $this->params['rules'], $this->params['id'])) {
$redis_service = new RedisCacheService();
$redis_service->groupCache($this->params['id']);
return $this->response(200, '成功');
}else{
return $this->response(100, '失败');
......@@ -279,12 +282,23 @@ class Auth extends Basic
* 设置权限状态
*/
public function updateRoleStatus(){
$table=New AuthRule;
$data=$this->request->param();
$table = new AuthRule;
$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)){
$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);
}else{
return $this->response(100,'失败',$data);
......@@ -302,6 +316,14 @@ class Auth extends Basic
$ids=trim($data['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);
}else{
return $this->response(100,'失败',$data);
......@@ -395,6 +417,8 @@ class Auth extends Basic
$data = input('post.');
$data['name'] = trim($data['name']);
$data['title'] = trim($data['title']);
if($table->repetition('name',$data['name'])&& empty($data['id'])){
return $this->response(100, 'url存在重复值');
}
......@@ -413,8 +437,9 @@ class Auth extends Basic
//新增或者编辑数据
//prt($data)
if ($table->editData($data,$id)) {
$redis_service = new RedisCacheService();
$redis_service->getRedisCache(4, $data['name']);
return $this->response(200, '成功');
} else {
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