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())));
|
}
|
|
|
}
|