Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #29

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
Expand All @@ -16,6 +17,6 @@ android {

dependencies {
compile 'com.facebook.react:react-native:+'
compile files('libs/libammsdk.jar')
compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}

Binary file removed android/libs/libammsdk.jar
Binary file not shown.
197 changes: 128 additions & 69 deletions android/src/main/java/cn/reactnative/modules/wx/WeChatModule.java

Large diffs are not rendered by default.

39 changes: 18 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function translateError(err, result) {
}
if (typeof err === 'object') {
if (err instanceof Error) {
return this.reject(ret);
return this.reject(err);
}
return this.reject(Object.assign(new Error(err.message), { errCode: err.errCode }));
} else if (typeof err === 'string') {
Expand All @@ -19,35 +19,21 @@ function translateError(err, result) {
}

// Save callback and wait for future event.
let savedCallback = undefined;
const savedCallback = {};
function waitForResponse(type) {
return new Promise((resolve, reject) => {
if (savedCallback) {
savedCallback('User canceled.');
}
savedCallback = result => {
savedCallback[type] = result => {
if (result.type !== type) {
//
//if (__DEV__) {
// throw new Error('Unsupported response type: ' + resp.type);
//}
return;
}
savedCallback = undefined;
if (result.errCode !== 0) {
const err = new Error(result.errMsg);
err.errCode = result.errCode;
reject(err);
} else {
resolve(result);
}
savedCallback[type] = null;
resolve && resolve(result);
};
});
}

NativeAppEventEmitter.addListener('WeChat_Resp', resp => {
const callback = savedCallback;
savedCallback = undefined;
const callback = savedCallback[resp.type];
callback && callback(resp);
});

Expand Down Expand Up @@ -80,7 +66,13 @@ function wrapApi(nativeFunc) {
if (!checkInstalled) {
throw new Error('没有安装微信!');
}
const checkSupport = await isWXAppSupportApi();
let type = '';
if (nativeFunc === WeChatAPI.pay) {
type = 'Pay';
} else if (nativeFunc === WeChatAPI.launchMiniPro) {
type = 'WXLaunchMiniProgram';
}
const checkSupport = await isWXAppSupportApi(type);
if (!checkSupport) {
throw new Error('微信版本不支持');
}
Expand All @@ -92,6 +84,7 @@ const nativeSendAuthRequest = wrapApi(WeChatAPI.login);
const nativeShareToTimelineRequest = wrapApi(WeChatAPI.shareToTimeline);
const nativeShareToSessionRequest = wrapApi(WeChatAPI.shareToSession);
const nativePayRequest = wrapApi(WeChatAPI.pay);
const nativeLaunchMiniProRequest = wrapApi(WeChatAPI.launchMiniPro);

export function login(config) {
const scope = (config && config.scope) || 'snsapi_userinfo';
Expand All @@ -114,3 +107,7 @@ export function pay(data) {
.then(() => waitForResponse("Pay.Resp"));
}

export function launchMiniPro(data) {
return nativeLaunchMiniProRequest(data)
.then(() => waitForResponse("WXLaunchMiniProgram.Resp"));
}
2 changes: 1 addition & 1 deletion ios/RCTWeChatAPI/RCTWeChatAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 erica. All rights reserved.
//

#import "RCTBridgeModule.h"
#import <React/RCTBridgeModule.h>

@interface RCTWeChatAPI : NSObject <RCTBridgeModule>

Expand Down
65 changes: 50 additions & 15 deletions ios/RCTWeChatAPI/RCTWeChatAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
//


#import "RCTLog.h"
#import <React/RCTLog.h>
#import "RCTWeChatAPI.h"
#import "WXApi.h"
#import "WXApiObject.h"
#import "RCTEventDispatcher.h"
#import "RCTBridge.h"
#import "RCTImageLoader.h"
#import <React/RCTEventDispatcher.h>
#import <React/RCTBridge.h>
#import <React/RCTImageLoader.h>

#define RCTWXShareTypeNews @"news"
#define RCTWXShareTypeImage @"image"
#define RCTWXShareTypeText @"text"
#define RCTWXShareTypeVideo @"video"
#define RCTWXShareTypeAudio @"audio"
#define RCTWXShareTypeMiniPro @"miniprogram"

#define RCTWXShareType @"type"
#define RCTWXShareTitle @"title"
Expand All @@ -28,6 +29,9 @@
#define RCTWXShareWebpageUrl @"webpageUrl"
#define RCTWXShareImageUrl @"imageUrl"
#define RCTWXShareThumbImageSize @"thumbImageSize"
#define RCTWXShareUserName @"userName"
#define RCTWXSharePath @"path"
#define RCTWXShareMiniProType @"miniprogramType"

#define NOT_REGISTERED (@"registerApp required.")
#define INVOKE_FAILED (@"WeChat API invoke returns false.")
Expand All @@ -49,9 +53,9 @@ - (NSDictionary *)constantsToExport
return @{ @"isAppRegistered":@(gIsAppRegistered)};
}

- (dispatch_queue_t)methodQueue
+ (BOOL)requiresMainQueueSetup
{
return dispatch_get_main_queue();
return YES;
}

- (instancetype)init
Expand All @@ -75,7 +79,8 @@ - (void)dealloc
callback(@[[NSNull null], @([WXApi isWXAppInstalled])]);
}

RCT_EXPORT_METHOD(isWXAppSupportApi:(RCTResponseSenderBlock)callback)
RCT_EXPORT_METHOD(isWXAppSupportApi:(NSString*)type
:(RCTResponseSenderBlock)callback)
{
callback(@[[NSNull null], @([WXApi isWXAppSupportApi])]);
}
Expand Down Expand Up @@ -116,6 +121,18 @@ - (void)dealloc
callback(@[success ? [NSNull null] : INVOKE_FAILED]);
}

RCT_EXPORT_METHOD(launchMiniPro:(NSDictionary *)data
:(RCTResponseSenderBlock)callback)
{
WXLaunchMiniProgramReq *req = [WXLaunchMiniProgramReq object];
req.userName = data[@"userName"];
req.path = data[@"path"];
req.miniProgramType = (WXMiniProgramType)[data[@"miniprogramType"] integerValue];

BOOL success = [WXApi sendReq:req];
callback(@[success ? [NSNull null] : INVOKE_FAILED]);
}

- (void)handleOpenURL:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
Expand Down Expand Up @@ -180,16 +197,25 @@ - (void)shareToWeixinWithData:(NSDictionary *)aData image:(UIImage *)aImage scen
videoObject.videoLowBandUrl = aData[@"videoLowBandUrl"];
mediaMessage.mediaObject = videoObject;
}
else if ([type isEqualToString:RCTWXShareTypeMiniPro]) {
WXMiniProgramObject *wxMiniObject = [WXMiniProgramObject new];
wxMiniObject.webpageUrl = aData[@"webpageUrl"];
wxMiniObject.userName = aData[@"userName"];
wxMiniObject.path = aData[@"path"];
wxMiniObject.miniProgramType = (WXMiniProgramType)[aData[@"miniprogramType"] integerValue];
if (aImage != nil) {
wxMiniObject.hdImageData = UIImageJPEGRepresentation(aImage, 0.7);
}
mediaMessage.mediaObject = wxMiniObject;
mediaMessage.thumbData = nil;
}
}

