package com.hdl.linkpm.sdk.user.controller; import com.google.gson.JsonObject; import com.hdl.hdlhttp.HxHttp; import com.hdl.linkpm.sdk.HDLLinkPMSdk; import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi; import com.hdl.linkpm.sdk.core.callback.IDefaultCallBack; import com.hdl.linkpm.sdk.core.callback.IDefaultStrCallBack; import com.hdl.linkpm.sdk.core.callback.IResponseCallBack; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.linkpm.sdk.core.response.HDLResponse; import com.hdl.linkpm.sdk.user.bean.HDLLoginBean; import com.hdl.linkpm.sdk.user.bean.HDLMemberInfoBean; import com.hdl.linkpm.sdk.user.bean.HDLUserRegionBean; import com.hdl.linkpm.sdk.user.bean.ImageSubmitInfo; import com.hdl.linkpm.sdk.user.bean.MqttInfo; import com.hdl.linkpm.sdk.user.bean.NewVersionBean; import com.hdl.linkpm.sdk.user.bean.UploadImageRequest; import com.hdl.linkpm.sdk.user.callback.ILoginCallBack; import com.hdl.linkpm.sdk.user.callback.IMemberInfoBack; import com.hdl.linkpm.sdk.user.callback.IRegionByAccountCallBack; import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; import com.hdl.linkpm.sdk.utils.HDLGsonUtils; import java.io.File; import io.reactivex.rxjava3.disposables.Disposable; /** * Created by jlchen on 12/3/21. * B端用户相关的网络请求 */ public class HDLPMUserController { /** * instance */ private volatile static HDLPMUserController instance; /** * getInstance * * @return HDLUserController */ public static synchronized HDLPMUserController getInstance() { if (instance == null) { synchronized (HDLPMUserController.class) { if (instance == null) { instance = new HDLPMUserController(); } } } return instance; } /** * 获取账号所在区域 * * @param account * @param callBack * @return https://nearest.hdlcontrol.com/smart-footstone/region/regionByUserAccount */ public Disposable regionByAccount(String account, IRegionByAccountCallBack callBack) { String requestUrl = HDLLinkPMSdk.getInitUrl() + HDLCloudUserApi.POST_RegionByAccount; return HxHttp.builder() .url(requestUrl) .params("account", account) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(HDLUserRegionBean response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, account, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 登录 * 通过账号和密码 * * @param isBooleanB true=B,false=C * @param account 手机或者邮箱 * @param loginPwd 密码 * @param callBack 回调 * @return - */ public Disposable loginByPassword(boolean isBooleanB, String account, String loginPwd, ILoginCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_LOGIN); if (!isBooleanB) { requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.C_POST_LOGIN); } JsonObject json = new JsonObject(); json.addProperty("account", account); json.addProperty("loginPwd", loginPwd); // json.addProperty("platform", "APP"); json.addProperty("grantType", "password"); String finalRequestUrl = requestUrl; return HxHttp.builder() .url(requestUrl) .raw(json.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(HDLLoginBean response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(finalRequestUrl, json, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 登录 * 通过验证码 * * @param account 手机或者邮箱 * @param vCode 验证码 * @param callBack 回调 * @return */ public Disposable loginByCode(String account, String vCode, ILoginCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_LOGIN); JsonObject json = new JsonObject(); json.addProperty("account", account); json.addProperty("verifyCode", vCode); // json.addProperty("platform", "APP"); json.addProperty("grantType", "verify"); return HxHttp.builder() .url(requestUrl) .raw(json.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(HDLLoginBean response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, json, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 通用 发送验证码方法 * LanguageTypeEnum CHINESE,ENGLISH * * @param verifyType 1:注册 2:找回密码 3:绑定 4:验证码登陆 5:敏感数据 * @param account 邮箱或者手机号 * @param isPhone 是否手机 * @param phoneZoneCode 手机国家区号 * @param languageType 语言 * @param callBack * @return */ public Disposable verificationCodeSend(int verifyType, String account, Boolean isPhone, String phoneZoneCode, String languageType, IDefaultCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_VERIFICATION_CODE_SEND); JsonObject json = new JsonObject(); json.addProperty("verifyType", verifyType); json.addProperty("languageType", languageType); json.addProperty("expireSecond", 300);////验证码过期时间(秒),默认5分钟 //是否手机 if (isPhone) { json.addProperty("phone", account); json.addProperty("phonePrefix", phoneZoneCode); } else { json.addProperty("mail", account); } return HxHttp.builder() .url(requestUrl) .raw(json.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(String response) { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, json, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 忘记密码,重置密码 * * @param account 邮箱或者手机号 * @param password 新密码 * @param vCode 验证码 * @param isPhone 是否手机 * @param callBack 回调 */ public Disposable forgetPassword(String account, String password, String vCode, Boolean isPhone, IDefaultCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_FORGET_PROGRAM_PWD); JsonObject json = new JsonObject(); json.addProperty("loginPwd", password); json.addProperty("verifyCode", vCode); //是否手机 if (isPhone) { json.addProperty("userPhone", account); } else { json.addProperty("userEmail", account); } return HxHttp.builder() .url(requestUrl) .raw(json.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(String response) { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, json, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 更改个人密码 * * @param userId 当前用户ID * @param loginOldPwd 老密码 * @param loginPwd 新密码 * @param callBack 回调 */ public Disposable updateProgramPassword(String userId, String loginOldPwd, String loginPwd, IDefaultCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_UPDATE_PROGRAM_PASSWORD); JsonObject json = new JsonObject(); json.addProperty("userId", userId); json.addProperty("loginOldPwd", loginOldPwd); json.addProperty("loginPwd", loginPwd); json.addProperty("confirmLoginPwd", loginPwd); return HxHttp.builder() .url(requestUrl) .raw(json.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(String response) { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, json, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 获取用户个人信息 * * @param callBack */ public Disposable getMemberInfo(IMemberInfoBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_GET_MEMBER_INFO); return HxHttp.builder() .url(requestUrl) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(HDLMemberInfoBean response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, null, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 更新个人信息 * memberHeadIcon、memberName、memberBirthday、languageType、memberSex * * @param callBack */ public Disposable updateMemberInfo(JsonObject updateInfo, IDefaultCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_UPDATE_MEMBER_INFO); return HxHttp.builder() .url(requestUrl) .raw(updateInfo.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(String response) { if (callBack != null) { callBack.onSuccess(); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, updateInfo, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 获取用户头像路径 * * @param memberHeadIconKey 用户头像imageKey * @param callBack */ public Disposable getHeadImageUrl(String memberHeadIconKey, IDefaultStrCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_GET_IMAGE_URL); return HxHttp.builder() .url(requestUrl) .params("imageKey", memberHeadIconKey) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(String response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, memberHeadIconKey, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 上传用户头像 * * @param userId 用户id * @param imageBytes 头像图片 * @param callBack * @return */ public Disposable uploadUserHeadImage(String userId, byte[] imageBytes, IDefaultStrCallBack callBack) { UploadImageRequest uploadImageRequest = new UploadImageRequest(); uploadImageRequest.setPrefix("Icon" + System.currentTimeMillis()); uploadImageRequest.setUid(userId); uploadImageRequest.setFileName("HeadImage.png"); uploadImageRequest.setContent(imageBytes); String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_UPLOAD_IMAGE); return HxHttp.builder() .url(requestUrl) .raw(HDLGsonUtils.toJson(uploadImageRequest)) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(String response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, uploadImageRequest, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 上传用户头像(新) * * @param file 图片 * @param callBack * @return */ public Disposable uplfadUserHeadImage(File file, IResponseCallBack callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_UPLOAD_IMAGE_NEW); return HxHttp.builder() .url(requestUrl) .file(file) .build() .upload() .subscribeWith(new HDLResponse() { @Override public void onResponse(ImageSubmitInfo response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, null, e); if (callBack != null) { callBack.onFailure(e); } } }); } // /** // * 版本检测更新 // * // * @param appName app名称 // * @param version 版本号 // * @param callBack 如果onSuccess 回调的bean为空,代表当前版本已经是最新版本,没有新版本 // * @return // */ // public Disposable checkAppVersion(String appName, String version, IResponseCallBack callBack) { // JsonObject json = new JsonObject(); // json.addProperty("appName", appName); // json.addProperty("version", version); // json.addProperty("version", version); // String requestUrl = HDLLinkPMSdk.getRegionUrl() + HDLCloudUserApi.POST_APP_APPVERSION_CHECK; // return HxHttp.builder() // .url(requestUrl) // .raw(json.toString()) // .build() // .post() // .subscribeWith(new HDLResponse() { // @Override // public void onResponse(NewVersionBean response) { // if (callBack != null) { // callBack.onSuccess(response); // } // } // // @Override // public void onFailure(HDLException e) { // if (callBack != null) { // callBack.onFailure(e); // } // } // }); // } /** * 版本检测更新 * 支持Android、iOS、wgt检测 * * @param appCode app编码 * @param version 版本号 * @param releaseSystem 发布系统,IOS、Android, wgt * @param callBack 如果onSuccess 回调的bean为空,代表当前版本已经是最新版本,没有新版本 * @return */ public Disposable checkAppVersion(String appCode, String version, String releaseSystem, IResponseCallBack callBack) { JsonObject json = new JsonObject(); json.addProperty("appCode", appCode); json.addProperty("version", version); json.addProperty("releaseSystem", releaseSystem); String requestUrl = HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.POST_APP_APPVERSION_CHECK); return HxHttp.builder() .url(requestUrl) .raw(json.toString()) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(NewVersionBean response) { if (callBack != null) { callBack.onSuccess(response); } } @Override public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, json, e); if (callBack != null) { callBack.onFailure(e); } } }); } /** * 获取mqtt信息 * * @return */ public static Disposable getMqttInfo(String attachClientId, String homeType, String deviceUuid, IResponseCallBack listener) { return HxHttp.builder() .url(HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.MQTT_INFO_URL)) .params("attachClientId", attachClientId) .params("homeType", homeType) .params("deviceUuid", deviceUuid) .build() .post() .subscribeWith(new HDLResponse() { @Override public void onResponse(MqttInfo response) { if (listener != null) { listener.onSuccess(response); } } @Override public void onFailure(HDLException e) { JsonObject json = new JsonObject(); json.addProperty("attachClientId", attachClientId); json.addProperty("homeType", homeType); json.addProperty("deviceUuid", deviceUuid); HDLExceptionSubmitUtils.submit(HDLCloudUserApi.getRequestUrl(HDLCloudUserApi.MQTT_INFO_URL), json, e); if (listener != null) { listener.onFailure(e); } } }); } }