apicloud 第三方登录授权、微信、扣扣、微博登录授权

授权登录、接入第三方的配置

例如:微信的登录授权。

首先在模块里面添加 wx 这个模块,然后在项目的配置文件里面进行配置。

配置的时候要现在微信开放平台

https://open.weixin.qq.com/

的移动应用里面,创建这个应用,等审核通过之后就会拿到相应的 appleid 以及 key。

具体配置请参考

https://docs.apicloud.com/Client-API/Open-SDK/wx

因为第三方的模块都是原生的,所以这个时候想要真机测试是不行的。必须得先编译以后才行。

要生成云编译的测试证书,同时要将测试的手机添加到项目中具体方法:

个人或公司账号生成的App Store类型mobileprovision证书,应用在没有发布到App Store之前只能在越狱设备上安装,若要在非越狱手机上面安装,则需要把设备udid添加到测试设备列表Devices里,并且生成Ad Hoc类型mobileprovision证书。
https://docs.apicloud.com/Dev-Guide/iOS-License-Application-Guidance#3

所有配置结束后编译之前一定要先提交一次云同步,因为配置里面的内容只有云同步之后才会编译生效,否则是不会生效的。

 有很多时候编译玩我们能调起来微信,但是在微信里面的操作没有任何反应,跳转不回我们的app。这是因为苹果的限制原因,具体:

https://docs.apicloud.com/Dev-Guide/app-config-manual#14-3

这个时候需要在配置文件中添加:

    <preference name="querySchemes" value="weixin,wechat" />

添加完之后还是一样要先云同步然后在编译。

云编译方法:

方法:编辑器的该项目右键,然后选择 云端自定义Loader 编译。编译完成之后,下载下来(例如ios),用微信扫码就能下载,之后打开,会有配置的小点,点开填写相应的配置,这个时候就是自定义Loader的调试了。

授权登录流程

 

具体微信登录授权代码

 

/**
     * 微信登录授权 (key id等在配置文件配置ok)
     * 1、判断是否安装
     * 2、获取 code
     * 3、获取 accessToken、openId
     * 4、获取wx头像,昵称
     * 5、绑定信息到后台
     */
    var wx, code;
    function wxLogin(){
        wx = api.require('wx');
        wx.isInstalled(function(ret, err) {
            if (!ret.installed) {
                api.toast({      
                    msg: '当前设备未安装微信客户端',
                    duration: '1300',
                    localtion: 'middle'    
                });
                return;
            } else {
                getWxCode();
            }
        });
    }
    function getWxCode(){
        wx.auth(function(ret, err) {
            if (ret.status) {
                code = ret.code;
                getWxAccessToken();
            } else {
                alert(JSON.stringify(err))
            }
        });
    }
    function getWxAccessToken(){
        wx.getToken({
            code: code
        }, function(ret, err) {
            if (ret.status) {
                geWxtUserInfor(ret);
            }
        });
    }
    function geWxtUserInfor(ret){
        api.showProgress({
           style: 'default',
           animationType: 'fade',
           title: '授权成功',
           text: '绑定处理中...',
           modal: true
       });
        wx.getUserInfo({
            accessToken: ret.accessToken,
            openId: ret.openId
        }, function(rets, err) {
            if (rets.status) {
                bindWxNews(rets)
            }
        });
    }
    function bindWxNews(rets){
        api.ajax({
            url: 'http://q7ns64.natappfree.cc/weixinLogin?openid=' +rets.openid+'&nickname=' +rets.nickname+'&headimgurl=' +rets.headimgurl,
            timeout: 10,
            dataType: 'json',
            method: 'get'
        }, function(reta, err) {
            api.hideProgress();
            if (reta.memberId){
                $api.setStorage('member_id', reta.memberId);
                $api.setStorage('openid', rets.openid);
                $api.setStorage('nickname', rets.nickname);
                $api.setStorage('headimgurl', rets.headimgurl);
            }
            $api.setStorage('isLogin', true);
            api.setPrefs({
                key: 'loginStatus',
                value: 'loginSuccess'
            });
            openIndex();

            /*** 登录异常 ***/
            if (err) {
                api.toast({      
                    msg: '绑定授权失败',
                    duration: '1300',
                    localtion: 'middle'    
                });
                return;
            }
        });
    }

 具体QQ登录授权代码

/**
    * qq 登录授权
    */
    var openIdQQ = '';
    function qqInstall(){
        qq = api.require('QQPlus');
        qq.installed(function(ret, err) {
            if (ret.status) {
                qqLogin();
            } else {
                api.toast({      
                    msg: '当前设备未安装QQ客户端',
                    duration: '1300',
                    localtion: 'middle'    
                });
                return;
            }
        });
    }

    function qqLogin(){
        qq = api.require('QQPlus');
        qq.login(function(ret, err) {
            openIdQQ = ret.openId;
            var accessToken = ret.accessToken;
            if(ret.status){
                getQQinfor();
            }
        });
    }
    function getQQinfor(){
        qq = api.require('QQPlus');
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '授权成功',
            text: '绑定处理中...',
            modal: true
        });
        qq.getUserInfo(function(rets, err) {
            if (rets.status) {
                bindQQnews(rets)
            } else {
                api.toast({      
                    msg: err.msg,
                    duration: '1300',
                    localtion: 'middle'    
                });
                api.hideProgress();
            }
        });
    }

 

posted @ 2018-05-14 18:06  haonanElva  阅读(1376)  评论(0编辑  收藏  举报