1
0
Files
helper/application/common/service/Menu.php
2020-08-06 14:58:51 +08:00

170 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\common\service;
use app\common\model\Auth as AuthModel;
use app\common\model\AuthUser as AuthUserModel;
use app\common\model\Menu as MenuModel;
use app\common\validate\Menu as MenuValidate;
use think\Config;
use think\Db;
use tools\Tree;
class Menu extends _Init
{
/**
* 树形展示
* @return [type] [description]
*/
public static function tree()
{
$map = [
'status' => 1,
'hide' => 0,
];
$field = [
'id',
'pid',
'title' => 'name',
"case when pid = 0 then true else false end" => 'spread',
];
$list = Db::name('Menu')->field($field)->where($map)->order('sort asc')->select();
$menu = Tree::list2tree($list);
return json_encode($menu);
}
/**
* 显示菜单
* 下一步增加权限管理
* @return [type] [description]
*/
public static function show($uid)
{
$map = [
'status' => 1,
'hide' => 0,
'pid' => 0,
];
if (!in_array($uid, Config::get('administrator'))) {
$authId = AuthUserModel::where('uid', $uid)->column('auth_id');
if (!empty($authId)) {
$rules = AuthModel::where('id', 'in', $authId)->column('rules');
$rules = implode($rules, ',');
$rules = explode(',', $rules);
$rules = array_unique($rules);
$map['id'] = ['in', $rules];
} else {
// return null;
// $map = [];
}
}
$field = [
'id',
'title',
'url' => 'href',
'icon',
];
$list = Db::name('Menu')->field($field)->where($map)->order('sort asc')->select();
foreach ($list as $key => $value) {
if ($key == 0) {
$list[$key]['spread'] = true;
}
$map = [
'status' => 1,
'hide' => 0,
'pid' => $value['id'],
];
$list[$key]['children'] = Db::name('Menu')->field($field)->where($map)->order('sort asc')->select();
}
return json_encode($list);
}
/**
* 新增菜单
* @param [type] $data [description]
*/
public static function add($data)
{
$validate = new MenuValidate();
if (!$validate->scene('add')->check($data)) {
return $validate->getError();
}
if (MenuModel::create($data)) {
Logs::write('新增菜单', $data);
return true;
} else {
return '新增菜单失败';
}
}
/**
* 编辑菜单
* @param [type] $data 需要编辑的数据
* @return [type] 成功返回 true 失败返回错误信息
*/
public static function edit($data)
{
$validate = new MenuValidate();
if (!$validate->scene('edit')->check($data)) {
return $validate->getError();
}
if (MenuModel::update($data)) {
Logs::write('编辑菜单', $data);
return true;
} else {
return '编辑菜单失败';
}
}
/**
* 删除菜单
* @param [type] $id 要删除的菜单id
* @return [type] 成功返回 true 失败返回错误信息
*/
public static function del($id)
{
if (MenuModel::where('pid', 'in', $id)->find()) {
return $this->error('该菜单有下级菜单,不允许直接删除');
}
if (MenuModel::destroy($id)) {
Logs::write('删除菜单', $id);
return true;
} else {
return '删除菜单失败';
}
}
/**
* 设置菜单状态
* @param [type] $id 要设置的id
* @param [type] $status 要设置的状态
* @param [type] $type 要设置的字段
* @return [type] [description]
*/
public static function status($id, $status, $type)
{
$info = MenuModel::get($id);
if (!$info) {
return '菜单不存在';
} elseif ($info->save([$type => $status])) {
return true;
} else {
return '设置菜单状态失败';
}
}
}