mirror of
https://github.com/little-snow-fox/react-native-wechat-lib.git
synced 2025-12-06 15:26:49 +08:00
feat 添加分享本地图片的方法,修改 ios 配置文档
This commit is contained in:
parent
721f79b191
commit
e7d34c677f
27
README.md
27
README.md
@ -152,6 +152,33 @@ WeChat.shareImage({
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### ShareLocalImage(ShareImageMetadata) 分享图片
|
||||||
|
|
||||||
|
ShareImageMetadata
|
||||||
|
|
||||||
|
| name | type | description |
|
||||||
|
|---------|--------|-------------------------------------|
|
||||||
|
| imageUrl| String | 图片地址 |
|
||||||
|
| scene | Number | 分享到, 0:会话 2:朋友圈 3:收藏 |
|
||||||
|
|
||||||
|
Return:
|
||||||
|
|
||||||
|
| name | type | description |
|
||||||
|
|---------|--------|-------------------------------------|
|
||||||
|
| errCode | Number | 0 if authorization successed |
|
||||||
|
| errStr | String | Error message if any error occurred |
|
||||||
|
|
||||||
|
|
||||||
|
```js
|
||||||
|
import * as WeChat from 'react-native-wechat-lib';
|
||||||
|
|
||||||
|
WeChat.ShareLocalImage({
|
||||||
|
imageUrl: '/sdcard/test.png',
|
||||||
|
scene: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
#### ShareMusic(ShareMusicMetadata) 分享音乐
|
#### ShareMusic(ShareMusicMetadata) 分享音乐
|
||||||
|
|
||||||
ShareMusicMetadata
|
ShareMusicMetadata
|
||||||
|
|||||||
@ -239,6 +239,33 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分享本地图片
|
||||||
|
* @param data
|
||||||
|
* @param callback
|
||||||
|
*/
|
||||||
|
@ReactMethod
|
||||||
|
public void shareLocalImage(final ReadableMap data, final Callback callback) {
|
||||||
|
FileInputStream fs = new FileInputStream(data.getString("imageUrl"));
|
||||||
|
Bitmap bmp = BitmapFactory.decodeStream(fs);
|
||||||
|
// 初始化 WXImageObject 和 WXMediaMessage 对象
|
||||||
|
WXImageObject imgObj = new WXImageObject(bmp);
|
||||||
|
WXMediaMessage msg = new WXMediaMessage();
|
||||||
|
msg.mediaObject = imgObj;
|
||||||
|
|
||||||
|
// 设置缩略图
|
||||||
|
msg.thumbData = bitmapResizeGetBytes(bmp, THUMB_SIZE);
|
||||||
|
|
||||||
|
// 构造一个Req
|
||||||
|
SendMessageToWX.Req req = new SendMessageToWX.Req();
|
||||||
|
req.transaction = "img";
|
||||||
|
req.message = msg;
|
||||||
|
// req.userOpenId = getOpenId();
|
||||||
|
req.scene = data.hasKey("scene") ? data.getInt("scene") : SendMessageToWX.Req.WXSceneSession;
|
||||||
|
callback.invoke(null, api.sendReq(req));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分享音乐
|
* 分享音乐
|
||||||
* @param data
|
* @param data
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
Add "URL Schema" as your app id for "URL type" in Targets > info, See
|
Add "URL Schema" as your app id for "URL type" in Targets > info, See
|
||||||
the following screenshot for the view on your XCode:
|
the following screenshot for the view on your XCode:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Cannot go back to APP from WeChat without configuration.
|
Cannot go back to APP from WeChat without configuration.
|
||||||
如果不配置,就无法从微信重新回到APP。
|
如果不配置,就无法从微信重新回到APP。
|
||||||
@ -27,6 +27,7 @@ then add:
|
|||||||
<array>
|
<array>
|
||||||
<string>weixin</string>
|
<string>weixin</string>
|
||||||
<string>wechat</string>
|
<string>wechat</string>
|
||||||
|
<string>weixinULAPI</string>
|
||||||
</array>
|
</array>
|
||||||
```
|
```
|
||||||
If not configured, apple will prevent you from jumping to WeChat due to security permissions.
|
If not configured, apple will prevent you from jumping to WeChat due to security permissions.
|
||||||
|
|||||||
BIN
image/url-types.png
Normal file
BIN
image/url-types.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 52 KiB |
3
index.d.ts
vendored
3
index.d.ts
vendored
@ -118,6 +118,9 @@ declare module "react-native-wechat-lib" {
|
|||||||
export function shareImage(
|
export function shareImage(
|
||||||
message: ShareImageMetadata
|
message: ShareImageMetadata
|
||||||
): Promise<{ errCode?: number; errStr?: string }>;
|
): Promise<{ errCode?: number; errStr?: string }>;
|
||||||
|
export function shareLocalImage(
|
||||||
|
message: ShareImageMetadata
|
||||||
|
): Promise<{ errCode?: number; errStr?: string }>;
|
||||||
export function shareMusic(
|
export function shareMusic(
|
||||||
message: ShareMusicMetadata
|
message: ShareMusicMetadata
|
||||||
): Promise<{ errCode?: number; errStr?: string }>;
|
): Promise<{ errCode?: number; errStr?: string }>;
|
||||||
|
|||||||
19
index.js
19
index.js
@ -151,6 +151,7 @@ const nativeShareToFavorite = wrapApi(WeChat.shareToFavorite);
|
|||||||
const nativeSendAuthRequest = wrapApi(WeChat.sendAuthRequest);
|
const nativeSendAuthRequest = wrapApi(WeChat.sendAuthRequest);
|
||||||
const nativeShareText = wrapApi(WeChat.shareText);
|
const nativeShareText = wrapApi(WeChat.shareText);
|
||||||
const nativeShareImage = wrapApi(WeChat.shareImage);
|
const nativeShareImage = wrapApi(WeChat.shareImage);
|
||||||
|
const nativeShareLocalImage = wrapApi(WeChat.shareLocalImage);
|
||||||
const nativeShareMusic = wrapApi(WeChat.shareMusic);
|
const nativeShareMusic = wrapApi(WeChat.shareMusic);
|
||||||
const nativeShareVideo = wrapApi(WeChat.shareVideo);
|
const nativeShareVideo = wrapApi(WeChat.shareVideo);
|
||||||
const nativeShareWebpage = wrapApi(WeChat.shareWebpage);
|
const nativeShareWebpage = wrapApi(WeChat.shareWebpage);
|
||||||
@ -211,6 +212,24 @@ export function shareImage(data) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Share local image
|
||||||
|
* @method shareLocalImage
|
||||||
|
* @param {Object} data
|
||||||
|
*/
|
||||||
|
export function shareLocalImage(data) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
nativeShareLocalImage(data);
|
||||||
|
emitter.once('SendMessageToWX.Resp', resp => {
|
||||||
|
if (resp.errCode === 0) {
|
||||||
|
resolve(resp);
|
||||||
|
} else {
|
||||||
|
reject(new WechatError(resp));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Share music
|
* Share music
|
||||||
* @method shareMusic
|
* @method shareMusic
|
||||||
|
|||||||
@ -284,6 +284,51 @@ RCT_EXPORT_METHOD(shareImage:(NSDictionary *)data
|
|||||||
[WXApi sendReq:req completion:completion];
|
[WXApi sendReq:req completion:completion];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 分享本地图片
|
||||||
|
RCT_EXPORT_METHOD(shareLocalImage:(NSDictionary *)data
|
||||||
|
:(RCTResponseSenderBlock)callback)
|
||||||
|
{
|
||||||
|
NSString *imageUrl = data[@"imageUrl"];
|
||||||
|
if (imageUrl == NULL || [imageUrl isEqual:@""]) {
|
||||||
|
callback([NSArray arrayWithObject:@"shareLocalImage: The value of ImageUrl cannot be empty."]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
NSRange range = [imageUrl rangeOfString:@"."];
|
||||||
|
if ( range.length == 0)
|
||||||
|
{
|
||||||
|
callback([NSArray arrayWithObject:@"shareLocalImage: ImageUrl value, Could not find file suffix."]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据路径下载图片
|
||||||
|
UIImage *image = [UIImage imageWithContentsOfFile:imageUrl];
|
||||||
|
// 从 UIImage 获取图片数据
|
||||||
|
NSData *imageData = UIImageJPEGRepresentation(image, 1);
|
||||||
|
// 用图片数据构建 WXImageObject 对象
|
||||||
|
WXImageObject *imageObject = [WXImageObject object];
|
||||||
|
imageObject.imageData = imageData;
|
||||||
|
|
||||||
|
WXMediaMessage *message = [WXMediaMessage message];
|
||||||
|
// 利用原图压缩出缩略图,确保缩略图大小不大于32KB
|
||||||
|
message.thumbData = [self compressImage: image toByte:32678];
|
||||||
|
message.mediaObject = imageObject;
|
||||||
|
message.title = data[@"title"];
|
||||||
|
message.description = data[@"description"];
|
||||||
|
|
||||||
|
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
|
||||||
|
req.bText = NO;
|
||||||
|
req.message = message;
|
||||||
|
req.scene = data[@"scene"] || WXSceneSession;
|
||||||
|
// [WXApi sendReq:req];
|
||||||
|
void ( ^ completion )( BOOL );
|
||||||
|
completion = ^( BOOL success )
|
||||||
|
{
|
||||||
|
callback(@[success ? [NSNull null] : INVOKE_FAILED]);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
[WXApi sendReq:req completion:completion];
|
||||||
|
}
|
||||||
|
|
||||||
// 分享音乐
|
// 分享音乐
|
||||||
RCT_EXPORT_METHOD(shareMusic:(NSDictionary *)data
|
RCT_EXPORT_METHOD(shareMusic:(NSDictionary *)data
|
||||||
:(RCTResponseSenderBlock)callback)
|
:(RCTResponseSenderBlock)callback)
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-wechat-lib",
|
"name": "react-native-wechat-lib",
|
||||||
"version": "1.1.2",
|
"version": "1.1.3",
|
||||||
"description": "react-native library for wechat app. 支持分享和拉起小程序。",
|
"description": "react-native library for wechat app. 支持分享和拉起小程序。",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user