feat(商城): 代码呀欧化

master
wayn 1 year ago
parent ab72fe9375
commit 42b705371d

@ -1,8 +1,11 @@
package com.wayn.common.enums; package com.wayn.common.enums;
import lombok.Getter;
/** /**
* enum * enum
*/ */
@Getter
public enum ReturnCodeEnum { public enum ReturnCodeEnum {
// 200通用操作成功 3xx资源重定向 4xx客户端错误 500通用操作失败 // 200通用操作成功 3xx资源重定向 4xx客户端错误 500通用操作失败
@ -88,14 +91,6 @@ public enum ReturnCodeEnum {
this.msg = msg; this.msg = msg;
} }
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public ReturnCodeEnum setMsg(String msg) { public ReturnCodeEnum setMsg(String msg) {
this.msg = msg; this.msg = msg;
return this; return this;

@ -9,6 +9,8 @@ import lombok.Getter;
@Getter @Getter
public enum RedisKeyEnum { public enum RedisKeyEnum {
CAPTCHA_KEY_CACHE(CacheConstants.CACHE_PREFIX + "captcha_key:", 300),
EMAIL_KEY_CACHE(CacheConstants.CACHE_PREFIX + "email_key:", 300),
ES_SYNC_CACHE(CacheConstants.CACHE_PREFIX + "es_sync_cache", 3600), ES_SYNC_CACHE(CacheConstants.CACHE_PREFIX + "es_sync_cache", 3600),
CART_LOCK(CacheConstants.CACHE_PREFIX + "cart_lock:", 3600), CART_LOCK(CacheConstants.CACHE_PREFIX + "cart_lock:", 3600),
EMAIL_CONSUMER_MAP(CacheConstants.CACHE_PREFIX + "email_consumer_map", 60), EMAIL_CONSUMER_MAP(CacheConstants.CACHE_PREFIX + "email_consumer_map", 60),

@ -1,5 +1,6 @@
package com.wayn.mobile.api.controller; package com.wayn.mobile.api.controller;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wayn.common.constant.SysConstants; import com.wayn.common.constant.SysConstants;
@ -13,6 +14,7 @@ import com.wayn.common.util.IdUtil;
import com.wayn.common.util.R; import com.wayn.common.util.R;
import com.wayn.common.util.mail.MailUtil; import com.wayn.common.util.mail.MailUtil;
import com.wayn.common.util.security.SecurityUtils; import com.wayn.common.util.security.SecurityUtils;
import com.wayn.data.redis.constant.RedisKeyEnum;
import com.wayn.data.redis.manager.RedisCache; import com.wayn.data.redis.manager.RedisCache;
import com.wayn.mobile.framework.security.LoginObj; import com.wayn.mobile.framework.security.LoginObj;
import com.wayn.mobile.framework.security.RegistryObj; import com.wayn.mobile.framework.security.RegistryObj;
@ -20,6 +22,7 @@ import com.wayn.mobile.framework.security.service.LoginService;
import com.wf.captcha.SpecCaptcha; import com.wf.captcha.SpecCaptcha;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Collections; import java.util.Collections;
@ -31,12 +34,10 @@ import java.util.concurrent.TimeUnit;
public class LoginController { public class LoginController {
private LoginService loginService; private LoginService loginService;
private RedisCache redisCache; private RedisCache redisCache;
private IMemberService iMemberService; private IMemberService iMemberService;
private IMailConfigService mailConfigService; private IMailConfigService mailConfigService;
private ThreadPoolTaskExecutor commonThreadPoolTaskExecutor;
@PostMapping("/login") @PostMapping("/login")
public R login(@RequestBody LoginObj loginObj) { public R login(@RequestBody LoginObj loginObj) {
@ -57,14 +58,6 @@ public class LoginController {
return R.error(ReturnCodeEnum.USER_PHONE_HAS_REGISTER_ERROR); return R.error(ReturnCodeEnum.USER_PHONE_HAS_REGISTER_ERROR);
} }
// String redisCode = redisCache.getCacheObject(registryObj.getKey());
// // 判断验证码
// if (registryObj.getCode() == null || !redisCode.equals(registryObj.getCode().trim().toLowerCase())) {
// return R.error("验证码不正确");
// }
//
// redisCache.deleteObject(registryObj.getKey());
String redisEmailCode = redisCache.getCacheObject(registryObj.getEmailKey()); String redisEmailCode = redisCache.getCacheObject(registryObj.getEmailKey());
// 判断邮箱验证码 // 判断邮箱验证码
if (registryObj.getEmailCode() == null || !redisEmailCode.equals(registryObj.getEmailCode().trim().toLowerCase())) { if (registryObj.getEmailCode() == null || !redisEmailCode.equals(registryObj.getEmailCode().trim().toLowerCase())) {
@ -87,28 +80,49 @@ public class LoginController {
@ResponseBody @ResponseBody
@RequestMapping("/captcha") @RequestMapping("/captcha")
public R captcha() { public R captcha() {
// 创建验证码对象,定义验证码图形的长、宽、以及字数
SpecCaptcha specCaptcha = new SpecCaptcha(80, 32, 4); SpecCaptcha specCaptcha = new SpecCaptcha(80, 32, 4);
// 生成验证码
String verCode = specCaptcha.text().toLowerCase(); String verCode = specCaptcha.text().toLowerCase();
String key = IdUtil.getUid(); // 生成验证码唯一key
String key = RedisKeyEnum.CAPTCHA_KEY_CACHE.getKey(IdUtil.getUid());
// 存入redis并设置过期时间为30分钟 // 存入redis并设置过期时间为30分钟
redisCache.setCacheObject(key, verCode, SysConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); redisCache.setCacheObject(key, verCode, RedisKeyEnum.CAPTCHA_KEY_CACHE.getExpireSecond());
// 将key和base64返回给前端 // 将key和base64返回给前端
return R.success().add("key", key).add("image", specCaptcha.toBase64()); return R.success().add("key", key).add("image", specCaptcha.toBase64());
} }
@PostMapping("/sendEmailCode") @PostMapping("/sendEmailCode")
public R sendEmailCode(@RequestBody RegistryObj registryObj) { public R sendEmailCode(@RequestBody RegistryObj registryObj) {
SpecCaptcha specCaptcha = new SpecCaptcha(80, 32, 4); // 判断图形验证码是否正确
String verCode = specCaptcha.text().toLowerCase(); String captchaKey = registryObj.getCaptchaKey();
String key = IdUtil.getUid(); String captchaCode = registryObj.getCaptchaCode();
if (StringUtils.isBlank(captchaKey)) {
return R.error(ReturnCodeEnum.CUSTOM_ERROR.setMsg("验证码 key为空"));
}
if (StringUtils.isBlank(captchaCode)) {
return R.error(ReturnCodeEnum.CUSTOM_ERROR.setMsg("验证码 code为空"));
}
String redisCode = redisCache.getCacheObject(captchaKey);
// 判断验证码code
if (!redisCode.equals(captchaCode.trim().toLowerCase())) {
return R.error(ReturnCodeEnum.CUSTOM_ERROR.setMsg("验证码输入错误"));
}
// 生成邮箱验证码code
String verCode = RandomUtil.randomString(6);
// 生成邮箱验证码唯一key
String key = RedisKeyEnum.EMAIL_KEY_CACHE.getKey(IdUtil.getUid());
// 存入redis并设置过期时间为20分钟 // 存入redis并设置过期时间为20分钟
redisCache.setCacheObject(key, verCode, SysConstants.CAPTCHA_EXPIRATION * 10, TimeUnit.MINUTES); redisCache.setCacheObject(key, verCode, RedisKeyEnum.EMAIL_KEY_CACHE.getExpireSecond());
commonThreadPoolTaskExecutor.execute(() -> {
EmailConfig emailConfig = mailConfigService.getById(1L); EmailConfig emailConfig = mailConfigService.getById(1L);
SendMailVO sendMailVO = new SendMailVO(); SendMailVO sendMailVO = new SendMailVO();
sendMailVO.setSubject("mall商城注册通知"); sendMailVO.setSubject("mall商城注册通知");
sendMailVO.setContent("邮箱验证码:" + verCode); sendMailVO.setContent("邮箱验证码:" + verCode);
sendMailVO.setTos(Collections.singletonList(registryObj.getEmail())); sendMailVO.setTos(Collections.singletonList(registryObj.getEmail()));
MailUtil.sendMail(emailConfig, sendMailVO, false, false); MailUtil.sendMail(emailConfig, sendMailVO, false, false);
redisCache.deleteObject(captchaKey);
});
return R.success().add("key", key); return R.success().add("key", key);
} }
} }

@ -33,7 +33,7 @@ public class RegistryObj {
/** /**
* *
*/ */
private String code; private String captchaCode;
/** /**
* *
@ -43,7 +43,7 @@ public class RegistryObj {
/** /**
* key * key
*/ */
private String key; private String captchaKey;
/** /**
* key * key

Loading…
Cancel
Save