166 lines
4.8 KiB
PHP
166 lines
4.8 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ywxapp<admin@ywxapp.cn>
|
|
// +----------------------------------------------------------------------
|
|
namespace ywxapp\model;
|
|
|
|
use think\model\concern\SoftDelete;
|
|
|
|
use ywxapp\model\BaseModel;
|
|
/**
|
|
* BackendPower 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class BackendPower extends BaseModel
|
|
{
|
|
use SoftDelete;
|
|
|
|
protected function getOptions(): array
|
|
{
|
|
return [
|
|
'strict' => false,
|
|
'name' => 'backend_power',
|
|
'autoWriteTimestamp' => 'int',
|
|
'createTime' => 'create_at',
|
|
'updateTime' => 'update_at',
|
|
'deleteTime' => 'delete_at',
|
|
'defaultSoftDelete' => 0,
|
|
'append' => ['target'],
|
|
'hidden' => ['create_at', 'update_at', 'delete_at'],
|
|
'readonly' => ['id'],
|
|
];
|
|
}
|
|
|
|
|
|
public static function onAfterRead($data)
|
|
{
|
|
// 这里可以直接使用$this访问当前模型
|
|
// if ($data->type == 2) {
|
|
// $data->append(['href', 'openType']);
|
|
// $data->openType = '_iframe';
|
|
// $data->href = $data->route;
|
|
// }
|
|
}
|
|
|
|
|
|
public static function onAfterDelete($data)
|
|
{
|
|
BackendRolePower::where('power_id', $data->id)->delete();
|
|
}
|
|
|
|
/**
|
|
* 获取器:获取状态文本
|
|
*/
|
|
public function getTargetAttr($value, $data)
|
|
{
|
|
return '_self';
|
|
}
|
|
|
|
// 自关联子菜单
|
|
|
|
public function children()
|
|
{
|
|
return $this->hasMany(BackendPower::class, 'pid', 'id')->order('sort', 'asc');
|
|
}
|
|
|
|
// 拥有该权限的角色
|
|
|
|
public function roles()
|
|
{
|
|
return $this->belongsToMany(BackendRole::class, BackendRolePower::class, 'role_id', 'power_id');
|
|
}
|
|
|
|
// 拥有该权限的用户(通过角色)
|
|
|
|
public function admins()
|
|
{
|
|
return $this->belongsToMany(BackendAdmin::class, BackendRoleAccess::class, 'admin_id', 'role_id')->via('roles');
|
|
}
|
|
|
|
/**
|
|
* 将扁平菜单数组转为树形结构
|
|
* @param array $menus 扁平菜单列表(每个元素是数组)
|
|
* @param int $parentId 父ID(默认0表示根)
|
|
* @return array 树形结构
|
|
*/
|
|
public static function buildTree(array $menus, int $parentId = 0): array
|
|
{
|
|
$branch = [];
|
|
foreach ($menus as $menu) {
|
|
if ($menu['pid'] == $parentId) {
|
|
$children = self::buildTree($menus, $menu['id']);
|
|
if (! empty($children)) {
|
|
$menu['child'] = $children;
|
|
}
|
|
$branch[] = $menu;
|
|
}
|
|
}
|
|
return $branch;
|
|
}
|
|
|
|
/**
|
|
* 【可选】从 Collection 转为树(如果你用模型查询)
|
|
*/
|
|
public static function buildTreeFromCollection(Collection $collection, int $parentId = 0): array
|
|
{
|
|
$menus = $collection->toArray();
|
|
return self::buildTree($menus, $parentId);
|
|
}
|
|
|
|
/**
|
|
* 获取权限树形结构
|
|
* @param int $parentId 父ID(默认0表示根)
|
|
* @return array 树形结构
|
|
*/
|
|
public static function getTree($parentId = 0)
|
|
{
|
|
$list = self::where('pid', $parentId)
|
|
->order('sort', 'asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
foreach ($list as &$item) {
|
|
$item['children'] = self::getTree($item['id']);
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 无限分类-权限
|
|
* @param array $cate 栏目
|
|
* @param string $lefthtml 分隔符
|
|
* @param int $pid 父ID
|
|
* @param int $level 层级
|
|
* @return array
|
|
*/
|
|
public static function cateTree($cate, $name = 'title', $lefthtml = '|— ', $pid = 0, $level = 0)
|
|
{
|
|
$arr = [];
|
|
foreach ($cate as $v) {
|
|
if ($v['pid'] == $pid) {
|
|
$v['level'] = $level + 1;
|
|
$v['lefthtml'] = str_repeat($lefthtml, $level);
|
|
$v['l' . $name] = $v['lefthtml'] . lang($v[$name]);
|
|
$arr[] = $v;
|
|
$arr = array_merge($arr, self::cateTree($cate, $name, $lefthtml, $v['id'], $level + 1));
|
|
}
|
|
}
|
|
return $arr;
|
|
}
|
|
|
|
/**
|
|
* 表结构自愈:建表 + 关键列兜底(install.sql 为事实源)。
|
|
* 查询/写入前调用,避免老库缺表导致 1146。
|
|
*/
|
|
public static function ensureSchema(): void
|
|
{
|
|
BaseModel::ensureTableFromInstall(BaseModel::currentPrefix(), 'backend_power');
|
|
}
|
|
}
|