request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return app_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return app_show($effetc['code'],$effetc['message']); } $uid = isset($post['id'])&&$post['id']!=='' ? intval($post['id']) :""; if($uid===''){ return error_show(1004,"参数id 不能为空"); } $role =isset($post['roleid']) && $post['roleid']!=="" ? intval($post['roleid']) :""; if($role===''){ return error_show(1004,"参数roleid 不能为空"); } $isRole = Db::name('role')->where([['id',"=",$role],['status',"=",1]])->find(); if(empty($isRole)){ return error_show(1004,"所选角色不存在"); } $data = [ 'uid'=>$uid, 'roleid'=>$role, 'status'=>1, 'addtime'=>date('Y-m-d H:i:s'), 'updatetime'=>date('Y-m-d H:i:s'), ]; $insert = Db::name('user_role')->insert($data); return $insert? app_show(0,'数据新建成功'):error_show(1004,'数据新建失败'); } /** * 显示资源列表 * * @return \think\Response */ public function list() { $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']); } $data = Getlist($token,$post); if(!empty($data) && $data['code']!=0){ return error_show($data['code'],$data['message']); } $list=[]; foreach($data['data']['list'] as $key=>$value){ $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$value['id']) ->field("roleid,role_name,a.status")->find(); $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :""; $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :""; $value['status'] = isset($role['status']) ? $role['status'] :"0"; $depart = Db::name("depart_user")->where([["uid","=",$value['id']],["is_del","=",0]])->column("itemid"); $value['depart']=""; if(!empty($depart)){ // $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->column("name"); $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->column("id,name,depart_link"); $value['depart_code'] = []; if(!empty($deparname)){ foreach ($deparname as $item) { $value['depart_code'][] = array_filter(explode("-",$item['depart_link'])) ; $value['depart_info'][] = Db::name("company_item")->where(["id"=>array_filter(explode("-", $item['depart_link'])),"is_del"=>0])->column("name"); } } $value['depart'] = $deparname; } $list[]=$value; } $data['data']['list'] = $list; return app_show($data['code'],$data['message'],$data['data']); } /** * @param token * @return \think\response\Json * @throws \think\exception\DbException */ public function userInfo(){ $post =$this->request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return app_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return app_show($effetc['code'],$effetc['message']); } // $userinfo=GetUserInfo($token); // if(empty($userinfo)||$userinfo['code']!=0){ // return app_show(1002,"员工信息不存在"); // } $data = $effetc['data']; $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$data['id']) ->field("roleid,role_name,a.status")->find(); $data['role_name']=isset($role['role_name']) ? $role['role_name'] :""; $data['roleid']=isset($role['roleid']) ? $role['roleid'] :""; $data['status'] = isset($role['status']) ? $role['status'] :"0"; $depart = Db::name("depart_user")->where([["uid","=",$data['id']],["is_del","=",0]])->column("itemid"); $data['depart']=""; if(!empty($depart)){ $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->field("id,name,depart_link")->select(); $value['depart_code'] = []; if(!empty($deparname)){ foreach ($deparname as $item) { $value['depart_code'][] = array_filter(explode("-",$item['depart_link'])) ; } } $data['depart'] = $deparname; } return app_show(0,"获取成功",$data); } public function userAll(){ $post =$this->request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return app_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return app_show($effetc['code'],$effetc['message']); } $userinfo=GetAccountall($token,$post); if(empty($userinfo)||$userinfo['code']!=0){ return app_show(1002,"员工信息不存在"); } $data = $userinfo['data']; $list=[]; foreach ($data as $value){ $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$value['id']) ->field("roleid,role_name,a.status")->find(); $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :""; $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :""; $value['status'] = isset($role['status']) ? $role['status'] :"0"; $depart = Db::name("depart_user")->where([["uid","=",$value['id']],["is_del","=",0]])->column("itemid"); $value['depart']=""; if(!empty($depart)){ $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->field("id,name,depart_link") ->select()->toArray(); array_walk($deparname,function (&$v){ $v = array_filter(explode("-",$v['depart_link'])) ; }); $value['depart'] = $deparname; } $list[]=$value; } return app_show(0,"获取成功",$list); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function UserSave() { $post=$this->request->post(); $toke= isset($post['token'])? trim($post['token']):""; if ($toke==""){ return app_show(100,"token不能为空"); } $verify = VerifyTokens($toke); if ($verify['code']!=0){ return app_show($verify['code'],$verify['message']); } $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):""; if($userid==""){ return error_show(1004,"参数id 不能为空"); } $nickname= isset($post['nickname'])? trim($post['nickname']):""; if ($nickname==""){ return app_show(1001,"昵称不能为空"); } $mobile= isset($post['mobile'])? trim($post['mobile']):""; if ($mobile==""){ return app_show(1002,"手机号不能为空"); } if (checkMobile($mobile)==false){ return app_show(1002,"手机号格式不正确"); } $email= isset($post['email'])? trim($post['email']):""; $role =isset($post['role'])? intval($post['role']):0; if($role!=0){ $roleinfo =Db::name("role")->where("id","=",$role)->find(); if(empty($roleinfo)){ return error_show(1002,"角色不存在"); } if($roleinfo['status']==0){ return error_show(1002,"角色已禁用"); } } $status = isset($post['status'])&&$post['status']!=""?intval($post['status']):"0"; $itemid = isset($post['itemid'])&&$post['itemid']!=="" ?$post['itemid'] :""; $item_arr = []; if($itemid!==""){ foreach ($itemid as $value){ $item_arr[]=end($value); } $iteminfo = Db::name("company_item")->where(["id"=>$item_arr,'is_del'=>0])->select()->toArray(); if(empty($iteminfo)){ return error_show(1002,"未找到部门信息"); } } Db::startTrans(); try{ $userrole = Db::name("user_role")->where(["uid"=>$userid,"is_del"=>0])->find(); if(empty($userrole)){ $roledata =[ "uid"=>$userid, "roleid"=>$role, "status"=>$status, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $up= Db::name("user_role")->insert($roledata); }else{ $userrole['roleid'] = $role; $userrole['status'] = $status; $userrole['updatetime'] = date("Y-m-d H:i:s"); $up=Db::name("user_role")->save($userrole); } if($up){ $data=[ "id"=>$userid, "nickname"=>$nickname, "mobile"=>$mobile, "email"=>$email ]; $save =resetinfo($toke,$data); if(!empty($save) && $save['code']!=0){ Db::rollback(); return error_show($save['code'],$save['message']); } Db::name("depart_user")->where(["uid"=>$userid])->save(['is_del'=>1]); if(isset($item_arr)&&!empty($item_arr)){ foreach ($item_arr as $v){ $depin =[ "uid"=>$userid, "nickname"=>$nickname, "itemid"=>$v, "is_del"=>0, "updatetime"=>date("Y-m-d H:i:s") ]; $ist = Db::name("depart_user")->where(["uid"=>$userid,"itemid"=>$v])->find(); if($ist){ $up = Db::name("depart_user")->where(["uid"=>$userid,"itemid"=>$v])->save($depin); }else{ $depin['addtime']=date("Y-m-d H:i:s"); $up = Db::name("depart_user")->insert($depin); } if($up==false){ Db::rollback(); return error_show(1005,'用户部门更新失败'); } } } Db::commit(); return app_show(0,"更新成功"); }else{ Db::rollback(); return error_show(1005,'角色更新失败'); } }catch(\Exception $e){ Db::rollback(); return app_show(1002,$e->getMessage()); } } public function UserStatus() { $post=$this->request->post(); $toke= isset($post['token'])? trim($post['token']):""; if ($toke==""){ return app_show(100,"token不能为空"); } $verify = VerifyTokens($toke); if ($verify['code']!=0){ return app_show($verify['code'],$verify['message']); } $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):""; if($userid==""){ return error_show(1004,"参数userid 不能为空"); } $status = isset($post['status'])&&$post['status']!=""?intval($post['status']):""; if($status===""){ return error_show(1004,"参数status 不能为空"); } if(!in_array($status,[0,1])){ return error_show(1004,"参数status无效"); } $message = $status==1?"启用":"禁用"; // $set = resetstatus($toke,["id"=>$userid,"status"=>$status]); $item = Db::name('user_role')->where(['uid'=>$userid])->find(); if($item==false){ $item=[ "uid"=>$userid, "roleid"=>0, "is_del"=>0, "addtime"=>date("Y-m-d H:i:s") ]; } $item['status']=$status; $item['updatetime'] = date("Y-m-d H:i:s"); $var = Db::name('user_role')->save($item); if($var==false){ return error_show(1005,"{$message}失败"); }else{ return app_show(0,"{$message}成功"); } } }