req.message = mediaMessage;
}

BOOL success = [WXApi sendReq:req];
if (success == NO)
{
callback(@[INVOKE_FAILED]);
}
callback(@[success ? [NSNull null] : INVOKE_FAILED]);
}


Expand All @@ -199,11 +225,15 @@ - (void)shareToWeixinWithData:(NSDictionary *)aData scene:(int)aScene callback:(
if (imageUrl.length && _bridge.imageLoader) {
CGSize size = CGSizeZero;
if (![aData[RCTWXShareType] isEqualToString:RCTWXShareTypeImage]) {
CGFloat thumbImageSize = 80;
if (aData[RCTWXShareThumbImageSize]) {
thumbImageSize = [aData[RCTWXShareThumbImageSize] floatValue];
if ([aData[RCTWXShareType] isEqualToString:RCTWXShareTypeMiniPro]) {
size = CGSizeMake(375.0f, 300.0f);
} else {
CGFloat thumbImageSize = 80;
if (aData[RCTWXShareThumbImageSize]) {
thumbImageSize = [aData[RCTWXShareThumbImageSize] floatValue];
}
size = CGSizeMake(thumbImageSize,thumbImageSize);
}
size = CGSizeMake(thumbImageSize,thumbImageSize);
}
[_bridge.imageLoader loadImageWithURLRequest:[RCTConvert NSURLRequest:imageUrl] size:size scale:1 clipped:FALSE resizeMode:UIViewContentModeScaleToFill progressBlock:nil partialLoadBlock: nil completionBlock:^(NSError *error, UIImage *image) {
[self shareToWeixinWithData:aData image:image scene:aScene callBack:aCallBack];
Expand Down Expand Up @@ -295,6 +325,11 @@ - (void)onResp:(BaseResp*)resp
body[@"returnKey"] = r.returnKey;
body[@"type"]= @"Pay.Resp";
}
else if ([resp isKindOfClass:[WXLaunchMiniProgramResp class]]) {
WXLaunchMiniProgramResp *r = (WXLaunchMiniProgramResp *)resp;
body[@"extMsg"] = r.extMsg;
body[@"type"] = @"WXLaunchMiniProgram.Resp";
}

[self.bridge.eventDispatcher sendAppEventWithName:@"WeChat_Resp" body:body];
}
Expand Down
37 changes: 32 additions & 5 deletions ios/libWeChatSDK/WXApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@

@end

#pragma mark - WXApiLogDelegate

@protocol WXApiLogDelegate <NSObject>

-(void) onLog:(NSString*)log logLevel:(WXLogLevel)level;

@end

#pragma mark - WXApi

Expand All @@ -51,7 +57,7 @@

/*! @brief WXApi的成员函数,向微信终端程序注册第三方应用。
*
* 需要在每次启动第三方应用程序时调用。第一次调用后,会在微信的可用应用列表中出现。
* 需要在每次启动第三方应用程序时调用。第一次调用后,会在微信的可用应用列表中出现,默认开启MTA数据上报
* iOS7及以上系统需要调起一次微信才会出现在微信的可用应用列表中。
* @attention 请保证在主线程中调用此函数
* @param appid 微信开发者ID
Expand All @@ -60,16 +66,16 @@
*/
+(BOOL) registerApp:(NSString *)appid;


/*! @brief WXApi的成员函数,向微信终端程序注册第三方应用。
*
* 需要在每次启动第三方应用程序时调用。第一次调用后,会在微信的可用应用列表中出现。
* @see registerApp
* iOS7及以上系统需要调起一次微信才会出现在微信的可用应用列表中。
* @attention 请保证在主线程中调用此函数
* @param appid 微信开发者ID
* @param appdesc 应用附加信息,长度不超过1024字节
* @param isEnableMTA 是否支持MTA数据上报
* @return 成功返回YES,失败返回NO。
*/
+(BOOL) registerApp:(NSString *)appid withDescription:(NSString *)appdesc;
+(BOOL) registerApp:(NSString *)appid enableMTA:(BOOL)isEnableMTA;


/*! @brief WXApi的成员函数,向微信终端程序注册应用支持打开的文件类型。
Expand Down Expand Up @@ -163,4 +169,25 @@
+(BOOL) sendResp:(BaseResp*)resp;


/*! @brief WXApi的成员函数,接受微信的log信息。byBlock
注意1:SDK会强引用这个block,注意不要导致内存泄漏,注意不要导致内存泄漏
注意2:调用过一次startLog by block之后,如果再调用一次任意方式的startLoad,会释放上一次logBlock,不再回调上一个logBlock
*
* @param level 打印log的级别
* @param logBlock 打印log的回调block
*/
+(void) startLogByLevel:(WXLogLevel)level logBlock:(WXLogBolock)logBlock;

/*! @brief WXApi的成员函数,接受微信的log信息。byDelegate
注意1:sdk会弱引用这个delegate,这里可加任意对象为代理,不需要与WXApiDelegate同一个对象
注意2:调用过一次startLog by delegate之后,再调用一次任意方式的startLoad,不会再回调上一个logDelegate对象
* @param level 打印log的级别
* @param logDelegate 打印log的回调代理,
*/
+ (void)startLogByLevel:(WXLogLevel)level logDelegate:(id<WXApiLogDelegate>)logDelegate;

/*! @brief 停止打印log,会清理block或者delegate为空,释放block
* @param
*/
+ (void)stopLog;
@end
Loading