admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.ks.app.service.impl.user;
 
import com.ks.app.dto.user.LoginInfoDTO;
import com.ks.app.entity.SystemEnum;
import com.ks.app.entity.config.SystemConfigKey;
import com.ks.app.entity.user.UserInfo;
import com.ks.app.entity.user.UserLoginRecord;
import com.ks.app.entity.user.WXUserInfo;
import com.ks.app.exception.user.LoginException;
import com.ks.app.exception.user.UserAccountException;
import com.ks.app.service.inter.config.SystemConfigService;
import com.ks.app.service.inter.user.*;
import com.ks.app.service.manager.VerifyCodeManager;
import com.ks.app.utils.AliyunOneKeyLoginUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.entity.wx.WeiXinUser;
import org.yeshi.utils.wx.WXAppLoginUtil;
 
import javax.annotation.Resource;
import java.util.UUID;
 
@Service
public class UserAccountServiceImpl implements UserAccountService {
 
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserLoginRecordService userLoginRecordService;
    @Resource
    private VerifyCodeManager verifyCodeManager;
 
    @Resource
    private WXUserInfoService wxUserInfoService;
 
    @Resource
    private QQUserInfoService qqUserInfoService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserInfo login(LoginInfoDTO loginInfo) throws LoginException {
 
        if (loginInfo == null) {
            throw new LoginException("登录信息为空");
        }
 
        UserInfo user = null;
        switch (loginInfo.getLoginType()) {
            case UserLoginRecord
                    .TYPE_LOGIN_PHONE: {
                if (!StringUtil.isNullOrEmpty(loginInfo.getPhoneAuthInfo())) {
                    //一键登录
                    //获取电话号码
                    String mobile = AliyunOneKeyLoginUtil.getMobile(loginInfo.getPhoneAuthInfo(), UUID.randomUUID().toString(), systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.aliyunOneKeyAuthAcessKey), systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.aliyunOneKeyAuthAcessSecret));
                    if (StringUtil.isNullOrEmpty(mobile)) {
                        throw new LoginException("电话号码获取失败");
                    }
                    loginInfo.setPhone(mobile);
                } else {
                    //验证码登录
                    if (!verifyCodeManager.isPhoneCodeRight(loginInfo.getSystem(), loginInfo.getPhone(), loginInfo.getVcode())) {
                        throw new LoginException("验证码错误");
                    }
                }
                //执行登录操作
                user = userInfoService.selectByPhoneAndSystemAndStatus(loginInfo.getPhone(), loginInfo.getSystem(), UserInfo.STATUS_NORMAL);
                if (user == null) {
                    //创建用户
                    UserInfo newUser = new UserInfo();
                    newUser.setPhone(loginInfo.getPhone());
                    newUser.setSystem(loginInfo.getSystem());
                    newUser.setNickName("");
                    newUser.setPortrait(systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.defaultPortrait));
                    try {
                        userInfoService.add(newUser);
                    } catch (Exception e) {
                        throw new LoginException("注册用户失败");
                    }
                    //更改昵称
                    UserInfo updateUser = new UserInfo();
                    updateUser.setId(newUser.getId());
                    updateUser.setNickName(systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.defaultNickNamePrefix) + newUser.getId());
                    userInfoService.update(updateUser);
                    user = newUser;
                }
            }
            break;
            case UserLoginRecord
                    .TYPE_LOGIN_WX: {
                String appId = systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.wxAppId);
                String appSecret = systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.wxAppSecret);
                WeiXinUser weiXinUser = WXAppLoginUtil.getWeiXinUser(loginInfo.getWxCode(), appId, appSecret);
                if (weiXinUser == null) {
                    throw new LoginException("微信授权信息获取失败");
                }
                //保存微信用户信息
                WXUserInfo wxUserInfo = null;
                try {
                    WXUserInfo temp = WXUserInfo.create(weiXinUser);
                    temp.setSystem(loginInfo.getSystem());
                    wxUserInfo = wxUserInfoService.save(temp);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new LoginException("保存微信授权信息失败");
                }
                wxUserInfo.setSystem(loginInfo.getSystem());
                user = userInfoService.selectByWXIdAndSystemAndStatus(wxUserInfo.getId(), wxUserInfo.getSystem(), UserInfo.STATUS_NORMAL);
                if (user == null) {
                    //新建用户
                    UserInfo newUser = new UserInfo();
                    newUser.setWxUser(wxUserInfo);
                    newUser.setSystem(loginInfo.getSystem());
                    newUser.setNickName(wxUserInfo.getNickName());
                    if (StringUtil.isNullOrEmpty(wxUserInfo.getHeadImgUrl())) {
                        newUser.setPortrait(systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.defaultPortrait));
                    } else {
                        newUser.setPortrait(wxUserInfo.getHeadImgUrl());
                    }
                    try {
                        userInfoService.add(newUser);
                    } catch (Exception e) {
                        throw new LoginException("注册用户失败");
                    }
                    user = newUser;
                }
 
            }
            break;
            case UserLoginRecord
                    .TYPE_LOGIN_QQ:
                //保存QQ信息
 
 
                break;
            case UserLoginRecord
                    .TYPE_LOGIN_EMAIL:
                //验证密码是否正确
                UserInfo oldUser = userInfoService.selectByEmailAndSystemAndStatus(loginInfo.getEmail(), loginInfo.getSystem(), UserInfo.STATUS_NORMAL);
                if (oldUser == null) {
                    throw new LoginException("邮箱未注册");
                }
 
                if (!StringUtil.Md5(loginInfo.getPwd()).equalsIgnoreCase(oldUser.getPwd())) {
                    throw new LoginException("密码错误");
                }
                user = oldUser;
                break;
            default:
                throw new LoginException("登录方式不存在");
        }
 
        loginSuccess(user.getId(), loginInfo);
        return user;
    }
 
    @Override
    public void unRegister(Long uid) {
        UserInfo update = new UserInfo();
        update.setId(uid);
        update.setStatus(UserInfo.STATUS_OWN_DELETE);
        userInfoService.update(update);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void bindWX(Long uid, String code) throws UserAccountException {
        UserInfo user = userInfoService.get(uid);
 
        if (user == null) {
            throw new UserAccountException(UserAccountException.CODE_USER_ERROR, "用户不存在");
        }
 
        SystemEnum system = user.getSystem();
        String appId = systemConfigService.getValueCache(system, SystemConfigKey.wxAppId);
        String appSecret = systemConfigService.getValueCache(system, SystemConfigKey.wxAppSecret);
        WeiXinUser weiXinUser = WXAppLoginUtil.getWeiXinUser(code, appId, appSecret);
        if (weiXinUser == null) {
            throw new UserAccountException(UserAccountException.CODE_ERROR_WX_INFO, "微信授权信息获取失败");
        }
        //保存微信用户信息
        WXUserInfo wxUserInfo = null;
        try {
            WXUserInfo temp = WXUserInfo.create(weiXinUser);
            temp.setSystem(system);
            wxUserInfo = wxUserInfoService.save(temp);
        } catch (Exception e) {
            throw new UserAccountException(UserAccountException.CODE_ERROR_WX_INFO, "保存微信授权信息失败");
        }
        UserInfo u = userInfoService.selectByWXIdAndSystemAndStatus(wxUserInfo.getId(), system, UserInfo.STATUS_NORMAL);
        if (u != null) {
            throw new UserAccountException(UserAccountException.CODE_ALREAD_BIND, "微信已绑定其他账号");
        }
        //绑定到用户
        UserInfo update = new UserInfo();
        update.setId(uid);
        update.setWxUser(wxUserInfo);
        userInfoService.update(update);
    }
 
    @Override
    public void bindPhone(Long uid, String phone, String vcode, String token) throws UserAccountException {
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserAccountException(UserAccountException.CODE_USER_ERROR, "用户不存在");
        }
        SystemEnum system = user.getSystem();
        if (!StringUtil.isNullOrEmpty(token)) {
            //一键登录
            //获取电话号码
            String mobile = AliyunOneKeyLoginUtil.getMobile(token, UUID.randomUUID().toString(), systemConfigService.getValueCache(system, SystemConfigKey.aliyunOneKeyAuthAcessKey), systemConfigService.getValueCache(system, SystemConfigKey.aliyunOneKeyAuthAcessSecret));
            if (StringUtil.isNullOrEmpty(mobile)) {
                throw new UserAccountException(UserAccountException.CODE_ERROR_PHONE, "电话号码获取失败");
            }
            phone = mobile;
        } else {
            //验证码登录
            if (!verifyCodeManager.isPhoneCodeRight(system, phone, vcode)) {
                throw new UserAccountException(UserAccountException.CODE_ERROR_PHONE, "验证码错误");
            }
        }
 
 
        UserInfo u = userInfoService.selectByPhoneAndSystemAndStatus(phone, system, UserInfo.STATUS_NORMAL);
        if (u != null) {
            throw new UserAccountException(UserAccountException.CODE_ALREAD_BIND, "手机号已绑定其他账号");
        }
 
        //绑定到用户
        UserInfo update = new UserInfo();
        update.setId(uid);
        update.setPhone(phone);
        userInfoService.update(update);
    }
 
 
    /**
     * @return void
     * @author hxh
     * @description 登录成功
     * @date 18:21 2021/11/15
     **/
    private void loginSuccess(Long uid, LoginInfoDTO dto) {
        UserLoginRecord record = new UserLoginRecord();
        record.setLoginIPInfo(dto.getIpInfo());
        record.setLoginType(dto.getLoginType());
        record.setUid(uid);
        userLoginRecordService.addUserLoginRecord(record);
    }
 
 
}