2025-04-01 16:23:28 +08:00

211 lines
6.3 KiB
PHP

<?php
namespace app\adminapi\model;
use app\common\model\BaseModel;
use think\Cache;
use think\Model;
class AuthRule extends BaseModel
{
protected $name = 'api_auth_rule';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 数据自动完成字段
protected $insert = ['py', 'pinyin'];
protected $update = ['py', 'pinyin'];
// 拼音对象
protected static $pinyin = null;
protected static function init()
{
self::$pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
self::beforeWrite(function ($row) {
if (isset($_POST['row']) && is_array($_POST['row']) && isset($_POST['row']['condition'])) {
$originRow = $_POST['row'];
$row['condition'] = $originRow['condition'] ?? '';
}
});
self::afterWrite(function ($row) {
Cache::rm('__menu__');
});
}
public function getTitleAttr($value, $data)
{
return __($value);
}
public function getMenutypeList()
{
return ['addtabs' => __('Addtabs'), 'dialog' => __('Dialog'), 'ajax' => __('Ajax'), 'blank' => __('Blank')];
}
public function setPyAttr($value, $data)
{
if (isset($data['title']) && $data['title']) {
return self::$pinyin->abbr(__($data['title']));
}
return '';
}
public function setPinyinAttr($value, $data)
{
if (isset($data['title']) && $data['title']) {
return self::$pinyin->permalink(__($data['title']), '');
}
return '';
}
/**
* 获取会员组别规则列表
* @return array|bool|\PDOStatement|string|\think\Collection
*/
public function getRuleList($admin_id,$where=[],$full = false)
{
$group_ids = AuthGroupAccess::where("uid",$admin_id)->column("group_id");
if(!$group_ids && !$full) return [];
$groups = AuthGroup::where('id', 'in', $group_ids)->select();
if (!$groups && !$full ) {
return [];
}
$rules = [];
foreach ($groups as $group){
$rules = array_merge($rules, explode(',', $group->rules));
}
//包含*全查,否则按值查
if ($full || in_array('*', $rules)) {
return AuthRule::where($where)->where('status', 'normal')->order('weigh desc,id desc')->select();
}
return AuthRule::where($where)->where('status', 'normal')->where('id', 'in', $rules)->order('weigh desc,id desc')->select();
}
//得到菜单列表
public function getMenulist($admin_id,$where= ["ismenu"=>1],$is_tree=false,$full = false,$id_name='id',$pid_name='pid',$child_name='children')
{
$menu = $this->getRuleList($admin_id,$where,$full);
if(!$is_tree){
return $menu;
}
// 生成菜单的树状结构 id pid ,pid=0为顶级菜单
$menulist = [];
foreach ($menu as $k => $v) {
$v = $v->toArray();
$v[$id_name] = $v['id'];
$v[$pid_name] = $v['pid'];
$menulist[$v[$id_name]] = $v;
}
$tree = [];
foreach ($menulist as $k => $v) {
if (isset($menulist[$v[$pid_name]])) {
$menulist[$v[$pid_name]][$child_name][] = &$menulist[$v[$id_name]];
} else {
$tree[] = &$menulist[$v[$id_name]];
}
}
return $tree;
}
public function authCheck($admin_id,$auth_name)
{
$group_ids = AuthGroupAccess::where("uid",$admin_id)->column("group_id");
if(!$group_ids) return null;
$groups = AuthGroup::where('id', 'in', $group_ids)->select();
if (!$groups) {
return null;
}
$rules = [];
foreach ($groups as $group){
$rules = array_merge($rules, explode(',', $group->rules));
}
//包含*全查,否则按值查
if (in_array('*', $rules)) {
return AuthRule::where('status', 'normal')->where( 'name|rule_name',$auth_name)->find() ;
}
return AuthRule::where('status', 'normal')->where( 'name|rule_name',$auth_name)->where('id', 'in', $rules)->find();
}
public function getAllRules($admin_id)
{
$group_ids = AuthGroupAccess::where("uid",$admin_id)->column("group_id");
if(!$group_ids) return null;
$groups = AuthGroup::where('id', 'in', $group_ids)->select();
if (!$groups) {
return null;
}
$rules = [];
foreach ($groups as $group){
$rules = array_merge($rules, explode(',', $group->rules));
}
//包含*全查,否则按值查
if (in_array('*', $rules)) {
return AuthRule::where('status', 'normal')->field("name,rule_name,ismenu")->select() ;
}
return AuthRule::where('status', 'normal')->where('id', 'in', $rules)->field("name,rule_name,ismenu")->select();
}
/** 通用新增(后台api版本)
* @param $params
* @param $trans
* @return $this
* @throws \Exception
*/
public function add($params,$trans=false){
if (empty($params)) {
throw new \Exception(__('Parameter %s can not be empty', ''));
}
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->validateFailException()->validate($validate);
}
$result = $this->allowField(true)->save($params);
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
}
return $this;
}
}