123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- <?php
- namespace menu\controller;
- use think\Db;
- class Menu {
- private $app = 'admin';
-
- public function __construct() {
- $_user_id = cmf_get_current_admin_id();
- if (1 != $_user_id) {
- exit('no auth');
- }
- }
-
- public function deleteMenu($controller, $app = 'admin', $action = '') {
- $_app = $app;
- if (empty($app)) {
- $_app = $app;
- }
- $_map = [
- 'app' => $_app,
- 'controller' => ['like', $controller.'%']
- ];
- if (!empty($action)) {
- $_map['action'] = $action;
- }
- $_rs = Db::name('AdminMenu')->where($_map)->delete();
- return $_rs;
- }
-
- public function addMenu($param = []) {
- $_data = $param;
- $_id = Db::name('AdminMenu')->strict(false)->field(true)->insert($_data, true, true);
- $_app = $_data['app'];
- $_controller = $_data['controller'];
- $_action = $_data['action'];
- $_param = $_data['param'];
- $_auth_rule_name = "$_app/$_controller/$_action";
- $_menu_name = $param['name'];
- $_map = [
- 'app' => $_app,
- 'name' => $_auth_rule_name,
- 'type' => $_app.'_url'
- ];
- $_find_auth_rule_count = Db::name('auth_rule')->where($_map)->count();
- if (empty($_find_auth_rule_count)) {
- Db::name('AuthRule')->insert(
- [
- "name" => $_auth_rule_name,
- "app" => $_app,
- "type" => $_app.'_url',
- "title" => $_menu_name,
- 'param' => $_param,
- ]
- );
- }
- return $_id;
- }
-
- public function addTop(
- $controller, $parent_id = 0, $list_order = 0, $name = '', $en_name = '', $icon = '', $action = 'default',
- $param = '',
- $type = 1,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => $param,
- 'name' => $name,
- 'en_name' => $en_name,
- 'icon' => $icon,
- 'remark' => $name,
- ];
- return $this->addMenu($_data);
- }
-
- public function addManage(
- $controller, $parent_id = 0, $list_order = 0, $name = '', $en_name = '', $icon = '', $action = 'manage',
- $type = 1,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => '',
- 'name' => $name.'管理',
- 'en_name' => $en_name.' Manage',
- 'icon' => $icon,
- 'remark' => $name.'管理',
- ];
- return $this->addMenu($_data);
- }
-
- public function addIndex(
- $controller, $parent_id = 0, $list_order = 0, $name = '', $en_name = '', $icon = '', $action = 'index',
- $type = 1,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => '',
- 'name' => $name.'列表',
- 'en_name' => $en_name.' List',
- 'icon' => $icon,
- 'remark' => $name.'列表',
- ];
- return $this->addMenu($_data);
- }
-
- public function addAdd(
- $controller, $parent_id = 0, $list_order = 1000, $name = '', $en_name = '', $action = 'add', $type = 2,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => '',
- 'name' => '添加'.$name,
- 'en_name' => 'Add '.$en_name,
- 'icon' => 'plus-square-o',
- 'remark' => '添加'.$name,
- ];
- $_id = $this->addMenu($_data);
- $_post_data = $_data;
- $_post_data['parent_id'] = $_id;
- $_post_data['name'] = $_post_data['name'].'操作函数';
- $_post_data['en_name'] = $_post_data['name'].' Function';
- $_post_data['action'] = $action.'Post';
- $_post_data['remark'] = $_post_data['name'];
- return $this->addMenu($_post_data);
- }
-
- public function addEdit(
- $controller, $parent_id = 0, $list_order = 900, $name = '', $en_name = '', $action = 'edit', $type = 2,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => '',
- 'name' => '编辑'.$name,
- 'en_name' => 'Edit '.$en_name,
- 'icon' => 'pencil-square-o',
- 'remark' => '编辑'.$name,
- ];
- $_id = $this->addMenu($_data);
- $_post_data = $_data;
- $_post_data['parent_id'] = $_id;
- $_post_data['name'] = $_post_data['name'].'操作函数';
- $_post_data['en_name'] = $_post_data['name'].' Function';
- $_post_data['action'] = $action.'Post';
- $_post_data['remark'] = $_post_data['name'];
- return $this->addMenu($_post_data);
- }
-
- public function addDelete(
- $controller, $parent_id = 0, $list_order = 700, $name = '', $en_name = '', $action = 'delete', $type = 2,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => '',
- 'name' => '删除'.$name,
- 'en_name' => 'delete '.$en_name,
- 'icon' => '',
- 'remark' => '删除'.$name,
- ];
- return $this->addMenu($_data);
- }
-
- public function addSetStatus(
- $controller, $parent_id = 0, $list_order = 800, $name = '', $en_name = '', $action = 'setStatus', $type = 2,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => '',
- 'name' => '设置'.$name.'状态',
- 'en_name' => 'Set '.$en_name.' Status',
- 'icon' => 'pencil-square-o',
- 'remark' => '设置'.$name.'状态',
- ];
- return $this->addMenu($_data);
- }
-
- public function addListOrders(
- $controller, $parent_id = 0, $list_order = 600, $name = '', $en_name = '', $action = 'listOrders', $type = 2,
- $status = 1
- ) {
- $_data = [
- 'parent_id' => $parent_id,
- 'type' => $type,
- 'status' => $status,
- 'list_order' => $list_order,
- 'app' => $this->app,
- 'controller' => $controller,
- 'action' => $action,
- 'param' => '',
- 'name' => '设置'.$name.'排序',
- 'en_name' => 'Set '.$en_name.' ListOrder',
- 'icon' => '',
- 'remark' => '设置'.$name.'排序',
- ];
- return $this->addMenu($_data);
- }
-
- public function addDefaultMenu(
- $controller, $parent_id, $list_order = 0, $name = '', $en_name = '', $icon = '',
- $type = 1,
- $status = 1, $sub_menu = ['add', 'edit', 'setStatus', 'delete']
- ) {
- $_id = $this->addIndex($controller, $parent_id, $list_order, $name, $en_name, $icon, 'index', $type, $status);
- $_list_order = 1000;
- if (in_array('add', $sub_menu)) {
- $this->addAdd($controller, $_id, $_list_order, $name, $en_name);
- $_list_order -= 100;
- }
- if (in_array('edit', $sub_menu)) {
- $this->addEdit($controller, $_id, $_list_order, $name, $en_name);
- $_list_order -= 100;
- }
- if (in_array('setStatus', $sub_menu)) {
- $this->addSetStatus($controller, $_id, $_list_order, $name, $en_name);
- $_list_order -= 100;
- }
- if (in_array('delete', $sub_menu)) {
- $this->addDelete($controller, $_id, $_list_order, $name, $en_name);
- }
- return $_id;
- }
-
- public function getApp() {
- return $this->app;
- }
-
- public function setApp($app) {
- $this->app = $app;
- }
- }
|