WXEntryActivity.java
7.83 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.cnlive.cloud.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.cnlive.libs.base.application.AppConfig;
import com.cnlive.libs.base.util.AlertUtil;
import com.cnlive.share.arouter.ARouterUtil;
import com.cnlive.share.data.ShareIntegralTypeInfo;
import com.cnlive.share.data.WeChatInfo;
import com.cnlive.share.observable.WechatLoginObservable;
import com.cnlive.share.observable.WechatShareObservable;
import com.cnlive.share.util.DateTimeUtil;
import com.cnlive.share.util.OkHttpUtils;
import com.cnlive.cloud.user.auth.UserService;
import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.cnlive.share.BuildConfig;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 作者: 吴奎庆
* <p>
* 时间: 2018/8/23
* <p>
* 简介:
*/
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI api;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, BuildConfig.WECHAT_SHARE,false);
api.handleIntent(getIntent(), this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
@Override
public void onReq(BaseReq baseReq) {
}
private SendAuth.Resp resp;
@Override
public void onResp(BaseResp baseResp) {
//微信登录为getType为1,分享为0
if (baseResp.getType() == 1) {
//登录回调
resp = (SendAuth.Resp) baseResp;
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:
String code = String.valueOf(resp.code);
//获取用户信息
getAccessToken(code);
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
AlertUtil.showDeftToast(this, "用户拒绝授权");
this.finish();
break;
case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
AlertUtil.showDeftToast(this, "用户取消登录");
this.finish();
break;
default:
this.finish();
break;
}
} else {
String result = "";
switch (baseResp.errCode) {
case BaseResp.ErrCode.ERR_OK:
result = "分享成功";
WechatShareObservable.getInstance().update(1);
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
result = "分享取消";
WechatShareObservable.getInstance().update(2);
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
result = "分享失败";
WechatShareObservable.getInstance().update(3);
break;
default:
result = "分享失败";
break;
}
AlertUtil.showDeftToast(this, result);
this.finish();
processShareType(baseResp);
}
}
private void getAccessToken(String code) {
String getTokenUrl = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=%s&secret=%s&code=%s&grant_type=authorization_code", AppConfig.getWechatAppid(),
BuildConfig.WECHAT_KEY, code);
OkHttpUtils.ResultCallback<String> resultCallback = new OkHttpUtils.ResultCallback<String>() {
@Override
public void onSuccess(String response) {
String access = null;
String openId = null;
try {
JSONObject jsonObject = new JSONObject(response);
access = jsonObject.getString("access_token");
openId = jsonObject.getString("openid");
} catch (JSONException e) {
e.printStackTrace();
}
//获取个人信息
String getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access + "&openid=" + openId + "";
OkHttpUtils.ResultCallback<WeChatInfo> resultCallback = new OkHttpUtils.ResultCallback<WeChatInfo>() {
@Override
public void onSuccess(WeChatInfo response) {
response.setErrCode(resp.errCode);
if (response != null) WechatLoginObservable.getInstance().update(response);
finish();
}
@Override
public void onFailure(Exception e) {
Toast.makeText(WXEntryActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
};
OkHttpUtils.get(getUserInfo, resultCallback);
}
@Override
public void onFailure(Exception e) {
Toast.makeText(WXEntryActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
};
OkHttpUtils.get(getTokenUrl, resultCallback);
}
/**
* 更具不同的分享类型处理不同的 分享逻辑
*
* @param baseResp
*/
private void processShareType(BaseResp baseResp) {
String shareType = baseResp.transaction;
if (TextUtils.isEmpty(shareType)) return;
try {
Gson gson = new Gson();
ShareIntegralTypeInfo integralTypeInfo = gson.fromJson(shareType, ShareIntegralTypeInfo.class);
if ("witnessVideo".equals(integralTypeInfo.getShareType())) {
SharedPreferences preferences = this.getSharedPreferences("sharevideo", MODE_PRIVATE);
boolean status = preferences.getBoolean(UserService.getUid(this), false);
String newTime = DateTimeUtil.getCurrentTime();
String oldtime = preferences.getString(UserService.getUid(this) + "time", "2019-04-10");
boolean sameDay = DateTimeUtil.isSameDay(newTime, oldtime);
if (!status) {
ARouterUtil.addIntegrationPic(this, "share_video", integralTypeInfo.getContentId(), "sharevideo");
}
if (status && !sameDay) {
ARouterUtil.addIntegrationPic(this, "share_video", integralTypeInfo.getContentId(), "sharevideo");
}
} else if (integralTypeInfo.getShareType() != null) {
SharedPreferences preferences = this.getSharedPreferences("sharearticle", MODE_PRIVATE);
boolean status = preferences.getBoolean(UserService.getUid(this), false);
String newTime = DateTimeUtil.getCurrentTime();
String oldtime = preferences.getString(UserService.getUid(this) + "time", "2019-04-10");
boolean sameDay = DateTimeUtil.isSameDay(newTime, oldtime);
if (!status) {
ARouterUtil.addIntegrationPic(this, "share_article", integralTypeInfo.getContentId(), "sharearticle");
}
if (status && !sameDay) {
ARouterUtil.addIntegrationPic(this, "share_article", integralTypeInfo.getContentId(), "sharearticle");
}
}
} catch (Exception e) {
return;
}
}
}