ios add share file

This commit is contained in:
Tom Xu 2021-01-06 16:03:58 +08:00
parent 85b032bedc
commit 8049474db8
4 changed files with 61 additions and 0 deletions

5
.prettierrc Normal file
View File

@ -0,0 +1,5 @@
{
"singleQuote": true,
"printWidth": 100,
"trailingComma": "all"
}

10
index.d.ts vendored
View File

@ -161,4 +161,14 @@ declare module 'react-native-wechat-lib' {
export function chooseInvoice(
data?: ChooseInvoice,
): Promise<{ errCode?: number; errStr?: string; cards: Invoice[] }>;
export interface ShareFileMetadata {
url: string;
title?: string;
ext?: string;
scene?: WXScene;
}
export function shareFile(
data: ShareFileMetadata,
): Promise<{ errCode?: number; errStr?: string }>;
}

View File

@ -159,6 +159,7 @@ const nativeShareMiniProgram = wrapApi(WeChat.shareMiniProgram);
const nativeSubscribeMessage = wrapApi(WeChat.subscribeMessage);
const nativeChooseInvoice = wrapApi(WeChat.chooseInvoice);
const nativeShareFile = wrapApi(WeChat.shareFile);
/**
* @method sendAuthRequest
@ -217,6 +218,24 @@ export function chooseInvoice(data) {
});
}
/**
* Share File
* @method shareFile
* @param {Object} data
*/
export function shareFile(data) {
return new Promise((resolve, reject) => {
nativeShareFile(data);
emitter.once('SendMessageToWX.Resp', (resp) => {
if (resp.errCode === 0) {
resolve(resp);
} else {
reject(new WechatError(resp));
}
});
});
}
/**
* Share image
* @method shareImage

View File

@ -260,6 +260,33 @@ RCT_EXPORT_METHOD(chooseInvoice:(NSDictionary *)data
[WXApi sendReq:req completion:completion];
}
//
RCT_EXPORT_METHOD(shareFile:(NSDictionary *)data
:(RCTResponseSenderBlock)callback)
{
NSString *url = data[@"url"];
WXFileObject *file = [[WXFileObject alloc] init];
file.fileExtension = data[@"ext"];
NSData *fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString: url]];
file.fileData = fileData;
WXMediaMessage *message = [WXMediaMessage message];
message.title = data[@"title"];
message.mediaObject = file;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = [data[@"scene"] integerValue];
void ( ^ completion )( BOOL );
completion = ^( BOOL success )
{
callback(@[success ? [NSNull null] : INVOKE_FAILED]);
return;
};
[WXApi sendReq:req completion:completion];
}
//
RCT_EXPORT_METHOD(shareImage:(NSDictionary *)data
:(RCTResponseSenderBlock)callback)