This commit is contained in:
ChangBaofeng 2018-10-31 12:37:04 +08:00
Родитель 0e1821e346
Коммит 9856a9bf76
3 изменённых файлов: 74 добавлений и 54 удалений

Просмотреть файл

@ -25,7 +25,6 @@ import com.microsoft.cef.prodsdk.utils.CEFConstants;
import com.microsoft.cef.prodsdk.utils.CEFHttpsApi;
import com.microsoft.cef.prodsdk.utils.CEFErrorMessages;
import com.microsoft.cef.prodsdk.utils.CEFUtils;
import com.microsoft.cef.prodsdk.bean.CEFSocialLoginResultCode;
import com.tencent.connect.common.Constants;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
@ -36,11 +35,22 @@ import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import static com.microsoft.cef.prodsdk.bean.CEFSocialLoginChannel.*;
import static com.microsoft.cef.prodsdk.bean.CEFSocialLoginResultCode.CEFSocialLoginResultHttpError;
import static com.microsoft.cef.prodsdk.bean.CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure;
import static com.microsoft.cef.prodsdk.bean.CEFSocialLoginResultCode.CEFSocialLoginResultSuccess;
import static com.microsoft.cef.prodsdk.utils.CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_QQNOTCONFIG;
import static com.microsoft.cef.prodsdk.utils.CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_WECHATNOTSUPPORTAPI;
import static com.microsoft.cef.prodsdk.utils.CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_WEIBONOTCONFIG;
import static com.microsoft.cef.prodsdk.utils.CEFErrorMessages.CEFEM_WECHAT_NOTCONFIG;
import static com.microsoft.cef.prodsdk.utils.CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_WECHATNOTINSTALLED;
import static com.microsoft.cef.prodsdk.utils.CEFUtils.CEF_LOG;
public class CEFServiceSocialLogin {
@SuppressLint("StaticFieldLeak")
private static volatile CEFServiceSocialLogin instance;
private CEFResponseSocialLoginListener responseSocialLoginListener;
private CEFCredential credential;
private CEFCredential mCredential;
private static Activity mActivity;
private CEFSocialLoginListener localLoginListener;
@ -48,7 +58,7 @@ public class CEFServiceSocialLogin {
localLoginListener = new CEFSocialLoginListener() {
@Override
public void onComplete(CEFResponseSocialLogin result) {
if (result.getResultCode().equals(CEFSocialLoginResultCode.CEFSocialLoginResultSuccess)) {
if (result.getResultCode().equals(CEFSocialLoginResultSuccess)) {
getUserInfo(result);
} else {
responseSocialLoginListener.onComplete(result, null);
@ -58,7 +68,7 @@ public class CEFServiceSocialLogin {
}
public static CEFServiceSocialLogin getInstance(Activity activity) {
CEFServiceSocialLogin.mActivity = activity;
mActivity = activity;
if (null == instance) {
synchronized (CEFServiceSocialLogin.class) {
if (null == instance) {
@ -70,32 +80,32 @@ public class CEFServiceSocialLogin {
}
/**
* Setup payment channels by CEFCredential.
* Setup channels by CEFCredential.
*
* @param credential A CEFCredential instance contains AppKey,AppScerect for different channels.
* Will only setup channels has valid keys in credential.
*/
public void setupChannel(CEFCredential credential) {
this.credential = credential;
if (TextUtils.isEmpty(credential.getWeChatAppId()) ||
TextUtils.isEmpty(credential.getWeChatAppSecret())) {
Log.e(CEFUtils.CEF_LOG, CEFErrorMessages.CEFEM_WECHAT_NOTCONFIG);
mCredential = credential;
if (TextUtils.isEmpty(mCredential.getWeChatAppId()) ||
TextUtils.isEmpty(mCredential.getWeChatAppSecret())) {
Log.e(CEF_LOG, CEFEM_WECHAT_NOTCONFIG);
} else {
WeChatManager.getInstance().initWeChat(CEFServiceSocialLogin.mActivity, credential);
WeChatManager.getInstance().initWeChat(mActivity, mCredential);
}
if (TextUtils.isEmpty(credential.getWeiboAppkey()) ||
TextUtils.isEmpty(credential.getWeiboSecret()) ||
TextUtils.isEmpty(credential.getWeiboRedirectURL())) {
Log.e(CEFUtils.CEF_LOG, CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_WEIBONOTCONFIG);
if (TextUtils.isEmpty(mCredential.getWeiboAppkey()) ||
TextUtils.isEmpty(mCredential.getWeiboSecret()) ||
TextUtils.isEmpty(mCredential.getWeiboRedirectURL())) {
Log.e(CEF_LOG, CEFEM_CEFSOCIALLOGIN_WEIBONOTCONFIG);
} else {
WeiboManager.getInstance().initWeibo(CEFServiceSocialLogin.mActivity, credential);
WeiboManager.getInstance().initWeibo(mActivity, mCredential);
}
if (TextUtils.isEmpty(credential.getQQAppId())) {
Log.e(CEFUtils.CEF_LOG, CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_QQNOTCONFIG);
if (TextUtils.isEmpty(mCredential.getQQAppId())) {
Log.e(CEF_LOG, CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_QQNOTCONFIG);
} else {
QQManager.getInstance().initQQ(CEFServiceSocialLogin.mActivity, credential.getQQAppId());
QQManager.getInstance().initQQ(mActivity, mCredential.getQQAppId());
}
}
@ -120,50 +130,50 @@ public class CEFServiceSocialLogin {
case CEFSocialLoginChannel_WeChat:
if (!WeChatManager.getInstance().isWeChatInstall()) {
CEFResponseSocialLogin auth = new CEFResponseSocialLogin();
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_WECHATNOTINSTALLED);
auth.setChannel(CEFSocialLoginChannel.CEFSocialLoginChannel_WeChat);
auth.setResultCode(CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFEM_CEFSOCIALLOGIN_WECHATNOTINSTALLED);
auth.setChannel(CEFSocialLoginChannel_WeChat);
responseSocialLoginListener.onComplete(auth, null);
return;
}
if (!WeChatManager.getInstance().isWeChatSupportAPI()) {
CEFResponseSocialLogin auth = new CEFResponseSocialLogin();
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_WECHATNOTSUPPORTAPI);
auth.setChannel(CEFSocialLoginChannel.CEFSocialLoginChannel_WeChat);
auth.setResultCode(CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFEM_CEFSOCIALLOGIN_WECHATNOTSUPPORTAPI);
auth.setChannel(CEFSocialLoginChannel_WeChat);
responseSocialLoginListener.onComplete(auth, null);
return;
}
if (TextUtils.isEmpty(credential.getWeChatAppId()) ||
TextUtils.isEmpty(credential.getWeChatAppSecret())) {
if (TextUtils.isEmpty(mCredential.getWeChatAppId()) ||
TextUtils.isEmpty(mCredential.getWeChatAppSecret())) {
CEFResponseSocialLogin auth = new CEFResponseSocialLogin();
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFErrorMessages.CEFEM_WECHAT_NOTCONFIG);
auth.setChannel(CEFSocialLoginChannel.CEFSocialLoginChannel_WeChat);
auth.setResultCode(CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFEM_WECHAT_NOTCONFIG);
auth.setChannel(CEFSocialLoginChannel_WeChat);
responseSocialLoginListener.onComplete(auth, null);
return;
}
WeChatManager.getInstance().evokeWeChat(localLoginListener);
break;
case CEFSocialLoginChannel_Weibo:
if (TextUtils.isEmpty(credential.getWeiboAppkey()) ||
TextUtils.isEmpty(credential.getWeiboSecret()) ||
TextUtils.isEmpty(credential.getWeiboRedirectURL())) {
if (TextUtils.isEmpty(mCredential.getWeiboAppkey()) ||
TextUtils.isEmpty(mCredential.getWeiboSecret()) ||
TextUtils.isEmpty(mCredential.getWeiboRedirectURL())) {
CEFResponseSocialLogin auth = new CEFResponseSocialLogin();
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_WEIBONOTCONFIG);
auth.setChannel(CEFSocialLoginChannel.CEFSocialLoginChannel_Weibo);
auth.setResultCode(CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFEM_CEFSOCIALLOGIN_WEIBONOTCONFIG);
auth.setChannel(CEFSocialLoginChannel_Weibo);
responseSocialLoginListener.onComplete(auth, null);
return;
}
WeiboManager.getInstance().evokeWeibo(localLoginListener);
break;
case CEFSocialLoginChannel_QQ:
if (TextUtils.isEmpty(credential.getQQAppId())) {
if (TextUtils.isEmpty(mCredential.getQQAppId())) {
CEFResponseSocialLogin auth = new CEFResponseSocialLogin();
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFErrorMessages.CEFEM_CEFSOCIALLOGIN_QQNOTCONFIG);
auth.setChannel(CEFSocialLoginChannel.CEFSocialLoginChannel_QQ);
auth.setResultCode(CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFEM_CEFSOCIALLOGIN_QQNOTCONFIG);
auth.setChannel(CEFSocialLoginChannel_QQ);
responseSocialLoginListener.onComplete(auth, null);
return;
}
@ -171,7 +181,7 @@ public class CEFServiceSocialLogin {
break;
default:
CEFResponseSocialLogin auth = new CEFResponseSocialLogin();
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure);
auth.setResultCode(CEFSocialLoginResultTokenFailure);
auth.setResultDescription(CEFErrorMessages.CEFEM_CHANNEL_ERROR);
auth.setChannel(channel);
responseSocialLoginListener.onComplete(auth, null);
@ -184,7 +194,7 @@ public class CEFServiceSocialLogin {
* @param mIWXAPIEventHandler WX interface IWXAPIEventHandler
*/
public void onCreate(IWXAPIEventHandler mIWXAPIEventHandler) {
WeChatManager.getInstance().onCreate(CEFServiceSocialLogin.mActivity, mIWXAPIEventHandler);
WeChatManager.getInstance().onCreate(mActivity, mIWXAPIEventHandler);
}
/**
@ -256,13 +266,13 @@ public class CEFServiceSocialLogin {
String channelPropertiesStr = jsonDes.getString("channelProperties");
switch (channelNameStr) {
case "qq":
cefUserAuthProfile.setChannelName(CEFSocialLoginChannel.CEFSocialLoginChannel_QQ);
cefUserAuthProfile.setChannelName(CEFSocialLoginChannel_QQ);
break;
case "weibo":
cefUserAuthProfile.setChannelName(CEFSocialLoginChannel.CEFSocialLoginChannel_Weibo);
cefUserAuthProfile.setChannelName(CEFSocialLoginChannel_Weibo);
break;
case "wechat":
cefUserAuthProfile.setChannelName(CEFSocialLoginChannel.CEFSocialLoginChannel_WeChat);
cefUserAuthProfile.setChannelName(CEFSocialLoginChannel_WeChat);
}
Gson gson = new Gson();
Map mapChannelPro = new HashMap<String, Object>();
@ -270,8 +280,8 @@ public class CEFServiceSocialLogin {
cefUserAuthProfile.setChannelProperties(mapChannelPro);
responseSocialLoginListener.onComplete(auth, cefUserAuthProfile);
} catch (Exception e) {
Log.e(CEFUtils.CEF_LOG, e.toString());
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultUnknown);
Log.e(CEF_LOG, e.toString());
auth.setResultCode(CEFSocialLoginResultHttpError);
auth.setChannel(auth.getChannel());
responseSocialLoginListener.onComplete(auth, null);
}
@ -279,7 +289,7 @@ public class CEFServiceSocialLogin {
@Override
public void onFail(String fail) {
auth.setResultCode(CEFSocialLoginResultCode.CEFSocialLoginResultTokenFailure);
auth.setResultCode(CEFSocialLoginResultHttpError);
auth.setResultDescription(fail);
auth.setChannel(auth.getChannel());
responseSocialLoginListener.onComplete(auth, null);

Просмотреть файл

@ -23,6 +23,9 @@ import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import static com.microsoft.cef.prodsdk.utils.CEFUtils.CEF_LOG;
import static com.microsoft.cef.prodsdk.utils.CEFUtils.prepareSharedAccessToken;
public class CEFHttpsApi {
private static volatile CEFHttpsApi instance;
private static OkHttpClient okHttpClient = null;
@ -78,7 +81,7 @@ public class CEFHttpsApi {
Request request = new Request.Builder()
.url(url)
.addHeader("Account", CEFUtils.getLocalAccount(context))
.addHeader("Authorization", CEFUtils.prepareSharedAccessToken(context))
.addHeader("Authorization", prepareSharedAccessToken(context))
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
@ -86,7 +89,7 @@ public class CEFHttpsApi {
@Override
public void onFailure(Call call, IOException e) {
String errorMsg = "{" +
"errorMessage:'" + e.toString() + '\'' +
"error:'" + e.toString() + '\'' +
'}';
mCEFCallback.onFail(errorMsg);
}
@ -97,7 +100,11 @@ public class CEFHttpsApi {
if (response.isSuccessful()) {
mCEFCallback.onSuccess(responseLabel);
} else {
mCEFCallback.onFail(responseLabel);
String msg = "{" +
"Code:'" + response.code() + '\'' +
", errorMessage:'" + responseLabel + '\'' +
'}';
mCEFCallback.onFail(msg);
}
}
});
@ -117,14 +124,14 @@ public class CEFHttpsApi {
Request request = new Request.Builder()
.get()
.addHeader("Account", CEFUtils.getAccountName())
.addHeader("Authorization", CEFUtils.prepareSharedAccessToken(context))
.addHeader("Authorization", prepareSharedAccessToken(context))
.url(url)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String errorMsg = "{" +
"errorMessage:'" + e.toString() + '\'' +
"error:'" + e.toString() + '\'' +
'}';
mCEFCallback.onFail(errorMsg);
}
@ -135,7 +142,11 @@ public class CEFHttpsApi {
if (response.isSuccessful()) {
mCEFCallback.onSuccess(responseLabel);
} else {
mCEFCallback.onFail(responseLabel);
String msg = "{" +
"Code:'" + response.code() + '\'' +
", errorMessage:'" + responseLabel + '\'' +
'}';
mCEFCallback.onFail(msg);
}
}
});
@ -147,7 +158,6 @@ public class CEFHttpsApi {
return (networkInfo != null && networkInfo.isConnected());
}
public enum Type {
POST, GET
}

Двоичный файл не отображается.