123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- declare (strict_types = 1);
- namespace app\Api\controller;
- use app\BaseController;
- use think\Request;
- use think\facade\Db;
- class UserInfo extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function UserList()
- {
- $post=$this->request->post();
- $toke= isset($post['token'])? trim($post['token']):"";
- if ($toke==""){
- return app_show(100,"token不能为空");
- }
- $verify = VerifyToken($toke);
- if ($verify['code']!=0){
- return app_show($verify['code'],$verify['message']);
- }
- $condition = [];
- isset($post['name'])&& $post['name']!="" ? $condition[]=["nickname","like","%{$post['name']}%"] : "";
- isset($post['username'])&& $post['username']!="" ? $condition[]=["username","like","%{$post['username']}%"] : "";
- isset($post['role'])&& $post['role']!="" ? $condition[]=["roleid","=",$post['role']] : "";
- isset($post['status'])&& $post['status']!=="" ? $condition[]=["status","=",$post['status']] : "";
- isset($post['uid'])&& $post['uid']!=="" ? $condition[]=["id","in",$post['uid']] : "";
- $page = isset($post['page'])&& $post['page']!=="" ? intval($post['page']) : 1;
- $size = isset($post['size'])&& $post['size']!=="" ? intval($post['size']) : 10;
- $count = Db::name("view_userinfo")->where($condition)->count();
- $total =intval(ceil($count/$size)) ;
- $page = $total>=$page? $page:$total;
- $list = Db::name("view_userinfo")->where($condition)->page($page,$size)->field("id,username,status,source,addtime,nickname,sex,mobile,email,portrait,post,department,roleid")->select();
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function UserStatus()
- {
- $post=$this->request->post();
- $toke= isset($post['token'])? trim($post['token']):"";
- if ($toke==""){
- return app_show(100,"token不能为空");
- }
- $verify = VerifyToken($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 不能为空");
- }
- $account=Db::name("account")->where("id","=",$userid)->find();
- if(empty($account)){
- return error_show(1003,"账户不存在");
- }
- $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?"启用":"禁用";
- $result= Db::name("account")->where("id","=",$userid)->save(['status'=>$status,"updatetime"=>date("Y-m-d
- H:i:s")]);
- return $result?app_show(0,"账户{$message}") : error_show(1005,"账户{$message}失败");
- }
- /**
- * 保存新建的资源
- *
- * @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 = VerifyToken($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 不能为空");
- }
- $account=Db::name("view_userinfo")->where("id","=",$userid)->find();
- if(empty($account)){
- return error_show(1003,"账户不存在");
- }
- $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']):"";
- $avatar = isset($post['portrait'])? trim($post['portrait']):"";
- $sex = isset($post['sex'])? trim($post['sex']):"";
- $postDa = isset($post['post'])? trim($post['post']):"";
- $department = isset($post['department'])? trim($post['department']):"";
- $role =isset($post['role'])&&$post['role']!==''? intval($post['role']):$account['roleid'];
- Db::startTrans();
- try{
- if(isset($account['user_id'])&& $account['user_id']!=""){
- $userinfo=[
- "id"=>$account['user_id'],
- "nickname"=>$nickname,
- "mobile"=>$mobile,
- "email"=>$email,
- "portrait"=>$avatar,
- "sex"=>$sex,
- "post"=> $postDa,
- "department"=>$department,
- "status"=>1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $dat=Db::table("sys_user")->save($userinfo);
- if(!$dat){
- Db::rollback();
- return error_show(1004,"信息修改失败");
- }
- }
- $relation = Db::name("user_relation")->where("accountid","=",$userid)->find();
- if($relation){
- $relation['user_id']=$account['user_id'];
- $relation['roleid']=$role;
- $relation['updatetime']=date("Y-m-d H:i:s");
- $rel=Db::name("user_relation")->save($relation);
- }else{
- $rela=["accountid"=>$userid,"user_id"=>$account['user_id'],"roleid"=>$role,"updatetime"=>date("Y-m-d H:i:s")];
- $rel= Db::name("user_relation")->insert($rela);
- }
- if(!$rel){
- Db::rollback();
- return error_show(1004,"关联信息修改失败");
- }
- $acc= [
- "id"=>$account['id'],
- "mobile"=>$mobile,
- "username"=>$mobile,
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $nu = Db::name("account")->save($acc);
- if($nu){
- Db::commit();
- return app_show(0,"信息修改成功");
- }else{
- Db::rollback();
- return error_show(1004,"账户信息修改失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- /**
- * 显示指定的资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function all()
- {
- $post=$this->request->post();
- $toke= isset($post['token'])? trim($post['token']):"";
- if ($toke==""){
- return app_show(100,"token不能为空");
- }
- $verify = VerifyToken($toke);
- if ($verify['code']!=0){
- return app_show($verify['code'],$verify['message']);
- }
- $condition = [];
- isset($post['name'])&& $post['name']!="" ? $condition[]=["nickname","like","%{$post['name']}%"] : "";
- isset($post['role'])&& $post['role']!="" ? $condition[]=["roleid","=",$post['role']] : "";
- isset($post['status'])&& $post['status']!=="" ? $condition[]=["status","=",$post['status']] : "";
- $list = Db::name("view_userinfo")->where($condition)->field("id,username,status,source,addtime,nickname,sex,mobile,email,portrait,post,department,roleid")->select();
- return app_show(0,"获取成功",$list);
- }
- /**
- * 显示编辑资源表单页.
- *
- * @param int $id
- * @return \think\Response
- */
- public function info()
- {
- $post=$this->request->post();
- $toke= isset($post['token'])? trim($post['token']):"";
- if ($toke==""){
- return app_show(100,"token不能为空");
- }
- $verify = VerifyToken($toke);
- if ($verify['code']!=0){
- return app_show($verify['code'],$verify['message']);
- }
- $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']) : "";
- if($id===""){
- return error_show(1003,"参数id 不能为空");
- }
- $list = Db::name("view_userinfo")->where("id","=",$id)->field("id,username,status,source,addtime,nickname,sex,mobile,email,portrait,post,department,roleid")->find();
- if(empty($list)){
- return error_show(1004,"未找到用户信息");
- }
- return app_show(0,"获取成功",$list);
- }
- /**
- * 保存更新的资源
- *
- * @param \think\Request $request
- * @param int $id
- * @return \think\Response
- */
- public function PassSet()
- {
- $post=$this->request->post();
- $toke= isset($post['token'])? trim($post['token']):"";
- if ($toke==""){
- return app_show(100,"token不能为空");
- }
- $verify = VerifyToken($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 不能为空");
- }
- $account=Db::name("account")->where("id","=",$userid)->find();
- if(empty($account)){
- return error_show(1003,"账户不存在");
- }
- $pass = isset($post["password"]) && $post['password']!="" ? trim($post['password']) : "";
- if($pass==""){
- return error_show(1003,"账户密码不能为空");
- }
- if(mb_strlen($pass)<6){
- return error_show(1003,"账户密码长度不能小于6位");
- }
- $salt=makeSalt();
- $password = sha1($pass.$salt);
- $account['password']=$password;
- $account['salt']=$salt;
- $account['is_pass']=1;
- $account['updatetime']=date("Y-m-d H:i:s");
- $up = Db::name("account")->save($account);
- return $up?app_show(0,"密码修改成功"):error_show(1005,"密码修改失败");
- }
- /**
- * 删除指定资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function delete($id)
- {
- //
- }
- }
|