123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace app\admin\logic;
- use app\model\AdminMenuModel;
- use app\model\CommonModel;
- use think\facade\Db;
- use think\response\Json;
- class MenuLogic extends BaseLogic
- {
- //获取用户菜单列表
- public static function list(): Json
- {
- //menu_action是一个视图
- $data = Db::name('menu_action')
- ->where(['status' => 1, 'cstatus' => 1, 'is_show' => 1])
- ->order(['weight' => 'desc', 'id' => 'asc', 'cweight' => 'desc', 'cid' => 'asc'])
- ->cursor();
- $list = [];
- $act = [];
- foreach ($data as $value) {
- $list[$value['id']]['menu_name'] = $value['menu_name'];
- $list[$value['id']]['menu_type'] = $value['menu_type'];
- $list[$value['id']]['menu_img'] = $value['menu_img'];
- $list[$value['id']]['menu_route'] = $value['menu_route'];
- $list[$value['id']]['menu_url'] = $value['menu_url'];
- $list[$value['id']]['status'] = $value['status'];
- $list[$value['id']]['weight'] = $value['weight'];
- $list[$value['id']]['addtime'] = $value['addtime'];
- $temp = [];
- $temp['menu_name'] = $value['cname'];
- $temp['menu_type'] = $value['ctype'];
- $temp['menu_img'] = $value['cmenu_img'];
- $temp['menu_route'] = $value['cmenu_route'];
- $temp['menu_url'] = $value['cmenu_url'];
- $temp['status'] = $value['cstatus'];
- $temp['weight'] = $value['cweight'];
- $temp['addtime'] = $value['caddtime'];
- $temp['is_private'] = $value['cprivate'];
- $list[$value['id']]['child'][$value['cid']] = $temp;
- $act[$value['id']][$value['cid']][] = $value['acode'];
- $list[$value['id']]['child'][$value['cid']]['action'] = $act[$value['id']][$value['cid']];
- }
- array_walk($list, function (&$value) {
- $value['child'] = array_values($value['child']);
- });
- return json_show(CommonModel::$success, '获取成功', array_values($list));
- }
- //获取全部菜单
- public static function all(): Json
- {
- $list = AdminMenuModel::field('id,menu_name,menu_type,menu_img,menu_route,menu_url,status,weight,addtime,is_private private')
- ->where(['pid' => 0, 'is_show' => AdminMenuModel::$show])
- ->append(['child'])
- ->withAttr('child', function ($val, $data) {
- return AdminMenuModel::field('id,menu_name,menu_type,menu_img,menu_route,menu_url,status,weight,addtime,is_private private,pid')
- ->where(['pid' => $data['id'], 'is_show' => AdminMenuModel::$show])
- ->order('weight', 'desc')
- ->select()
- ->toArray();
- })
- ->order('weight', 'desc')
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取成功', $list);
- }
- //添加菜单
- public static function add(array $data = []): Json
- {
- $data = array_merge($data, [
- 'is_private' => $data['private'],
- 'is_show' => AdminMenuModel::$show,
- 'status' => AdminMenuModel::$status_normal,
- ]);
- $res = AdminMenuModel::create($data)->save();
- //write_log('菜单{$data['menu_name']}新建成功',$this->userinfo,'order','add','0');
- return $res ? json_show(CommonModel::$success, '添加菜单成功') : json_show(CommonModel::$error_param, '添加菜单失败');
- }
- //编辑菜单
- public static function edit(array $data = []): Json
- {
- $rs = AdminMenuModel::field('id')
- ->where(['id' => $data['id']])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) return json_show(CommonModel::$error_param, '菜单信息不存在');
- $data = array_merge($data, ['is_private' => $data['private'], 'updatetime' => date('Y-m-d H:i:s')]);
- $res = AdminMenuModel::where('id', $data['id'])->strict(false)->save($data);
- return $res ? json_show(CommonModel::$success, '编辑菜单成功') : json_show(CommonModel::$error_param, '编辑菜单失败');
- }
- //删除菜单
- public static function delete(int $id = 0): Json
- {
- $rs = AdminMenuModel::field('id')
- ->where(['id' => $id, 'is_show' => AdminMenuModel::$show, 'status' => AdminMenuModel::$status_normal])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) return json_show(CommonModel::$error_param, '菜单信息不存在');
- $res = AdminMenuModel::where('id', $id)
- ->save([
- 'is_show' => AdminMenuModel::$hide,
- 'status' => AdminMenuModel::$status_disable
- ]);
- //write_log('菜单{$data['menu_name']}新建成功',$this->userinfo,'order','add','0');
- return $res ? json_show(CommonModel::$success, '删除菜单成功') : json_show(CommonModel::$error_param, '删除菜单失败');
- }
- //启禁用菜单
- public static function status(array $data = []): Json
- {
- $rs = AdminMenuModel::field('id')
- ->where('id', $data['id'])
- ->where('status', '<>', $data['status'])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) return json_show(CommonModel::$error_param, '菜单信息不存在或重复操作');
- $res = AdminMenuModel::where('id', $data['id'])
- ->where('status', '<>', $data['status'])
- ->save(['status' => $data['status']]);
- //write_log('菜单{$data['menu_name']}新建成功',$this->userinfo,'order','add','0');
- return $res ? json_show(CommonModel::$success, '状态更新成功') : json_show(CommonModel::$error_param, '状态更新失败');
- }
- }
|