123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\BaseController;
- use think\App;
- use think\facade\Db;
- class User extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- 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']);
- }
- }
- public function list()
- {
- $post =$this->request->post();
- $page = isset($post['page'])&& $post['page']!='' ? intval($post['page']) : 1;
- $size = isset($post['size'])&& $post['size']!='' ? intval($post['size']) : 10;
- $condition = ['page'=>$page,'size'=>$size];
- $token = isset($post['token']) ? trim($post['token']) : "";
- $data = Getlist($token,$condition);
- 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")->find();
- $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :"";
- $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :"";
- $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']) : "";
- $userinfo=GetUserInfo($token);
- if(empty($userinfo)||$userinfo['code']!=0){
- return app_show(1002,"员工信息不存在");
- }
- $data = $userinfo['data'];
- $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$data['id'])
- ->field("roleid,role_name")->find();
- $data['role_name']=isset($role['role_name']) ? $role['role_name'] :"";
- $data['roleid']=isset($role['roleid']) ? $role['roleid'] :"";
- return app_show(0,"获取成功",$data);
- }
- /**
- * @param token
- * @param passwd
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function resetPwd(){
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = verfiyToken($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $newPwd= isset($post['passwd']) ? trim($post['passwd']) : "";
- if($newPwd==""){
- return error_show(1001,'新密码不能为空');
- }
- $oldpasswd= isset($post['oldpasswd']) ? trim($post['oldpasswd']) : "";
- if($oldpasswd==""){
- return error_show(1001,'旧密码不能为空');
- }
- $account =model("AdminAccount")->GetAccountByUid($effetc['user']['id']);
- if(!empty ($account) && $account->status!=1){
- return error_show(10005, '账户已被禁止登录');
- }
- if($account->password!=sha1($oldpasswd.$account->salt)){
- return error_show(10005, '旧密码错误!');
- }
- $salt=makeSalt();
- $data=['password'=>sha1($newPwd.$salt),'salt'=>$salt,"updatetime"=>date("Y-m-d H:i:s")];
- return AdminAccount::update($data,["id"=>$account->id]) ? app_show(0, '密码修改成功') : error_show(1001,"密码修改失败");
- }
- public function userAll(){
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- $userinfo=GetAccountall($token);
- if(empty($userinfo)||$userinfo['code']!=0){
- return app_show(1002,"员工信息不存在");
- }
- $data = $userinfo['data'];
- $role = Db::name("role")->column("role_name","id");
- $role[0]="";
- $list=[];
- foreach ($data as $value){
- $value["role_name"] = isset($role[$value["roleid"]]) ? $role[$value["roleid"]]:"";
- $list[]=$value;
- }
- return app_show(0,"获取成功",$list);
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function userList(){
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- $userinfo=GetList($token,$post);
- if(empty($userinfo)||$userinfo['code']!=0){
- return app_show($userinfo['code'],$userinfo['msg']);
- }
- $data = $userinfo['data']['list'];
- $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")->find();
- $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :"";
- $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :"";
- $list[]=$value;
- }
- return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
- }
- public function setRole(){
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- $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,'数据新建失败');
- }
- }
|