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

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

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

官方文檔

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

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

前端點(diǎn)擊一個(gè)按鈕,請(qǐng)求后端接口條微信作用域

后端php代碼如下:

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

最終跳轉(zhuǎn)頁(yè)面如下:

2:內(nèi)嵌js,在當(dāng)前頁(yè)面顯示登錄二維碼

第一種操作實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,但是個(gè)人感覺(jué)用戶體驗(yàn)稍微差一點(diǎn)。

最好還是在當(dāng)前頁(yè)面就是顯示微信登錄的二維碼,直接掃描就好。

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

(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: "回調(diào)地址",//這里的回調(diào)地址可以寫(xiě)后端的接口,也可以寫(xiě)前端的頁(yè)面地址,我這里寫(xiě)的是前端的頁(yè)面地址
        state: "",
        style: "black",
        href: "", //https://某個(gè)域名下的css文件
     });
});
// 將方法掛載到window主鏈上
        // 從iframe中獲取到回調(diào)函數(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)
                        {// 跳學(xué)生首頁(yè)
                            self.$router.push("/manager/student/reportList");
                        }
                        else if(result.data.type == 1 || result.data.type == 9)
                        {// 跳選擇身份頁(yè)
                            self.$router.push("/manager/teacher/index");
                        }
                    }
                })
                .catch(err => {});//*/
        };
 
</script>

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

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

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

回調(diào)地址:

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>請(qǐng)?jiān)谖⑿胖悬c(diǎn)擊確認(rèn)即可登錄</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://www.mools.net/lims/web/common/common.js"></script>
    <script>
        if (parent) {
                     // 將從url中解析出來(lái)的參數(shù)傳到iframe的父級(jí)(調(diào)用父級(jí)方法)
            parent.jumpTop(ml.get("code"));
        }
    </script>
</body>
</html>

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

    /**
     * @name: 微信掃碼登陸回調(diào)(不跳頁(yè)二維碼)
     * @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 == '')
            {
                //通過(guò)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);
            }
            // 對(duì)象轉(zhuǎn)數(shù)組
            $resultArray = json_decode($jsonResult, true);
            $access_token = $resultArray["access_token"];
            $openid = $resultArray["openid"];
            //通過(guò)access_token + openid 獲得用戶所有信息,結(jié)果全部存儲(chǔ)在$infoArray里,后面再寫(xiě)自己的代碼邏輯
            $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;
            $infoResult = file_get_contents($infoUrl);
            $infoArray = json_decode($infoResult, true);
            // 沒(méi)有unionid ,跳官網(wǎng)
            if (!isset($infoArray['unionid'])) 
            {
                // echo "<script >alert('登錄失敗,用戶信息錯(cuò)誤!')</script>";die;
                $result['code'] = -1;
                $result['msg'] = '登錄失敗,用戶信息錯(cuò)誤!';
                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;
 
                // 教師跳頁(yè)
                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) 
                { // 學(xué)生跳頁(yè)
                    // 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ú)效用戶跳至官網(wǎng)
                    // echo "<script> top.location.; </script>";die;
                    $result['code'] =-2;
                    $result['msg'] = '用戶身份有誤!';
                    return $result;
                }
            } 
            else 
            {
                // echo "<script >alert('登錄失敗,用戶信息錯(cuò)誤~')</script>";die;
                $result['code'] = -3;
                $result['msg'] = '用戶身份有誤!';
                return $result;
            }
        } 
        else 
        {
            // echo "<script >alert('登錄失敗,請(qǐng)重試!')</script>";die;
            $result['code'] = -4;
            $result['msg'] = '登錄失敗,請(qǐng)重試!';
            return $result;
        }
    }

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

相關(guān)文章

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

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

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

    PHP 引用是個(gè)壞習(xí)慣

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

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

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

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

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

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

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

    PHP設(shè)計(jì)模式中工廠模式深入詳解

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

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

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

    自己前幾天寫(xiě)的無(wú)限分類類

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

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

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

    CodeIgniter圖像處理類的深入解析

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

最新評(píng)論

神农架林区| 西吉县| 宜丰县| 梅河口市| 凉城县| 新和县| 德化县| 新余市| 岳阳县| 永吉县| 肃南| 鄂伦春自治旗| 昂仁县| 大洼县| 壶关县| 澎湖县| 彭阳县| 青海省| 克什克腾旗| 仪陇县| 珠海市| 赤峰市| 城固县| 胶州市| 茂名市| 南丰县| 宜君县| 吴旗县| 商河县| 渭南市| 磐石市| 华坪县| 高阳县| 深水埗区| 长武县| 乐清市| 荥经县| 安丘市| 若尔盖县| 收藏| 铜陵市|