123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\BaseController;
- use think\facade\Db;
- class User extends BaseController
- {
- public function setRole(){
- $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']);
- }
- $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}成功");
- }
- }
- }
|