最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP實現(xiàn)微信掃碼登錄功能的兩種方式總結

 更新時間:2022年08月23日 10:15:12   作者:camellia  
這篇文章主要為大家介紹了利用PHP實現(xiàn)微信掃碼登錄功能的兩種方式,文中的示例代碼講解詳細,對我們學習有一定借鑒價值,需要的可以參考一下

官方文檔

微信掃碼登錄目前有兩種方式:

1:在微信作用域執(zhí)行 ,就是條一個新頁面

前端點擊一個按鈕,請求后端接口條微信作用域

后端php代碼如下:

$redirect_uri="http://你的微信開放平臺綁定域名下處理掃碼事件的方法";
$redirect_uri=urlencode($redirect_uri);//該回調需要url編碼
$appID="你的appid";
$scope="snsapi_login";//寫死,微信暫時只支持這個值
//準備向微信發(fā)請求
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $appID."&redirect_uri=".$redirect_uri
."&response_type=code&scope=".$scope."&state=STATE#wechat_redirect";
//請求返回的結果(實際上是個html的字符串)
$result = file_get_contents($url);
//替換圖片的src才能顯示二維碼
$result = str_replace("/connect/qrcode/", "https://open.weixin.qq.com/connect/qrcode/", $result);
return $result; //返回頁面

最終跳轉頁面如下:

2:內嵌js,在當前頁面顯示登錄二維碼

第一種操作實現(xiàn)起來比較簡單,但是個人感覺用戶體驗稍微差一點。

最好還是在當前頁面就是顯示微信登錄的二維碼,直接掃描就好。

微信也為我們提供了這種方式。

(1):引入js

<script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/jquery.min.js"></script>
<script  src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>

(2):html部分

<div id="wx_login_container"></div>

(3):js示例

<script>
 
$(document).ready(function()
{
    var obj = new WxLogin({
        self_redirect: true,
        id:"wx_login_container",
        appid: "appid",
        scope: "snsapi_login",
        redirect_uri: "回調地址",//這里的回調地址可以寫后端的接口,也可以寫前端的頁面地址,我這里寫的是前端的頁面地址
        state: "",
        style: "black",
        href: "", //https://某個域名下的css文件
     });
});
// 將方法掛載到window主鏈上
        // 從iframe中獲取到回調函數(shù)中獲取的微信返回的code
        window.jumpTop = function(code){
            console.log(code);
            var data = {
                code: code
            };
            console.log(data);
            self.axios
                .post("/index.php/xxx/wxlogin_notice", data)
                .then(result => {
                    if(result.data.code > 0)
                    {
                        Message.success(result.data.msg);
                        if(result.data.type == 0)
                        {// 跳學生首頁
                            self.$router.push("/manager/student/reportList");
                        }
                        else if(result.data.type == 1 || result.data.type == 9)
                        {// 跳選擇身份頁
                            self.$router.push("/manager/teacher/index");
                        }
                    }
                })
                .catch(err => {});//*/
        };
 
</script>

注意其中href里指向的css文件必須放在https協(xié)議下才能引用的到,大體上不需改變默認樣式,浪費腦細胞,可以針對div 來改變二維碼的大小和位置,里邊是內嵌一個iframe

整理的實現(xiàn)邏輯如下圖所示:

微信的二維碼嵌入在一個iframe中,微信掃碼成功,手機點擊確定后,回調地址接收到微信給我們的參數(shù)code,這里微信使用的是get傳參,因此我們只需要在回調地址的頁面中獲取當前頁面的URL中的code參數(shù)傳給上一層(父級),上一層接收到code參數(shù)再請求后端接口執(zhí)行登錄邏輯即可。

回調地址:

https://www.xxx.xxx/lims/web/wechat/login.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <link rel="stylesheet"  rel="external nofollow" >
    <link  rel="external nofollow"  rel="Shortcut Icon">
</head>
<body style="color: rgb(55, 55, 55);">
    <div style="">
        <div class="main impowerBox">
            <div class="loginPanel normalPanel">
                <div>微信登錄</div>
                <div class="waiting panelContent">
                    <div>
                        <img class="qrcode lightBorder" src="./img.jpg ">
                    </div>
                    <div>
                        <div class="status status_succ js_status js_wx_after_scan" style="display: block;" id="wx_after_scan">
                            <i class="status_icon icon38_msg succ"></i>
                            <div>
                                <h4>掃描成功</h4>
                                <p>請在微信中點擊確認即可登錄</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://www.mools.net/lims/web/common/common.js"></script>
    <script>
        if (parent) {
                     // 將從url中解析出來的參數(shù)傳到iframe的父級(調用父級方法)
            parent.jumpTop(ml.get("code"));
        }
    </script>
</body>
</html>

PHP回調代碼:(上邊的兩種掃碼方式都可用)

    /**
     * @name: 微信掃碼登陸回調(不跳頁二維碼)
     * @author: camellia
     * @date: 2020-12-25 11:47:17
     */
    public function wxlogin_notice(Request $request)
    {
        $code = $request->input("code");
        if (!empty($code)) 
        {
            $jsonResult = '';
            if($jsonResult == '')
            {
                //通過code獲得 access_token + openid
                $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $code . "&grant_type=authorization_code";
                $jsonResult = file_get_contents($url);
            }
            // 對象轉數(shù)組
            $resultArray = json_decode($jsonResult, true);
            $access_token = $resultArray["access_token"];
            $openid = $resultArray["openid"];
            //通過access_token + openid 獲得用戶所有信息,結果全部存儲在$infoArray里,后面再寫自己的代碼邏輯
            $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;
            $infoResult = file_get_contents($infoUrl);
            $infoArray = json_decode($infoResult, true);
            // 沒有unionid ,跳官網(wǎng)
            if (!isset($infoArray['unionid'])) 
            {
                // echo "<script >alert('登錄失敗,用戶信息錯誤!')</script>";die;
                $result['code'] = -1;
                $result['msg'] = '登錄失敗,用戶信息錯誤!';
                return $result;
            }
            // 獲取unionid
            $unionid = $infoArray['unionid'];
            $userinfo = DB::table('user')->where('unionid', $unionid)->first();
            $userinfObj = json_decode(json_encode($userinfo), true);
            if ($userinfo) 
            {
                // 存session
                $request->session()->put('userinfo', $userinfObj);
 
                // $session = $this->getSession($request);
                // var_dump($session);die;
 
                // 教師跳頁
                if (($userinfo->type == 9) || ($userinfo->type == 1 && $userinfo->islogin == 9)) 
                {
                    // echo "<script> top.location.; </script>";die;
                    $result['code'] = 1;
                    $result['msg'] = '登錄成功';
                    $result['type'] = $userinfo->type;
                    return $result;
                } 
                else if ($userinfo->type == 1 && $userinfo->islogin >= 3) 
                { // 學生跳頁
                    // echo "<script> top.location.; </script>";die;
                    $result['code'] = 2;
                    $result['msg'] = '登錄成功';
                    $result['type'] = $userinfo->type;
                    return $result;
                }
                else if($userinfo->type == 0)
                {
                    // echo "<script> top.location.; </script>";die;
                    $result['code'] = 3;
                    $result['msg'] = '登錄成功';
                    $result['type'] = $userinfo->type;
                    return $result;
                }
                else 
                { // 無效用戶跳至官網(wǎng)
                    // echo "<script> top.location.; </script>";die;
                    $result['code'] =-2;
                    $result['msg'] = '用戶身份有誤!';
                    return $result;
                }
            } 
            else 
            {
                // echo "<script >alert('登錄失敗,用戶信息錯誤~')</script>";die;
                $result['code'] = -3;
                $result['msg'] = '用戶身份有誤!';
                return $result;
            }
        } 
        else 
        {
            // echo "<script >alert('登錄失敗,請重試!')</script>";die;
            $result['code'] = -4;
            $result['msg'] = '登錄失敗,請重試!';
            return $result;
        }
    }

