Commit 0a01f627 authored by shiyu's avatar shiyu

Merge remote-tracking branch 'origin/master'

parents c3a5f553 3d65d66d
...@@ -7,7 +7,19 @@ public class CacheCodeConstants { ...@@ -7,7 +7,19 @@ public class CacheCodeConstants {
/** /**
* 微信接口调用凭证access_token * 微信接口调用凭证access_token
*/ */
public static final String WX_ACCESS_TOKEN = "quanku:wx:access_token"; public static final String WX_ACCESS_TOKEN = "cjxc:wx:access_token";
/**
* 缓存用户登录验证码
* <p>{0}:手机号</p>
*/
public static final String USER_LOGIN_SMS_KEY = "cjxc:wx:user:sms:login:{0}";
/**
* 缓存用户修改密码验证码
* <p>{0}:手机号</p>
*/
public static final String USER_UPDATE_SMS_KEY = "cjxc:wx:user:sms:update:{0}";
/** /**
* 短信验证码类型枚举 * 短信验证码类型枚举
......
...@@ -2,10 +2,7 @@ package com.wwdz.ch.wx.impl; ...@@ -2,10 +2,7 @@ package com.wwdz.ch.wx.impl;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.qiniu.storage.model.FileInfo; import com.qiniu.storage.model.FileInfo;
import com.wwdz.ch.core.consts.CommConsts; import com.wwdz.ch.core.consts.*;
import com.wwdz.ch.core.consts.CommonEnum;
import com.wwdz.ch.core.consts.MediaTypeEnum;
import com.wwdz.ch.core.consts.ResultCode;
import com.wwdz.ch.core.storage.QiniuStorage; import com.wwdz.ch.core.storage.QiniuStorage;
import com.wwdz.ch.core.type.Result; import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.MediaUtil; import com.wwdz.ch.core.util.MediaUtil;
...@@ -74,7 +71,7 @@ public class ItemServiceImpl implements ItemService { ...@@ -74,7 +71,7 @@ public class ItemServiceImpl implements ItemService {
if (StringUtils.isNotBlank(dto.getName())) { if (StringUtils.isNotBlank(dto.getName())) {
EsSearchRequestDto esSearchRequestDto = new EsSearchRequestDto(); EsSearchRequestDto esSearchRequestDto = new EsSearchRequestDto();
esSearchRequestDto.setPage(1); esSearchRequestDto.setPage(1);
esSearchRequestDto.setSize(1000); esSearchRequestDto.setSize(10000);
esSearchRequestDto.setContent(dto.getName()); esSearchRequestDto.setContent(dto.getName());
EsPageInfo esPageInfo = itemEsDao.findList(esSearchRequestDto); EsPageInfo esPageInfo = itemEsDao.findList(esSearchRequestDto);
if (Objects.nonNull(esPageInfo) && CollectionUtils.isNotEmpty(esPageInfo.getDataList())) { if (Objects.nonNull(esPageInfo) && CollectionUtils.isNotEmpty(esPageInfo.getDataList())) {
...@@ -93,7 +90,9 @@ public class ItemServiceImpl implements ItemService { ...@@ -93,7 +90,9 @@ public class ItemServiceImpl implements ItemService {
if (name.length() > 15) { if (name.length() > 15) {
name = name.substring(0, 15); name = name.substring(0, 15);
} }
if (name.contains(dto.getName())) {
set.add(name); set.add(name);
}
}); });
// 取16个名称 // 取16个名称
if (set.size() <= 16) { if (set.size() <= 16) {
...@@ -204,6 +203,7 @@ public class ItemServiceImpl implements ItemService { ...@@ -204,6 +203,7 @@ public class ItemServiceImpl implements ItemService {
item.setSourceType(CommonEnum.SourceTypeEnum.USER_UPLOAD.getCode()); item.setSourceType(CommonEnum.SourceTypeEnum.USER_UPLOAD.getCode());
item.setSource(CommonEnum.SourceEnum.USER_UPLOAD.getCode()); item.setSource(CommonEnum.SourceEnum.USER_UPLOAD.getCode());
item.setIsDeleted(false); item.setIsDeleted(false);
item.setKind(ItemKindEnum.COIN.getCode());
itemDao.insert(item); itemDao.insert(item);
// 新增用户和藏品关联记录 // 新增用户和藏品关联记录
......
package com.wwdz.ch.wx.impl; package com.wwdz.ch.wx.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.wwdz.ch.core.captcha.CaptchaCodeManager;
import com.wwdz.ch.core.consts.ResultCode; import com.wwdz.ch.core.consts.ResultCode;
import com.wwdz.ch.core.notify.AliSmsSender; import com.wwdz.ch.core.notify.AliSmsSender;
import com.wwdz.ch.core.notify.NotifyService; import com.wwdz.ch.core.notify.NotifyService;
...@@ -14,16 +13,17 @@ import com.wwdz.ch.core.util.UUID; ...@@ -14,16 +13,17 @@ import com.wwdz.ch.core.util.UUID;
import com.wwdz.ch.core.util.bcrypt.BCryptPasswordEncoder; import com.wwdz.ch.core.util.bcrypt.BCryptPasswordEncoder;
import com.wwdz.ch.db.dao.UserDao; import com.wwdz.ch.db.dao.UserDao;
import com.wwdz.ch.db.domain.User; import com.wwdz.ch.db.domain.User;
import com.wwdz.ch.db.dto.request.FavoritesRequestDto;
import com.wwdz.ch.wx.constant.CacheCodeConstants; import com.wwdz.ch.wx.constant.CacheCodeConstants;
import com.wwdz.ch.wx.dao.UserInfo; import com.wwdz.ch.wx.dao.UserInfo;
import com.wwdz.ch.wx.dao.UserToken; import com.wwdz.ch.wx.dao.UserToken;
import com.wwdz.ch.db.dto.request.FavoritesRequestDto;
import com.wwdz.ch.wx.entity.request.UserRequestDto; import com.wwdz.ch.wx.entity.request.UserRequestDto;
import com.wwdz.ch.wx.manager.UserTokenManager;
import com.wwdz.ch.wx.service.FavoritesService; import com.wwdz.ch.wx.service.FavoritesService;
import com.wwdz.ch.wx.service.UserService; import com.wwdz.ch.wx.service.UserService;
import com.wwdz.ch.wx.manager.UserTokenManager;
import com.wwdz.ch.wx.service.WxLoginService; import com.wwdz.ch.wx.service.WxLoginService;
import com.wwdz.ch.wx.util.IpUtil; import com.wwdz.ch.wx.util.IpUtil;
import com.wwdz.ch.wx.util.RedisUtil;
import com.wwdz.ch.wx.util.StringUtil; import com.wwdz.ch.wx.util.StringUtil;
import com.wwdz.ch.wx.util.WxResponseCode; import com.wwdz.ch.wx.util.WxResponseCode;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
...@@ -58,6 +59,8 @@ public class UserServiceImpl implements UserService { ...@@ -58,6 +59,8 @@ public class UserServiceImpl implements UserService {
private FavoritesService favoritesService; private FavoritesService favoritesService;
@Autowired @Autowired
private WxLoginService wxLoginService; private WxLoginService wxLoginService;
@Autowired
private RedisUtil redisUtil;
@Value("${avatar-url}") @Value("${avatar-url}")
private String avatarUrl; private String avatarUrl;
...@@ -181,6 +184,16 @@ public class UserServiceImpl implements UserService { ...@@ -181,6 +184,16 @@ public class UserServiceImpl implements UserService {
logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc()); logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc());
return Result.failed(WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc()); return Result.failed(WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc());
} }
String key = "";
if (dto.getSmsType().equals(CacheCodeConstants.SmsTypeEnum.LOGIN.getCode())) {
key = MessageFormat.format(CacheCodeConstants.USER_LOGIN_SMS_KEY, dto.getMobile());
} else {
key = MessageFormat.format(CacheCodeConstants.USER_UPDATE_SMS_KEY, dto.getMobile());
}
if (redisUtil.hasKey(key)) {
return Result.failed("验证码请求过于频繁");
}
String activeProfile = applicationContext.getEnvironment().getActiveProfiles()[0]; String activeProfile = applicationContext.getEnvironment().getActiveProfiles()[0];
String code = "123456"; String code = "123456";
if (!StringUtils.equals("dev", activeProfile)) { if (!StringUtils.equals("dev", activeProfile)) {
...@@ -190,16 +203,11 @@ public class UserServiceImpl implements UserService { ...@@ -190,16 +203,11 @@ public class UserServiceImpl implements UserService {
code = CharUtil.getRandomNum(6); code = CharUtil.getRandomNum(6);
SmsResult smsResult = aliSmsSender.sendWithTemplate(dto.getMobile(), code); SmsResult smsResult = aliSmsSender.sendWithTemplate(dto.getMobile(), code);
if (smsResult != null) { if (smsResult != null) {
logger.info("*****腾讯云短信发送->请求验证码,短信发送结果:{}", JSONObject.toJSONString(smsResult)); logger.info("*****阿里云短信发送->请求验证码,短信发送结果:{}", JSONObject.toJSONString(smsResult));
} }
} }
boolean successful = CaptchaCodeManager.addToCache(dto.getSmsType() + dto.getMobile(), code,1); redisUtil.set(key, code, 60);
if (!successful) {
logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_FREQUENCY.desc());
return Result.failed(WxResponseCode.AUTH_CAPTCHA_FREQUENCY.desc());
}
logger.info("【请求结束】请求验证码成功"); logger.info("【请求结束】请求验证码成功");
return Result.success(); return Result.success();
} catch (Exception e) { } catch (Exception e) {
...@@ -212,11 +220,12 @@ public class UserServiceImpl implements UserService { ...@@ -212,11 +220,12 @@ public class UserServiceImpl implements UserService {
@Override @Override
public Result loginByMobile(UserRequestDto dto, HttpServletRequest request) { public Result loginByMobile(UserRequestDto dto, HttpServletRequest request) {
try { try {
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeConstants.SmsTypeEnum.LOGIN.getCode() + dto.getMobile()); String key = MessageFormat.format(CacheCodeConstants.USER_LOGIN_SMS_KEY, dto.getMobile());
if (StringUtils.isBlank(mobileCode)) { Object object = redisUtil.get(key);
if (Objects.isNull(object)) {
return Result.failed("验证码已过期"); return Result.failed("验证码已过期");
} }
if (!StringUtils.equals(dto.getSmsCode(), mobileCode)) { if (!StringUtils.equals(dto.getSmsCode(), object.toString())) {
return Result.failed("验证码不正确"); return Result.failed("验证码不正确");
} }
...@@ -294,6 +303,8 @@ public class UserServiceImpl implements UserService { ...@@ -294,6 +303,8 @@ public class UserServiceImpl implements UserService {
result.put("userInfo", userInfo); result.put("userInfo", userInfo);
logger.info("【请求结束】手机号登录,响应结果:{}", JSONObject.toJSONString(result)); logger.info("【请求结束】手机号登录,响应结果:{}", JSONObject.toJSONString(result));
// 清除验证码缓存
redisUtil.del(key);
return Result.success(result); return Result.success(result);
} catch (Exception e) { } catch (Exception e) {
logger.error("手机号登录失败", e); logger.error("手机号登录失败", e);
...@@ -392,11 +403,12 @@ public class UserServiceImpl implements UserService { ...@@ -392,11 +403,12 @@ public class UserServiceImpl implements UserService {
if (!StringUtils.equals(user.getMobile(), dto.getBeforeMobile())) { if (!StringUtils.equals(user.getMobile(), dto.getBeforeMobile())) {
return Result.failed("原手机号码不正确"); return Result.failed("原手机号码不正确");
} }
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeConstants.SmsTypeEnum.UPDATE.getCode() + dto.getAfterMobile()); String key = MessageFormat.format(CacheCodeConstants.USER_UPDATE_SMS_KEY, dto.getAfterMobile());
if (StringUtils.isBlank(mobileCode)) { Object object = redisUtil.get(key);
if (Objects.isNull(object)) {
return Result.failed("验证码已过期"); return Result.failed("验证码已过期");
} }
if (!StringUtils.equals(dto.getSmsCode(), mobileCode)) { if (!StringUtils.equals(dto.getSmsCode(), object.toString())) {
return Result.failed("验证码不正确"); return Result.failed("验证码不正确");
} }
if (userDao.isExistedByMobile(dto.getBeforeMobile(), dto.getAfterMobile())) { if (userDao.isExistedByMobile(dto.getBeforeMobile(), dto.getAfterMobile())) {
...@@ -405,6 +417,8 @@ public class UserServiceImpl implements UserService { ...@@ -405,6 +417,8 @@ public class UserServiceImpl implements UserService {
user.setMobile(dto.getAfterMobile()); user.setMobile(dto.getAfterMobile());
userDao.updateById(user); userDao.updateById(user);
logger.info("【请求结束】用户修改手机号成功"); logger.info("【请求结束】用户修改手机号成功");
// 清除验证码缓存
redisUtil.del(key);
return Result.success(); return Result.success();
} catch (Exception e) { } catch (Exception e) {
logger.error("用户修改手机号失败", e); logger.error("用户修改手机号失败", e);
......
...@@ -24,9 +24,9 @@ public class UserTokenManager { ...@@ -24,9 +24,9 @@ public class UserTokenManager {
private Gson gson = new Gson(); private Gson gson = new Gson();
private static final String TOKEN_PREFIX = "token_prefix:"; private static final String TOKEN_PREFIX = "cjxc:wx:token:token_prefix:";
private static final String TOKEN_ID_PREFIX = "token_id_prefix:"; private static final String TOKEN_ID_PREFIX = "cjxc:wx:token:token_id_prefix:";
private static Map<String, UserToken> tokenMap = new HashMap<>(); private static Map<String, UserToken> tokenMap = new HashMap<>();
private static Map<Long, UserToken> idMap = new HashMap<>(); private static Map<Long, UserToken> idMap = new HashMap<>();
private static long SURVIVAL_TIME = 86400; private static long SURVIVAL_TIME = 86400;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment