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
package com.everyday.word.controller.client.user;
 
import com.everyday.word.dto.QQUserInfo;
import com.everyday.word.entity.SystemEnum;
import com.everyday.word.entity.config.SystemConfigKeyEnum;
import com.everyday.word.entity.user.IdentityType;
import com.everyday.word.entity.user.User;
import com.everyday.word.entity.user.UserAuth;
import com.everyday.word.exception.SMSException;
import com.everyday.word.factory.user.UserFactory;
import com.everyday.word.service.inter.SMSService;
import com.everyday.word.service.inter.config.SystemConfigService;
import com.everyday.word.service.inter.user.UserService;
import com.everyday.word.utils.AliyunOneKeyLoginUtil;
import com.everyday.word.vo.AcceptData;
import com.everyday.word.vo.user.UserInfoVO;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.entity.wx.WeiXinUser;
import org.yeshi.utils.wx.WXAppLoginUtil;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
 
/**
 * @author hxh
 * @title: UserController
 * @description:
 * @date 2025/2/7 15:59
 */
@Controller
@RequestMapping("api/user/login")
public class LoginController {
 
    @Resource
    private SMSService smsService;
 
    @Resource
    private UserService userService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
    private final Gson gson = JsonUtil.getSimpleGson();
 
    private UserInfoVO getUserInfoVO(Long uid) {
        User user = userService.selectUser(uid);
        if (user == null) {
            return null;
        }
        List<UserAuth> authList = userService.listUserAuth(uid);
        return UserFactory.createVO(user, authList);
    }
 
 
    private User loginByPhone(String phone, SystemEnum system) {
        User user = userService.loginByPhone(phone, system);
        if (user == null) {
            // 注册
            userService.register(UserFactory.createAuth(phone, system));
        }
        user = userService.loginByPhone(phone, system);
        return user;
    }
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 电话号码登录
     * @date 11:28 2025/2/13
     * @param: acceptData
     * @param: phone
     * @param: vcode
     * @param: accessToken 一键登录的Token
     * @param: session
     **/
    @ResponseBody
    @RequestMapping("phone")
    public String phoneLogin(AcceptData acceptData, String phone, String vcode, HttpSession session) {
        // 电话号码登录
        if (StringUtil.isNullOrEmpty(phone)) {
            return JsonUtil.loadFalseResult("电话号码不能为空");
        }
        if (StringUtil.isNullOrEmpty(vcode)) {
            return JsonUtil.loadFalseResult("验证码不能为空");
        }
        // 验证验证码
        String cacheCode = smsService.getVerifyCode(acceptData.getSystem(), phone, 1);
        if (!vcode.equalsIgnoreCase(cacheCode)) {
            return JsonUtil.loadFalseResult("验证码错误");
        }
 
        User user = loginByPhone(phone, acceptData.getSystem());
        if (user == null) {
            return JsonUtil.loadFalseResult("登录失败");
        }
        return JsonUtil.loadTrueResult(gson.toJson(getUserInfoVO(user.getId())));
    }
 
    @ResponseBody
    @RequestMapping("sendVerifyCode")
    public String sendSms(AcceptData acceptData, String phone, Long uid, HttpSession session) {
        try {
            if (phone.contains("**") && uid != null && uid > 0) {
                UserAuth userAuth = userService.selectUserAuth(uid, IdentityType.PHONE);
                if (userAuth == null) {
                    return JsonUtil.loadFalseResult(2, "用户不存在/未绑定手机号");
                }
                phone = userAuth.getIdentifier();
            }
 
            if (!StringUtil.isMobile(phone)) {
                return JsonUtil.loadFalseResult(4, "电话号码格式不正确");
            }
            smsService.sendVerifyCode(acceptData.getSystem(), phone, 6, 1);
            return JsonUtil.loadTrueResult("发送成功");
        } catch (SMSException e) {
            return JsonUtil.loadFalseResult(e.getCode(), e.getMsg());
        }
    }
 
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 一键登录
     * @date 16:09 2025/2/7
     * @param: acceptData
     * @param: token
     * @param: session
     **/
    @ResponseBody
    @RequestMapping("one_key")
    public String oneKeyLogin(AcceptData acceptData, String token, HttpSession session) {
        String phone = AliyunOneKeyLoginUtil.getMobile(token, "");
        if (StringUtil.isNullOrEmpty(phone)) {
            return JsonUtil.loadFalseResult("手机号获取失败");
        }
        User user = loginByPhone(phone, acceptData.getSystem());
        if (user == null) {
            return JsonUtil.loadFalseResult("登录失败");
        }
        return JsonUtil.loadTrueResult(gson.toJson(getUserInfoVO(user.getId())));
    }
 
 
    private WeiXinUser getWeiXinUserByCode(String code, SystemEnum system) {
        String wxAppId = systemConfigService.getValueCache(SystemConfigKeyEnum.wxAppId, system);
        String wxAppSecret = systemConfigService.getValueCache(SystemConfigKeyEnum.wxAppSecret, system);
        if (StringUtil.isNullOrEmpty(wxAppId) || StringUtil.isNullOrEmpty(wxAppSecret)) {
            return null;
        }
        return WXAppLoginUtil.getWeiXinUser(code, wxAppId, wxAppSecret);
    }
 
 
    @ResponseBody
    @RequestMapping("wx")
    public String wxLogin(AcceptData acceptData, String code, HttpSession session) {
        if (StringUtil.isNullOrEmpty(code)) {
            return JsonUtil.loadFalseResult("微信授权码未上传");
        }
        WeiXinUser weiXinUser = getWeiXinUserByCode(code, acceptData.getSystem());
        if (weiXinUser == null) {
            return JsonUtil.loadFalseResult("微信授权失败");
        }
        User user = userService.loginByWX(weiXinUser, acceptData.getSystem());
        if (user == null) {
            userService.register(UserFactory.createAuth(weiXinUser, acceptData.getSystem()));
            user = userService.loginByWX(weiXinUser, acceptData.getSystem());
        }
        if (user == null) {
            return JsonUtil.loadFalseResult("登录失败");
        }
        return JsonUtil.loadTrueResult(gson.toJson(getUserInfoVO(user.getId())));
    }
 
    @ResponseBody
    @RequestMapping("qq")
    public String qqLogin(AcceptData acceptData, QQUserInfo qqUserInfo, HttpSession session) {
        if (StringUtil.isNullOrEmpty(qqUserInfo.getOpenid())) {
            return JsonUtil.loadFalseResult("QQ授权码未上传");
        }
        UserAuth userAuth = UserFactory.createAuth(qqUserInfo, acceptData.getSystem());
        User user = userService.loginByQQ(qqUserInfo.getOpenid(), acceptData.getSystem());
        if (user == null) {
            userService.register(userAuth);
            user = userService.loginByQQ(qqUserInfo.getOpenid(), acceptData.getSystem());
        }
        if (user == null) {
            return JsonUtil.loadFalseResult("登录失败");
        }
        return JsonUtil.loadTrueResult(gson.toJson(getUserInfoVO(user.getId())));
    }
 
 
}