WechatApi.java
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.polysoft.nafmii.wechat;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.polysoft.nafmii.BuildConfig;
import com.polysoft.nafmii.R;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
public class WechatApi {
private static WechatApi INSTANCE = null;
private final Context ctx;
private final IWXAPI api;
private WechatApi(Context ctx, boolean isDebug) {
this.ctx = ctx;
this.api = WXAPIFactory.createWXAPI(ctx, BuildConfig.WX_APPID, isDebug);
this.api.registerApp(BuildConfig.WX_APPID);
}
public static WechatApi getInstance(Context ctx) {
return getInstance(ctx, false);
}
public static WechatApi getInstance(Context ctx, boolean isDebug) {
if (null == WechatApi.INSTANCE) {
WechatApi.INSTANCE = new WechatApi(ctx, isDebug);
}
return WechatApi.INSTANCE;
}
public void sendRequest(BaseReq req) {
this.api.sendReq(req);
}
public boolean isWXAppInstalled() {
return this.api.isWXAppInstalled();
}
public void handleIntent(Intent intent, IWXAPIEventHandler handler) {
this.api.handleIntent(intent, handler);
}
public void pay(String payInfo) {
this.sendRequest(JSON.parseObject(payInfo, PayReq.class));
}
public void share(Context ctx, final ShareBean bean) {
String webImg = bean.getWebImg();
String shareType = bean.getShareType();
if ("0".equals(shareType)) { // 分享文字
this.shareText(bean);
} else if ("1".equals(shareType)) { // 分享图片
this.shareImage(bean);
} else if ("2".equals(shareType)) { // 分享url地址
this.loadImageUrl(ctx, webImg, new Callback<Bitmap>() {
@Override
public void onBack(Bitmap data) {
shareWebPage(bean, data);
}
});
}
}
private void shareText(ShareBean bean) {
}
private void shareImage(ShareBean bean) {
}
private void shareWebPage(ShareBean bean, Bitmap bitmap) {
WXWebpageObject web = new WXWebpageObject();
web.webpageUrl = bean.getWebUrl();
WXMediaMessage msg = new WXMediaMessage(web);
// 填写网页标题、描述、位图
msg.title = bean.getTitle();
//网页描述
msg.description = bean.getContent();
msg.setThumbImage(bitmap);
bitmap.recycle();
SendMessageToWX.Req req = new SendMessageToWX.Req();
// transaction用于唯一标识一个请求(可自定义)
req.transaction = "webpage";
// 上文的WXMediaMessage对象
req.message = msg;
req.scene = this.getShareScene(bean);
// 向微信发送请求
this.sendRequest(req);
}
private int getShareScene(ShareBean bean) {
String scene = bean.getScene();
if ("0".equals(scene)) { // 朋友圈
return SendMessageToWX.Req.WXSceneTimeline;
} else if ("1".equals(scene)) { // 分享到好友
return SendMessageToWX.Req.WXSceneSession;
} else if ("2".equals(scene)) { // 分享到收藏夹
return SendMessageToWX.Req.WXSceneFavorite;
}
// 默认分享到好友
return SendMessageToWX.Req.WXSceneSession;
}
private void loadImageUrl(Context ctx, String url, final Callback<Bitmap> callback) {
SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
callback.onBack(resource);
}
};
if (TextUtils.isEmpty(url)) {
Glide.with(ctx).asBitmap().load(R.mipmap.ic_launcher).override(120, 120).into(target);
} else {
Glide.with(ctx).asBitmap().load(url).override(120, 120).into(target);
}
}
private static interface Callback<T> {
public void onBack(T data);
}
}