package com.hdl.photovoltaic.other;
|
|
|
import com.google.gson.Gson;
|
import com.google.gson.JsonObject;
|
import com.hdl.photovoltaic.ui.bean.LoginBean;
|
import com.hdl.photovoltaic.ui.bean.LoginUserRegionBean;
|
import com.hdl.photovoltaic.bean.HttpResponsePack;
|
import com.hdl.photovoltaic.config.AppConfigManage;
|
import com.hdl.photovoltaic.config.UserConfigManage;
|
import com.hdl.photovoltaic.internet.HttpClient;
|
import com.hdl.photovoltaic.internet.api.HttpApi;
|
import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
|
import com.hdl.photovoltaic.listener.CloudCallBeak;
|
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 登录界面的逻辑
|
*/
|
public class HdlAccountLogic {
|
|
private static volatile HdlAccountLogic sHdlAccountLogic;
|
|
/**
|
* 获取当前对象
|
*
|
* @return HdlAccountLogic
|
*/
|
public static synchronized HdlAccountLogic getInstance() {
|
if (sHdlAccountLogic == null) {
|
synchronized (HdlAccountLogic.class) {
|
if (sHdlAccountLogic == null) {
|
sHdlAccountLogic = new HdlAccountLogic();
|
}
|
}
|
|
}
|
return sHdlAccountLogic;
|
}
|
|
|
/**
|
* 初始化账号的Url区域地址
|
*
|
* @param i_account 账号
|
*/
|
public void regionByAccount(String i_account, CloudCallBeak<LoginUserRegionBean> cloudCallBeak) {
|
JsonObject jsonObject = new JsonObject();
|
jsonObject.addProperty("account", i_account);
|
String full = AppConfigManage.getAPPRegionUrl() + HttpApi.POST_RegionByUserAccount;
|
HttpClient.getInstance().requestFullHttp(full, jsonObject.toString(), true, true, new BaseSuccessFailureCallBeak() {
|
@Override
|
public void onSuccess(HttpResponsePack httpResponsePack) {
|
if (httpResponsePack != null && httpResponsePack.getData() != null) {
|
Gson gson = new Gson();
|
String json = gson.toJson(httpResponsePack.getData());
|
LoginUserRegionBean loginUserRegionBean = new Gson().fromJson(json, LoginUserRegionBean.class);
|
if (cloudCallBeak != null) {
|
cloudCallBeak.onSuccess(loginUserRegionBean);
|
}
|
}
|
}
|
|
@Override
|
public void onFailure(Exception exception) {
|
if (cloudCallBeak != null) {
|
cloudCallBeak.onFailure(exception);
|
}
|
}
|
});
|
}
|
|
/**
|
* 登录
|
* 通过账号和密码
|
*
|
* @param account 手机或者邮箱
|
* @param loginPwd 密码
|
* @param cloudCallBeak -
|
*/
|
public void loginByPassword(String account, String loginPwd, CloudCallBeak<LoginBean> cloudCallBeak) {
|
String requestUrl = HttpApi.POST_Login;
|
JsonObject json = new JsonObject();
|
json.addProperty("account", account);
|
json.addProperty("loginPwd", loginPwd);
|
// json.addProperty("platform", "APP");
|
json.addProperty("grantType", "password");
|
|
HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
|
@Override
|
public void onSuccess(HttpResponsePack httpResponsePack) {
|
if (httpResponsePack != null && httpResponsePack.getData() != null) {
|
Gson gson = new Gson();
|
String json = gson.toJson(httpResponsePack.getData());
|
LoginBean loginBean = new Gson().fromJson(json, LoginBean.class);
|
saveUserData(loginBean);
|
if (cloudCallBeak != null) {
|
cloudCallBeak.onSuccess(loginBean);
|
}
|
}
|
}
|
|
@Override
|
public void onFailure(Exception exception) {
|
if (cloudCallBeak != null) {
|
cloudCallBeak.onFailure(exception);
|
}
|
}
|
});
|
}
|
|
|
public boolean isPhone(String phone) {
|
Pattern p = Pattern.compile("^((13[0-9])|(14[0|5|6|7|9])|(15[0-3])|(15[5-9])|(16[6|7])|(17[2|3|5|6|7|8])|(18[0-9])|(19[1|8|9]))\\d{8}$");
|
Matcher m = p.matcher(phone);
|
return m.matches();
|
}
|
|
public boolean isMailbox(String mailbox) {
|
String regex = "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$";
|
return mailbox.matches(regex);
|
}
|
|
/**
|
* 登录成功保存
|
*/
|
private void saveUserData(LoginBean obj) {
|
if (obj != null) {
|
UserConfigManage.getInstance().setLogin(true);
|
UserConfigManage.getInstance().setAcceiptPolicy(true);
|
UserConfigManage.getInstance().setLoginDateTime(System.currentTimeMillis());
|
UserConfigManage.getInstance().setUserId(obj.getUserId());
|
UserConfigManage.getInstance().setAccount(obj.getAccount());
|
UserConfigManage.getInstance().setToken(obj.getAccessToken());
|
UserConfigManage.getInstance().setHeaderPrefix(obj.getHeaderPrefix());
|
UserConfigManage.getInstance().setRefreshToken(obj.getRefreshToken());
|
boolean isUserId = UserConfigManage.getInstance().getUserId().equals(obj.getUserId());
|
UserConfigManage.getInstance().setTheSameLoginAccount(isUserId);
|
}
|
|
}
|
|
}
|