登录模块界面
This commit is contained in:
@ -8,6 +8,7 @@ import com.gh.common.constant.Config;
|
||||
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;
|
||||
@ -19,6 +20,7 @@ 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.text.SimpleDateFormat;
|
||||
@ -35,7 +37,7 @@ public class LoginUtils {
|
||||
|
||||
private Tencent mTencent;
|
||||
private IWXAPI mIWXAPI;
|
||||
private SsoHandler mSsoHandler;
|
||||
public SsoHandler mSsoHandler;
|
||||
|
||||
private Oauth2AccessToken mAccessToken; // wb
|
||||
public static final String SCOPE =
|
||||
@ -49,7 +51,7 @@ public class LoginUtils {
|
||||
WbSdk.install(context,new AuthInfo(context, Config.WEIBO_APPKEY, "http://www.sina.com", SCOPE));
|
||||
mSsoHandler = new SsoHandler((Activity) context);
|
||||
mContext = context;
|
||||
Utils.log("LoginUtils", "initLogin");
|
||||
Utils.log(LoginUtils.class.getSimpleName(), "initLogin");
|
||||
}
|
||||
|
||||
public static LoginUtils getInstance(Activity context) {
|
||||
@ -64,9 +66,18 @@ public class LoginUtils {
|
||||
@Override
|
||||
public void onComplete(Object o) {
|
||||
Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();
|
||||
Utils.log(LoginUtils.class.getSimpleName(), "QQ 登录成功");
|
||||
if (o instanceof JSONObject) {
|
||||
String s = o.toString();
|
||||
Utils.log("LoginUtils", "QQLoginComplete::" + s);
|
||||
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();
|
||||
@ -74,17 +85,17 @@ public class LoginUtils {
|
||||
userInfo.getUserInfo(new IUiListener() {
|
||||
@Override
|
||||
public void onComplete(Object o) {
|
||||
Utils.log("LoginUtils", "QQUserInfo::" + o.toString());
|
||||
Utils.log(LoginUtils.class.getSimpleName(), "QQUserInfo::" + o.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(UiError uiError) {
|
||||
Utils.log("LoginUtils", "QQUserInfoUiError::" + uiError.errorDetail + "==" + uiError.errorMessage + "==" + uiError.errorCode);
|
||||
Utils.log(LoginUtils.class.getSimpleName(), "QQUserInfoUiError::" + uiError.errorDetail + "==" + uiError.errorMessage + "==" + uiError.errorCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
Utils.log("LoginUtils", "QQUserInfoonCancel");
|
||||
Utils.log(LoginUtils.class.getSimpleName(), "QQUserInfoonCancel");
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -92,17 +103,20 @@ public class LoginUtils {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@ -121,15 +135,20 @@ public class LoginUtils {
|
||||
req.scope = "all";
|
||||
req.state = "光环助手";
|
||||
boolean b = mIWXAPI.sendReq(req);
|
||||
Utils.log("LoginUtils", "微信注册状态::" + register + "\n 发送状态::" + b);
|
||||
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 com.sina.weibo.sdk.auth.WbAuthListener{
|
||||
private class SelfWbAuthListener implements WbAuthListener {
|
||||
@Override
|
||||
public void onSuccess(final Oauth2AccessToken token) {
|
||||
((Activity) mContext).runOnUiThread(new Runnable() {
|
||||
@ -168,12 +187,12 @@ public class LoginUtils {
|
||||
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::WB_TOKEN::" + token);
|
||||
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::WB_MESSAGE::" + message);
|
||||
Utils.log(LoginUtils.class.getSimpleName(), "::WB_MESSAGE::" + message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user