當前位置:首頁 > IT技術(shù) > 微信平臺 > 正文

apiCloud授權(quán)綁定第三方賬號,微信、QQ、微博。
2021-08-05 17:14:57

apiCloud授權(quán)綁定第三方賬號,微信、QQ、微博。_微信

1.檢測軟件是否安裝

2.授權(quán)獲取code

3.獲取token,openid等

4.獲取頭像昵稱

var wx,qq,weibo;
var loginParam={};
apiready = function () {
    fix_status_bar();
    var user = $api.getStorage('user');
    var customerId = user.customer_id;
    var memberId   = user.member_id;

    // 獲取第三方綁定信息
    api.ajax({
        url: BASE_REQUEST_URL+'/Customer/GetExternal',
        method: 'post',
        data: {
            values: {
                customerId: customerId,
                memberId: memberId
            }
        }
    }, function(json, err) {
        if (json.result) {
            var interText = doT.template($("#info_tmpl").text());
            $("#info_area").html(interText(json.info));
        } else {
            var toast = new auiToast();
            toast.fail({
                title: json.msg,
                duration: 2000
            });

        }
    });

}


function initWxBind() {
    wx = api.require('wx');
    wx.isInstalled(function(ret, err) {
        if (!ret.installed) {
            alert('當前設(shè)備未安裝微信客戶端');
        } else {
            wx.auth(function(ret, err) {
                if (ret.status) {
                    wx.getToken({
                        code: ret.code
                    }, function(ret, err) {
                        if (ret.status) {
                            api.showProgress({
                                style: 'default',
                                animationType: 'fade',
                                title: '授權(quán)成功',
                                text: '綁定處理中...',
                                modal: true
                            });
                            var provideName="wx_" + BASE_APP_TYPE;
                            //臨時存儲openId,與accessToken后面綁定賬號時用到(綁定用完需要清理掉)
                            loginParam.provideName=provideName;
                            loginParam.openId=ret.openId;
                            loginParam.accessToken=ret.accessToken;
                            loginParam.refreshToken=ret.dynamicToken;
                            $api.setStorage('loginParam', loginParam);
                            //獲取wx頭像,昵稱
                            wx.getUserInfo({
                                accessToken: ret.accessToken,
                                openId: ret.openId
                            }, function(ret, err) {
                                if (ret.status) {
                                    // 綁定成功
                                    var loginParam=$api.getStorage('loginParam');
                                    loginParam.nickName=ret.nickname;
                                    loginParam.avatar=ret.headimgurl;
                                    $api.setStorage('loginParam', loginParam);
                                    // 實現(xiàn)綁定動作
                                    bindAccount(function (res) {
                                        if (res == true) {
                                            // 發(fā)送頁面刷新事件
                                            var toast = new auiToast();
                                            toast.success({
                                                title: "綁定成功",
                                                duration: 2000
                                            });
                                            setTimeout("location.reload()", 2000);
                                        }
                                    });
                                    api.hideProgress();
                                }
                            });

                        } else {
                            alert(JSON.stringify(err))
                        }
                    });
                } else {
                    var toast = new auiToast();
                    toast.fail({
                        title: "微信授權(quán)綁定失敗",
                        duration: 1500
                    });
                }
            });
        }
    });
}

function initQQBind() {
    qq = api.require('qq');
    qq.login(function(ret, err) {
        if (ret.status) {
            api.showProgress({
                style: 'default',
                animationType: 'fade',
                title: '授權(quán)成功',
                text: '綁定處理中...',
                modal: true
            });
            var provideName="qq_" + BASE_APP_TYPE;
            loginParam.provideName=provideName;
            loginParam.openId=ret.openId;
            loginParam.accessToken=ret.accessToken;
            $api.setStorage('loginParam', loginParam);
            //獲取qq頭像,昵稱
            qq.getUserInfo(function(ret, err) {
                if (ret.status) {
                    // 綁定成功
                    var loginParam=$api.getStorage('loginParam');
                    loginParam.nickName=ret.info.nickname;
                    loginParam.avatar=ret.info.figureurl_qq_2;
                    $api.setStorage('loginParam', loginParam);
                    //實現(xiàn)綁定動作
                    bindAccount(function (res) {
                        if (res == true) {
                            // 發(fā)送頁面刷新事件
                            var toast = new auiToast();
                            toast.success({
                                title: "綁定成功",
                                duration: 2000
                            });
                            setTimeout("location.reload()", 2000);
                        }
                    });
                    api.hideProgress();

                }
            });
            
        } else {
            var toast = new auiToast();
            toast.fail({
                title: "授權(quán)失敗",
                duration: 1500
            });
        }
    });
}


