wjc
2023-06-28 975b91521a04e159f45fb34fc7b55afbf455f7f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.hdl.photovoltaic.other;
 
 
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hdl.photovoltaic.ui.bean.LoginUserBean;
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);
                }
            }
        });
    }
 
    /**
     * 登录(B端账号)
     * 通过账号和密码
     *
     * @param account       手机或者邮箱
     * @param loginPwd      密码
     * @param cloudCallBeak -
     */
    public void loginByPassword(String account, String loginPwd, CloudCallBeak<LoginUserBean> 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());
                    LoginUserBean loginUserBean = new Gson().fromJson(json, LoginUserBean.class);
                    saveUserData(loginUserBean);
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(loginUserBean);
                    }
                }
            }
 
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
 
    /**
     * 刷新Token(B端账号)
     *
     * @param account       手机或者邮箱
     * @param loginPwd      密码
     * @param cloudCallBeak -
     */
    public void refreshToken(String account, String loginPwd, CloudCallBeak<LoginUserBean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Login;
        JsonObject json = new JsonObject();
        json.addProperty("grantType", "refresh_token");
//        json.addProperty("refreshToken", UserConfigManage.getInstance().getRefreshToken());
 
        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());
                    LoginUserBean loginUserBean = new Gson().fromJson(json, LoginUserBean.class);
                    saveUserData(loginUserBean);
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(loginUserBean);
                    }
                }
            }
 
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
 
 
    /**
     * 退出登录时调用,清除推送数据
     */
    public void SignOutClearData() {
        String requestUrl = HttpApi.POST_GET_IMAGE_LOGOUT;
        //通知云端,已经退出登陆
    }
 
    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(LoginUserBean obj) {
        if (obj != null) {
            UserConfigManage.getInstance().setLogin(true);//是否登录
            UserConfigManage.getInstance().setAcceiptPolicy(true);//是否选择隐私政策
            UserConfigManage.getInstance().setLoginDateTime(System.currentTimeMillis());//登录时间
            UserConfigManage.getInstance().setUserId(obj.getUserId());//用户id
            UserConfigManage.getInstance().setAccount(obj.getAccount());//账号
            UserConfigManage.getInstance().setToken(obj.getAccessToken());//Token
            UserConfigManage.getInstance().setHeaderPrefix(obj.getHeaderPrefix());//认证请求头前缀(底层请求统一加)
            UserConfigManage.getInstance().setRefreshToken(obj.getRefreshToken());//刷新Token用的刷新Token用的
            boolean isUserId = UserConfigManage.getInstance().getUserId().equals(obj.getUserId());
            UserConfigManage.getInstance().setTheSameLoginAccount(isUserId);//与上一个账号是否同一个
        }
 
    }
 
 
}