#CSDN 年度征文|回顾 2023,赢专属铭牌等定制奖品#
一、创建菜单模块
1、创建系统菜单表
CREATE TABLE `menu` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '菜单ID',`menu_name` varchar(32) NOT NULL DEFAULT '' COMMENT '菜单名称',`path` varchar(255) NOT NULL DEFAULT '' COMMENT '路径',`redirect` varchar(255) NOT NULL COMMENT '跳转地址',`components` varchar(255) NOT NULL DEFAULT '' COMMENT '组件',`hidden` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '隐藏',`create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',`pid` int(11) unsigned NOT NULL DEFAULT '1' COMMENT '父级菜单ID',`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态:1.有效 0.无效',`is_del` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否删除',PRIMARY KEY (`id`) USING BTREE,KEY `role_name` (`menu_name`) USING BTREE,KEY `status` (`status`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统菜单表';
2、使用命令创建菜单控制器
php think make:controller admin@Menu --plain
2、在Menu.php控制中创建菜单模块的方法
<?php
declare (strict_types = 1);namespace app\admin\controller;class Menu
{// 菜单列表public function list(){echo '菜单列表';}// 新增菜单public function create(){echo '新增菜单';}// 编辑菜单public function edit(){echo '编辑菜单';}// 删除菜单public function del(){echo '删除菜单';}// 启用/禁用菜单public function status(){echo '启用/禁用菜单';}
}
3、使用命令创建菜单模型
php think make:model admin@Menu
4、使用模型获取菜单数据
// 数据分页
public function data_page(array $where = [], array $pageData = [], array $sort = [], string $fields = '*'){if(empty($where)) $where = querymap();$result = $this->where($where)->field($fields)->order($sort)->paginate($pageData);if(is_object($result)) $result = $result->toArray();return $result;
}// 数据列表
public function data_list(array $where = [], array $sort = [], string $fields = '*', $limit = 0){empty($where) and $where = querymap();if(empty($limit)){$result = $this->where($where)->field($fields)->order($sort)->select();}else{$result = $this->where($where)->field($fields)->limit($limit)->order($sort)->select();}if(is_object($result)) $result = $result->toArray();return $result;
}// 数据值总和
public function data_sum(array $where = [], string $field, array $sort = []){$sum = $this->where($where)->order($sort)->sum($field);$result['sum'] = $sum;return $result;
}// 数据量
public function data_count(array $where = [], array $sort = []){$count = $this->where($where)->order($sort)->count();$result['count'] = $count;return $result;
}//新增数据
public function data_create(array $data)
{if(!empty($data[0]) && is_array($data[0])){$result = $this->saveAll($data);if(is_object($result)) $result = $result->toArray();}else{$data['create_time'] = time();$this->save($data);$data['id'] = $this->id;$result = $data;}return $result;
}//更新数据
public function data_update(array $data = [], array $where = [])
{if(!empty($data[0]) && is_array($data[0])){$ids = array_column($data, 'id');$idlength = count($ids); $datalength = count($data);if($idlength != $datalength) return arrayData(500, '缺少更新条件');$result = $this->saveAll($data);}else{$result = $this->update($data, $where);}if(is_object($result)) $result = $result->toArray();if(empty($result)) $result = [];return $result;
}//查看数据
public function data_view(array $where, string $field = "*", array $sort = [], array $hidden = [])
{$result = $this->where($where)->field($field)->order($sort)->hidden($hidden)->find();if(is_object($result)) $result = $result->toArray();if(empty($result)) {$result = [];}return $result;
}//查看字段
public function data_value(array $where, string $field)
{$result = $this->where($where)->value($field);// if(is_object($result)) $result = $result->toArray();if(empty($result)) {$result = '';}return $result;
}// 删除数据
public function data_delete(array $where, array $sort = [])
{$result = $this->where($where)->order($sort)->delete();
}// 分组数据
public function data_group(array $where, string $group = '', string $field = '*', array $sort = [])
{$result = $this->where($where)->field($field)->group($group)->order($sort)->select();if(is_object($result)) $result = $result->toArray();if(is_null($result)) $result = [];return $result;
}
5、使用命令创建Service文件
php think make:service admin@Menu
6、创建菜单方法处理菜单业务逻辑