123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- <?php
- namespace app\admin\controller;
- use think\App;
- use think\facade\Db;
- use think\facade\Validate;
- use app\admin\model\ActionProcess as APModel;
- use app\admin\model\Process as PModel;
- //角色
- class Role extends Base
- {
- public function __construct(App $app)
- {
- parent::__construct($app);
- }
- /**角色权限列表分页 page size
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function roleList()
- {
- $post = $this->request->only(["status" => "", "role_name" => "", "relaComNo" => "", "companyName" => "", "companyNo" => "", "page" => 1, "size" => 10,'level'=>''], "post", "trim");
- $condition = [];
- isset($post['status']) && $post['status'] !== "" ? $condition[] = ['a.status', "=", $post['status']] : "";
- isset($post['role_name']) && $post['role_name'] !== "" ? $condition[] = ['a.role_name', "like", "%" . $post['role_name'] . "%"] : "";
- isset($post['relaComNo']) && $post['relaComNo'] !== "" ? $condition[] = ["a.companyNo", "=", $post['relaComNo']] : "";
- isset($post['companyNo']) && $post['companyNo'] !== "" ? $condition[] = ["a.companyNo", "=", $post['companyNo']] : "";
- // isset($post['companyName']) && $post['companyName'] !== "" ? $condition[] = ["b.name", "like", "%{$post['companyName']}%"] : "";
- isset($post['level']) && $post['level'] !== "" ? $condition[] = ["a.role_level", "=", $post['level']] : "";
- $count = Db::name("role")
- ->alias("a")
- // ->leftJoin("supplier_info b", "a.companyNo=b.code")
- ->where($condition)
- ->count('a.id');
- $page = isset($post['page']) ? intval($post['page']) : 1;
- $size = isset($post['size']) ? intval($post['size']) : 10;
- $page >= ceil($count / $size) ? $page = (int)ceil($count / $size) : "";
- $list = Db::name("role")
- ->alias("a")
- // ->leftJoin("supplier_info b", "a.companyNo=b.code")
- ->leftJoin("role_action t", "a.id=t.role_id")
- ->field("a.*,t.action_conllect as action,t.action_data")
- ->order("a.addtime desc")
- ->where($condition)
- ->page($page, $size)
- ->select()
- ->toArray();
- $userComon=\app\admin\common\User::getIns();
- $supplier =$userComon->handle("getCodeAndName",["code"=>array_column($list,"companyNo")]);
- $supplierArr=$supplier['data']??[];
- foreach ($list as &$value){
- $value['companyName'] = $supplierArr[$value['companyNo']]??"";
- }
- return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
- }
- /** 角色新建
- * @role_name 角色名称
- * @level 角色等级
- * @action 功能权限
- * @action_data 操作权限
- * @private_data 私有权限
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function roleAdd()
- {
- $post = $this->request->only(["role_name", "action", "action_data" => "", "private_data" => "", "relaComNo" => "", 'is_allow_see_price', 'private_field','level','desc'=>''], "post");
- $valid = Validate::rule([
- "role_name|角色名称" => "require|max:255|min:2",
- "relaComNo|业务公司编号" => "requireIf:level,2|max:255",
- // "companyNo|业务公司编号" => "requireWithout:relaComNo|max:255",
- "action|角色权限" => "require|array",
- "is_allow_see_price|是否允许查看价格" => "require|in:0,1",
- 'private_field|关键字段权限' => 'require|array|max:3',
- "level|角色级别"=>"require|number|in:1,2,3",
- ]);
- if ($valid->check($post) == false) return error_show(1004, $valid->getError());
- // if ($post['relaComNo'] != "" && $post["level"] == 1) return error_show(1002, "超管权限角色不可关联业务公司");
- // if ($this->level == 1) $companyNo = $post['companyNo'] ?? "";
- // else $companyNo = $post['relaComNo'] ?? "";
- // $companyNo = $post['relaComNo']=='' ? $post['companyNo']:'';
- $where = [["role_name", "=", $post['role_name']]];
- if ($post['relaComNo'] != "") $where[] = ["companyNo", "=", $post['relaComNo']];
- $isT = Db::name("role")->field('id')->where($where)->find();
- if ($isT) return error_show(1002, "角色名称已存在");
- $action = isset($post['action']) && is_array($post['action']) ? implode(",", $post['action']) : "";
- $data = isset($post['action_data']) && is_array($post['action_data']) ? implode(",", $post['action_data']) : "";
- $private_data = isset($post['private_data']) && is_array($post['private_data']) ? implode(",", $post['private_data']) : "";
- Db::startTrans();
- try {
- $date = date("Y-m-d H:i:s");
- $list = [
- "role_name" => $post['role_name'],
- "companyNo" => $post['level']==2?$post['relaComNo']:'',
- "status" => 1,
- "addtime" => $date,
- "updatetime" => $date,
- 'role_level'=>$post['level'],
- 'is_allow_see_price' => $post['is_allow_see_price'],
- 'desc' => $post['desc']
- ];
- $role = Db::name("role")->insert($list, true);
- if ($role > 0) {
- $role_action = [
- "role_id" => $role,
- "action_conllect" => $action,
- "action_data" => $data,
- "private_data" => $private_data,
- 'private_field' => is_array($post['private_field']) ? implode(',', $post['private_field']) : $post['private_field'],
- "status" => 1,
- "addtime" => $date,
- "updatetime" => $date,
- ];
- $roleaction = Db::name("role_action")->insert($role_action, true);
- if ($roleaction) {
- Db::commit();
- return app_show(0, "新建成功");
- } else {
- Db::rollback();
- return app_show(1005, "权限录入失败");
- }
- } else {
- Db::rollback();
- return app_show(1006, "角色新建失败");
- }
- } catch (\Exception $e) {
- Db::rollback();
- return error_show(1008, $e->getMessage());
- }
- }
- /**查询角色信息
- * @roleid 角色id
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function roleInfo()
- {
- $post = $this->post;
- $roleid = isset($post['roleid']) ? intval($post['roleid']) : "";
- if ($roleid == "") {
- return error_show(1001, 'roleid不能为空');
- }
- $info = Db::name("role")
- ->alias("a")
- ->leftJoin("role_action t", "a.id=t.role_id")
- ->field("a.*,t.action_conllect,t.action_data,t.private_data,t.private_field")
- ->where("a.id", "=", $roleid)
- ->find();
- if (!$info) {
- return error_show(1002, "未找到对应的数据");
- }
- $info['action'] = explode(",", $info['action_conllect']);
- $info['action_data'] = $info['action_data'] != "" ? explode(",", $info['action_data']) : "";
- $info['private_data'] = $info['private_data'] != "" ? explode(",", $info['private_data']) : "";
- return app_show(0, "获取成功", $info);
- }
- /** 角色信息编辑
- * @param role_name 角色名称
- * @param level 等级
- * @param action 功能权限
- * @param action_data 操作权限
- * @param roleid 角色id
- * @param private_data 私有权限
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function roleSave()
- {
- $post = $this->request->only(["roleid" => "", "role_name" => "", "relaComNo" => "", "action" => "", "action_data" => "", "private_data" => "", 'is_allow_see_price', 'private_field', 'level', 'desc' => ''], "post");
- $valid = Validate::rule([
- "role_name|角色名称" => "require|max:255|min:2",
- // "relaComNo|业务公司编号" => "max:255",
- // "companyNo|业务公司编号" => "max:255",
- "relaComNo|业务公司编号" => "requireIf:level,2|max:255",
- "action|角色权限" => "require|array",
- "is_allow_see_price|是否允许查看价格" => "require|in:0,1",
- 'private_field|关键字段权限'=>'require|array|max:3',
- "level|角色级别"=>"require|number|in:1,2,3",
- ]);
- if ($valid->check($post) == false) return error_show(1004, $valid->getError());
- $info = Db::name("role")->where("id", "=", $post['roleid'])->find();
- if (!$info) return error_show(1002, "未找到对应的数据");
- // if ($post['relaComNo'] != "" && $info["level"] == 1) return error_show(1002, "超管权限角色不可关联业务公司");
- // if($this->level==1){
- // $companyNo =$post['companyNo']??"";
- // }else{
- // $companyNo =$post['relaComNo']??"";
- // }
- $tmp_where=[
- ["id", '<>', $post['roleid']],
- ["role_name", "=", $post['role_name']],
- ];
- if($post['level']==2) $tmp_where[]=['companyNo','=',$post['relaComNo']];
- $vers = Db::name("role")
- ->field('id')
- ->where($tmp_where)
- ->find();
- if ($vers) return error_show(1002, "角色名已存在");
- $action = isset($post['action']) && is_array($post['action']) ? implode(",", $post['action']) : "";
- // if ($action == "") {
- // return error_show(1004, "功能权限不能为空");
- // }
- $data = isset($post['action_data']) && is_array($post['action_data']) ? implode(",", $post['action_data']) : "";
- $private_data = isset($post['private_data']) && is_array($post['private_data']) ? implode(",", $post['private_data']) : "";
- Db::startTrans();
- try {
- $list = [
- "role_name" => $post['role_name'],
- "updatetime" => date("Y-m-d H:i:s"),
- 'id' => $post['roleid'],
- 'is_allow_see_price' => $post['is_allow_see_price'],
- 'role_level'=>$post['level'],
- "companyNo" => $post['level']==2?$post['relaComNo']:'',
- 'desc' => $post['desc']
- ];
- $role = Db::name("role")->save($list);
- if ($role) {
- $role_action = [
- "role_id" => $post['roleid'],
- "action_conllect" => $action,
- "action_data" => $data,
- 'private_field'=>is_array($post['private_field'])?implode(',',$post['private_field']):$post['private_field'],
- "private_data" => $private_data,
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $roleaction = Db::name("role_action")->where("role_id", "=", $post['roleid'])->update($role_action);
- if ($roleaction) {
- Db::commit();
- return app_show(0, "更新成功");
- } else {
- Db::rollback();
- return app_show(1005, "权限更新失败");
- }
- } else {
- Db::rollback();
- return app_show(1006, "角色更新失败");
- }
- } catch (\Exception $e) {
- Db::rollback();
- return error_show(1008, $e->getMessage());
- }
- }
- /**
- * 角色状态更新
- * @roleid 角色id
- * @status 角色状态 0 /1
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function roleStatus()
- {
- $post = $this->request->only(["roleid" => "", "status" => ""], "post", "intval");
- $valid = Validate::rule([
- "roleid|角色id" => "require|number|gt:0",
- "status|状态" => "require|number|in:0,1"
- ]);
- if ($valid->check($post) == false) return error_show(1004, $valid->getError());
- Db::startTrans();
- try {
- $dat = [
- 'status' => $post['status'], 'updatetime' => date("Y-m-d H:i:s"), 'id' => $post['roleid']
- ];
- $re = Db::name("role")->save($dat);
- if ($re) {
- $dat2 = [
- 'status' => $post['status'], 'updatetime' => date("Y-m-d H:i:s")
- ];
- $action = Db::name("role_action")->where("role_id", "=", $post['roleid'])->update($dat2);
- if ($action) {
- Db::commit();
- return app_show(0, "状态更新成功");
- }
- }
- Db::rollback();
- return error_show(1003, "状态更新失败");
- } catch (\Exception $e) {
- Db::rollback();
- return error_show(1002, $e->getMessage());
- }
- }
- /**
- * 获取所有角色
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleAll()
- {
- $post = $this->request->only(["relaComNo" => "", "companyNo" => "",'level'=>''], "post", "trim");
- $where = [];
- if ($post['relaComNo'] != '') $where[] = ["companyNo", "=", $post['relaComNo']];
- if ($post['companyNo'] != '') $where[] = ["companyNo", "=", $post['companyNo']];
- if ($post['level'] != '') $where[] = ["role_level", "=", $post['level']];
- $list = Db::name("role")
- ->field(true)
- ->where($where)
- ->select()
- ->toArray();
- $companyNo = array_column($list,'companyNo');
- $companyNames = get_headquarters_code_and_name($companyNo);
- foreach ($list as &$value){
- $value['companyName'] = $companyNames[$value['companyNo']]??'';
- }
- return app_show(0, "获取成功", $list);
- }
- //获取角色对应的流程权限id
- public function roleProcessInfo()
- {
- $roleid = $this->request->filter('trim')->post('roleid/d', 0);
- $res = Db::name("role")
- ->alias("a")
- ->leftJoin("role_process b", "a.id=b.role_id AND b.is_del=0")
- ->field("a.*,b.action_data")
- ->withAttr('action_data', function ($val) {
- return explode(',', $val);
- })->where("a.id", $roleid)
- ->findOrEmpty();
- return app_show(0, '请求成功', $res);
- }
- //修改角色对应的流程权限
- public function roleProcessSave()
- {
- $param = $this->request->filter('trim')->only(['token', 'roleid', 'action_data'], 'post');
- $val = Validate::rule([
- 'token' => 'require',
- 'roleid|角色ID' => 'require|number|gt:0',
- 'action_data|所选节点id' => 'require|array',
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- // $user = GetUserInfo($param['token']);
- $uid = $this->uid;//isset($user['data']['id']) ? $user['data']['id'] : 0;
- $uname = $this->uname;//isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
- $date = date('Y-m-d H:i:s');
- $res = Db::name('role_process')
- ->where(['is_del' => 0, 'role_id' => $param['roleid']])
- ->field('id')
- ->findOrEmpty();
- if (empty($res)) {
- $rs = Db::name('role_process')
- ->insert([
- 'role_id' => $param['roleid'],
- 'action_data' => implode(',', $param['action_data']),
- 'is_del' => 0,
- 'createrid' => $uid,
- 'creater' => $uname,
- 'addtime' => $date,
- 'updaterid' => $uid,
- 'updater' => $uname,
- 'updatetime' => $date,
- ]);
- } else {
- $rs = Db::name('role_process')
- ->where('id', $res['id'])
- ->update([
- 'action_data' => implode(',', $param['action_data']),
- 'updaterid' => $uid,
- 'updater' => $uname,
- 'updatetime' => $date
- ]);
- }
- return $rs ? app_show(0, '操作成功') : error_show(1005, '操作失败');
- }
- //获取角色对应的流程权限详情
- public function roleProcessDetail()
- {
- $roleid = $this->request->filter('trim')->post('roleid/d', 0);
- $action_data = Db::name("role_process")
- ->where('role_id', $roleid)
- ->value('action_data');
- $data = PModel::where(['is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
- ->append(['child'])
- ->withAttr('child', function () {
- return [];
- })
- ->column('id,process_name,process_type', 'id');
- $action = APModel::where([
- 'is_del' => APModel::$is_del_normal,
- 'status' => APModel::$status_normal,
- 'operation_type' => APModel::$operation_type_approval
- ])->whereIn('id', $action_data)
- ->field('id,process_id,order_process,status_name')
- ->cursor();
- foreach ($action as $item) {
- if (isset($data[$item->process_id])) $data[$item->process_id]['child'][] = $item->toArray();
- }
- return app_show(0, '请求成功', array_column($data, null, null));
- }
- }
|