User.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. class User extends BaseController
  8. {
  9. /**
  10. * 显示资源列表
  11. *
  12. * @return \think\Response
  13. */
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. $post =$this->request->post();
  18. $token = isset($post['token']) ? trim($post['token']) : "";
  19. if($token==""){
  20. return error_show(101,'token不能为空');
  21. }
  22. $effetc =VerifyTokens($token);
  23. if(!empty($effetc) && $effetc['code']!=0){
  24. return error_show($effetc['code'],$effetc['message']);
  25. }
  26. }
  27. public function list()
  28. {
  29. $post =$this->request->post();
  30. $page = isset($post['page'])&& $post['page']!='' ? intval($post['page']) : 1;
  31. $size = isset($post['size'])&& $post['size']!='' ? intval($post['size']) : 10;
  32. $condition = ['page'=>$page,'size'=>$size];
  33. $token = isset($post['token']) ? trim($post['token']) : "";
  34. $data = Getlist($token,$condition);
  35. if(!empty($data) && $data['code']!=0){
  36. return error_show($data['code'],$data['message']);
  37. }
  38. $list=[];
  39. foreach($data['data']['list'] as $key=>$value){
  40. $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$value['id'])
  41. ->field("roleid,role_name")->find();
  42. $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :"";
  43. $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :"";
  44. $list[]=$value;
  45. }
  46. $data['data']['list'] = $list;
  47. return app_show($data['code'],$data['message'],$data['data']);
  48. }
  49. /**
  50. * @param token
  51. * @return \think\response\Json
  52. * @throws \think\exception\DbException
  53. */
  54. public function userInfo(){
  55. $post =$this->request->post();
  56. $token = isset($post['token']) ? trim($post['token']) : "";
  57. $userinfo=GetUserInfo($token);
  58. if(empty($userinfo)||$userinfo['code']!=0){
  59. return app_show(1002,"员工信息不存在");
  60. }
  61. $data = $userinfo['data'];
  62. $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$data['id'])
  63. ->field("roleid,role_name")->find();
  64. $data['role_name']=isset($role['role_name']) ? $role['role_name'] :"";
  65. $data['roleid']=isset($role['roleid']) ? $role['roleid'] :"";
  66. return app_show(0,"获取成功",$data);
  67. }
  68. /**
  69. * @param token
  70. * @param passwd
  71. * @return \think\response\Json
  72. * @throws \think\exception\DbException
  73. */
  74. public function resetPwd(){
  75. $post =$this->request->post();
  76. $token = isset($post['token']) ? trim($post['token']) : "";
  77. if($token==""){
  78. return error_show(101,'token不能为空');
  79. }
  80. $effetc = verfiyToken($token);
  81. if(!empty($effetc) && $effetc['code']!=0){
  82. return error_show($effetc['code'],$effetc['message']);
  83. }
  84. $newPwd= isset($post['passwd']) ? trim($post['passwd']) : "";
  85. if($newPwd==""){
  86. return error_show(1001,'新密码不能为空');
  87. }
  88. $oldpasswd= isset($post['oldpasswd']) ? trim($post['oldpasswd']) : "";
  89. if($oldpasswd==""){
  90. return error_show(1001,'旧密码不能为空');
  91. }
  92. $account =model("AdminAccount")->GetAccountByUid($effetc['user']['id']);
  93. if(!empty ($account) && $account->status!=1){
  94. return error_show(10005, '账户已被禁止登录');
  95. }
  96. if($account->password!=sha1($oldpasswd.$account->salt)){
  97. return error_show(10005, '旧密码错误!');
  98. }
  99. $salt=makeSalt();
  100. $data=['password'=>sha1($newPwd.$salt),'salt'=>$salt,"updatetime"=>date("Y-m-d H:i:s")];
  101. return AdminAccount::update($data,["id"=>$account->id]) ? app_show(0, '密码修改成功') : error_show(1001,"密码修改失败");
  102. }
  103. public function userAll(){
  104. $post =$this->request->post();
  105. $token = isset($post['token']) ? trim($post['token']) : "";
  106. $userinfo=GetAccountall($token);
  107. if(empty($userinfo)||$userinfo['code']!=0){
  108. return app_show(1002,"员工信息不存在");
  109. }
  110. $data = $userinfo['data'];
  111. $role = Db::name("role")->column("role_name","id");
  112. $role[0]="";
  113. $list=[];
  114. foreach ($data as $value){
  115. $value["role_name"] = isset($role[$value["roleid"]]) ? $role[$value["roleid"]]:"";
  116. $list[]=$value;
  117. }
  118. return app_show(0,"获取成功",$list);
  119. }
  120. /**
  121. * @return \think\response\Json|void
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. */
  127. public function userList(){
  128. $post =$this->request->post();
  129. $token = isset($post['token']) ? trim($post['token']) : "";
  130. $userinfo=GetList($token,$post);
  131. if(empty($userinfo)||$userinfo['code']!=0){
  132. return app_show($userinfo['code'],$userinfo['msg']);
  133. }
  134. $data = $userinfo['data']['list'];
  135. $list=[];
  136. foreach ($data as $value){
  137. $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$value['id'])
  138. ->field("roleid,role_name")->find();
  139. $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :"";
  140. $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :"";
  141. $list[]=$value;
  142. }
  143. return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
  144. }
  145. public function setRole(){
  146. $post =$this->request->post();
  147. $token = isset($post['token']) ? trim($post['token']) : "";
  148. $uid = isset($post['id'])&&$post['id']!=='' ? intval($post['id']) :"";
  149. if($uid===''){
  150. return error_show(1004,"参数id 不能为空");
  151. }
  152. $role =isset($post['roleid']) && $post['roleid']!=="" ? intval($post['roleid']) :"";
  153. if($role===''){
  154. return error_show(1004,"参数roleid 不能为空");
  155. }
  156. $isRole = Db::name('role')->where([['id',"=",$role],['status',"=",1]])->find();
  157. if(empty($isRole)){
  158. return error_show(1004,"所选角色不存在");
  159. }
  160. $data = [
  161. 'uid'=>$uid,
  162. 'roleid'=>$role,
  163. 'status'=>1,
  164. 'addtime'=>date('Y-m-d H:i:s'),
  165. 'updatetime'=>date('Y-m-d H:i:s'),
  166. ];
  167. $insert = Db::name('user_role')->insert($data);
  168. return $insert? app_show(0,'数据新建成功'):error_show(1004,'数据新建失败');
  169. }
  170. }