1、html代码
<form method="post" action="{{url('Index/loginAction')}}"> <h3> <a href="" class="fl">忘记密码?</a> <a href="{{$url}}" class="fl"><img src="/qq.png" alt=""></a> <a href="" class="fr">邮箱找回密码?</a> </h3> </form>
2、PHP代码
namespace App\Http\Controllers\Index; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Session;
public function login() { $urlencode = urlencode("http://www.XXX.top/callback"); $url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id='APP ID'&redirect_uri={$urlencode}"; $data = compact("url"); return view('index/login',$data); }
2.1登录处理
public function loginAction() { $account = request()->input("username"); // 用户的账号 $password = md5(md5(request()->input("password"))); // 用户的密码 // 账号可以用,用户名,邮箱,手机号 都可以登录,该如何实现? $reg_phone = '/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/'; $reg_email = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/'; $where["member_password"] = $password; $where['is_forbidden'] = 0; if(preg_match($reg_phone,$account)){ // 用手机号来登录 $where['member_phone'] = $account; }elseif (preg_match($reg_email,$account)){ // 用邮箱来登录 $where['member_email'] = $account; }else{ // 用户名来登录 $where['member_name'] = $account; } if($row = DB::table("member")->where($where)->first()){ // 用户名,密码,都正确,已经被激活了 $data['code'] = 0; // 存session Session::put("member_flag",1); Session::put("mid",$row['id']); Session::put("username",$row['username']); // 查询上次的登录时间 // $last_login_time = Db::name("member")->where(["id"=>$row['id']])->value("login_time"); Session::put("last_login_time",$row['login_time']); // 把上次登录时间取出来,存session // 每次登录成功后,修改登录时间 DB::table("member")->where(["id"=>$row['id']])->update(["login_time"=>date("Y-m-d H:i:s")]); }else{ $data['code'] = 1; } return response()->json($data); }
2.2回调地址
// 回调地址 public function callback(Request $request) { $code = $request->input('code'); // 1.获取code // 2.发起请求 获取access_token $urlencode = urlencode("http://www.XXX.top/callback"); $url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id='APP ID'&client_secret='APP Key'&code={$code}&redirect_uri={$urlencode}&fmt=json"; $json = file_get_contents($url); // dd($json); $obj = json_decode($json); // dd($obj); $access_token = $obj->access_token; // 3.发起请求 获取openid $url = "https://graph.qq.com/oauth2.0/me?access_token={$access_token}&fmt=json"; $obj = json_decode(file_get_contents($url)); $openid = $obj->openid; // dd($openid); // 4.获取用户的信息 $url = "https://graph.qq.com/user/get_user_info?access_token={$access_token}&oauth_consumer_key='APP ID'&openid={$openid}"; // dd(file_get_contents($url)); $obj = json_decode(file_get_contents($url)); // dd($obj); // openid // 1.根据openid 查询这条记录是否已经存在?如果不存在,则写入,如果存在,则不用写 $row = DB::table("qq")->where(["openid"=>$openid])->first(); // dd($row); if($row){ // 如果查到了,意味着此QQ已经登录过了 // 查询是否已经绑定 if($row->mid){ // 已经绑定了 用户信息存入session $userinfo = DB::table("member")->where(["id"=>$row->mid])->first(); // 绑定成功了 Session::put("member_flag",1); Session::put("mid",$row->mid); Session::put("username",$userinfo->member_name); // 跳转到首页 // return redirect((string)url('index')); return view('index/index'); }else{ // 还未绑定 // 跳转到绑定账号的页面 // 把openid 存入session Session::put("openid",$openid); DB::table("qq")->where("openid",$openid)->increment("login_num"); return redirect((string)url('bindAccount')); } }else{ // dd($obj); // 说明初次登录 // 1.qq表写入数据,id,openid,nickname,gender,login_num,mid $data["nickname"] = $obj->nickname; $data["gender"] = $obj->gender; $data["openid"] = $openid; $data["login_num"] = 1; $id = DB::table("qq")->insertGetId($data); // 假如写入成功 // 跳转到绑定账号的页面 // 把openid 存入session Session::put("openid",$openid); return redirect((string)url('bindAccount')); } // 2.写session // 3. 跳转到首页 }
2.3绑定账号
// 绑定账号 public function bindAccount() { return view('index/bind'); }
2.4绑定页面js代码
<script> $("#login").submit(function () { var username = $("#username").val(); var password = $("#password").val(); console.log(username); console.log(password); // return false; // 绑定处理 $.post("{{url('bindAccountAction')}}",{username:username,password:password,_token:'{{csrf_token()}}'},function (data) { if(data.code == 1){ // 登录失败 alert("绑定失败了,请重试") }else{ // 登录成功 alert("绑定成功了") location.href="{{url('/')}}"; } },"json"); return false; }) </script>
2.5绑定处理
// 绑定处理 public function bindAccountAction() { // 绑定处理 $username = request()->input("username"); $password = request()->input("password"); if($row = DB::table("member")->where(["member_name"=>$username,"member_pass"=>$password,"is_forbidden"=>0])->first()){ // 绑定的账号,密码没问题 // 所谓的绑定就是把用户的id写入到qq表的mid $openid = Session::get("openid"); if(DB::table("qq")->where(["openid"=>$openid])->update(["mid"=>$row->id])){ // 绑定成功了 Session::put("member_flag",1); Session::put("mid",$row->id); Session::put("username",$username); // 跳转到首页 $data['code'] = 0; }else{ // 绑定失败了 $data['code'] = 1; } return response()->json($data); // $data['code'] = 0; // // 存session }else{ $data['code'] = 1; } return response()->json($data); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具