Skip to content
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
1 change: 1 addition & 0 deletions RCTWeChat.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/little-snow-fox/react-native-wechat-lib.git", :tag => "master" }
s.source_files = "ios/*.{h,m}"
s.dependency "React"
s.dependency "WechatOpenSDK-XCFramework", "2.0.4"
s.vendored_libraries = "ios/libWeChatSDK.a"
s.requires_arc = true
s.frameworks = 'SystemConfiguration','CoreTelephony'
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ allprojects {

dependencies {
api 'com.facebook.react:react-native:+'
api files('libs/libammsdk.jar')
api 'com.tencent.mm.opensdk:wechat-sdk-android:6.8.30'
// compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
Binary file removed android/libs/libammsdk.jar
Binary file not shown.
4 changes: 4 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<queries>
<package android:name="com.tencent.mm" /> // 指定微信包名
</queries>
</manifest>
82 changes: 32 additions & 50 deletions android/src/main/java/com/theweflex/react/WeChatModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;

import androidx.annotation.Nullable;

import com.facebook.common.executors.UiThreadImmediateExecutorService;
import com.facebook.common.internal.Files;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.util.UriUtil;
import com.facebook.datasource.DataSource;
Expand All @@ -29,8 +27,11 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.SubscribeMessage;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
Expand All @@ -44,21 +45,13 @@
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbiz.SubscribeMessage;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.UUID;

Expand Down Expand Up @@ -92,7 +85,8 @@ private static byte[] bitmapResizeGetBytes(Bitmap image, int size) {
if (options > 10) {
options -= 8;
} else {
return bitmapResizeGetBytes(Bitmap.createScaledBitmap(image, 280, image.getHeight() / image.getWidth() * 280, true), size);
float height = (float) image.getHeight() / image.getWidth() * 280;
return bitmapResizeGetBytes(Bitmap.createScaledBitmap(image, 280, (int) height , true), size);
}
// 这里压缩options%,把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
Expand All @@ -106,7 +100,7 @@ public WeChatModule(ReactApplicationContext context) {

@Override
public String getName() {
return "RCTWeChat";
return "WeChat";
}

/**
Expand All @@ -128,8 +122,8 @@ public void initialize() {
}

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
public void invalidate() {
super.invalidate();
if (api != null) {
api = null;
}
Expand Down Expand Up @@ -262,47 +256,34 @@ public void invoke(@Nullable Bitmap bitmap) {
*/
@ReactMethod
public void shareLocalImage(final ReadableMap data, final Callback callback) {
FileInputStream fs = null;
try {
String path = data.getString("imageUrl");
if (path.indexOf("file://") > -1) {

Bitmap originalBitmap = null;
if (path.startsWith("file://")) {
path = path.substring(7);
FileInputStream fs = new FileInputStream(path);
originalBitmap = BitmapFactory.decodeStream(fs);
} else {
originalBitmap = BitmapFactory.decodeStream(getReactApplicationContext().getContentResolver().openInputStream(Uri.parse(path)));
}
// int maxWidth = data.hasKey("maxWidth") ? data.getInt("maxWidth") : -1;
fs = new FileInputStream(path);
Bitmap bmp = BitmapFactory.decodeStream(fs);

// if (maxWidth > 0) {
// bmp = Bitmap.createScaledBitmap(bmp, maxWidth, bmp.getHeight() / bmp.getWidth() * maxWidth, true);
// }

// File f = Environment.getExternalStoragePublicDirectory(SDCARD_ROOT + "/react-native-wechat-lib");
// String fileName = "wechat-share.jpg";
// String tempPath = SDCARD_ROOT + "/react-native-wechat-lib";
// File file = new File(f, fileName);
// try {
// FileOutputStream fos = new FileOutputStream(file);
// bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// fos.flush();
// fos.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }

// int size = bmp.getByteCount();
// ByteArrayOutputStream var2 = new ByteArrayOutputStream();
// bmp.compress(Bitmap.CompressFormat.JPEG, 85, var2);
// int size2 = var2.toByteArray().length;
// 初始化 WXImageObject 和 WXMediaMessage 对象

WXImageObject imgObj = new WXImageObject(bmp);

float maxHeight = 3000;
float originalBitmapWidth = originalBitmap.getWidth();
float originalBitmapHeight = originalBitmap.getHeight();

float currentHeight = originalBitmapHeight > maxHeight ? maxHeight : originalBitmapHeight;
float currentWidth = currentHeight / originalBitmapHeight * originalBitmapWidth;
WXMediaMessage msg = new WXMediaMessage();

Bitmap imageBitmap = Bitmap.createScaledBitmap(originalBitmap, (int) currentWidth, (int) currentHeight, false);

WXImageObject imgObj = new WXImageObject(imageBitmap);

msg.mediaObject = imgObj;
// 设置缩略图
msg.thumbData = bitmapResizeGetBytes(bmp, THUMB_SIZE);
bmp.recycle();
// 设置缩略图
msg.thumbData = bitmapResizeGetBytes(imageBitmap, THUMB_SIZE);

// 构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "img";
Expand Down Expand Up @@ -477,7 +458,8 @@ public void shareMiniProgram(final ReadableMap data, final Callback callback) {
public void invoke(@Nullable Bitmap bmp) {
// 小程序消息封面图片,小于128k
if (bmp != null) {
msg.thumbData = bitmapResizeGetBytes(bmp, 128);
// 设置size为128有时候微信会报错图片太大, 无法成功分享, 故设为100
msg.thumbData = bitmapResizeGetBytes(bmp, 100);
}
// 构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
Expand Down
16 changes: 9 additions & 7 deletions ios/RCTWeChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ - (NSData *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
if (data.length > maxLength) {
return [self compressImage:resultImage toByte:maxLength];
}

return data;
}

Expand Down Expand Up @@ -255,15 +255,15 @@ - (NSData *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
callback([NSArray arrayWithObject:@"shareImage: ImageUrl value, Could not find file suffix."]);
return;
}

// 根据路径下载图片
UIImage *image = [self getImageFromURL: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];
Expand Down Expand Up @@ -300,15 +300,17 @@ - (NSData *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
callback([NSArray arrayWithObject:@"shareLocalImage: ImageUrl value, Could not find file suffix."]);
return;
}

// 根据路径下载图片
if ([imageUrl hasPrefix:@"file"]) {
imageUrl = [imageUrl stringByReplacingOccurrencesOfString: @"file://" withString: @""];
}
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];
Expand Down Expand Up @@ -540,7 +542,7 @@ -(void) onResp:(BaseResp*)resp
if([resp isKindOfClass:[SendMessageToWXResp class]])
{
SendMessageToWXResp *r = (SendMessageToWXResp *)resp;

NSMutableDictionary *body = @{@"errCode":@(r.errCode)}.mutableCopy;
body[@"errStr"] = r.errStr;
body[@"lang"] = r.lang;
Expand All @@ -555,7 +557,7 @@ -(void) onResp:(BaseResp*)resp
body[@"lang"] = r.lang;
body[@"country"] =r.country;
body[@"type"] = @"SendAuth.Resp";

if (resp.errCode == WXSuccess) {
if (self.appId && r) {
// ios第一次获取不到appid会卡死,加个判断OK
Expand Down
5 changes: 1 addition & 4 deletions ios/RCTWeChat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
10D6D7A91F160C250066F0F3 /* libRCTWeChat.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */; };
10D6D7AF1F160C3A0066F0F3 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 10D6D76D1F160B090066F0F3 /* libReact.a */; };
86D238561BD0BC1000C75D01 /* RCTWeChat.m in Sources */ = {isa = PBXBuildFile; fileRef = 86D238551BD0BC1000C75D01 /* RCTWeChat.m */; };
86D2385D1BD0CA3D00C75D01 /* libWeChatSDK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238571BD0BC2200C75D01 /* libWeChatSDK.a */; settings = {ATTRIBUTES = (Required, ); }; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -128,7 +127,6 @@
86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTWeChat.a; sourceTree = BUILT_PRODUCTS_DIR; };
86D238541BD0BC1000C75D01 /* RCTWeChat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTWeChat.h; sourceTree = SOURCE_ROOT; };
86D238551BD0BC1000C75D01 /* RCTWeChat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTWeChat.m; sourceTree = SOURCE_ROOT; };
86D238571BD0BC2200C75D01 /* libWeChatSDK.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libWeChatSDK.a; sourceTree = "<group>"; };
86D238581BD0BC2200C75D01 /* WXApi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXApi.h; sourceTree = "<group>"; };
86D238591BD0BC2200C75D01 /* WXApiObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXApiObject.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand All @@ -147,7 +145,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
86D2385D1BD0CA3D00C75D01 /* libWeChatSDK.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -185,7 +182,6 @@
86D238391BD0BB9E00C75D01 = {
isa = PBXGroup;
children = (
86D238571BD0BC2200C75D01 /* libWeChatSDK.a */,
86D238581BD0BC2200C75D01 /* WXApi.h */,
86D238591BD0BC2200C75D01 /* WXApiObject.h */,
86D238441BD0BB9E00C75D01 /* RCTWeChat */,
Expand Down Expand Up @@ -276,6 +272,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 86D238391BD0BB9E00C75D01;
Expand Down
Binary file removed ios/libWeChatSDK.a
Binary file not shown.