|
@@ -8,14 +8,17 @@ import com.macro.mall.model.UmsMemberLevel;
|
|
|
import com.macro.mall.model.UmsMemberLevelExample;
|
|
|
import com.macro.mall.portal.domain.CommonResult;
|
|
|
import com.macro.mall.portal.domain.MemberDetails;
|
|
|
+import com.macro.mall.portal.service.RedisService;
|
|
|
import com.macro.mall.portal.service.UmsMemberService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.security.authentication.encoding.PasswordEncoder;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
@@ -33,6 +36,12 @@ public class UmsMemberServiceImpl implements UmsMemberService {
|
|
|
private UmsMemberLevelMapper memberLevelMapper;
|
|
|
@Autowired
|
|
|
private PasswordEncoder passwordEncoder;
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
+ @Value("${redis.key.prefix.authCode}")
|
|
|
+ private String REDIS_KEY_PREFIX_AUTH_CODE;
|
|
|
+ @Value("${authCode.expire.seconds}")
|
|
|
+ private Long AUTH_CODE_EXPIRE_SECONDS;
|
|
|
|
|
|
@Override
|
|
|
public UmsMember getByUsername(String username) {
|
|
@@ -46,19 +55,23 @@ public class UmsMemberServiceImpl implements UmsMemberService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public UmsMember register(String username, String password, String telephone, String authCode) {
|
|
|
- // TODO: 2018/8/3 对验证码进行验证
|
|
|
+ public CommonResult register(String username, String password, String telephone, String authCode) {
|
|
|
+ //验证验证码
|
|
|
+ if(!verifyAuthCode(authCode,telephone)){
|
|
|
+ return new CommonResult().failed("验证码错误");
|
|
|
+ }
|
|
|
//查询是否已有该用户
|
|
|
UmsMemberExample example = new UmsMemberExample();
|
|
|
example.createCriteria().andUsernameEqualTo(username);
|
|
|
example.or(example.createCriteria().andPhoneEqualTo(telephone));
|
|
|
List<UmsMember> umsMembers = memberMapper.selectByExample(example);
|
|
|
if (!CollectionUtils.isEmpty(umsMembers)) {
|
|
|
- return null;
|
|
|
+ return new CommonResult().failed("该用户已经存在");
|
|
|
}
|
|
|
//没有该用户进行添加操作
|
|
|
UmsMember umsMember = new UmsMember();
|
|
|
umsMember.setUsername(username);
|
|
|
+ umsMember.setPhone(telephone);
|
|
|
umsMember.setPassword(passwordEncoder.encodePassword(password, null));
|
|
|
umsMember.setCreateTime(new Date());
|
|
|
umsMember.setStatus(1);
|
|
@@ -71,7 +84,7 @@ public class UmsMemberServiceImpl implements UmsMemberService {
|
|
|
}
|
|
|
memberMapper.insert(umsMember);
|
|
|
umsMember.setPassword(null);
|
|
|
- return umsMember;
|
|
|
+ return new CommonResult().success("注册成功",null);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -81,7 +94,9 @@ public class UmsMemberServiceImpl implements UmsMemberService {
|
|
|
for(int i=0;i<6;i++){
|
|
|
sb.append(random.nextInt(10));
|
|
|
}
|
|
|
- // TODO: 2018/8/6 验证码进行存储
|
|
|
+ //验证码绑定手机号并存储到redis
|
|
|
+ redisService.set(REDIS_KEY_PREFIX_AUTH_CODE+telephone,sb.toString());
|
|
|
+ redisService.expire(REDIS_KEY_PREFIX_AUTH_CODE+telephone,AUTH_CODE_EXPIRE_SECONDS);
|
|
|
return new CommonResult().success("获取验证码成功",sb.toString());
|
|
|
}
|
|
|
|
|
@@ -93,7 +108,10 @@ public class UmsMemberServiceImpl implements UmsMemberService {
|
|
|
if(CollectionUtils.isEmpty(memberList)){
|
|
|
return new CommonResult().failed("该账号不存在");
|
|
|
}
|
|
|
- // TODO: 2018/8/6 验证验证码
|
|
|
+ //验证验证码
|
|
|
+ if(!verifyAuthCode(authCode,telephone)){
|
|
|
+ return new CommonResult().failed("验证码错误");
|
|
|
+ }
|
|
|
UmsMember umsMember = memberList.get(0);
|
|
|
umsMember.setPassword(passwordEncoder.encodePassword(password,null));
|
|
|
memberMapper.updateByPrimaryKeySelective(umsMember);
|
|
@@ -107,5 +125,13 @@ public class UmsMemberServiceImpl implements UmsMemberService {
|
|
|
MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
|
|
|
return memberDetails.getUmsMember();
|
|
|
}
|
|
|
+ //对输入的验证码进行校验
|
|
|
+ private boolean verifyAuthCode(String authCode, String telephone){
|
|
|
+ if(StringUtils.isEmpty(authCode)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String realAuthCode = redisService.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone);
|
|
|
+ return authCode.equals(realAuthCode);
|
|
|
+ }
|
|
|
|
|
|
}
|