123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace app\admin\controller;
- use think\facade\Db;
- use think\facade\Validate;
- use app\admin\model\DataShare as DataShareModel;
- use app\admin\model\DataGroup as DataGroupModel;
- //数据共享规则
- class DataShare extends Base
- {
- //列表
- public function getDataShareList()
- {
- $param = $this->request->filter('trim')->only(['page' => 1, 'size' => 10, 'source_nickname' => '', 'source_group_title' => '', 'nickname' => '', 'group_title' => '', 'type' => '']);
- $db = new DataShareModel();
- $rs = $db
- ->alias('a')
- ->leftJoin('depart_user b', 'b.uid=a.source_uid AND b.is_del=0')
- ->leftJoin('data_group c', 'c.id=a.source_data_group_id AND c.is_del=' . DataGroupModel::$delete_正常)
- ->leftJoin('depart_user d', 'd.uid=a.uid AND d.is_del=0')
- ->leftJoin('data_group e', 'e.id=a.data_group_id AND e.is_del=' . DataGroupModel::$delete_正常)
- ->where('a.is_del', $db::$delete_正常);
- $val = Validate::rule([
- 'page|页码' => 'require|number|gt:0',
- 'size|每页数量' => 'require|number|gt:0|elt:100',
- 'status|状态' => 'number|in:' . $db::$status_启用 . ',' . $db::$status_禁用,
- 'source_nickname|数据源用户' => 'max:100',
- 'source_group_title|数据源用户组' => 'max:100',
- 'nickname|共享用户' => 'max:100',
- 'type|共享权限' => 'number|in:' . $db::$type_只读 . ',' . $db::$type_读写,
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- if ($param['source_nickname'] != '') $rs->whereLike('b.nickname', '%' . $param['source_nickname'] . '%');
- if ($param['source_group_title'] != '') $rs->whereLike('c.title', '%' . $param['source_group_title'] . '%');
- if ($param['nickname'] != '') $rs->whereLike('d.nickname', '%' . $param['nickname'] . '%');
- if ($param['group_title'] != '') $rs->whereLike('e.title', '%' . $param['group_title'] . '%');
- if ($param['type'] != '') $rs->whereLike('a.type', $param['type']);
- $count = $rs->count('a.id');
- $list = $rs
- ->field('a.*,b.nickname source_nickname,c.title source_group_title,d.nickname,e.title group_title')
- ->append(['menu_list'])
- ->withAttr('menu_list', function ($val, $data) {
- return Db::name('admin_menu')
- ->field('id,menu_name')
- ->whereIn('id', $data['menu_ids'])
- ->where('is_del', 0)
- ->select()
- ->toArray();
- })
- ->page($param['page'], $param['size'])
- ->order('a.id', 'desc')
- ->select()
- ->toArray();
- return app_show(0, '请求成功', ['count' => $count, 'list' => $list]);
- }
- //新建
- public function addDataShare()
- {
- $param = $this->request->filter('trim')->only(['title' => '', 'menu_ids', 'source_uid', 'source_data_group_id', 'uid', 'data_group_id', 'type', 'remark' => '']);
- $val = Validate::rule([
- 'title|共享规则名称' => 'max:100',
- 'menu_ids|菜单数据' => 'require|array|max:100',
- 'source_uid|数据源用户' => 'requireWithout:source_data_group_id|number|gt:0',
- 'source_data_group_id|数据源用户组' => 'requireWithout:source_uid|number|gt:0',
- 'uid|数据共享用户' => 'requireWithout:data_group_id|number|gt:0',
- 'data_group_id|数据共享用户组' => 'requireWithout:uid|number|gt:0',
- 'type|数据权限' => 'require|number|in:' . DataShareModel::$type_只读 . ',' . DataShareModel::$type_读写,
- 'remark|备注' => 'max:100',
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- $rs = DataShareModel::create([
- 'title' => $param['title'],
- 'menu_ids' => implode(',', $param['menu_ids']),
- 'source_uid' => $param['source_uid'],
- 'source_data_group_id' => $param['source_data_group_id'],
- 'uid' => $param['uid'],
- 'data_group_id' => $param['data_group_id'],
- 'type' => $param['type'],
- 'remark' => $param['remark'],
- 'is_del' => DataShareModel::$delete_正常,
- 'status' => DataShareModel::$status_启用,
- 'createrid' => $this->uid,
- 'creater' => $this->uname,
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- ])->save();
- return $rs ? app_show(0, '新建共享规则成功') : error_show(1004, '新建共享规则失败');
- }
- //获取详情
- public function getDataShareDetail()
- {
- $id = $this->request->filter('trim')->post('id/d', 0);
- $info = DataShareModel::field('a.*,b.nickname source_nickname,c.title source_group_title,d.nickname,e.title group_title')
- ->alias('a')
- ->leftJoin('depart_user b', 'b.uid=a.source_uid AND b.is_del=0')
- ->leftJoin('data_group c', 'c.id=a.source_data_group_id AND c.is_del=' . DataGroupModel::$delete_正常)
- ->leftJoin('depart_user d', 'd.uid=a.uid AND d.is_del=0')
- ->leftJoin('data_group e', 'e.id=a.data_group_id AND e.is_del=' . DataGroupModel::$delete_正常)
- ->where(['a.id' => $id, 'a.is_del' => DataShareModel::$delete_正常])
- ->append(['menu_list'])
- ->withAttr('menu_list', function ($val, $data) {
- return Db::name('admin_menu')
- ->field('id,menu_name')
- ->whereIn('id', $data['menu_ids'])
- ->where('is_del', 0)
- ->select()
- ->toArray();
- })
- ->findOrEmpty()
- ->toArray();
- return app_show(0, '获取详情成功', $info);
- }
- //修改
- public function editDataShare()
- {
- $param = $this->request->filter('trim')->only(['id', 'menu_ids', 'source_uid', 'source_data_group_id', 'uid', 'data_group_id', 'type', 'remark']);
- $val = Validate::rule([
- 'id' => 'require|number|gt:0',
- 'title|共享规则名称' => 'max:100',
- 'menu_ids|菜单数据' => 'require|array|max:100',
- 'source_uid|数据源用户' => 'requireWithout:source_data_group_id|number|gt:0',
- 'source_data_group_id|数据源用户组' => 'requireWithout:source_uid|number|gt:0',
- 'uid|数据共享用户' => 'requireWithout:data_group_id|number|gt:0',
- 'data_group_id|数据共享用户组' => 'requireWithout:uid|number|gt:0',
- 'type|数据权限' => 'require|number|in:' . DataShareModel::$type_只读 . ',' . DataShareModel::$type_读写,
- 'remark|备注' => 'max:100',
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- $res = DataShareModel::field('id')
- ->where(['id' => $param['id'], 'is_del' => DataShareModel::$delete_正常])
- ->findOrEmpty()
- ->isEmpty();
- if ($res) return error_show(1005, '该记录不存在');
- if (isset($param['menu_ids']) && is_array($param['menu_ids'])) $param['menu_ids'] = implode(',', $param['menu_ids']);
- $param['updatetime'] = date('Y-m-d H:i:s');
- $rs = DataShareModel::where(['id' => $param['id'], 'is_del' => DataShareModel::$delete_正常])->strict(false)->save($param);
- return $rs ? app_show(0, '修改共享规则成功') : error_show(1005, '修改共享规则失败');
- }
- //删除
- public function deleteDataShare()
- {
- $param = $this->request->filter('trim')->only(['id']);
- $val = Validate::rule(['id' => 'require|array|max:100']);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- $rs = DataShareModel::whereIn('id', $param['id'])
- ->where(['is_del' => DataShareModel::$delete_正常])
- ->update(['is_del' => DataShareModel::$delete_删除, 'updatetime' => date('Y-m-d H:i:s')]);
- return $rs ? app_show(0, '删除共享规则成功') : error_show(1005, '删除共享规则失败');
- }
- //启/禁用
- public function statusDataShare()
- {
- $param = $this->request->filter('trim')->only(['id', 'status']);
- $val = Validate::rule([
- 'id' => 'require|array|max:100',
- 'status|状态' => 'require|number|in:' . DataShareModel::$status_启用 . ',' . DataShareModel::$status_禁用,
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- $status = [0 => '禁用', 1 => '启用'];
- $rs = DataShareModel::where([
- ['id', 'in', $param['id']],
- ['is_del', '=', DataShareModel::$delete_正常],
- ['status', '<>', $param['status']],
- ])->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- return $rs ? app_show(0, $status[$param['status']] . '共享规则成功') : error_show(1005, $status[$param['status']] . '共享规则失败');
- }
- }
|