post['status'])&&$this->post['status']!==''){ $uid =Db::name("user_role")->where(["status"=>1,"is_del"=>0])->column("uid"); if($this->post['status']==1){ $this->post['uid']= isset($uids)?array_unique(array_merge($uids,$uid)):$uid; }else{ $this->post['nouid']=$uid; } unset($this->post['status']); } $userinfo=GetList($this->post['token'],$this->post); if(empty($userinfo)||$userinfo['code']!=0){ return app_show($userinfo['code'],$userinfo['msg']); } $data = $userinfo['data']['list']; $role = Db::name("role")->column("role_name","id"); $roleArr = Db::name("user_role")->where(["status"=>1,"is_del"=>0])->column("roleid,status","uid"); $role[0]=""; $list=[]; foreach ($data as $value){ $value['roleid']=isset($roleArr[$value['id']]['roleid'])?$roleArr[$value['id']]['roleid'] :""; $value['status']=isset($roleArr[$value['id']]['status'])?$roleArr[$value['id']]['status']:""; $value["role_name"] = isset($role[$value["roleid"]]) ? $role[$value["roleid"]]:""; $list[]=$value; } return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]); } /**获取当前访问用户的信息 * @param token * @return \think\response\Json * @throws \think\exception\DbException */ public function userInfo(){ $userinfo=GetUserInfo($this->post['token']); if(empty($userinfo)||$userinfo['code']!=0){ return app_show(1002,"员工信息不存在"); } $data = $userinfo['data']; $data['roleid']= $this->roleid; $role = Db::name("role")->where("id","=",$data['roleid'])->find(); $data['role_name']=isset($role['role_name']) ? $role['role_name'] :""; return app_show(0,"获取成功",$data); } //修改用户状态 status 0 禁用1启用 /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function setStatus(){ $post =$this->post; $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):""; if($userid==""){ return error_show(1004,"参数id 不能为空"); } $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?"启用":"禁用"; $userinfo =Db::name("user_role")->where(["uid"=>$userid,"is_del"=>0])->find(); if($userinfo==false){ $data=[ "uid"=>$userid, "roleid"=>0, "status"=>$status, "is_del"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; $sa=Db::name("user_role")->save($data); }else{ $userinfo['status']=$status; $userinfo['updatetime']=date("Y-m-d H:i:s"); $sa=Db::name("user_role")->save($userinfo); } if($sa==false){ return error_show(1005,"{$message}失败"); }else{ return app_show(0,"{$message}成功"); } } //获取所有用户的信息 public function userAll(){ if(isset($this->post['status'])&&$this->post['status']!==''){ $uid =Db::name("user_role")->where(["status"=>1,"is_del"=>0])->column("uid"); if($this->post['status']==1){ $this->post['uid']= isset($uids)?array_unique(array_merge($uids,$uid)):$uid; }else{ $this->post['nouid']=$uid; } unset($this->post['status']); } $userinfo=GetAccountall($this->post['token'],$this->post); if(empty($userinfo)||$userinfo['code']!=0){ return app_show(1002,"员工信息不存在"); } $data = $userinfo['data']; $role = Db::name("role")->column("role_name","id"); $roleArr = Db::name("user_role")->where(["status"=>1,"is_del"=>0])->column("roleid,status","uid"); $role[0]=""; $list=[]; foreach ($data as $value){ $value['roleid']=isset($roleArr[$value['id']]['roleid'])?$roleArr[$value['id']]['roleid'] :""; $value['status']=isset($roleArr[$value['id']]['status'])?$roleArr[$value['id']]['status']:""; $value["role_name"] = isset($role[$value["roleid"]]) ? $role[$value["roleid"]]:""; $list[]=$value; } return app_show(0,"获取成功",$list); } /** * 修改用户基础信息 */ public function userSave(){ $post=$this->post; $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,"角色已禁用"); } } $userrole = Db::name("user_role")->where(["uid"=>$userid,"is_del"=>0])->find(); Db::startTrans(); try{ if(empty($userrole)){ $roledata =[ "uid"=>$userid, "roleid"=>$role, "nickname"=>$nickname, "status"=>1, "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['updatetime'] = date("Y-m-d H:i:s"); $up=Db::name("user_role")->save($userrole); } if($up==false){ Db::rollback(); return error_show(1004,'用户信息更新失败'); } $data=[ "id"=>$userid, "nickname"=>$nickname, "mobile"=>$mobile, "email"=>$email ]; $save =resetinfo($post['token'],$data); if(!empty($save) && $save['code']!=0){ Db::rollback(); return error_show($save['code'],$save['message']); } Db::commit(); return app_show(0,'用户信息更新成功'); }catch (\Exception $e){ Db::rollback(); return error_show(1004,$e->getMessage()); } } //角色设置 /** id roleid * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function setRole(){ $post =$this->post; $uid = isset($post['id'])&&$post['id']!=='' ? intval($post['id']) :""; if($uid===''){ return error_show(1004,"参数id 不能为空"); } $roleinfo = Db::name("user_role")->where(["uid"=>$uid])->find(); $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,"所选角色不存在"); } if($roleinfo==false){ $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); }else{ $roleinfo['roleid']=$role; $roleinfo['status']=1; $roleinfo['is_del']=0; $roleinfo['updatetime']=date('Y-m-d H:i:s'); $insert = Db::name('user_role')->save($roleinfo); } return $insert? app_show(0,'数据新建成功'):error_show(1004,'数据新建失败'); } //密码设置 public function setPwd(){ $post =$this->post; $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):""; if($userid==""){ return error_show(1004,"参数id 不能为空"); } $newPwd= isset($post['password']) ? trim($post['password']) : ""; if($newPwd==""){ return error_show(1001,'密码不能为空'); } $data = resetpwd($post['token'],["id"=>$userid,'password'=>$newPwd]); if(empty($data)||$data['code']!=0){ return error_show($data['code'],$data['message']); }else{ return app_show(0,"密码修改成功"); } } }