不要泛滥,不要泛滥,不要泛滥 和谐了,你我都不好受
不知道是啥的,看演示地址: https://forum.runpod.cn/OAuthLogin/WeChat
php代码如下,大佬们自行研究吧
做的太简单和谐的快
- <?php
-
- namespace AppPluginsOAuthLoginsrcControllerWeChat;
-
- use AppPluginsOAuthLoginsrcModelOauthLoginWechat;
- use AppPluginsOAuthLoginsrcModelOauthLoginWechatBind;
- use AppPluginsUsersrcEventAfterLogin;
- use AppPluginsUsersrcLibUserAuth;
- use AppPluginsUsersrcModelsUser;
- use AppPluginsUsersrcModelsUserClass;
- use AppPluginsUsersrcModelsUsersOption;
- use HyperfHttpServerAnnotationController;
- use HyperfHttpServerAnnotationGetMapping;
- use HyperfHttpServerAnnotationPostMapping;
- use HyperfUtilsStr;
- use HyperfExtHashingHash;
-
- #[Controller(prefix:"/api/OAuthLogin/WeChat")]
- class ApiController
- {
- private string $appid = "wx311697afe4355554";
- private string $redirect_uri = "https://lol.qq.com/";
- private string $qrcode_uri = "https://open.weixin.qq.com/connect/qrcode/";
-
- // 获取二维码
- public function getQrCode(){
- return $this->qrcode_uri.$this->getUuid();
- }
-
- private function getSessionId(){
- if(!session()->get("OAuthLogin_WeChat")){
- session()->set("OAuthLogin_WeChat",Str::random());
- }
- return session()->get("OAuthLogin_WeChat");
- }
-
- // 获取uuid
- private function getUuid(){
- $session_id = $this->getSessionId();
- if(cache()->has("oauth.wechat.uuid.".$session_id)){
- return cache()->get("oauth.wechat.uuid.".$session_id);
- }
- $data = http("raw")->get("https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appid."&redirect_uri=".$this->redirect_uri."&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect")->getBody();
- $uuid = Str::after((string)$data,’"https://long.open.weixin.qq.com/connect/l/qrconnect?uuid=’);
- $uuid = Str::before($uuid,’"’);
- cache()->set("oauth.wechat.uuid.".$session_id,$uuid,300);
- return cache()->get("oauth.wechat.uuid.".$session_id);
- }
-
- // get open id
- public function getWxCode(){
- $data = http("raw")->get("https://long.open.weixin.qq.com/connect/l/qrconnect?uuid=".$this->getUuid())->getBody();
- $wx_errcode = Str::after((string)$data,’window.wx_errcode=’);
- $wx_errcode = Str::before($wx_errcode,";");
- if($wx_errcode!=405){
- return null;
- }
- $wx_code = Str::after((string)$data,"window.wx_code=’");
- $wx_code = Str::before($wx_code,"’");
- return $wx_code;
- }
-
- // 获取SMsg
- public function getSMsg(){
- if(!cache()->has("oauth.wechat.wxcode.".$this->getWxCode())){
- $config = [
- "headers" =>[
- "authority" => "apps.game.qq.com",
- "referer" => "https://lol.qq.com/"
- ]
- ];
- $data = http(‘array’)->get("https://apps.game.qq.com/ams/ame/codeToOpenId.php?appid=".$this->appid."&wxcode=".$this->getWxCode()."&sServiceType=lol&wxcodedomain=lol.qq.com&acctype=wx",$config);
- cache()->set("oauth.wechat.wxcode.".$this->getWxCode(),$data,600);
- }
- return cache()->get("oauth.wechat.wxcode.".$this->getWxCode());
- }
-
- private function getOpenId(){
- $data = $this->getSMsg();
- if($data[‘iRet’]!=0){
- return null;
- }
- return json_decode($data[‘sMsg’],true)[‘openid’];
- }
-
- public function get_access_token(){
- $data = $this->getSMsg();
- if($data[‘iRet’]!=0){
- return null;
- }
- return json_decode($data[‘sMsg’],true)[‘access_token’];
- }
-
- #[PostMapping(path:"get_user_info")]
- public function get_user_info(){
- $data = http("raw")->get("https://api.weixin.qq.com/sns/userinfo?access_token=".$this->get_access_token()."&openid=".$this->getOpenId())->getBody();
- return json_decode(stripcslashes($data),true);
- }
-
- #[PostMapping(path:"status")]
- public function status(){
- if(!$this->getUuid()){
- return Json_Api(403,false,[‘msg’ => ‘uuid不存在’]);
- }
- $data = http("raw")->get("https://long.open.weixin.qq.com/connect/l/qrconnect?uuid=".$this->getUuid())->getBody();
- $wx_errcode = Str::after((string)$data,’window.wx_errcode=’);
- $wx_errcode = Str::before($wx_errcode,";");
- $msg = match (@(string)$wx_errcode){
- ‘404’ => ‘扫码成功’,
- ‘405’ => ‘确认登录’,
- ‘403’ => ‘取消登陆’,
- ‘402’ => ‘超时或获取失败,需要重新获取二维码’,
- ‘408’ => ‘暂未扫码’
- };
- if($wx_errcode==405){
- return Json_Api(200,true,[‘msg’ => $msg]);
- }
-
- return Json_Api((int)$wx_errcode,false,[‘msg’ => $msg]);
- }
-
- #[PostMapping(path:"remove_cookie")]
- public function remove_cookie(){
-
- // get session id
- $session_id = $this->getSessionId();
- // remove uuid
- cache()->delete("oauth.wechat.uuid.".$session_id);
- return Json_Api(200,true,[‘msg’ => ‘cookie清理完毕’]);
- }
-
- #[PostMapping(path:"bind")]
- public function bind(){
- // 绑定
- if($this->get_wechat_id()===false){
- // 没读到openid
- return Json_Api(403,false,[‘msg’ => ‘未获取到open_id’]);
- }
- if(auth()->check()){
- // 开始绑定
- return $this->logind_bind($this->get_wechat_id());
- }
- if(OauthLoginWechatBind::query()->where([‘wechat_id’=>$this->get_wechat_id()])->exists()){
- $user_id = OauthLoginWechatBind::query()->where([‘wechat_id’=>$this->get_wechat_id()])->first()->user_id;
- $user_data = User::query()->where(‘id’,$user_id)->first();
- $this->SignIn($user_data);
- $this->remove_cookie();
- return Json_Api(302,true,[‘msg’ => ‘登陆成功!’]);
- }
- return Json_Api(301,true,[‘msg’ => ‘未登录,注册或登录后自动绑定’]);
- }
-
- private function SignIn($user){
- // 数据库里的密码
- $token = Str::random(17);
- session()->set(‘auth’, $token);
- (new UserAuth())->create($user->id,$token);
- session()->set("auth_data",User::query()->where("id",auth()->id())->with("Class")->first());
- session()->set("auth_data_class",UserClass::query()->where("id",auth()->data()->class_id)->first());
- session()->set("auth_data_options",UsersOption::query()->where("id",auth()->data()->options_id)->first());
- EventDispatcher()->dispatch(new AfterLogin($user));
- return true;
- }
-
- private function logind_bind($wechat_id){
- if(OauthLoginWechatBind::query()->where("wechat_id",$wechat_id)->exists()){
- return Json_Api(403,false,[‘msg’ => ‘绑定失败,此微信已被别的用户绑定!’]);
- }
- OauthLoginWechatBind::query()->where("wechat_id",$wechat_id)->create([
- ‘user_id’ => auth()->id(),
- ‘wechat_id’ => $wechat_id
- ]);
- $this->remove_cookie();
- return Json_Api(200,true,[‘msg’ => ‘绑定成功!’]);
- }
-
- // 读oauth_login_wechat表数据:id
- private function get_wechat_id():bool|int{
- if($this->getOpenId()){
- if(OauthLoginWechat::query()->where("open_id",$this->getOpenId())->exists()){
- return (int)OauthLoginWechat::query()->where("open_id",$this->getOpenId())->first()->id;
- }
- $data = http("raw")->get("https://api.weixin.qq.com/sns/userinfo?access_token=".$this->get_access_token()."&openid=".$this->getOpenId())->getBody();
- OauthLoginWechat::query()->create([
- ‘open_id’ => $this->getOpenId(),
- ‘data’ => $data
- ]);
- return (int)OauthLoginWechat::query()->where("open_id",$this->getOpenId())->first()->id;
-
- }
- return false;
- }
- }
复制代码
|