diff --git a/src/android/EntryActivity.java b/src/android/EntryActivity.java index 0be0760..4f9c741 100644 --- a/src/android/EntryActivity.java +++ b/src/android/EntryActivity.java @@ -10,6 +10,8 @@ import com.tencent.mm.opensdk.modelbase.BaseResp; import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram; import com.tencent.mm.opensdk.modelmsg.SendAuth; +import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX; +import com.tencent.mm.opensdk.modelmsg.WXMediaMessage; import com.tencent.mm.opensdk.openapi.IWXAPI; import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; import com.tencent.mm.opensdk.modelbiz.ChooseCardFromWXCardPackage; @@ -22,6 +24,8 @@ import xu.li.cordova.wechat.Wechat; + + /** * Created by xu.li on 9/1/15. */ @@ -30,6 +34,7 @@ public class EntryActivity extends Activity implements IWXAPIEventHandler { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Wechat.data = ""; IWXAPI api = Wechat.getWxAPI(this); @@ -111,6 +116,17 @@ public void onResp(BaseResp resp) { @Override public void onReq(BaseReq req) { + //获取开放标签传递的 extinfo 数据逻辑 + if(req.getType() == ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX && req instanceof ShowMessageFromWX.Req) { + ShowMessageFromWX.Req showReq = (ShowMessageFromWX.Req) req; + WXMediaMessage mediaMsg = showReq.message; + String extInfo = mediaMsg.messageExt; + Wechat.data = extInfo; + // Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(getApplicationContext().getPackageName()); + // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + // getApplicationContext().startActivity(intent); + Wechat.callJS(extInfo); + } finish(); } @@ -173,4 +189,4 @@ protected void plunckInvoiceData(BaseResp resp) { ctx.success(response); } -} \ No newline at end of file +} diff --git a/src/android/Wechat.java b/src/android/Wechat.java index 291110c..55b8f8b 100644 --- a/src/android/Wechat.java +++ b/src/android/Wechat.java @@ -111,6 +111,7 @@ public class Wechat extends CordovaPlugin { protected static IWXAPI wxAPI; protected static String appId; protected static CordovaPreferences wx_preferences; + public static String data = ""; @Override protected void pluginInitialize() { @@ -171,6 +172,8 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback return chooseInvoiceFromWX(args, callbackContext); }else if(action.equals("openMiniProgram")){ return openMiniProgram(args,callbackContext); + }else if(action.equals("openApp")){ + return openApp(callbackContext); } return false; @@ -758,4 +761,11 @@ protected boolean openMiniProgram(CordovaArgs args, CallbackContext callbackCont return true; } + protected boolean openApp(CallbackContext callbackContext) { + callbackContext.success(Wechat.data); + Wechat.data = ""; + + return true; + } + } diff --git a/src/ios/CDVWechat.h b/src/ios/CDVWechat.h index 8d4ae16..a727da8 100644 --- a/src/ios/CDVWechat.h +++ b/src/ios/CDVWechat.h @@ -25,6 +25,7 @@ enum CDVWechatSharingType { @property (nonatomic, strong) NSString *currentCallbackId; @property (nonatomic, strong) NSString *wechatAppId; +@property (nonatomic, strong) NSString *wechatData; - (void)isWXAppInstalled:(CDVInvokedUrlCommand *)command; - (void)share:(CDVInvokedUrlCommand *)command; @@ -34,6 +35,7 @@ enum CDVWechatSharingType { - (void)jumpToWechat:(CDVInvokedUrlCommand *)command; - (void)chooseInvoiceFromWX: (CDVInvokedUrlCommand *)command; - (void)openMiniProgram: (CDVInvokedUrlCommand *)command; +- (void)openApp:(CDVInvokedUrlCommand *)command; - (BOOL)handleUserActivity:(NSUserActivity *)userActivity; - (BOOL)handleWechatOpenURL:(NSURL *)url; @end diff --git a/src/ios/CDVWechat.m b/src/ios/CDVWechat.m index 4f7b424..8cfab07 100644 --- a/src/ios/CDVWechat.m +++ b/src/ios/CDVWechat.m @@ -24,6 +24,7 @@ - (void)pluginInitialize { NSLog(@"cordova-plugin-wechat has been initialized. Wechat SDK Version: %@. APP_ID: %@.", [WXApi getApiVersion], appId); } + self.wechatData = @""; } - (void)isWXAppInstalled:(CDVInvokedUrlCommand *)command @@ -33,6 +34,15 @@ - (void)isWXAppInstalled:(CDVInvokedUrlCommand *)command [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; } +- (void)openApp:(CDVInvokedUrlCommand *)command +{ + CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:self.wechatData]; + + [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; + + self.wechatData = @""; +} + - (void)share:(CDVInvokedUrlCommand *)command { // if not installed @@ -275,6 +285,14 @@ - (void)jumpToWechat:(CDVInvokedUrlCommand *)command - (void)onReq:(BaseReq *)req { NSLog(@"%@", req); + //获取开放标签传递的 extinfo 数据逻辑 + if ([req isKindOfClass:[LaunchFromWXReq class]]) + { + LaunchFromWXReq* wxReq = (LaunchFromWXReq*)req; + WXMediaMessage *msg = wxReq.message; + NSString *extinfo = msg.messageExt; + self.wechatData = extinfo; + } } - (void)onResp:(WXLaunchMiniProgramResp *)resp diff --git a/www/wechat.js b/www/wechat.js index 03458dc..cc3addf 100644 --- a/www/wechat.js +++ b/www/wechat.js @@ -28,6 +28,10 @@ module.exports = { exec(onSuccess, onError, "Wechat", "isWXAppInstalled", []); }, + openApp: function (onSuccess, onError) { + exec(onSuccess, onError, "Wechat", "openApp", []); + }, + /** * Share a message to wechat app *