基於h5+的微信登錄,hbuilder打包


1、打開app項目的manifest.json的文件,選擇模塊權限配置,將OAuth(登錄鑒權)模塊添加至已選模塊

2、選擇SDK配置,在plus.oauuth·登錄鑒權中,勾選□ 微信登錄,配置好appidappsecret值(appid/appsecret可以在微信開發者平台創建應用獲取)

3、項目JS文件中的微信支付代碼如下(基於vue開發):

wxLoginFn() {
    let self = this;
    getService()

    // 微信授權登錄對象
    let aweixin = null;
    // 當前環境支持的所有授權登錄對象
    let auths = null;

    // 獲取登錄授權認證服務列表,單獨保存微信登錄授權對象
    function getService(){
        plus.oauth.getServices(function(services){
            // plus.nativeUI.alert("services:"+JSON.stringify(services));
            auths = services;
            authLogin()
        }, function(e){
            plus.nativeUI.alert("獲取登錄授權服務列表失敗,請稍后重試");
            // plus.nativeUI.alert("獲取登錄授權服務列表失敗:"+JSON.stringify(e));
        } );
    }

    // 獲取微信登錄授權對象后可進行登錄認證操作
    function authLogin(){
        for(let i = 0; i < auths.length; i++){
            if(auths[i].id == 'weixin'){
                aweixin = auths[i];
                break;
            }
        }
        if(!aweixin){
            plus.nativeUI.alert("當前環境不支持微信登錄");
            return;
        }
        if(!aweixin.authResult){
            aweixin.login(function(e){
                // plus.nativeUI.alert("登錄認證成功!"+JSON.stringify(e));
                authUserInfo()
            }, function(e){
                // plus.nativeUI.alert("登錄認證失敗: "+JSON.stringify(e));
            } );
        }else{
            authUserInfo()
            console.log("已經登錄認證!");
        }
    }

    // 獲取微信登錄授權對象后獲取用戶信息操作
    function authUserInfo(){
        Toast.loading({
            mask: true,
            message: '微信登錄中...'
        });

        if(!aweixin){
            Toast.clear();
            plus.nativeUI.alert("當前環境不支持微信登錄");
            return;
        }
        if(aweixin.authResult){
            aweixin.getUserInfo( function(e){
          //登錄成功處理 Toast.clear();
// plus.nativeUI.alert("獲取用戶信息成功:"+JSON.stringify(aweixin.userInfo)); let wxUserInfo = aweixin.userInfo; Storage.set('wxUserInfo', JSON.stringify(wxUserInfo)); authLoginOut(); //注銷登錄防止切換賬號獲取到舊信息 }, function(e){ console.log("獲取用戶信息失敗: "+JSON.stringify(e)); } ); }else{ Toast.clear(); plus.nativeUI.alert("未登錄認證!"); } } // 注銷登錄認證 function authLoginOut(){ if(!aweixin){ plus.nativeUI.alert("當前環境不支持微信登錄"); return; } aweixin.logout(function(e){ // plus.nativeUI.alert("注銷登錄認證成功!"+JSON.stringify(e)); }, function(e){ console.log("注銷登錄認證失敗: "+JSON.stringify(e)); }); } }

ps:HTML5+參考文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM