|
@@ -6,13 +6,16 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.lang.Validator;
|
|
import cn.hutool.core.lang.Validator;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.EnvCommonService;
|
|
import com.cyksj.common.EnvCommonService;
|
|
|
|
|
+import com.cyksj.common.annotation.NoSubmit;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.snowflake.Sequence;
|
|
import com.cyksj.common.snowflake.Sequence;
|
|
|
import com.cyksj.common.util.Codec;
|
|
import com.cyksj.common.util.Codec;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.SmsUtil;
|
|
import com.cyksj.common.util.SmsUtil;
|
|
|
|
|
+import com.cyksj.common.util.StringUtil;
|
|
|
import com.cyksj.config.WeChatConfig;
|
|
import com.cyksj.config.WeChatConfig;
|
|
|
import com.cyksj.config.wxlogin.WxOpenPlatYHLXLoginConfig;
|
|
import com.cyksj.config.wxlogin.WxOpenPlatYHLXLoginConfig;
|
|
|
import com.cyksj.dto.Result;
|
|
import com.cyksj.dto.Result;
|
|
@@ -34,6 +37,7 @@ import com.cyksj.service.authorization.AuthorizationService;
|
|
|
import com.cyksj.service.order.OrderDonService;
|
|
import com.cyksj.service.order.OrderDonService;
|
|
|
import com.cyksj.service.user.UserService;
|
|
import com.cyksj.service.user.UserService;
|
|
|
import com.cyksj.service.wechat.WeChatService;
|
|
import com.cyksj.service.wechat.WeChatService;
|
|
|
|
|
+import com.cyksj.web.controller.mail.MailService;
|
|
|
import com.cyksj.web.util.StpUserUtil;
|
|
import com.cyksj.web.util.StpUserUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -48,7 +52,6 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
-import java.util.Random;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author chan
|
|
* @author chan
|
|
@@ -88,6 +91,8 @@ public class AuthorizationController {
|
|
|
|
|
|
|
|
private final UserDistributeSharedMapper userDistributeSharedMapper;
|
|
private final UserDistributeSharedMapper userDistributeSharedMapper;
|
|
|
|
|
|
|
|
|
|
+ private final MailService mailService;
|
|
|
|
|
+
|
|
|
@GetMapping(value = "/weChat")
|
|
@GetMapping(value = "/weChat")
|
|
|
public void base(String url,Long sharedId, String authType, HttpServletResponse response) throws Exception {
|
|
public void base(String url,Long sharedId, String authType, HttpServletResponse response) throws Exception {
|
|
|
|
|
|
|
@@ -255,25 +260,18 @@ public class AuthorizationController {
|
|
|
* 获取手机验证码
|
|
* 获取手机验证码
|
|
|
*/
|
|
*/
|
|
|
@GetMapping("/get/phone/code")
|
|
@GetMapping("/get/phone/code")
|
|
|
|
|
+ @NoSubmit
|
|
|
public Result<String> getPhoneCode(String phone, Integer digit) {
|
|
public Result<String> getPhoneCode(String phone, Integer digit) {
|
|
|
if (StrUtil.isBlank(phone) || !Validator.isMobile(phone)) {
|
|
if (StrUtil.isBlank(phone) || !Validator.isMobile(phone)) {
|
|
|
throw BusinessRuntimeException.getInstance("请输入正确手机号");
|
|
throw BusinessRuntimeException.getInstance("请输入正确手机号");
|
|
|
}
|
|
}
|
|
|
- if (null == digit) {
|
|
|
|
|
- digit = 6;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, phone).last("limit 1"));
|
|
PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, phone).last("limit 1"));
|
|
|
if (phoneCode == null) {
|
|
if (phoneCode == null) {
|
|
|
phoneCode = new PhoneCode();
|
|
phoneCode = new PhoneCode();
|
|
|
phoneCode.setPhone(phone);
|
|
phoneCode.setPhone(phone);
|
|
|
}
|
|
}
|
|
|
- String code;
|
|
|
|
|
- // 生成验证码
|
|
|
|
|
- if (digit == 4) {
|
|
|
|
|
- code = new Random().nextInt(8999) + 1000 + "";
|
|
|
|
|
- } else {
|
|
|
|
|
- code = new Random().nextInt(899999) + 100000 + "";
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ String code = StringUtil.getRandomCodeStr(digit);
|
|
|
phoneCode.setCode(code);
|
|
phoneCode.setCode(code);
|
|
|
if (phoneCode.getId() == null) {
|
|
if (phoneCode.getId() == null) {
|
|
|
phoneCodeMapper.insert(phoneCode);
|
|
phoneCodeMapper.insert(phoneCode);
|
|
@@ -285,35 +283,79 @@ public class AuthorizationController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 手机号授权登录
|
|
|
|
|
|
|
+ * 发放邮箱验证码
|
|
|
*/
|
|
*/
|
|
|
- @PostMapping("/login/phone")
|
|
|
|
|
- public Result<LoginPhoneRep> loginByPhone(@RequestBody LoginPhoneReq loginPhoneReq, HttpServletResponse response) throws Exception {
|
|
|
|
|
- if (StrUtil.hasEmpty(loginPhoneReq.getPhone(), loginPhoneReq.getCode())) {
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("请输入对应信息");
|
|
|
|
|
|
|
+ @GetMapping("/get/email/code")
|
|
|
|
|
+ @NoSubmit
|
|
|
|
|
+ public Result<String> getEmailCode(String email, Integer digit) {
|
|
|
|
|
+ if (email == null || !Validator.isEmail(email)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确邮箱");
|
|
|
}
|
|
}
|
|
|
- DateTime now = DateTime.now();
|
|
|
|
|
- PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, loginPhoneReq.getPhone()).last("limit 1"));
|
|
|
|
|
- if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("请重新获取手机验证码");
|
|
|
|
|
|
|
+ String code = mailService.sendLoginCodeEmailToPeople(digit, email);
|
|
|
|
|
+ if (code == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("获取邮箱验证码错误");
|
|
|
}
|
|
}
|
|
|
- //3分钟失效
|
|
|
|
|
- Date updateTime = phoneCode.getUpdateTime();
|
|
|
|
|
- DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
|
|
|
|
|
- if (now.isAfter(afterFiveMinutes)) {
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("手机验证码已失效,请重新获取");
|
|
|
|
|
- }
|
|
|
|
|
- if (!StrUtil.equals(loginPhoneReq.getCode(), phoneCode.getCode())) {
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("手机验证码错误,请重新输入");
|
|
|
|
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult("发送成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 手机号授权登录
|
|
|
|
|
+ */
|
|
|
|
|
+ @PostMapping("/login/phone")
|
|
|
|
|
+ @NoSubmit
|
|
|
|
|
+ public Result<LoginPhoneRep> loginByPhone(@RequestBody LoginPhoneReq loginPhoneReq) throws Exception {
|
|
|
|
|
+ LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).last("limit 1");
|
|
|
|
|
+ if (loginPhoneReq.getType() == 1) {
|
|
|
|
|
+ if (StrUtil.hasEmpty(loginPhoneReq.getPhone(), loginPhoneReq.getCode())) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入对应信息");
|
|
|
|
|
+ }
|
|
|
|
|
+ DateTime now = DateTime.now();
|
|
|
|
|
+ PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, loginPhoneReq.getPhone()).last("limit 1"));
|
|
|
|
|
+ if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("请重新获取手机验证码");
|
|
|
|
|
+ }
|
|
|
|
|
+ //3分钟失效
|
|
|
|
|
+ Date updateTime = phoneCode.getUpdateTime();
|
|
|
|
|
+ DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
|
|
|
|
|
+ if (now.isAfter(afterFiveMinutes)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码已失效,请重新获取");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StrUtil.equals(loginPhoneReq.getCode(), phoneCode.getCode())) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码错误,请重新输入");
|
|
|
|
|
+ }
|
|
|
|
|
+ wrapper.eq(User::getLoginPhone, loginPhoneReq.getPhone());
|
|
|
|
|
+ }else if (loginPhoneReq.getType() == 2){
|
|
|
|
|
+ if (StrUtil.isEmpty(loginPhoneReq.getEmail())) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginPhoneReq.getEmail()));
|
|
|
|
|
+ if (StrUtil.isEmpty(code)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码已失效,请重新获取");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StrUtil.equals(loginPhoneReq.getCode(), code)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码错误,请重新输入");
|
|
|
|
|
+ }
|
|
|
|
|
+ wrapper.eq(User::getEmail, loginPhoneReq.getEmail());
|
|
|
|
|
+ redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginPhoneReq.getEmail()));
|
|
|
|
|
+ }else {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("系统异常,请刷新页面重新操作");
|
|
|
}
|
|
}
|
|
|
//保存用户信息
|
|
//保存用户信息
|
|
|
- User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getLoginPhone, loginPhoneReq.getPhone()).last("limit 1"));
|
|
|
|
|
|
|
+ User user = userService.getOne(wrapper);
|
|
|
UserDistributeShared userDistributeShared = null;
|
|
UserDistributeShared userDistributeShared = null;
|
|
|
Long sharedId = loginPhoneReq.getSharedId();
|
|
Long sharedId = loginPhoneReq.getSharedId();
|
|
|
if (user == null) {
|
|
if (user == null) {
|
|
|
user = new User();
|
|
user = new User();
|
|
|
- user.setLoginPhone(loginPhoneReq.getPhone());
|
|
|
|
|
- user.setNickname(String.format("银河用户%s", Codec.DoDigest.custom().setAlgorithm(Codec.DoDigest.Algorithm.MD5).setStringData(String.format("%s%s", user.getId(), user.getLoginPhone())).toHexString().substring(0, 6)));
|
|
|
|
|
|
|
+ if (loginPhoneReq.getType() == 1) {
|
|
|
|
|
+ user.setLoginPhone(loginPhoneReq.getPhone());
|
|
|
|
|
+ } else if (loginPhoneReq.getType() == 2) {
|
|
|
|
|
+ user.setEmail(loginPhoneReq.getEmail());
|
|
|
|
|
+ }
|
|
|
|
|
+ String name = "银河用户";
|
|
|
|
|
+ if (loginPhoneReq.getType() == 2) {
|
|
|
|
|
+ name += "@";
|
|
|
|
|
+ }
|
|
|
|
|
+ user.setNickname(String.format("%s%s", name, Codec.DoDigest.custom().setAlgorithm(Codec.DoDigest.Algorithm.MD5).setStringData(String.format("%s%s", user.getId(), loginPhoneReq.getType() == 1 ? user.getLoginPhone() : user.getEmail())).toHexString().substring(0, 6)));
|
|
|
//默认头像
|
|
//默认头像
|
|
|
user.setHeadimgurl("https://cdn.sxfoundation.com/picture/f6ee11101c4f5f0bc48d88aca7a5dbfd-1666945673391.png");
|
|
user.setHeadimgurl("https://cdn.sxfoundation.com/picture/f6ee11101c4f5f0bc48d88aca7a5dbfd-1666945673391.png");
|
|
|
userService.save(user);
|
|
userService.save(user);
|