app = $app; $this->request = $this->app->request; } public function register() { $post = $this->request->post(); $username = isset($post['username']) ? trim($post['username']):""; if($username==""){ return app_show(1002,"账户名不能为空!"); } $password = isset($post['password']) ? trim($post['password']):""; if($password==""){ return app_show(1002,"密码不能为空!"); } $mobile = isset($post['mobile']) ? trim($post['mobile']):""; if($mobile==""){ return app_show(1002,"手机号不能为空!"); } if(checkMobile($mobile)==false){ return app_show(1002,"手机号格式不正确!"); } $source = isset($post['source']) ? trim($post['source']):""; $uiq = Db::table("sys_account")->where(["username"=>$username])->find(); if($uiq){ return app_show(1002,"账户名已存在!"); } $uiq = Db::table("sys_account")->where(["mobile"=>$mobile])->find(); if($uiq){ return app_show(1002,"手机号已注册!"); } Db::startTrans(); try { $salt =makeSalt(); $password = sha1($password.$salt); $data = ['username'=>$username,"password"=>$password,"salt"=>$salt,"mobile"=>$mobile,"source"=>$source, "status"=>1,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")]; $reuslt = Db::table('sys_account')->insert($data,true); if($reuslt){ $data=[ "nickname"=>"", "mobile"=>$mobile, "email"=>"", "portrait"=>"", "sex"=>1, "post"=>"", "department"=>"", "status"=>1, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $user=Db::table("sys_user")->insert($data,true); if($user){ $table=[ "accountid"=>$reuslt, "user_id"=>$user, "roleid"=>0, "updatetime"=>date("Y-m-d H:i:s") ]; $rela = Db::table("sys_user_relation")->save($table); if($rela){ Db::commit(); return app_show(0,"账户注册成功"); } Db::rollback(); return error_show(1002,"用户信息管联失败"); } Db::rollback(); return error_show(1002,"用户信息注册失败"); } Db::rollback(); return error_show(1002,"账户注册失败"); }catch (\Exception $e){ Db::rollback(); var_dump($e->getMessage()); return error_show(1002,"账户注册失败"); } } /** * 显示创建资源表单页. * * @return \think\Response */ public function verify_code() { $post = $this->request->post(); $code = make_verify(); $mobile = isset($post['mobile'])&&checkMobile($post['mobile']) ? $post['mobile'] :"" ; if($mobile==""){ return app_show(1001,"手机号格式不正确"); } $mess =Db::name("send_message")->where(['mobile'=>$mobile,"status"=>0,"msg_type"=>1])->find(); if($mess){ if($mess['expire']>time()-60){ return app_show(1001,"验证码发送中!"); } $mess['status']=1; Db::name("send_message")->save($mess); } // $sendJson = sendMessage($mobile, $code); // $sendResult = json_decode($sendJson, TRUE); // if($sendResult['description'] != 'Success') { // return app_show(1002, '短信发送失败,请重试'); // } $data=['code'=>$code,"mobile"=>$mobile,"status"=>0,"msg_type"=>1,"addtime"=>date("Y-m-d H:i:s"), "expire"=>time()]; $result = Db::name("send_message")->insert($data); return $result ? app_show(0,"验证码已发送",$code): app_show(1001,"验证码发送失败"); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function login() { $post = $this->request->post(); $username = isset($post['username']) ? trim($post['username']):""; if($username==""){ return app_show(1002,"账户名不能为空!"); } $password = isset($post['password']) ? trim($post['password']):""; if($password==""){ return app_show(1002,"密码不能为空!"); } $acc= Db::table("sys_account")->where(['username'=>$username])->find(); if($acc==false){ return app_show(1003,'账户名不存在'); } if($acc['status']==0){ return app_show(1003,'账户名已禁用'); } $sha1=sha1($password.$acc['salt']); if($sha1!=$acc['password']){ return app_show(1003,'账户或密码错误'); } $userinfo = Db::name("view_userinfo")->where("id","=", $acc['id'])->find(); $token = makeToken($userinfo,time()+1800); if($token==""){ return app_show(1003,'token生成失败'); } $user=['userinfo'=>$userinfo ,"token"=>$token]; // $data = [ // "info"=>"钉钉登录", // "action"=>"/Api", // "url"=>"/login", // "param"=>"", // "name"=>$userinfo['nickname'], // "rolename"=>"", // "addtime"=>date("Y-m-d H:i:s") // ]; // Db::name("system_log")->save($data); return app_show(0,"登录成功",$user); } /** * 显示指定的资源 * * @param int $id * @return \think\Response */ public function prefect() { $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']); } $userinfo = Db::name("view_userinfo")->where("id","=",$verify['user']['id'])->find(); $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']):""; if ($email==""){ return app_show(1003,"邮箱不能为空"); } if (checkEmail($email)==false){ return app_show(1003,"邮箱格式不正确"); } $avatar = isset($post['portrait'])? trim($post['portrait']):""; $sex = isset($post['sex'])? trim($post['sex']):""; $post = isset($post['post'])? trim($post['post']):""; $department = isset($post['department'])? trim($post['department']):""; $userio=[ "id"=>$userinfo['user_id'], "nickname"=>$nickname, "mobile"=>$mobile, "email"=>$email, "portrait"=>$avatar, "sex"=>$sex, "post"=>$post, "department"=>$department, "updatetime"=>date("Y-m-d H:i:s") ]; Db::startTrans(); $result=Db::name("account")->where("id","=",$userinfo['id'])->save(["mobile"=>$mobile,"username"=>$mobile,"updatetime"=>date("Y-m-d")]); if($result){ $user= Db::name("user")->save($userio); if($user){ Db::commit(); return app_show(0,"信息修改成功"); } } Db::rollback(); return app_show(1004,"信息修改失败"); } /** * @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 GetUserInfo() { $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= $verify['user']['id']; $userinfo = Db::name("view_userinfo")->where(['id'=>$userid])->find(); if(!$userinfo){ return app_show(106,"用户信息未完善"); } return app_show(0,"信息获取成功",$userinfo); } /** * 保存更新的资源 * * @param \think\Request $request * @param int $id * @return \think\Response */ public function DingTalk() { $config= Config("app")['dingtalk']; $dingtalk =new \DingTalk($config); $code = $this->request->post("code"); if($code==""){ return error_show(106,"code不能为空"); } $li = $dingtalk->getUserByCode($code); Log::record(var_export($li,true)); if($li['errcode']!=0){ return app_show(107,"授权失败",$li); } $list = $dingtalk->getUser($li['userid']); if($list['errcode']!=0){ return app_show(107,"授权失败",$list); } $userinfo = Db::name("view_userinfo")->where(['DTuserid'=>$list['userid'],"unionid"=>$list['unionid']])->find(); if(empty($userinfo)){ $userinfo = $this->DingTalkRegister($list); } $token = makeToken($userinfo,time()+1800); $userinfo['token']=$token; // $data = [ // "info"=>"钉钉登录", // "action"=>"/Api", // "url"=>"/login", // "param"=>"", // "name"=>$userinfo['nickname'], // "rolename"=>"", // "addtime"=>date("Y-m-d H:i:s") // ]; // Db::name("system_log")->save($data); return app_show(0,"授权成功",$userinfo); } private function DingTalkRegister($Dingtalinfo){ Db::startTrans(); $verify = Db::name("user")->where("mobile","=",$Dingtalinfo['mobile'])->find(); if(!empty($verify)){ $verify['unionid']=$Dingtalinfo['unionid']; $verify['openId']=$Dingtalinfo['openId']; $verify['DTuserid']=$Dingtalinfo['userid']; $verify['mobile']=$Dingtalinfo['mobile']; isset($verify['portrait'])??$verify['portrait']=$Dingtalinfo['avatar']; isset($verify['email'])??$verify['email']=$Dingtalinfo['email']; $verify['updatetime']=date("Y-m-d H:i:s"); $user =Db::table("sys_user")->save($verify); $uid = $verify["id"]; }else{ $data=[ "nickname"=>$Dingtalinfo['name'], "mobile"=>$Dingtalinfo['mobile'], "email"=>$Dingtalinfo['email'], "portrait"=>$Dingtalinfo['avatar'], "sex"=>1, "post"=>"", "unionid"=>$Dingtalinfo['unionid'], "openId"=>$Dingtalinfo['openId'], "DTuserid"=>$Dingtalinfo['userid'], "department"=>"", "status"=>1, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $uid =Db::table("sys_user")->insert($data,true); } if($uid<=0){ Db::rollback(); return []; } $relation = Db::table("sys_user_relation")->where("user_id","=",$uid)->find(); if(empty($relation)){ $salt=makeSalt(); $data=[ "username"=>$Dingtalinfo['mobile'], "password"=>sha1("dingding123".$salt), "salt"=>$salt, "status"=>1, "source"=>"dingtalk", "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $account = Db::table("sys_account")->insert($data,true); $rela =['accountid'=>$account,"user_id"=>$uid,"roleid"=>0,"updatetime"=>date("Y-m-d H:i:s")]; $account_relation = Db::table("sys_user_relation")->save($rela); if(!$account_relation){ Db::rollback(); return []; } } Db::commit(); $userinfo = Db::name("view_userinfo")->where("user_id","=",$uid)->find(); return $userinfo; } /** * */ public function verify_token(){ $post=$this->request->post(); $toke= isset($post['token'])? trim($post['token']):""; if ($toke==""){ return app_show(100,"token不能为空"); } $verify = VerifyToken($toke); return app_show($verify['code'],$verify['message'],["user"=>isset($verify['user'])?$verify['user']:""]); } /** * @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 reset_password(){ $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']); } $userinfo = Db::name("account")->where("id","=",$verify['user']['id'])->find(); $oldpwd = isset($post['oldpwd'])?trim($post['oldpwd']):""; if($oldpwd==""){ return error_show(1002,"原密码不能为空"); } if(sha1($oldpwd.$userinfo['salt'])!=$userinfo['password']){ return error_show(1002,"原密码错误"); } $newpwd = isset($post['newpwd'])?trim($post['newpwd']):""; if($newpwd==""){ return error_show(1002,"新密码不能为空"); } $salt=makeSalt(); $pass=sha1($newpwd.$salt); $userinfo['salt']=$salt; $userinfo['password']=$pass; $userinfo['is_pass']=1; $result= Db::name("account")->save($userinfo); return $result ?app_show(0,"密码修改成功"):error_show(1003,"密码修改失败"); } /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function reset_password_mobile(){ $post=$this->request->post(); $mobile = isset($post['mobile'])? trim($post['mobile']):""; if($mobile==""){ return error_show(1001,"手机号不能为空"); } if(checkMobile($mobile)==false){ return error_show(1002,"手机号格式不正确!"); } $code = isset($post['code'])? trim($post['code']):""; if($code==""){ return error_show(1001,"验证码不能为空"); } $username = isset($post['username'])?trim($post['username']):""; if($username==""){ return error_show(1001,"参数username 不能为空"); } $account = Db::name("account")->where("username","=",$username)->find(); if($account['mobile']!=$mobile){ return error_show(1004,"账户关联手机号不正确"); } $password = isset($post['password'])?trim($post['password']):""; if($password==""){ return error_show(1001,"新密码不能为空"); } if(sha1($password.$account['salt'])==$account['password']){ return error_show(1001,"新密码不能与原密码相同"); } $codeinfo = Db::name("send_message")->where(["mobile"=>$mobile,"status"=>0,"msg_type"=>1])->find(); if($code!=$codeinfo['code']){ return error_show(1003,"验证码错误"); } $codeinfo['status']=1; Db::name("send_message")->save($codeinfo); $account['salt']=makeSalt(); $account['updatetime']=date("Y-m-d"); $account['is_pass']=1; $account['password']=sha1($password.$account['salt']); $result=Db::name("account")->save($account); return $result?app_show(0,"密码修改成功"):app_show(1003,"密码修改失败"); } /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function reset_mobile(){ $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']); } $mobile = isset($post['mobile'])? trim($post['mobile']):""; if($mobile==""){ return error_show(1001,"手机号不能为空"); } if(checkMobile($mobile)==false){ return error_show(1002,"手机号格式不正确!"); } $code = isset($post['code'])? trim($post['code']):""; if($code==""){ return error_show(1001,"验证码不能为空"); } $account = Db::name("view_userinfo")->where("id","=",$verify['user']['id'])->find(); if($account['mobile']==$mobile){ return error_show(1004,"新手机号与原手机号相同"); } $codeinfo = Db::name("send_message")->where(["mobile"=>$mobile,"status"=>0,"msg_type"=>1])->find(); if(!$codeinfo||$code!=$codeinfo['code']){ return error_show(1003,"验证码错误"); } $codeinfo['status']=1; Db::name("send_message")->save($codeinfo); Db::startTrans(); $result=Db::name("account")->where("id","=",$account['id'])->save(["mobile"=>$mobile,"username"=>$mobile,"updatetime"=>date("Y-m-d")]); if($result){ $user= Db::name("user")->where("id","=",$account['user_id'])->save(["mobile"=>$mobile]); if($user){ Db::commit(); return app_show(0,"手机号修改成功"); } } Db::rollback(); return app_show(1003,"手机号修改失败"); } /** * @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 GetUserlist(){ $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 = "1"; $id = isset($post['id'])&&is_array($post['id'])? $post['id'] :[]; if(!empty($id)){ $condition .= " and id in (".implode(",",$id).")"; } $roleid = isset($post['roleid'])? intval($post['roleid']) :""; if($roleid!=""){ $condition .= " and roleid ={$roleid}"; } $user = Db::name("view_userinfo")->where($condition)->select(); return app_show(0,"获取成功",$user); } }