240 lines
8.8 KiB
Java
240 lines
8.8 KiB
Java
package com.gh.common.util;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Context;
|
||
import android.widget.Toast;
|
||
|
||
import com.gh.base.AppController;
|
||
import com.gh.common.constant.Config;
|
||
import com.lightgame.utils.Utils;
|
||
import com.sina.weibo.sdk.WbSdk;
|
||
import com.sina.weibo.sdk.auth.AuthInfo;
|
||
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
|
||
import com.sina.weibo.sdk.auth.WbAuthListener;
|
||
import com.sina.weibo.sdk.auth.WbConnectErrorMessage;
|
||
import com.sina.weibo.sdk.auth.sso.SsoHandler;
|
||
import com.tencent.connect.UserInfo;
|
||
import com.tencent.connect.auth.QQToken;
|
||
import com.tencent.mm.sdk.openapi.IWXAPI;
|
||
import com.tencent.mm.sdk.openapi.SendAuth;
|
||
import com.tencent.mm.sdk.openapi.WXAPIFactory;
|
||
import com.tencent.tauth.IUiListener;
|
||
import com.tencent.tauth.Tencent;
|
||
import com.tencent.tauth.UiError;
|
||
|
||
import org.json.JSONException;
|
||
import org.json.JSONObject;
|
||
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.InputStream;
|
||
import java.net.HttpURLConnection;
|
||
import java.net.URL;
|
||
import java.text.SimpleDateFormat;
|
||
|
||
/**
|
||
* Created by khy on 14/06/17.
|
||
*/
|
||
|
||
public class LoginUtils {
|
||
|
||
private static LoginUtils instance;
|
||
|
||
private Context mContext;
|
||
|
||
private Tencent mTencent;
|
||
private IWXAPI mIWXAPI;
|
||
public SsoHandler mSsoHandler;
|
||
|
||
private Oauth2AccessToken mAccessToken; // wb
|
||
public static final String SCOPE =
|
||
"email,direct_messages_read,direct_messages_write,"
|
||
+ "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
|
||
+ "follow_app_official_microblog," + "invitation_write";
|
||
|
||
private LoginUtils(Context context) {
|
||
mTencent = Tencent.createInstance(Config.TENCENT_APPID, context); //初始化QQ分享
|
||
mIWXAPI = WXAPIFactory.createWXAPI(context, Config.WECHAT_APPID, true); //初始化微信分享
|
||
WbSdk.install(context, new AuthInfo(context, Config.WEIBO_APPKEY, "http://www.sina.com", SCOPE));
|
||
mSsoHandler = new SsoHandler((Activity) context);
|
||
mContext = context;
|
||
Utils.log(LoginUtils.class.getSimpleName(), "initLogin");
|
||
}
|
||
|
||
public static LoginUtils getInstance(Activity context) {
|
||
if (instance == null) {
|
||
instance = new LoginUtils(context);
|
||
}
|
||
return instance;
|
||
}
|
||
|
||
//QQ登录回调处理
|
||
public IUiListener QqLoginListener = new IUiListener() {
|
||
@Override
|
||
public void onComplete(Object o) {
|
||
Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQ 登录成功");
|
||
if (o instanceof JSONObject) {
|
||
JSONObject jsonObject = (JSONObject) o;
|
||
String s = jsonObject.toString();
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQLoginComplete::" + s);
|
||
try {
|
||
mTencent.setOpenId(jsonObject.getString("openid"));
|
||
mTencent.setAccessToken(jsonObject.getString("access_token"), jsonObject.getString("expires_in"));
|
||
} catch (JSONException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
}
|
||
|
||
QQToken qqToken = mTencent.getQQToken();
|
||
UserInfo userInfo = new UserInfo(mContext, qqToken);
|
||
userInfo.getUserInfo(new IUiListener() {
|
||
@Override
|
||
public void onComplete(Object o) {
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQUserInfo::" + o.toString());
|
||
}
|
||
|
||
@Override
|
||
public void onError(UiError uiError) {
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQUserInfoUiError::" + uiError.errorDetail + "==" + uiError.errorMessage + "==" + uiError.errorCode);
|
||
}
|
||
|
||
@Override
|
||
public void onCancel() {
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQUserInfoonCancel");
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void onError(UiError uiError) {
|
||
Toast.makeText(mContext, "登录失败", Toast.LENGTH_SHORT).show();
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQ 登录失败");
|
||
}
|
||
|
||
@Override
|
||
public void onCancel() {
|
||
Toast.makeText(mContext, "登录取消", Toast.LENGTH_SHORT).show();
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQ 登录取消");
|
||
}
|
||
};
|
||
|
||
|
||
public void QQLogin() {
|
||
if (mTencent != null && !mTencent.isSessionValid()) {
|
||
Utils.log(LoginUtils.class.getSimpleName(), "QQLogin");
|
||
mTencent.login((Activity) mContext, "all", QqLoginListener);
|
||
}
|
||
}
|
||
|
||
public void QQLogout() {
|
||
if (mTencent != null) {
|
||
mTencent.logout(mContext);
|
||
}
|
||
}
|
||
|
||
public void WCLogin() {
|
||
if (mIWXAPI != null) {
|
||
boolean register = mIWXAPI.registerApp("wx3ffd0785fad18396");
|
||
|
||
SendAuth.Req req = new SendAuth.Req();
|
||
req.scope = "all";
|
||
req.state = "光环助手";
|
||
boolean b = mIWXAPI.sendReq(req);
|
||
Utils.log(LoginUtils.class.getSimpleName(), "微信注册状态::" + register + "\n 发送状态::" + b);
|
||
}
|
||
}
|
||
|
||
public void WeiBoLogin() {
|
||
mSsoHandler.authorizeClientSso(new SelfWbAuthListener());
|
||
// 第一次启动本应用,AccessToken 不可用
|
||
mAccessToken = AccessTokenKeeper.readAccessToken(mContext);
|
||
if (mAccessToken.isSessionValid()) {
|
||
updateTokenView(true);
|
||
}
|
||
}
|
||
|
||
private class SelfWbAuthListener implements WbAuthListener {
|
||
@Override
|
||
public void onSuccess(final Oauth2AccessToken token) {
|
||
((Activity) mContext).runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
mAccessToken = token;
|
||
if (mAccessToken.isSessionValid()) {
|
||
// 显示 Token
|
||
updateTokenView(false);
|
||
// 保存 Token 到 SharedPreferences
|
||
AccessTokenKeeper.writeAccessToken(mContext, mAccessToken);
|
||
Toast.makeText(mContext, "授权成功", Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
});
|
||
|
||
AppController.MAIN_EXECUTOR.execute(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
getWeiBoUserInfo(token.getToken(), token.getUid());
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void cancel() {
|
||
Toast.makeText(mContext, "取消授权", Toast.LENGTH_LONG).show();
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(WbConnectErrorMessage errorMessage) {
|
||
Toast.makeText(mContext, errorMessage.getErrorMessage(), Toast.LENGTH_LONG).show();
|
||
}
|
||
}
|
||
|
||
private void getWeiBoUserInfo(String accessToken, String uid) {
|
||
String path = "https://api.weibo.com/2/users/show.json?access_token=" + accessToken + "&uid=" + uid;
|
||
Utils.log(LoginUtils.class.getSimpleName(), "getWeiBoUserInfo-url::" + path);
|
||
|
||
try {
|
||
URL url = new URL(path);
|
||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||
conn.setConnectTimeout(5000);
|
||
conn.setRequestMethod("GET");
|
||
conn.setDoInput(true);
|
||
int code = conn.getResponseCode();
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||
byte[] buffer = new byte[1024];
|
||
int len = 0;
|
||
Utils.log(LoginUtils.class.getSimpleName(), "getWeiBoUserInfo-RequestCode::" + code);
|
||
if (code == 200) {
|
||
InputStream is = conn.getInputStream();
|
||
while ((len = is.read(buffer)) != -1) {
|
||
baos.write(buffer, 0, len);
|
||
}
|
||
String str = new String(baos.toByteArray());
|
||
Utils.log(LoginUtils.class.getSimpleName(), "getWeiBoUserInfo-Body::" + str);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 显示当前 Token 信息。
|
||
*
|
||
* @param hasExisted 配置文件中是否已存在 token 信息并且合法
|
||
*/
|
||
private void updateTokenView(boolean hasExisted) {
|
||
String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(
|
||
new java.util.Date(mAccessToken.getExpiresTime()));
|
||
String format = "Token:%1$s \\n有效期:%2$s";
|
||
String token = String.format(format, mAccessToken.getToken(), date);
|
||
Utils.log(LoginUtils.class.getSimpleName(), "::WB_TOKEN::" + token);
|
||
String message = String.format(format, mAccessToken.getToken(), date);
|
||
if (hasExisted) {
|
||
message = "Token 仍在有效期内,无需再次登录。" + "\n" + message;
|
||
}
|
||
Utils.log(LoginUtils.class.getSimpleName(), "::WB_MESSAGE::" + message);
|
||
}
|
||
|
||
}
|