server/app/Model/Menu.php

205 lines
6.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;
use function Hyperf\Config\config;
/**
* @property int $menu_id
* @property int $pid
* @property string $title
* @property int $account_type
* @property int $type
* @property string $method
* @property string $flag
* @property string $name
* @property string $path
* @property string $icon
* @property int $rank
* @property int $hidden
* @property string $create_time
* @property string $update_time
* @property string $deleted_at
*/
class Menu extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'menu';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['menu_id' => 'integer', 'pid' => 'integer', 'account_type' => 'integer', 'type' => 'integer', 'rank' => 'integer', 'hidden' => 'integer'];
/**
* 根据账号类型获取权限(主账号)
* Author: cfn <cfn@leapy.cn>
* @param int $account_type
* @return array
*/
public static function getAuth1(int $account_type)
{
return self::where("account_type", $account_type)
->where("type", 2)
->select([Db::raw("concat(method,':',flag) as auth")])
->groupBy(["auth"])
->pluck("auth")
->toArray();
}
/**
* 根据账号获取权限(子账号+角色)
* Author: cfn <cfn@leapy.cn>
* @param int $account_id
* @param int $account_type
* @return array
*/
public static function getAuth2(int $account_id, int $account_type)
{
return Db::table("account_role")
->leftJoin("role_menu", "account_role.role_id", "=", "role_menu.role_id")
->leftJoin("menu", "menu.menu_id", "=", "role_menu.menu_id")
->where("account_role.account_id", $account_id)
->where("menu.account_type", $account_type)
->where("menu.type", 2)
->select([Db::raw("concat(method,':',flag) as auth")])
->pluck("auth")
->toArray();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param $method
* @param $flag
* @param $belong
* @return string
*/
public static function getTitleByCache($method, $flag, $belong)
{
// 先从缓存取数据,缓存中没则从数据库中取数据
$container = ApplicationContext::getContainer();
$redis = $container->get(\Hyperf\Redis\Redis::class);
$menus = $redis->get("AUTH:" . $belong);
if (!empty($menus) && isset($menus[$method . ":" . $flag])) {
// 存在则从缓存中读取
return $menus[$method . ":" . $flag];
} else {
$title = self::where('del_flag', 0)
->where('type', 2)
->where('method', $method)
->where('flag', $flag)
->where('belong', $belong)
->value("title");
if ($title) {
// 代表缓存数据已过期需更新
self::setCache($belong);
return $title;
}
return "未知操作";
}
}
/**
* Author: cfn <cfn@leapy.cn>
* @param $belong
* @return bool|\Redis
*/
private static function setCache($belong)
{
$arr = self::where('del_flag', 0)
->where('type', 2)
->where('belong', $belong)
->select(["title", "concat(method,':',flag) as flag"]);
// 数组形式转换
$newArr = [];
foreach ($arr as $v) {
$newArr[$v['title']] = $v['flag'];
}
$container = ApplicationContext::getContainer();
$redis = $container->get(\Hyperf\Redis\Redis::class);
return $redis->set("AUTH:" . $belong, json_encode($newArr), 72 * 60 * 60);
}
/**
* Author: cfn <cfn@leapy.cn>
* @param int $account_type
* @param array $field
* @return array
*/
public static function getMenu(int $account_type, array $field = ['*'])
{
return self::from("menu as m")
->where("m.belong", $account_type)
->where("m.type", 0)
->orderByDesc("m.rank")
->select($field)
->get()
->toArray();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param int $account_type
* @return array
*/
public static function getButton(int $account_type)
{
return self::from("menu as m")
->where("m.belong", $account_type)
->where("m.type", 1)
->orderByDesc("m.rank")
->pluck('m.flag')
->toArray();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param int $account_id
* @param int $account_type
* @param array $field
* @return array
*/
public static function getMenu2(int $account_id, int $account_type, array $field = ['*'])
{
return Db::table("account_role as ar")
->leftJoin("role_menu as rm", "ar.role_id", "=", "rm.role_id")
->leftJoin("menu as m", "m.menu_id", "=", "rm.menu_id")
->where("ar.account_id", $account_id)
->where("m.account_type", $account_type)
->where("m.type", 0)
->orderByDesc("rank")
->select($field)
->get()
->toArray();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param int $account_id
* @param int $account_type
* @return array
*/
public static function getButton2(int $account_id, int $account_type)
{
return Db::table("account_role as ar")
->leftJoin("role_menu as rm", "ar.role_id", "=", "rm.role_id")
->leftJoin("menu as m", "m.menu_id", "=", "rm.menu_id")
->where("ar.account_id", $account_id)
->where("m.account_type", $account_type)
->where("m.type", 1)
->pluck('m.flag')
->toArray();
}
}