1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace app\admin\logic;
- use app\model\ActionModel;
- use app\model\CommonModel;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\response\Json;
- class ActionLogic extends BaseLogic
- {
- //获取权限列表
- public static function list(int $menuid = 0): Json
- {
- $data = ActionModel::alias('a')
- ->field('a.*,action_name')
- ->leftJoin('action_list l', 'a.action_code=l.action_code')
- ->where(['menuid' => $menuid])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取成功', $data);
- }
- //添加权限
- public static function add(array $data = []): Json
- {
- $rs = ActionModel::field('id')
- ->where(['menuid' => $data['menuid'], 'action_code' => $data['action_code']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) throw new ValidateException('此功能已存在');
- $data = array_merge($data, [
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- return ActionModel::create($data)->save() ? json_show(CommonModel::$success, '添加成功') : json_show(CommonModel::$error_default, '添加失败');
- }
- //获取权限详情
- public static function read(int $id = 0): Json
- {
- $menu = ActionModel::alias('a')
- ->field('a.*,action_name')
- ->leftJoin('action_list l', 'l.is_show = ' . CommonModel::$del_normal . ' AND a.action_code=l.action_code')
- ->where('a.id', $id)
- ->where('')
- ->findOrEmpty()
- ->toArray();
- return empty($menu) ? json_show(CommonModel::$error_param, '未找到对应的数据') : json_show(CommonModel::$success, '获取成功!', $menu);
- }
- //编辑权限
- public static function edit(array $data = []): Json
- {
- $rs = ActionModel::field(true)
- ->where('id', $data['id'])
- ->findOrEmpty();
- if ($rs->isEmpty()) throw new ValidateException('该菜单下的按钮不存在');
- if ($rs->action_code != $data['action_code']) {
- $temp = ActionModel::field('id')
- ->where(['menuid' => $rs->menuid, 'action_code' => $data['action_code']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$temp) throw new ValidateException('该按钮已存在');
- }
- $data['updatetime'] = date('Y-m-d H:i:s');
- $res = ActionModel::where('id', $data['id'])
- ->save($data);
- return $res ? json_show(CommonModel::$success, '修改按钮成功') : json_show(CommonModel::$error_param, '修改按钮失败');
- }
- //删除权限
- public static function delete(int $id = 0): Json
- {
- $rs = ActionModel::where('id', $id)->delete();
- return $rs ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败');
- }
- //启禁用权限
- public static function status(array $data = []): Json
- {
- $rs = ActionModel::where(['id' => $data['id']])
- ->where('status', '<>', $data['status'])
- ->save(['status' => $data['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- return $rs ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_default, '操作失败');
- }
- }
|