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

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

二、選擇SDK配置,在plus.oauuth·登陸鑑權中,勾選□ 微信登陸,配置好appidappsecret值(appid/appsecret能夠在微信開發者平臺建立應用獲取)vue

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

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+參考文檔json

相關文章
相關標籤/搜索