server/app/Model/Menu.php

200 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Db;
/**
* @property int $menu_id
* @property int $parent_id
* @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 int $del_flag
* @property string $create_time
* @property string $update_time
* @property string $remark
*/
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', 'parent_id' => 'integer', 'account_type' => 'integer', 'type' => 'integer', 'rank' => 'integer', 'hidden' => 'integer', 'del_flag' => 'integer'];
/**
* 获取账号权限
* Author: cfn <cfn@leapy.cn>
* @param array $account
* @return array
*/
public static function getMenu(array $account): array
{
if ($account['master_flag'] == 1) {
$menus = self::getM($account['account_type']);
$buttons = self::getB($account['account_type']);
$roles = ['MASTER_ACCOUNT'];
} else {
$menus = self::getMByRole($account['account_type'], $account['account_id']);
$buttons = self::getBByRole($account['account_type'], $account['account_id']);
$roles = ['SUB_ACCOUNT'];
}
$roles[] = match ($account['account_type']) {
0 => "ADMIN",
1 => "TENANT",
2 => "PARTNER",
3 => "MERCHANT"
};
return compact("menus", "buttons", "roles");
}
/**
* Author: cfn <cfn@leapy.cn>
* @param $account_type
* @return array
*/
public static function getM($account_type)
{
return self::from("menu as m")
->where('m.del_flag', 0)
->where('m.account_type', $account_type)
->where('m.type', 0)
->orderByDesc("m.rank")
->select(['m.menu_id', 'm.parent_id', 'm.title', 'm.name', 'm.path', 'm.icon', 'm.hidden'])->get()->toArray();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param int $accountType
* @param int $accountId
* @return array
*/
private static function getMByRole(int $accountType, int $accountId)
{
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", $accountId)
->where("m.account_type", $accountType)
->where("m.type", 0)
->where("m.del_flag", 0)
->orderByDesc("rank")
->select(['m.menu_id', 'm.parent_id', 'm.title', 'm.name', 'm.path', 'm.icon', 'm.hidden'])->get()->toArray();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param int $accountType
* @return array
*/
public static function getB(int $accountType): array
{
return self::where('del_flag', 0)
->where('type', 1)
->where('account_type', $accountType)
->pluck('flag')->toArray();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param int $accountType
* @param int $accountId
* @return array
*/
private static function getBByRole(int $accountType, int $accountId): array
{
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", $accountId)
->where("m.account_type", $accountType)
->where("m.type", 1)
->where("m.del_flag", 0)
->pluck('m.flag')->toArray();
}
public static function getMenusV1(array $param)
{
return self::where('del_flag', 0)
->where('account_type', $param['account_type'])
->orderByDesc("rank")
->select(["menu_id","title","flag","parent_id","type","method","name","path","icon","rank","hidden","account_type"])
->get()->toArray();
}
public static function getMenus(array $account)
{
if (in_array($account['account_type'], [1,2,3])) {
return self::from("trade_open as to")
->leftJoin("trade_menu as tm","tm.trade_id","=","to.trade_id")
->leftJoin("menu as m", "m.menu_id","=","tm.menu_id")
->where("tm.account_type", $account['account_type'])
->where("to.account_type", $account['account_type'])
->where("to.belong_id", $account['belong_id'])
->where("to.status",1)
->where("m.account_type", $account['account_type'])
->where("m.del_flag",0)
->groupBy(["m.menu_id"])
->select(["m.menu_id","m.title","m.flag","m.parent_id","m.type","m.method","m.name","m.path","m.icon","m.rank","m.hidden","m.account_type"])
->get()->toArray();
}else{
return self::where('del_flag', 0)
->where('account_type', $account['account_type'])
->orderByDesc("rank")
->select(["menu_id","title","flag","parent_id","type","method","name","path","icon","rank","hidden","account_type"])
->get()->toArray();
}
}
public static function add(array $data)
{
$data['create_time'] = date("Y-m-d H:i:s");
$data['del_flag'] = 0;
return self::insertGetId($data);
}
/**
* 修改
* @param array $data
* @return bool
*/
public static function edit(array $data)
{
$data['update_time'] = date("Y-m-d H:i:s");
return self::where("menu_id", $data['menu_id'])->where('del_flag', 0)->update($data);
}
/**
* 删除
* @param string $menu_id
* @return int
*/
public static function del(string $menu_id)
{
return self::whereIn('menu_id', explode(",", $menu_id))
->update([
'update_time' => date('Y-m-d H:i:s'),
'del_flag' => 1
]);
}
}