function initWbBind() {
    weibo = api.require('weibo');
    weibo.auth(function(ret, err) {
        //alert(JSON.stringify(ret));
        if (ret.status) {
            api.showProgress({
                style: 'default',
                animationType: 'fade',
                title: '授權(quán)成功',
                text: '綁定處理中...',
                modal: true
            });
            var provideName="wb_" + BASE_APP_TYPE;
            //臨時存儲openId,與accessToken后面綁定賬號時用到(綁定用完需要清理掉)
            loginParam.provideName=provideName;
            loginParam.openId=ret.userId;
            loginParam.accessToken=ret.token;
            $api.setStorage('loginParam', loginParam);
            //獲取微博頭像,昵稱
            weibo.getUserInfo(function(ret,err){
                if (ret.status) {
                    // 綁定成功
                    var loginParam=$api.getStorage('loginParam');
                    loginParam.nickName=ret.userInfo.screen_name;
                    loginParam.avatar=ret.userInfo.avatar_large;
                    $api.setStorage('loginParam', loginParam);
                    // 實現(xiàn)綁定動作
                    bindAccount(function (res) {
                        if (res == true) {
                            // 發(fā)送頁面刷新事件
                            var toast = new auiToast();
                            toast.success({
                                title: "綁定成功",
                                duration: 2000
                            });
                            setTimeout("location.reload()", 2000);
                        }
                    });
                    api.hideProgress();
                }
            });
            
        }else {
            var toast = new auiToast();
            toast.fail({
                title: "授權(quán)失敗",
                duration: 1500
            });
        }
    });
}



// 解除綁定
function removeAuth(externalId) {
    var user = $api.getStorage('user');
    var customerId = user.customer_id;

    // 獲取第三方綁定信息
    api.ajax({
        url: BASE_REQUEST_URL+'/Customer/RemoveAuthentication',
        method: 'post',
        data: {
            values: {
                customerId: customerId,
                externalId:externalId
            }
        }
    }, function(json, err) {
        if (json.result) {
            var toast = new auiToast();
            toast.success({
                title: '解綁成功',
                duration: 2000
            });
            setTimeout("location.reload();", 2000);
        } else {
            var toast = new auiToast();
            toast.fail({
                title: json.msg,
                duration: 2000
            });

        }
    });
    
}

function bindAccount(callback) {
    var loginParam = $api.getStorage('loginParam');
    var user       = $api.getStorage('user');
    api.ajax({
        url: BASE_REQUEST_URL + '/Customer/AssociateAccountWithExistCustomer',
        method: 'post',
        data: {
            values: {
                provideName: loginParam.provideName,
                openId: loginParam.openId,
                accessToken: loginParam.accessToken,
                refreshToken: loginParam.refreshToken,
                userName: user.login_name,
                nickName: loginParam.nickName,
                avatar: loginParam.avatar,
            }
        }
    }, function(ret, err) {
        if (ret.status == 1) {
            callback(true);
        }
        callback(false);
    });
}

function showAction() {
    var dialogBox = api.require('dialogBox');
    dialogBox.actionMenu ({
        tapClose: true, // 點擊關(guān)閉
        rect:{
            h: 150
        },
        texts:{
            cancel: '取消'
        },
        items:[
            {
                text: '綁定微信',
                icon: 'widget://image/share_wx.png'
            },
            {
                text: '綁定QQ',
                icon: 'widget://image/share_qq.png'
            },
            {
                text: '綁定微博',
                icon: 'widget://image/share_wb.png'
            },
        ],
        styles:{
            bg:'#FFF',
            column: 3,
            itemText: {
                color: '#000',
                size: 12,
                marginT:8
            },
            itemIcon:{
                size:50
            },
            cancel:{
                bg: 'fs://icon.png',
                color:'#000',
                h: 44 ,
                size: 14
            }
        }
    }, function(ret){
        if (ret.index == '0') { // 微信
            initWxBind();
            dialogBox.close();
        }

        if (ret.index == '1') { // QQ
            initQQBind();
            dialogBox.close();
        }

        if (ret.index == '2') { // 微博
            initWbBind();
            dialogBox.close();
        }

        if (ret.eventType == 'cancel') {
            dialogBox.close();
        }
    });
}
 
?

確保已經(jīng)有這些數(shù)據(jù)申請,申請要些時日。

<feature name="wx"> 
    <param name="urlScheme" value="wx65fbcf8b5a4765cc"/>  
    <param name="apiKey" value="wx65fbcf8b5a4765cc"/>  
    <param name="apiSecret" value="6f9e7a1cb328745de623263c18773368"/> 
</feature>  
<feature name="qq"> 
    <param name="urlScheme" value="tencent1105688283"/>  
    <param name="apiKey" value="1105688283"/> 
</feature>  
<feature name="weibo"> 
    <param name="urlScheme" value="wb3537030359"/>  
    <param name="apiKey" value="3537030359"/>  
    <param name="registUrl" value="http://www.diandodo.com"/> 
</feature>   
?
?
?
?
?

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務(wù)立即開通 >