123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use think\App;
- use think\facade\Db;
- class Role extends BaseController
- {
- public function __construct(App $app)
- {
- parent::__construct($app);
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- }
- /**
- * @param status
- * @param role_name
- * @param level
- * @param page
- * @param size
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleList(){
- $post =$this->request->post();
- $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['item'])&&$post['item']!=="" ? $condition[]=["a.item","=",$post['item']]:"";
- isset($post['createrId'])&&$post['createrId']!=="" ? $condition[]=["a.createrId","=",$post['createrId']]:"";
- $roleList = Db::name("role");
- $count =$roleList->alias("a")->where($condition)->count();
- $page = isset($post['page'])&& $post['page']!=''? intval($post['page']) : 1;
- $size = isset($post['size'])&& $post['size']!=''? intval($post['size']) : 10;
- $page >=ceil($count/$size) ? $page=ceil($count/$size) :"";
- $list=$roleList->alias("a")->leftJoin("role_action t","a.id=t.role_id")->leftJoin("company_item i","a
- .item=i.id")->field("a.*,i.name as item_name,t.action_conllect as action,t.action_data")->where($condition)
- ->page($page,$size)->select();
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- /**
- * @param role_name
- * @param level
- * @param action
- * @param action_data
- * @param role_name
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleAdd(){
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $uid= isset($effetc['data']['id']) ? $effetc['data']['id']: "0";
- $username = isset($effetc['data']['nickname']) ? $effetc['data']['nickname']: "";
- $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
- if($rolename==""){
- return error_show(1002,"角色名称不能为空");
- }
- $isT=Db::name("role")->where(['role_name'=>$rolename])->find();
- if($isT){
- return error_show(1002,"角色名称已存在");
- }
- $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :"";
- // if($role_code==""){
- // return error_show(1002,"参数role_code 不能为空");
- // }
- // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find();
- // if($isCode){
- // return error_show(1002,"角色编号已存在");
- // }
- // $item = isset($post['item']) ? trim($post['item']) : "";
- // if($item==""){
- // return error_show(1003,"参数item 不能为空");
- // }
- // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find();
- // if(empty($iteminfo) || $iteminfo['is_del']==1){
- // return error_show(1003,"该部门不存在");
- // }
- $level = isset($post['level']) ? intval($post['level']) : 0;
- $remark = isset($post['remark']) ? trim($post['remark']) : '';
- // if($level==""){
- // return error_show(1003,"参数不能为空");
- // }
- $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']): "";
- // if($data==""){
- // return error_show(1004,"字段权限不能为空");
- // }
- Db::startTrans();
- try {
- $list = [
- "role_name"=>$rolename,
- "role_code"=>$role_code,
- "status"=>1,
- 'creater'=>$username,
- 'createrId'=>$uid,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s"),
- "level"=>$level,
- "desc"=>$remark,
- "item"=>"",
- ];
- $role= Db::name("role")->insert($list,true);
- // $st = ["order_code"=>$role_code,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
- // ActionLog::logAdd($token,$st,"role",1,$st);
- if($role>0){
- $role_action = [
- "role_id"=>$role,
- "action_conllect"=>$action,
- "action_data"=>$data,
- "private_data"=>$private_data,
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $roleaction= Db::name("role_action")->insert($role_action,true);
- if($roleaction){
- // $sty = ["order_code"=>$role,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
- // ActionLog::logAdd($token,$sty,"role_action",1,$sty);
- Db::commit();
- return app_show(0,"新建成功");
- }else{
- Db::rollback();
- return error_show(1005,"权限录入失败");
- }
- }else{
- Db::rollback();
- return error_show(1006,"角色新建失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1008,$e->getMessage());
- }
- }
- /**
- * @param roleid
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleInfo(){
- $post =$this->request->post();
- $roleid= isset($post['roleid']) ? trim($post['roleid']) : "";
- if($roleid==""){
- return error_show(1001,'roleid不能为空');
- }
- $info = $list=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")->where("a.id","=",$roleid)->find();
- if(!$info){
- return error_show(1002,"未找到对应的数据");
- }
- $info['item_name'] ='';
- if($info['item']!=''){
- $iteminfo = Db::name('company_item')->where('id',"=",$info['item'])->find();
- $info['item_name'] = isset($iteminfo['name'])? $iteminfo['name']:"";
- }
- $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
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleSave(){
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
- if($roleid==""){
- return error_show(1001,'roleid不能为空');
- }
- $info =Db::name("role")->where("id","=",$roleid)->find();
- if(!$info){
- return error_show(1002,"未找到对应的数据");
- }
- $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
- if($rolename==""){
- return error_show(1002,"角色名称不能为空");
- }
- $vers= Db::name("role")->where("id",'<>',$roleid)->where("role_name","=",$rolename)->find();
- if($vers){
- return error_show(1002,"角色名已存在");
- }
- $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :"";
- // if($role_code==""){
- // return error_show(1002,"参数role_code 不能为空");
- // }
- // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find();
- // if($isCode){
- // return error_show(1002,"角色编号已存在");
- // }
- // $item = isset($post['item']) ? trim($post['item']) : "";
- // if($item==""){
- // return error_show(1003,"参数item 不能为空");
- // }
- // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find();
- // if($iteminfo['is_del']==1){
- // return error_show(1003,"该部门不存在");
- // }
- $level = isset($post['level']) ? intval($post['level']) : 0;
- $remark = isset($post['remark']) ? trim($post['remark']) : '';
- $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']): "";
- // if($data==""){
- // return error_show(1004,"字段权限不能为空");
- // }
- Db::startTrans();
- try {
- $list = [
- "role_name"=>$rolename,
- "level"=>$level,
- 'desc'=>$remark,
- 'role_code'=>$role_code,
- 'item'=>"",
- "updatetime"=>date("Y-m-d H:i:s"),
- 'id'=>$roleid
- ];
- $role= Db::name("role")->save($list);
- // $stm = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"];
- // ActionLog::logAdd($token,$stm,"resign_info",0,$stm);
- if($role){
- $role_action = [
- "role_id"=>$roleid,
- "action_conllect"=>$action,
- "action_data"=>$data,
- "private_data"=>$private_data,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $roleaction= Db::name("role_action")->where("role_id","=",$roleid)->update($role_action);
- if($roleaction){
- // $stl = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"];
- // ActionLog::logAdd($token,$stl,"role_action",0,$stl);
- 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());
- }
- }
- /**
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleStatus(){
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
- if($roleid==""){
- return error_show(1001,'roleid不能为空');
- }
- $status = isset($post['status']) ? intval($post['status']) : "";
- if($status===""){
- return error_show(1001,'status不能为空');
- }
- if(!in_array($status,[0,1])){
- return error_show(1001,'status参数非法');
- }
- Db::startTrans();
- try {
- // $re = PasRoleList::update(['status'=>$status,'update'=>date("Y-m-d H:i:s")],['id'=>$roleid]);
- $dat=[
- 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s"), 'id'=>$roleid
- ];
- $re = Db::name("role")->save($dat);
- if($re){
- $dat2=[
- 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s")
- ];
- $action = Db::name("role_action")->where("role_id","=",$roleid)->update($dat2);
- if($action){
- // $sti = ["order_code"=>$roleid,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
- // ActionLog::logAdd($token,$sti,"role_action",$status,$sti);
- 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->post();
- $list =Db::name("role")->select();
- return app_show(0,"获取成功",$list);
- }
- }
|