2023-03-14 19:48:08 +08:00

133 lines
3.3 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
/**
* 商户菜单 model
*
* @author刘孝全
* @emailq8197264@126.com
* @date 2023年03月3日
*/
declare (strict_types = 1);
namespace app\common\model\merchant\system\auth;
use think\db\BaseQuery;
use think\Model;
use think\exception\ValidateException;
/**
* Class Menu
*/
class Menu extends Model
{
protected $connection = 'shop';
protected $table = 'eb_system_menu';
protected $pk = 'menu_id';
/**
* 获取权限列表
*/
public function getList(array $where, int $is_mer=2, ?int $page=NULL, ?int $limit=NULL)
{
$query = $this->search($where, $is_mer);
// 查询记录总行数
$count = $query->count();
if (isset($page) && isset($limit)) {
$query->page($page)->limit($limit);
}
$list = $query->order('sort DESC,menu_id ASC')->hidden(['update_time', 'path'])->select()
->toArray();
// 合并为一个数组并返回
$data = compact('count', 'list');
return $data;
}
/**
* 按条件获取商户菜单数据
*
* @param array $where 过滤字段条件
* @param int $is_mer 菜单类型: 0商城平台菜单 1商户菜单
*
* @return array
*/
protected function search(array $where=[], int $is_mer=0)
{
// 按条件 拼接 select 前sql
$query = self::where('is_mer', $is_mer)
->when(isset($where['pid'])&& $where['pid']!=='',
function($query)use($where){
$query->where('pid', (int)$where['pid']);
}
)
->when(isset($where['keyword'])&&$where['keyword']!=='',
function ($query)use($where){
$query->whereLike('menu_name|route', "%{$where['keyword']}%");
}
)
->when(isset($where['is_menu'])&& $where['is_menu']!=='',
function($query)use($where){
$query->where('is_menu', (int)$where['is_menu']);
}
)
->field('menu_id as id,pid, sort, route as src,icon,menu_name as title,is_show as status, is_menu as menu');
return $query;
}
/**
* 获取指定行数据
*
* @return array $row
*/
function Find($id) {
$row = self::where('menu_id', $id)
->field('menu_id as id,pid, sort, route as src,icon,menu_name as title,is_show as status, is_menu as menu')
->find();
return $row;
}
/**
* 添加
*/
function Add() {}
/**
* 更新
*/
function modify()
{
return true;
}
/**
* 删除指定菜单项
* 由于有三种结果,建议用 try{}catch() 捕获
*
* @return bool||throw Exception
*/
function Del($id)
{
$count = self::where(["pid" => $id])->count();
if ($count > 0) {
throw new ValidateException('该节点下还有子节点,无法删除');
return false;
}
if (self::delete($id) !== false) {
clear_cache('adminRules');
add_log('delete', $id, []);
throw new ValidateException('删除节点成功');
} else {
throw new ValidateException('删除失败');
return false;
}
return true;
}
}