到此這篇關于PHP實現(xiàn)微信掃碼登錄功能的兩種方式總結的文章就介紹到這了,更多相關PHP微信掃碼登錄內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • php實現(xiàn)多城市切換特效

    php實現(xiàn)多城市切換特效

    本文給大家主要分享的是在PHP中如何獲取用戶IP地址、PHP根據(jù)IP判斷用戶所在城市以及PHP根據(jù)IP實現(xiàn)城市切換或跳轉的問題。
    2015-08-08
  • PHP 引用是個壞習慣

    PHP 引用是個壞習慣

    在寫PHP 程序的時候,很多人在傳遞參數(shù)的時候,喜歡用一個引用。特別是在一個數(shù)組非常的大的時候,更是喜歡加。
    2010-03-03
  • phpmailer在服務器上不能正常發(fā)送郵件的解決辦法

    phpmailer在服務器上不能正常發(fā)送郵件的解決辦法

    這篇文章主要介紹了phpmailer在服務器上不能正常發(fā)送郵件的解決辦法,本文的原因是服務器的安全設置造成,服務器中屏蔽fsockopen函數(shù)的使用權限,所以導致發(fā)送失敗,需要的朋友可以參考下
    2014-07-07
  • php使用function_exists判斷函數(shù)可用的方法

    php使用function_exists判斷函數(shù)可用的方法

    這篇文章主要介紹了php使用function_exists判斷函數(shù)可用的方法,通過一個圖像處理函數(shù)中使用function_exists函數(shù)判斷并輸出來實現(xiàn)函數(shù)存在判斷與流程靈活控制的功能,具有很好的借鑒價值,需要的朋友可以參考下
    2014-11-11
  • php實現(xiàn)session共享的實例方法

    php實現(xiàn)session共享的實例方法

    在本篇文章里小編給大家整理的是關于php如何實現(xiàn)session共享知識點內容,有需要的朋友們跟著學習參考下。
    2019-09-09
  • PHP設計模式中工廠模式深入詳解

    PHP設計模式中工廠模式深入詳解

    工廠模式,是一種實例化對象的方式,只要輸入需要實例化對象的名字,就可以通過工廠對象的相應工廠函數(shù)來制造你需要的對象,工廠模式的最主要作用就是對象創(chuàng)建的封裝、簡化創(chuàng)建對象操作
    2022-11-11
  • PHP超全局變量實現(xiàn)原理及代碼解析

    PHP超全局變量實現(xiàn)原理及代碼解析

    這篇文章主要介紹了PHP超全局變量實現(xiàn)原理及代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • 自己前幾天寫的無限分類類

    自己前幾天寫的無限分類類

    自己前幾天寫的無限分類類...
    2007-02-02
  • PHP+JavaScript實現(xiàn)無刷新上傳圖片

    PHP+JavaScript實現(xiàn)無刷新上傳圖片

    本文主要介紹了PHP+JavaScript實現(xiàn)無刷新上傳圖片的方法,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • CodeIgniter圖像處理類的深入解析

    CodeIgniter圖像處理類的深入解析

    本篇文章是對CodeIgniter的圖像處理類進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06

最新評論

准格尔旗| 虹口区| 政和县| 中宁县| 思南县| 甘谷县| 罗田县| 昔阳县| 湟中县| 邮箱| 水城县| 泸州市| 蓬莱市| 海伦市| 尖扎县| 龙州县| 梅河口市| 教育| 闽清县| 盖州市| 伊春市| 家居| 丰顺县| 汉川市| 瑞安市| 德安县| 鞍山市| 三亚市| 孟州市| 丹凤县| 南澳县| 甘南县| 宝坻区| 安丘市| 新蔡县| 台东县| 太保市| 古田县| 逊克县| 蚌埠市| 彰武县|