luck-draw/app/model/Menu.php

72 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2022-02-22 09:27:27 +00:00
<?php
namespace app\model;
use Exception;
class Menu extends Base
{
public const SHOW_YES = 1;
public const SHOW_NO = 0;
public const STATUS_NORMAL = 1;
public const STATUS_DISABLE = 0;
public const TYPE_MENU = 'menu';
public const TYPE_ACTION = 'action';
/**
* 默认操作
*
* @return string[]
*/
public static function defaultAction(): array
{
return [
'index' => '查看',
'add' => '添加',
'edit' => '编辑',
'del' => '删除',
'sort' => '排序',
'modify' => '属性设置',
];
}
/**
* 自从生成常规操作权限
*
* @param int $id
* @param string $name
* @param string $path
* @throws Exception
*/
public static function generate(int $id, string $name, string $path)
{
$actions = self::defaultAction();
$delete = [];
$insert = [];
$created = date('Y-m-d H:i:s');
foreach ($actions as $key => $action) {
$name = explode(':', $name)[0];
$delete[] = $name.':'.$key;
$arr = [];
$arr['title'] = $action;
$arr['pid'] = $id;
$arr['name'] = $name.':'.$key;
$arr['type'] = self::TYPE_ACTION;
$arr['path'] = $path.$id.',';
$arr['remark'] = sprintf("自动生成[%s][%s]操作", $name, $action);
$arr['created_at'] = $created;
$insert[] = $arr;
}
//删除已有常规操作
self::where('pid', $id)->whereIn('name', $delete)->delete();
//新增常规操作
(new self())->saveAll($insert);
}
}