From 842e365f8bba3b7a2c608e08459053d33f0bb9b6 Mon Sep 17 00:00:00 2001 From: liuxiaoquan Date: Fri, 3 Mar 2023 23:33:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E6=88=B7=E8=8F=9C=E5=8D=95=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=8C=E7=A7=BB=E5=8A=A8=E5=95=86=E6=88=B7model?= =?UTF-8?q?=E5=88=B0commom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../merchant/system/merchant/auth/Menu.php | 120 +++++++++++++++--- app/admin/route/menu.php | 48 ++----- app/admin/view/merchant/system/auth/add.html | 6 +- app/common/controller/FormatList.php | 34 ++++- app/common/model/merchant/system/Merchant.php | 51 ++++++++ .../merchant/system/MerchantApplyments.php | 22 ++++ .../model/merchant/system/auth/Menu.php | 108 ++++++++++++++++ .../model/merchant/system/auth/Role.php | 14 ++ .../merchant/system/merchant/MerchantType.php | 14 ++ 9 files changed, 353 insertions(+), 64 deletions(-) create mode 100644 app/common/model/merchant/system/Merchant.php create mode 100644 app/common/model/merchant/system/MerchantApplyments.php create mode 100644 app/common/model/merchant/system/auth/Menu.php create mode 100644 app/common/model/merchant/system/auth/Role.php create mode 100644 app/common/model/merchant/system/merchant/MerchantType.php diff --git a/app/admin/controller/merchant/system/merchant/auth/Menu.php b/app/admin/controller/merchant/system/merchant/auth/Menu.php index 1c4efe0..7e0398a 100644 --- a/app/admin/controller/merchant/system/merchant/auth/Menu.php +++ b/app/admin/controller/merchant/system/merchant/auth/Menu.php @@ -9,8 +9,10 @@ namespace app\admin\controller\merchant\system\merchant\auth; use app\admin\BaseController; -use app\admin\model\system\auth\Menu as MenuModel; +use app\common\model\merchant\system\auth\Menu as MenuModel; +use think\facade\View; use app\common\controller\FormatList; +use think\exception\ValidateException; /** * class Menu @@ -18,38 +20,114 @@ use app\common\controller\FormatList; class Menu extends BaseController { protected $menu; - protected $is_mer; - protected $url; - protected $category_id; + protected $params; function __construct(MenuModel $menu) { // $this->adminInfo = get_login_admin(); $this->menu = $menu; - // 模块,0 平台, 1商户 - $this->is_mer = get_params('is_m', 0) == 0 ? 2 : 2; - $this->category_id=354; - $this->url=[ - '/admin/nk.classroom/index?category_id='.$this->category_id, - '/admin/nk.classroom/add', - '/admin/nk.classroom/edit', - '/admin/nk.classroom/del', - '/admin/nk.classroom/read', - ]; + $this->params = get_params() ; } + /** + * 空白菜单页,由前端 ajax 获取数据 + */ function Index() { - return view('merchant/system/auth/index',['url'=>$this->url]); + return view('merchant/system/auth/index'); } - function Lst(FormatList $formatList){ + /** + * 菜单列表 + */ + function Lst(){ // 查出商户所有菜单数据 - $list = $this->menu->Search([], 1); + $data = $this->menu->Search([], 1); - // 格式化成多维数组 - // $data = $formatList->FormatCategory($list['list'], "menu_id"); - to_assign(0,'操作成功',$list); - // return view('merchant/system/auth/index',['url'=>$this->url]); + to_assign(0,'',$data['list']); + } + + /** + * 表单页 + * TODO: 操作日志要另建表做关联 + * 操作日志待开发 + * + */ + function AddForm(FormatList $format){ + $id = isset($this->params['id']) ? $this->params['id'] : 0; + $pid = isset($this->params['pid']) ? $this->params['pid'] : 0; + if($id>0){ + $detail = $this->menu->Find($id); + $detail['name'] = $detail['title']; + View::assign('detail', $detail); + } + // 查出商户所有菜单数据 + $data = $this->menu->Search([], 1); + $menus = $format->DropDownMenu($data['list']); + + View::assign('id', $id); + View::assign('pid', $pid); + View::assign('menus',$menus); + + return view('merchant/system/auth/add'); + } + + /** + * 添加菜单 + */ + function Add(){ + echo 'add'; + $this->params['src'] = preg_replace('# #','',$this->params['src']); + // if ($param['id'] == 0) { + try { + validate(RuleCheck::class)->scene('add')->check($this->params); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + return to_assign(1, $e->getError()); + } + $this->params['create_time'] = time(); + $rid = Db::name('AdminRule')->strict(false)->field(true)->insertGetId($this->params); + //自动为系统所有者管理组分配新增的节点 + $group = Db::name('AdminGroup')->find(1); + if (!empty($group)) { + $newGroup['id'] = 1; + $newGroup['rules'] = $group['rules'] . ',' . $rid; + Db::name('AdminGroup')->strict(false)->field(true)->update($newGroup); + add_log('add', $rid, $this->params); + } + // } + // 删除后台节点缓存 + clear_cache('adminRules'); + return to_assign(); + } + + /** + * 编辑菜单 + */ + function Edit(){ + echo 'edit'; + if ($this->params['id'] > 0) { + try { + validate(RuleCheck::class)->scene('edit')->check($this->params); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + return to_assign(1, $e->getError()); + } + Db::name('AdminRule')->strict(false)->field(true)->update($this->params); + add_log('edit', $this->params['id'], $this->params); + } + } + + /** + * 删除菜单 + */ + function Del(){ + $id = get_params("id"); + try{ + $this->menu->Del($id); + return to_assign(0, "删除节点成功"); + }catch(ValidateException $e){ + return to_assign(1, $e->getMessage()); + } } } \ No newline at end of file diff --git a/app/admin/route/menu.php b/app/admin/route/menu.php index 7465915..5b15ac9 100644 --- a/app/admin/route/menu.php +++ b/app/admin/route/menu.php @@ -17,35 +17,6 @@ use app\common\middleware\LogMiddleware; Route::group(function () { - //权限管理 - // Route::group('system/menu', function () { - // Route::get('lst', '/getList')->name('systemMenuGetLst')->option([ - // '_alias' => '平台菜单/权限列表', - // ]); - // Route::get('create/form', '/createForm')->name('systemMenuCreateForm')->option([ - // '_alias' => '平台菜单/权限添加表单', - // '_auth' => false, - // '_form' => 'systemMenuCreate', - // ]); - // Route::get('update/form/:id', '/updateForm')->name('systemMenuUpdateForm')->option([ - // '_alias' => '平台菜单/权限编辑表单', - // '_auth' => false, - // '_form' => 'systemMenuUpdate', - // ]); - // Route::post('create', '/create')->name('systemMenuCreate')->option([ - // '_alias' => '平台菜单/权限添加', - // ]); - // Route::post('update/:id', '/update')->name('systemMenuUpdate')->option([ - // '_alias' => '平台菜单/权限编辑', - // ]); - // Route::delete('delete/:id', '/delete')->name('systemMenuDelete')->option([ - // '_alias' => '平台菜单/权限删除', - // ]); - // })->prefix('admin.system.auth.Menu')->option([ - // '_path' => '/setting/menu', - // '_auth' => true, - // ]); - //商户权限管理 Route::group('merchant/menu', function () { Route::get('index', '/index')->name('systemMerchantMenuIndex')->option([ @@ -56,23 +27,24 @@ Route::group(function () { Route::get('lst', '/lst')->name('systemMerchantMenuGetLst')->append(['merchant' => 1])->option([ '_alias' => '商户菜单/权限列表', ]); - Route::get('create/form', '/createForm')->name('systemMerchantMenuCreateForm')->append(['merchant' => 1])->option([ + + Route::post('add', '/add')->name('systemMerchantMenuCreate')->append(['merchant' => 1])->option([ + '_alias' => '商户菜单/权限添加', + ]); + Route::post('edit/:id', '/edit')->name('systemMerchantMenuUpdate')->append(['merchant' => 1])->option([ + '_alias' => '商户菜单/权限编辑', + ]); + Route::get('addform', '/AddForm')->name('systemMerchantMenuCreateForm')->append(['merchant' => 1])->option([ '_alias' => '商户菜单/权限添加表单', '_auth' => false, '_form' => 'systemMerchantMenuCreate', ]); - Route::get('update/form/:id', '/updateForm')->name('systemMerchantMenuUpdateForm')->append(['merchant' => 1])->option([ + Route::get('editform', '/editform')->name('systemMerchantMenuUpdateForm')->append(['merchant' => 1])->option([ '_alias' => '商户菜单/权限编辑表单', '_auth' => false, '_form' => 'systemMerchantMenuUpdate', ]); - Route::post('create', '/create')->name('systemMerchantMenuCreate')->append(['merchant' => 1])->option([ - '_alias' => '商户菜单/权限添加', - ]); - Route::post('update/:id', '/update')->name('systemMerchantMenuUpdate')->append(['merchant' => 1])->option([ - '_alias' => '商户菜单/权限编辑', - ]); - Route::delete('delete/:id', '/delete')->name('systemMerchantMenuDelete')->append(['merchant' => 1])->option([ + Route::delete('del/:id', '/del')->name('systemMerchantMenuDelete')->append(['merchant' => 1])->option([ '_alias' => '商户菜单/权限删除', ]); })->prefix('merchant.system.merchant.auth.Menu')->option([ diff --git a/app/admin/view/merchant/system/auth/add.html b/app/admin/view/merchant/system/auth/add.html index 49d2fca..2b494c5 100644 --- a/app/admin/view/merchant/system/auth/add.html +++ b/app/admin/view/merchant/system/auth/add.html @@ -11,7 +11,7 @@ @@ -65,8 +65,8 @@ diff --git a/app/common/controller/FormatList.php b/app/common/controller/FormatList.php index b7355ce..92eadef 100644 --- a/app/common/controller/FormatList.php +++ b/app/common/controller/FormatList.php @@ -4,7 +4,7 @@ * * @author:刘孝全 * @email:q8197264@126.com - * @date :2023年03月2日 + * @date :2023年03月3日 */ namespace app\common\controller; @@ -19,7 +19,7 @@ class FormatList * @param string $childrenKey 子级字段名 * @return array * - * @date 2020-03-27 + * @date 2023-03-3 */ function FormatCategory(array $data, string $idName = "id", string $fieldName = 'pid', $childrenKey = 'children') { @@ -37,4 +37,34 @@ class FormatList } return $result; } + + /** + * 递归排序,用于下拉分类选择菜单 + * + * @param array $data 数据源 + * @param string $idName 主键 + * @param string $fieldName 父级字段 + * @param string $childrenKey 子级字段名 + * @return array + * + * @date 2023-03-3 + */ + function DropDownMenu($result, $pid = 0, $level=-1) + { + /*记录排序后的类别数组*/ + static $list = array(); + static $space = ['','├─','§§├─','§§§§├─','§§§§§§├─']; + $level++; + foreach ($result as $k => $v) { + if ($v['pid'] == $pid) { + if ($pid != 0) { + $v['title'] = $space[$level] . $v['title']; + } + /*将该类别的数据放入list中*/ + $list[] = $v; + self::DropDownMenu($result, $v['id'],$level); + } + } + return $list; + } } \ No newline at end of file diff --git a/app/common/model/merchant/system/Merchant.php b/app/common/model/merchant/system/Merchant.php new file mode 100644 index 0000000..0078b08 --- /dev/null +++ b/app/common/model/merchant/system/Merchant.php @@ -0,0 +1,51 @@ +field('id,user_id,title,content,create_time,status,is_read,read_time') + ->page($offset) + ->limit($limit) + ->select(); + + return $list; + } + + /** + * @ 店铺类型说明 + * + * return string + */ + public function GetDescription(){} + + /** + * @ 店铺保证金 + * + * return list + */ + public function Getdeposit(){} + +} \ No newline at end of file diff --git a/app/common/model/merchant/system/MerchantApplyments.php b/app/common/model/merchant/system/MerchantApplyments.php new file mode 100644 index 0000000..620fcb8 --- /dev/null +++ b/app/common/model/merchant/system/MerchantApplyments.php @@ -0,0 +1,22 @@ +select(); + + return $list; + } +} diff --git a/app/common/model/merchant/system/auth/Menu.php b/app/common/model/merchant/system/auth/Menu.php new file mode 100644 index 0000000..82aaff3 --- /dev/null +++ b/app/common/model/merchant/system/auth/Menu.php @@ -0,0 +1,108 @@ +field('menu_id as id,pid, sort, route as src,icon,menu_name as title,is_show as status, is_menu as menu') + ->order('sort DESC,menu_id ASC'); + if (isset($where['pid'])) $query->where('pid', (int)$where['pid']); + if (isset($where['keyword'])) $query->whereLike('menu_name|route', "%{$where['keyword']}%"); + if (isset($where['is_menu'])) $query->where('is_menu', (int)$where['is_menu']); + + // 查询记录总行数 + $count = $query->count(); + + if (isset($where['offset'])) $query->page($where['offset'])->limit($where['limit']); + + // 隐藏指定字段 + $list = $query->hidden(['update_time', 'path'])->select()->toArray(); + // 合并为一个数组并返回 + $data = compact('count', 'list'); + + return $data; + } + + /** + * 获取指定行数据 + * + * @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; + } + +} diff --git a/app/common/model/merchant/system/auth/Role.php b/app/common/model/merchant/system/auth/Role.php new file mode 100644 index 0000000..97c3196 --- /dev/null +++ b/app/common/model/merchant/system/auth/Role.php @@ -0,0 +1,14 @@ +