mirror of
https://github.com/zhigang1992/react-native-wechat.git
synced 2026-05-01 05:41:42 +08:00
150 lines
4.4 KiB
Java
150 lines
4.4 KiB
Java
package com.theweflex.react;
|
|
|
|
import android.content.Intent;
|
|
|
|
import com.facebook.react.bridge.Arguments;
|
|
import com.facebook.react.bridge.Callback;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
import com.facebook.react.bridge.ReactMethod;
|
|
import com.facebook.react.bridge.WritableMap;
|
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
import com.tencent.mm.sdk.modelbase.BaseReq;
|
|
import com.tencent.mm.sdk.modelbase.BaseResp;
|
|
import com.tencent.mm.sdk.modelmsg.SendAuth;
|
|
import com.tencent.mm.sdk.openapi.IWXAPI;
|
|
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
|
|
import com.tencent.mm.sdk.openapi.WXAPIFactory;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
/**
|
|
* Created by tdzl2_000 on 2015-10-10.
|
|
*/
|
|
public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEventHandler {
|
|
private String appId;
|
|
|
|
private IWXAPI api = null;
|
|
private final static String NOT_REGISTERED = "registerApp required.";
|
|
private final static String INVOKE_FAILED = "WeChat API invoke returns false.";
|
|
|
|
public WeChatModule(ReactApplicationContext context) {
|
|
super(context);
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "RCTWeChat";
|
|
}
|
|
|
|
private static ArrayList<WeChatModule> modules = new ArrayList<>();
|
|
|
|
@Override
|
|
public void initialize() {
|
|
super.initialize();
|
|
modules.add(this);
|
|
}
|
|
|
|
@Override
|
|
public void onCatalystInstanceDestroy() {
|
|
super.onCatalystInstanceDestroy();
|
|
if (api != null){
|
|
api = null;
|
|
}
|
|
modules.remove(this);
|
|
}
|
|
|
|
public static void handleIntent(Intent intent) {
|
|
for (WeChatModule mod : modules){
|
|
mod.api.handleIntent(intent, mod);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void registerApp(String appid, Callback callback) {
|
|
api = WXAPIFactory.createWXAPI(this.getReactApplicationContext().getBaseContext(), appid, true);
|
|
callback.invoke(api.registerApp(appid) ? null : INVOKE_FAILED);
|
|
}
|
|
|
|
@ReactMethod
|
|
public void isWXAppInstalled(Callback callback) {
|
|
if (api == null) {
|
|
callback.invoke(NOT_REGISTERED);
|
|
return;
|
|
}
|
|
callback.invoke(null, api.isWXAppInstalled());
|
|
}
|
|
|
|
@ReactMethod
|
|
public void isWXAppSupportApi(Callback callback) {
|
|
if (api == null) {
|
|
callback.invoke(NOT_REGISTERED);
|
|
return;
|
|
}
|
|
callback.invoke(null, api.isWXAppSupportAPI());
|
|
}
|
|
|
|
@ReactMethod
|
|
public void getApiVersion(Callback callback) {
|
|
if (api == null) {
|
|
callback.invoke(NOT_REGISTERED);
|
|
return;
|
|
}
|
|
callback.invoke(null, api.getWXAppSupportAPI());
|
|
}
|
|
|
|
@ReactMethod
|
|
public void openWXApp(Callback callback) {
|
|
if (api == null) {
|
|
callback.invoke(NOT_REGISTERED);
|
|
return;
|
|
}
|
|
callback.invoke(api.openWXApp() ? null : INVOKE_FAILED);
|
|
}
|
|
|
|
@ReactMethod
|
|
public void sendAuthRequest(String scope, String state, Callback callback) {
|
|
if (api == null) {
|
|
callback.invoke(NOT_REGISTERED);
|
|
return;
|
|
}
|
|
SendAuth.Req req = new SendAuth.Req();
|
|
req.scope = scope;
|
|
req.state = state;
|
|
callback.invoke(api.sendReq(req) ? null : INVOKE_FAILED);
|
|
}
|
|
|
|
// TODO: 实现sendRequest、sendSuccessResponse、sendErrorCommonResponse、sendErrorUserCancelResponse
|
|
|
|
@Override
|
|
public void onReq(BaseReq baseReq) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onResp(BaseResp baseResp) {
|
|
WritableMap map = Arguments.createMap();
|
|
map.putInt("errCode", baseResp.errCode);
|
|
map.putString("errStr", baseResp.errStr);
|
|
map.putString("openId", baseResp.openId);
|
|
map.putString("transaction", baseResp.transaction);
|
|
|
|
if (baseResp instanceof SendAuth.Resp) {
|
|
SendAuth.Resp resp = (SendAuth.Resp)(baseResp);
|
|
|
|
map.putString("type", "SendAuth.Resp");
|
|
map.putString("code", resp.code);
|
|
map.putString("state", resp.state);
|
|
map.putString("url", resp.url);
|
|
map.putString("lang", resp.lang);
|
|
map.putString("country", resp.country);
|
|
}
|
|
|
|
this.getReactApplicationContext()
|
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
.emit("WeChat_Resp", map);
|
|
}
|
|
|
|
|
|
}
|