cocos creator安卓接入默往登录

一、libs中放入mostone sdk

路径为 >> 游戏客户端\build-templates\jsb-default\frameworks\runtime-src\proj.android-studio\app\libs\mostone-open-sdk-1.5.jar

二、cocos java文件内放入App.java,进行初始化设置,调用默往的初始化接口:

路径为 >> 游戏客户端\build-templates\jsb-default\frameworks\runtime-src\proj.android-studio\app\src\org\cocos2dx\javascript\App.java
App.Java内容为

package org.cocos2dx.javascript;

import android.app.Application;

import com.mostone.open.sdk.MConfigure;

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        MConfigure.debug(true);
        MConfigure.init(this, Constants.MW_APPID);
    }
}

三、build.gradle中引入sdk包

compile files('libs/mostone-open-sdk-1.5.jar')

路径为 >> 游戏客户端\build-templates\jsb-default\frameworks\runtime-src\proj.android-studio\app\build.gradle

四、AppActivity.java里改动

//引包,
import android.app.Application;
import com.mostone.open.sdk.MConfigure;
import com.mostone.open.sdk.listener.MAuthListener;
import com.mostone.open.sdk.model.BeanMAuth;
import com.mostone.open.sdk.MAuthApi;
import com.mostone.open.sdk.model.BeanMResp;
//初始化
public class AppActivity extends Cocos2dxActivity{

    public static Application application;
    public static Context sContext;
    public static AppActivity app;
//....
    //==========================================默往方法开始=======================================
    //默往登陆
    public static void sendMoWangLogin()
    {
        //可以检测登陆是否已经安装
        new Thread(new Runnable() {

            public void run() {
                Looper.prepare();

                if (!MConfigure.isMOAppInstalled()) {
                    Toast.makeText(AppActivity.sContext, "登陆应用未安装,请先安装登陆应用", Toast.LENGTH_SHORT).show();
                    return;
                }
                System.out.println("默往登录");
                BeanMAuth.Req req = new BeanMAuth.Req();
                req.state = "none";
                Log.d(LOG_TAG,"默往登录");
                Toast.makeText(AppActivity.sContext, "默往登录",Toast.LENGTH_SHORT ).show();
                MAuthApi.get(app).sendAuth(req, new MAuthListener() {
                    @Override
                    public void onResult(BeanMAuth.Resp resp) {
                        Toast.makeText(AppActivity.sContext, "返回码:" + resp.resCode,Toast.LENGTH_SHORT ).show();
                        Log.d(LOG_TAG,"返回码:" + resp.resCode);
                        switch (resp.resCode) {
                            case BeanMResp.ResCode.RES_SEND_OK: {//授权成功
                                StringBuffer sb = new StringBuffer();
                                sb.append("\n授权码: " + resp.authCode);
                                sb.append("\n扩展参数: " + resp.state);
                                Log.d(LOG_TAG,"\n授权码:" + resp.authCode);
                                final String fnStr = "CallLoginFunc('onMoWangCode','"+resp.authCode+"')";
                                app.runOnGLThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Cocos2dxJavascriptJavaBridge.evalString(fnStr);
                                    }
                                });
                                Toast.makeText(AppActivity.sContext, "授权成功", Toast.LENGTH_LONG).show();
                                break;
                            }
                            case BeanMResp.ResCode.RES_SENT_FAILED: {
                                Toast.makeText(AppActivity.sContext, "授权失败", Toast.LENGTH_LONG).show();
                                break;
                            }
                            case BeanMResp.ResCode.RES_USER_CANCEL: {
                                Toast.makeText(AppActivity.sContext, "取消授权", Toast.LENGTH_LONG).show();
                                break;
                            }
                        }
                    }
                });
                Looper.loop();
            }
        }).start();
    }
//.......
}

五、Constans.java里加入默往AppID

//默往appid
public static final String MW_APPID = "AppID";

六、Define.js里加入默往AppID,AppSecret

//默往
MW_AppID= 'AppID';
MW_AppSecret= 'AppSecret';

七、ThirdParty.js里加入默往登录调用方法

//默往登录相关
function ThirdPartyMoWangLogin (){
    if (cc.sys.OS_IOS == cc.sys.os){
       jsb.reflection.callStaticMethod("AppController", "sendMoWangLogin");
    }else if (cc.sys.OS_ANDROID == cc.sys.os){
        jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendMoWangLogin", "()V");
    }
}

八、LoginSence.js接入默往登录点击事件及回调

点击事件调用

ThirdPartyMoWangLogin();

回调code为

    onMoWangCode: function (code) {
        console.log('js 默往: ' + code);

        //获取授权码
        var getTokenUrl = 'https://api.open.mostonetech.com/oauth2/access_token?';

        getTokenUrl += 'appKey=' + MW_APPKEY;
        getTokenUrl += '&appSecret=' + MW_SECRET;
        getTokenUrl += '&grant_type=authorization_code';
        getTokenUrl += '&code=' + code;
        console.log('url = ' + getTokenUrl);
        HttpUtils.getInstance().httpGets(getTokenUrl, function (data) {
            
            console.log('data = ' + data);
            var authorization = JSON.parse(data);
            console.log('authorization = ' + authorization);
            if (authorization.code == null) return this.ShowAlert('默往授权失败!');
            if (authorization.code == 400) return this.ShowAlert(authorization.err_msg);
            if (authorization.code == 200) {
                //获取信息
                var getInfoUrl = 'https://api.open.mostonetech.com/mw/userinfo?';
                getInfoUrl += 'accessToken=' + authorization.data.access_token;
                getInfoUrl += '&openId=' + authorization.data.open_id;
                console.log(getInfoUrl);
                HttpUtils.getInstance().httpGets(getInfoUrl, function (data) {
                    console.log('infodata = ' + data);
                    var infoData = JSON.parse(data);
                    if (infoData.code == null) return this.ShowAlert('默往授权失败!');
                    if (infoData.code == 400) return this.ShowAlert('无效的授权码!');
                    if (infoData.code == 200) {
                        var webUrl = PHP_URL + '/UserFunc.php?&GetMark=27';
                        webUrl += '&data=' + encodeURI(data);
                        console.log(webUrl);
                        HttpUtils.getInstance().httpGets(webUrl, function (data) {
                            console.log('onXianLiaoCode ' + data);
                            var Login = JSON.parse(data);
                            if (Login.errcode != null) return this.ShowAlert('ErrCode:' + Login.errcode);
                            this.LoginAccount(Login.Accounts, Login.LogonPass, PLATFORM_XL);
                        }.bind(this));

                    }

                }.bind(this));
            }
        }.bind(this));
    },

九、AndroidManifest.xml里引入

配置Android Manifest XML sdk中需要的Activity


App引入
   android:name="org.cocos2dx.javascript.App"
配置sdk中需要的Activity


你可能感兴趣的:(cocos creator安卓接入默往登录)