研究COCOSCREATOR 原生平台微信登录接入流程
准备工作(APP_ID等参数 官网上教程很全)
需要注意包名及签名是否正确
creator 连接 Android 代码
jsb.reflection.callStaticMethod(“org/cocos2dx/javascript/AppActivity”, “StartWeiXin”, “()V”);
creator 接收 Android 回调
window.onGameLoginRsp =
function(code)
{
let request = loader.getXMLHttpRequest();
var url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + PublicDefine.AppIDWX + "&secret=" + PublicDefine.AppSecretWX + "&code=" + code + "&grant_type=authorization_code";
request.open("GET" , url,true) ;
request.onreadystatechange = function()
{
if( request. readyState == 4 && (request. status >= 200 && request. status < 300))
{
let response = request.responseText;
console.log (response);
var responseJson = JSON. parse (response)
if(responseJson.access_token)
{
var requestInfo = loader.getXMLHttpRequest();
var url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + responseJson.access_token + "&openid=" + responseJson.openid;
SceneManager.getInstance().showWaitLayer();
console.log(url);
requestInfo. open("GET" , url,true) ;
requestInfo . onreadystatechange = function()
{
if( requestInfo. readyState == 4 && (requestInfo. status >= 200 && requestInfo. status < 300))
{
var response = requestInfo.responseText;
cc.log("Pando: sendHttpGet second ");
console.log (response);
var responseJson = JSON. parse (response)
if(responseJson){ }
}
}
requestInfo.send( );
}
}
}
request.send();
};
Android studio 修改
build.gradle 文件 dependencies 引用微信Android sdk
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
implementation fileTree(dir: "F:/CocosCreator/2.2.2/resources/cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])
implementation project(':libcocos2dx')
api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
微信API
获取IWXAPI的实例 并将应用的appId注册到微信
api = WXAPIFactory.createWXAPI(this, ConstInfo.APP_ID, true);
api.registerApp(ConstInfo.APP_ID);
public static void StartWeiXin() {
System.out.println("Pando: StartWeiXin");
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "order";
api.sendReq(req);
}
WXEntryActivity 建立
注意 WXEntryActivity 路径问题 你的包名.wxapi.WXEntryActivity 回调cocos方法可能出现参数问题 字符串一定补充好 \" 否则会报错
@Override
public void onResp(BaseResp resp) {
System.out.println("Pando:----resp type " + resp.getType() + ", resp code " + resp.errCode);
if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
final SendAuth.Resp authResp = (SendAuth.Resp)resp;
System.out.println("Pando: StartWeiXin success");
Cocos2dxHelper.runOnGLThread(new Runnable() {
@Override
public void run() {
String funCocos = "onGameLoginRsp(\"" + authResp.code + "\")";
System.out.println(funCocos);
Cocos2dxJavascriptJavaBridge.evalString(funCocos);
}
});
} else {
System.out.println("Pando: StartWeiXin fail");
}
}
finish();
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="你的包名"
android:installLocation="auto">
<uses-feature android:glEsVersion="0x00020000" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher">
<meta-data android:name="android.app.lib_name"
android:value="cocos2djs" />
<meta-data
android:name="android.max_aspect"
android:value="2.4" />
<activity
android:name="org.cocos2dx.javascript.AppActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="你的包名.wxapi.WXEntryActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true"
android:launchMode="singleTask">
</activity>
</application>
</manifest>
注意
WXEntryActivity 路径如果放在 org.cocos2dx.javascript.wxapi 下可能导致微信回调接受不到 (我出了这问题 纠结半天)
PROP_TARGET_SDK_VERSION 版本为30 android 11手机安装失败.
|