Commit bcd995f534b6c6d42de017790434d77a44b07293
1 parent
67acd2ba
三方登录功能抽取
Showing
3 changed files
with
163 additions
and
87 deletions
cloud/third_tool/src/main/java/com/cnlive/login/action/UserThirdAction.java
| 1 | 1 | package com.cnlive.login.action; |
| 2 | 2 | |
| 3 | +import android.accounts.Account; | |
| 3 | 4 | import android.content.Context; |
| 5 | +import android.text.TextUtils; | |
| 6 | + | |
| 7 | +import com.cnlive.cloud.user.auth.UserService; | |
| 8 | +import com.cnlive.cloud.user.model.CNLiveUserInfo; | |
| 9 | +import com.cnlive.core.network.HttpConn; | |
| 10 | +import com.cnlive.core.network.NetCallBack; | |
| 11 | +import com.cnlive.core.network.model.BaseResult; | |
| 12 | +import com.cnlive.core.network.model.UserData; | |
| 13 | + | |
| 14 | +import io.reactivex.Observable; | |
| 4 | 15 | |
| 5 | 16 | //import com.cnlive.strike.user.model.CNLiveUserInfo; |
| 6 | 17 | |
| ... | ... | @@ -11,23 +22,89 @@ import android.content.Context; |
| 11 | 22 | public class UserThirdAction { |
| 12 | 23 | |
| 13 | 24 | |
| 14 | -// public static void otherLogin(Context context, String thirdPartyPlat, String thirdPartyId, Event<CNLiveUserInfo> event) { | |
| 15 | -// Logic.create() | |
| 16 | -// .action(UserThirdUtilAction.otherLogin(context, thirdPartyPlat, thirdPartyId)) | |
| 17 | -// .action(loginAppAction(context, "", "", true)) | |
| 18 | -// .action(loginIMAction()) | |
| 19 | -// .start(event); | |
| 20 | -// } | |
| 21 | -// | |
| 22 | -// | |
| 23 | -// public static void otherLoginBindMobile(Context context, String phone, String countryCode, String ems, | |
| 24 | -// String thirdPartyPlat, String thirdPartyId, String nickName, String gender, | |
| 25 | -// String location, String faceUrl, Event<CNLiveUserInfo> event) { | |
| 26 | -// Logic.create() | |
| 27 | -// .action(UserThirdUtilAction.otherLoginBindMobile(context, phone, countryCode, ems, | |
| 28 | -// thirdPartyPlat, thirdPartyId, nickName, gender, location, faceUrl)) | |
| 29 | -// .action(loginAppAction(context, "", "", true)) | |
| 30 | -// .action(loginIMAction()) | |
| 31 | -// .start(event); | |
| 32 | -// } | |
| 25 | + private static final int MAX_USER_COUNT = 1; | |
| 26 | + | |
| 27 | + public static void otherLogin(Context context, String thirdPartyPlat, String thirdPartyId, CallBack callBack) { | |
| 28 | + | |
| 29 | + HttpConn.action(UserThirdUtilAction.otherLogin(context, thirdPartyPlat, thirdPartyId), new NetCallBack<BaseResult<UserData>>() { | |
| 30 | + @Override | |
| 31 | + public void onRequestState(int state) { | |
| 32 | + if (null != callBack) { | |
| 33 | + callBack.callback(state, "", null); | |
| 34 | + } | |
| 35 | + } | |
| 36 | + | |
| 37 | + @Override | |
| 38 | + public void onSuccess(BaseResult<UserData> data) { | |
| 39 | + loginAppAction(data, context, "", "", true, callBack); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public void onFailure(String errCode, String errMsg, Exception e) { | |
| 44 | + if (null != callBack) { | |
| 45 | + callBack.callback(Integer.parseInt(errCode), errMsg, null); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + }); | |
| 49 | + } | |
| 50 | + | |
| 51 | + | |
| 52 | + public static void otherLoginBindMobile(Context context, String phone, String countryCode, String ems, | |
| 53 | + String thirdPartyPlat, String thirdPartyId, String nickName, String gender, | |
| 54 | + String location, String faceUrl, CallBack callBack) { | |
| 55 | + | |
| 56 | + HttpConn.action(UserThirdUtilAction.otherLoginBindMobile(context, phone, countryCode, ems, | |
| 57 | + thirdPartyPlat, thirdPartyId, nickName, gender, location, faceUrl), new NetCallBack<BaseResult<UserData>>() { | |
| 58 | + @Override | |
| 59 | + public void onRequestState(int state) { | |
| 60 | + if (null != callBack) { | |
| 61 | + callBack.callback(state, "", null); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public void onSuccess(BaseResult<UserData> data) { | |
| 67 | + loginAppAction(data,context, "", "", true, callBack); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public void onFailure(String errCode, String errMsg, Exception e) { | |
| 72 | + | |
| 73 | + } | |
| 74 | + }); | |
| 75 | + } | |
| 76 | + | |
| 77 | + | |
| 78 | + private static Observable<CNLiveUserInfo> loginAppAction(BaseResult<UserData> data, Context context, String userName, String userPassword, boolean otherLogin, CallBack callBack) { | |
| 79 | + CNLiveUserInfo userinfo = null; | |
| 80 | + if (null == data || null == data.getData()) { | |
| 81 | + if (null != callBack) { | |
| 82 | + callBack.callback(-5, "获取账户信息失败 请重试.", null); | |
| 83 | + } | |
| 84 | + } else if (TextUtils.isEmpty(data.getData().getMobile())) { | |
| 85 | + callBack.callback(-5, "绑定手机号已失效..", null); | |
| 86 | + } else { | |
| 87 | + //更新登录用户信息 | |
| 88 | + UserService userService = UserService.getInstance(context); | |
| 89 | + userService.loginAppAccount( | |
| 90 | + otherLogin ? data.getData().getMobile() : userName, | |
| 91 | + otherLogin ? data.getData().getMobile() : userPassword, | |
| 92 | + data.getData()); | |
| 93 | + //保留近期登录用户 | |
| 94 | + Account[] accounts = userService.getUserList(); | |
| 95 | + userinfo = userService.getAppAccount(); | |
| 96 | + if (accounts.length <= MAX_USER_COUNT) { | |
| 97 | + callBack.callback(0, "", userinfo); | |
| 98 | + } else { | |
| 99 | + CNLiveUserInfo finalUserinfo = userinfo; | |
| 100 | + userService.removeUser(accounts[0], future -> callBack.callback(0, "", finalUserinfo)); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + return Observable.just(userinfo); | |
| 104 | + } | |
| 105 | + | |
| 106 | + | |
| 107 | + public interface CallBack { | |
| 108 | + void callback(int state, String msg, Object object); | |
| 109 | + } | |
| 33 | 110 | } | ... | ... |
cloud/third_tool/src/main/java/com/cnlive/login/action/UserThirdUtilAction.java
| ... | ... | @@ -3,82 +3,81 @@ package com.cnlive.login.action; |
| 3 | 3 | import android.content.Context; |
| 4 | 4 | |
| 5 | 5 | import com.cnlive.core.libs.base.application.AppConfig; |
| 6 | +import com.cnlive.core.network.api.ApiBaseServiceOpen; | |
| 7 | +import com.cnlive.core.network.model.BaseResult; | |
| 6 | 8 | import com.cnlive.core.network.model.UserData; |
| 9 | +import com.cnlive.core.network.request.BaseRequest; | |
| 7 | 10 | |
| 8 | 11 | import java.util.HashMap; |
| 9 | 12 | import java.util.Map; |
| 10 | 13 | |
| 14 | +import io.reactivex.Observable; | |
| 15 | + | |
| 11 | 16 | /** |
| 12 | 17 | * created by hanyayun |
| 13 | 18 | * on 2019/12/4 |
| 14 | 19 | */ |
| 15 | 20 | public class UserThirdUtilAction { |
| 16 | 21 | |
| 17 | -// /** | |
| 18 | -// * 其他平台账户登录 | |
| 19 | -// * | |
| 20 | -// * @param context | |
| 21 | -// * @param thirdPartyPlat 平台类型 1:sina 2:qq 3:weixin 4:renren | |
| 22 | -// * @param thirdPartyId 第三方用户ID | |
| 23 | -// * [qq: 建议使用openid, wx: 建议使用unionid, sina: 建议使用idstr]; | |
| 24 | -// * 注意:同一用户在登录Sp下不同app时,thirdPartyId必须相同 | |
| 25 | -// */ | |
| 26 | -// public static Logic.Action<Object, BaseInfo<UserData>> otherLogin(Context context, String thirdPartyPlat, String thirdPartyId) { | |
| 27 | -// return (data, callback) -> { | |
| 28 | -// Map<String, String> requestMap = new HashMap<>(); | |
| 29 | -// | |
| 30 | -// requestMap.put("appId", AppConfig.getAppId()); | |
| 31 | -// requestMap.put("thirdPartyPlat", thirdPartyPlat); | |
| 32 | -// requestMap.put("thirdPartyId", thirdPartyId); | |
| 33 | -// requestMap.put("frmId", AppConfig.getAppChannel()); | |
| 34 | -// requestMap.put("clientPlat", "a"); | |
| 35 | -// | |
| 36 | -// return ApiRequest.service(ApiServiceOpen.class, service -> service.loginIn3rdForWJJ(requestMap)).subscribe(context, callback); | |
| 37 | -// }; | |
| 38 | -// } | |
| 39 | -// | |
| 40 | -// /** | |
| 41 | -// * 其他平台账户版定手机号并登录 | |
| 42 | -// * | |
| 43 | -// * @param context | |
| 44 | -// * @param thirdPartyPlat 平台类型 1:sina 2:qq 3:weixin 4:renren | |
| 45 | -// * @param thirdPartyId 第三方用户ID | |
| 46 | -// * [qq: 建议使用openid, wx: 建议使用unionid, sina: 建议使用idstr]; | |
| 47 | -// * 注意:同一用户在登录Sp下不同app时,thirdPartyId必须相同 | |
| 48 | -// */ | |
| 49 | -// | |
| 50 | -// | |
| 51 | -// public static Logic.Action<Object, BaseInfo<UserData>> otherLoginBindMobile(Context context, | |
| 52 | -// String phone, | |
| 53 | -// String countryCode, | |
| 54 | -// String ems, | |
| 55 | -// String thirdPartyPlat, | |
| 56 | -// String thirdPartyId, | |
| 57 | -// String nickName, | |
| 58 | -// String gender, | |
| 59 | -// String location, | |
| 60 | -// String faceUrl) { | |
| 61 | -// return (data, callback) -> { | |
| 62 | -// Map<String, String> requestMap = new HashMap<>(); | |
| 63 | -// | |
| 64 | -// requestMap.put("frmId", AppConfig.getAppChannel()); | |
| 65 | -// requestMap.put("clientPlat", "a"); | |
| 66 | -// requestMap.put("uuid", AppConfig.getAppUUID()); | |
| 67 | -// | |
| 68 | -// requestMap.put("appId", AppConfig.getAppId()); | |
| 69 | -// requestMap.put("mobile", phone); | |
| 70 | -// requestMap.put("countryCode", countryCode); | |
| 71 | -// requestMap.put("verificationCode", ems); | |
| 72 | -// | |
| 73 | -// requestMap.put("thirdPartyPlat", thirdPartyPlat); | |
| 74 | -// requestMap.put("thirdPartyId", thirdPartyId); | |
| 75 | -// requestMap.put("nickName", nickName); | |
| 76 | -// requestMap.put("gender", gender); | |
| 77 | -// requestMap.put("location", location); | |
| 78 | -// requestMap.put("faceUrl", faceUrl); | |
| 79 | -// | |
| 80 | -// | |
| 81 | -// return ApiRequest.service(ApiServiceOpen.class, service -> service.loginIn3rdAndMobileForWJJ(requestMap)).subscribe(context, callback); | |
| 82 | -// }; | |
| 83 | -// } | |
| 22 | + /** | |
| 23 | + * 其他平台账户登录 | |
| 24 | + * | |
| 25 | + * @param context | |
| 26 | + * @param thirdPartyPlat 平台类型 1:sina 2:qq 3:weixin 4:renren | |
| 27 | + * @param thirdPartyId 第三方用户ID | |
| 28 | + * [qq: 建议使用openid, wx: 建议使用unionid, sina: 建议使用idstr]; | |
| 29 | + * 注意:同一用户在登录Sp下不同app时,thirdPartyId必须相同 | |
| 30 | + */ | |
| 31 | + public static Observable<BaseResult<UserData>> otherLogin(Context context, String thirdPartyPlat, String thirdPartyId) { | |
| 32 | + | |
| 33 | + Map<String, String> requestMap = new HashMap<>(); | |
| 34 | + requestMap.put("appId", AppConfig.getAppId()); | |
| 35 | + requestMap.put("thirdPartyPlat", thirdPartyPlat); | |
| 36 | + requestMap.put("thirdPartyId", thirdPartyId); | |
| 37 | + requestMap.put("frmId", AppConfig.getAppChannel()); | |
| 38 | + requestMap.put("clientPlat", "a"); | |
| 39 | + return BaseRequest.init().service(ApiBaseServiceOpen.class).loginIn3rdForWJJ(requestMap); | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * 其他平台账户版定手机号并登录 | |
| 44 | + * | |
| 45 | + * @param context | |
| 46 | + * @param thirdPartyPlat 平台类型 1:sina 2:qq 3:weixin 4:renren | |
| 47 | + * @param thirdPartyId 第三方用户ID | |
| 48 | + * [qq: 建议使用openid, wx: 建议使用unionid, sina: 建议使用idstr]; | |
| 49 | + * 注意:同一用户在登录Sp下不同app时,thirdPartyId必须相同 | |
| 50 | + */ | |
| 51 | + | |
| 52 | + | |
| 53 | + public static Observable<BaseResult<UserData>> otherLoginBindMobile(Context context, | |
| 54 | + String phone, | |
| 55 | + String countryCode, | |
| 56 | + String ems, | |
| 57 | + String thirdPartyPlat, | |
| 58 | + String thirdPartyId, | |
| 59 | + String nickName, | |
| 60 | + String gender, | |
| 61 | + String location, | |
| 62 | + String faceUrl) { | |
| 63 | + Map<String, String> requestMap = new HashMap<>(); | |
| 64 | + | |
| 65 | + requestMap.put("frmId", AppConfig.getAppChannel()); | |
| 66 | + requestMap.put("clientPlat", "a"); | |
| 67 | + requestMap.put("uuid", AppConfig.getAppUUID()); | |
| 68 | + | |
| 69 | + requestMap.put("appId", AppConfig.getAppId()); | |
| 70 | + requestMap.put("mobile", phone); | |
| 71 | + requestMap.put("countryCode", countryCode); | |
| 72 | + requestMap.put("verificationCode", ems); | |
| 73 | + | |
| 74 | + requestMap.put("thirdPartyPlat", thirdPartyPlat); | |
| 75 | + requestMap.put("thirdPartyId", thirdPartyId); | |
| 76 | + requestMap.put("nickName", nickName); | |
| 77 | + requestMap.put("gender", gender); | |
| 78 | + requestMap.put("location", location); | |
| 79 | + requestMap.put("faceUrl", faceUrl); | |
| 80 | + | |
| 81 | + return BaseRequest.init().service(ApiBaseServiceOpen.class).loginIn3rdAndMobileForWJJ(requestMap); | |
| 82 | + } | |
| 84 | 83 | } | ... | ... |