2018.03.22 增加微信分享
简介
⚠️本文不包含sdk 集成、appId申请等基础步骤,入门移步 微信官方集成文档,查找错误码请戳微信官方错误码。
项目地址: WechatHelper
微信授权登陆
使用方式:
请在在
UserConstants
类中设置AppID
及AppSecret
请在需要接收微信回调页面增加
WechatObserver
处理回调后的结果(参见MainActivity.WechatAuthObserver)-
请在发起微信授权时调用
WechatHelper.getInstance().addWechatObserver(mWechatLoginObserver); WechatHelper.getInstance().authorizeByWechat();
请注意观察者收到回调后的线程问题
整体流程:
外部进行授权操作时,首先需要注册Observer,然后调用接口方法authorizeByWechat。当授权状态发生变化时,Observer会接收到对应的状态,最终通过Handler触发不同状态的下的操作。
微信分享(在线链接/网络图片/在线图片)
使用方式:
- 请在在
UserConstants
类中设置AppID
及AppSecret
方法说明:
- 对话分享接口:
WechatHelper.getInstance().shareWechat(ShareParams params)
使用参考:
分享类型 | 具体方法 |
---|---|
链接分享 | WechatShareTest.shareLinkToWechat |
网络图片分享 | WechatShareTest.shareNetworkImageToWechat |
本地图片分享 | WechatShareTest.shareLocalImageToWechat |
- 朋友圈分享接口:
WechatHelper.getInstance().shareWechatMoments(ShareParams params)
使用参考:
分享类型 | 具体方法 |
---|---|
链接分享 | WechatShareTest.shareLinkToWechatMoments |
网络图片分享 | WechatShareTest.shareNetworkImageToWechatMoments |
本地图片分享 | WechatShareTest.shareLocalImageToWechatMoments |
-
分享参数ShareParams
ShareParams属性 功能 取值 mShareType 分享类型 ShareType.LOCAL_IMAGE
(本地图片分享),ShareType.NETWORK_IMAGE
(网络图片分享),ShareType.LINK
(链接分享)mTitle 分享标题 String类型 mText 分享内容 String类型 mLinkUrl 分享链接 String类型 mNetworkImageUrl 分享网络图片链接 String类型 mLocalBitmap 分享本地图片 Bitmap类型
核心类
WechatHelper
public class WechatHelper {
private static final String TAG = "WechatManager";
private final String GET_REQUEST_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
private final String GET_EXPIRE_TOKEN_URL = "https://api.weixin.qq.com/sns/aaswuth";
private final String GET_USER_INFO_URL = "https://api.weixin.qq.com/sns/userinfo";
private final String GET_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
public static final int WECHAT_CHECK_SUCCESS = 166;
public static final int WECHAT_CHECK_FAILURE = 167;
private static IWXAPI api;
private static WechatHelper instance;
private static WechatObservable mWechatObservable;
private WXErrorInfo mWXErrorInfo;
public void addWechatObserver(WechatObserver observer) {
mWechatObservable.addObserver(observer);
}
public void removeWechatObserver(WechatObserver observer) {
mWechatObservable.deleteObserver(observer);
}
public void removeAllWechatObserver() {
mWechatObservable.deleteObservers();
}
public void sendUserConfirmMessage(int code) {
mWechatObservable.sendStateChange(code);
}
private WechatHelper() {
api = WXAPIFactory.createWXAPI(GlobalConfig.getAppContext(), UserConstants.WECHAT_APPIDS, true);
api.registerApp(UserConstants.WECHAT_APPIDS);
mWechatObservable = new WechatObservable();
}
public static WechatHelper getInstance() {
if (instance == null) {
synchronized (WechatHelper.class) {
if (instance == null) {
instance = new WechatHelper();
}
}
}
return instance;
}
public IWXAPI getApi() {
return api;
}
public void authorizeByWechat() {
Log.i(TAG, "authorizeByWechat: ");
if (!isAvailable()) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
return;
}
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "app_wechat";
api.sendReq(req);
}
public void checkAccessToken(String code) {
// 从手机本地获取存储的授权口令信息,判断是否存在access_token,不存在请求获取,存在就判断是否过期
String accessToken = WechatInfoSPHelper.getWechatAccessToken();
String openid = WechatInfoSPHelper.getWechatOpenid();
if (!TextUtils.isEmpty(accessToken) && !TextUtils.isEmpty(openid)) {
// 有access_token,判断是否过期有效
isExpireAccessToken(accessToken, openid);
} else {
getTokenFromCode(code);
}
}
private void isExpireAccessToken(final String accessToken, final String openid) {
Log.i(TAG, "isExpireAccessToken: ");
if (TextUtils.isEmpty(accessToken) || TextUtils.isEmpty(openid)) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
return;
}
// 使用网络库发起请求
NetworkHttpParam params = new NetworkHttpParam();
params.put("access_token", accessToken);
params.put("openid", openid);
OkHttpHelper.getInstance().requestStringGet(GET_EXPIRE_TOKEN_URL, params, new WechatDataHandler(false) {
@Override
public void onWechatSuccess(String response) {
super.onWechatSuccess(response);
if (validateSuccess(response)) {
// accessToken没有过期,获取用户信息
getUserInfo(accessToken, openid);
} else {
// 过期了,使用refresh_token来刷新accesstoken
refreshAccessToken();
}
}
});
}
private void refreshAccessToken() {
Log.i(TAG, "refreshAccessToken: ");
// 从本地获取存储的refresh_token
final String refreshToken = WechatInfoSPHelper.getWechatRefreshToken();
if (refreshToken == null) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
return;
}
// 发起网络请求 刷新token
NetworkHttpParam params = new NetworkHttpParam();
params.put("appid", UserConstants.WECHAT_APPIDS);
params.put("grant_type", "refresh_token");
params.put("refresh_token", refreshToken);
OkHttpHelper.getInstance().requestStringGet(GET_REFRESH_TOKEN_URL, params, new WechatDataHandler(true) {
@Override
public void onWechatDataSuccess(String response) {
super.onWechatDataSuccess(response);
WXAccessTokenInfo tokenInfo = JsonFactory.fromJson(response, WXAccessTokenInfo.class);
if (tokenInfo == null) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
} else {
WechatInfoSPHelper.saveWechatAccessInfoToSP(tokenInfo);
getUserInfo(tokenInfo.access_token, tokenInfo.openid);
}
}
});
}
/**
* 使用code获取微信登陆token凭证
*/
private void getTokenFromCode(String code) {
Log.i(TAG, "getTokenFromCode: ");
if (TextUtils.isEmpty(code)) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
return;
}
// 发起网络请求 刷新token
NetworkHttpParam params = new NetworkHttpParam();
params.put("appid", UserConstants.WECHAT_APPIDS);
params.put("secret", UserConstants.WECHAT_SECRET);
params.put("code", code);
params.put("grant_type", "authorization_code");
OkHttpHelper.getInstance().requestStringGet(GET_REQUEST_ACCESS_TOKEN_URL, params, new WechatDataHandler(true) {
@Override
public void onWechatDataSuccess(String response) {
super.onWechatDataSuccess(response);
WXAccessTokenInfo tokenInfo = JsonFactory.fromJson(response, WXAccessTokenInfo.class);
if (tokenInfo == null) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
} else {
WechatInfoSPHelper.saveWechatAccessInfoToSP(tokenInfo);
getUserInfo(tokenInfo.access_token, tokenInfo.openid);
}
}
});
}
// 使用access_token获取用户信息
private void getUserInfo(String access_token, String openid) {
Log.i(TAG, "getUserInfo: ");
if (TextUtils.isEmpty(access_token) || TextUtils.isEmpty(openid)) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
return;
}
// 发起网络请求 刷新token
NetworkHttpParam params = new NetworkHttpParam();
params.put("access_token", access_token);
params.put("lang", "zh_CN");
params.put("openid", openid);
OkHttpHelper.getInstance().requestStringGet(GET_USER_INFO_URL, params, new WechatDataHandler(true) {
@Override
public void onWechatDataSuccess(String response) {
super.onWechatDataSuccess(response);
WXUserInfo userInfo = JsonFactory.fromJson(response, WXUserInfo.class);
if (userInfo == null) {
mWechatObservable.sendStateChange(WECHAT_CHECK_FAILURE);
} else {
WechatInfoSPHelper.saveWechatUserInfoToSP(userInfo);
mWechatObservable.sendStateChange(WECHAT_CHECK_SUCCESS);
}
}
});
}
private boolean validateSuccess(String response) {
Log.i(TAG, "validateSuccess: " + response);
if (response.contains("errcode") && response.contains("errmsg")) {
int code = 66666;
try {
JSONObject obj = new JSONObject(response);
code = obj.getInt("errcode");
if (code != 66666) {
return false;
}
} catch (JSONException e) {
e.printStackTrace();
return true;
}
}
return true;
}
private class LeWechatHandler implements NetworkHandler
混淆代码:-keep class com.gjf.wc.model.** {*;}
参考链接
微信工具类封装参考自什么,集成微信登录只要三行代码 , 目前为止发现的封装最好的工具类!给作者大赞!