Skip to content

Commit

Permalink
微信bug修复
Browse files Browse the repository at this point in the history
  • Loading branch information
RelinRan committed May 21, 2020
1 parent 9e13490 commit 6c48eeb
Showing 1 changed file with 99 additions and 36 deletions.
135 changes: 99 additions & 36 deletions app/src/main/java/com/android/pay/wechat/WeChatShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.android.pay.R;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
Expand Down Expand Up @@ -38,6 +39,26 @@ public class WeChatShare {
*/
public static final int SCENE_FAVORITE = SendMessageToWX.Req.WXSceneFavorite;

/**
* 纯文本
*/
public static final int TYPE_TEXT = 0x010;
/**
* 纯图片
*/
public static final int TYPE_IMAGE = 0x020;
/**
* 音乐
*/
public static final int TYPE_MUSIC = 0x030;
/**
* 视频
*/
public static final int TYPE_VIDEO = 0x040;
/**
* 网页
*/
public static final int TYPE_WEB = 0x050;

/**
* 微信api对象
Expand Down Expand Up @@ -151,6 +172,11 @@ public class WeChatShare {
*/
public final OnWeChatShareListener listener;

/**
* 分享类型
*/
public final int type;

private WeChatReceiver receiver;

/**
Expand Down Expand Up @@ -180,37 +206,10 @@ public WeChatShare(Builder builder) {
this.videoLowBandUrl = builder.videoLowBandUrl;
this.webpageUrl = builder.webpageUrl;
this.listener = builder.listener;
this.type = builder.type;
share();
}

/**
* 分享
*/
private void share() {
if (listener != null && context != null && receiver == null) {
receiver = new WeChatReceiver();
IntentFilter filter = new IntentFilter(WeChatConstants.ACTION);
context.registerReceiver(receiver, filter);
}
api = WXAPIFactory.createWXAPI(context, WeChatConstants.APP_ID, true);
api.registerApp(appId);
shareText();
if (imageUrl != null) {
ShareHelper.decodeUrl(imageUrl, new ShareHelper.OnUrlDecodeByteListener() {
@Override
public void onUrlDecode(byte[] data) {
shareImage(data);
}
});
} else {
shareImage(null);
}
shareMusic();
shareWebPage();
shareVideo();
}


/**
* 构建者
*/
Expand Down Expand Up @@ -321,6 +320,11 @@ public static class Builder {
*/
private OnWeChatShareListener listener;

/**
* 分享类型(默认Web)
*/
private int type = TYPE_WEB;

/**
* 分享构建者
*
Expand Down Expand Up @@ -677,6 +681,7 @@ public Builder videoLowBandUrl(String videoLowBandUrl) {

/**
* html 链接,限制长度不超过 10KB
*
* @return
*/
public String webageUrl() {
Expand All @@ -685,6 +690,7 @@ public String webageUrl() {

/**
* html 链接,限制长度不超过10KB
*
* @param webpageUrl
*/
public void webpageUrl(String webpageUrl) {
Expand All @@ -709,6 +715,26 @@ public void listener(OnWeChatShareListener listener) {
this.listener = listener;
}

/**
* 分享类型
*
* @return
*/
public int type() {
return type;
}

/**
* 分享类型
*
* @param type
* @return
*/
public Builder type(int type) {
this.type = type;
return this;
}

/**
* 构建分享对象进行分享
*
Expand All @@ -720,6 +746,44 @@ public WeChatShare build() {

}

/**
* 分享
*/
private void share() {
if (listener != null && context != null && receiver == null) {
receiver = new WeChatReceiver();
IntentFilter filter = new IntentFilter(WeChatConstants.ACTION);
context.registerReceiver(receiver, filter);
}
api = WXAPIFactory.createWXAPI(context, WeChatConstants.APP_ID, true);
api.registerApp(appId);
//分享类型
if (type == TYPE_TEXT) {
shareText();
}
if (type == TYPE_IMAGE) {
if (imageUrl != null) {
ShareHelper.decodeUrl(imageUrl, new ShareHelper.OnUrlDecodeByteListener() {
@Override
public void onUrlDecode(byte[] data) {
shareImage(data);
}
});
} else {
shareImage(null);
}
}
if (type == TYPE_MUSIC) {
shareMusic();
}
if (type == TYPE_VIDEO) {
shareVideo();
}
if (type == TYPE_WEB) {
shareWebPage();
}
}

/**
* 分享纯文本
*/
Expand All @@ -731,27 +795,27 @@ private void shareText() {
textObj.text = text;
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
if (!TextUtils.isEmpty(text)) {
msg.description = text;
}
msg.description = text;
shareMessage("text" + System.currentTimeMillis(), msg, scene, "");
}

/**
* 分享本地图片
*/
private void shareImage(byte[] imageData) {
if (imagePath == null) {
//图片的二进制数据 和 图片的本地路径都为空
if (imageData == null && imagePath == null) {
return;
}
WXImageObject imgObj = new WXImageObject();
if (imageData != null) {
imgObj.imageData = imageData;
}
if (imagePath != null) {
imgObj.imagePath = imagePath;
}
if (imageData != null && imageData.length != 0) {
imgObj.imageData = imageData;
}
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
if (!TextUtils.isEmpty(title)) {
msg.title = title;
}
Expand All @@ -770,7 +834,6 @@ private void shareImage(byte[] imageData) {
if (thumbData != null && thumbData.length != 0) {
msg.thumbData = thumbData;
}
msg.mediaObject = imgObj;
shareMessage("image" + System.currentTimeMillis(), msg, scene, "");
}

Expand Down

0 comments on commit 6c48eeb

Please sign in to comment.