Commit db2d6b8e by 段启岩

修复重复关注的问题

parent d741e04b
package cn.meteor.beyondclouds.core.constant;
/**
* 正则表达式
* @author meteor
*/
public class RegexPatterns {
/**
* 手机号
*/
public static final String MOBILE = "^[1](([3|5|8][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$";
}
......@@ -244,7 +244,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
if (!CollectionUtils.isEmpty(topicIds)) {
blog.setTopics(topicService.listByIds(topicIds));
} else {
blog.setTags(List.of());
blog.setTopics(List.of());
}
//装配并返回查询到的数据
......
package cn.meteor.beyondclouds.modules.sms.api;
import cn.meteor.beyondclouds.common.exception.SmsException;
import cn.meteor.beyondclouds.core.annotation.Anonymous;
import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.exception.ServiceException;
import cn.meteor.beyondclouds.core.constant.RegexPatterns;
import cn.meteor.beyondclouds.modules.sms.enums.SmsErrorCode;
import cn.meteor.beyondclouds.modules.sms.exception.SmsServiceException;
import cn.meteor.beyondclouds.modules.sms.service.ISmsService;
import cn.meteor.beyondclouds.util.VerifyCodeUtils;
......@@ -36,6 +36,9 @@ public class SmsApi {
@ApiOperation("发送验证码")
@GetMapping("/verifyCode")
public Response sendVerify(@RequestParam("mobile") String mobile) {
if (!mobile.matches(RegexPatterns.MOBILE)) {
return Response.error(SmsErrorCode.INVALID_MOBILE);
}
try {
smsService.sendVerifyCode(mobile, VerifyCodeUtils.randomVerifyCode());
return Response.success();
......
......@@ -9,7 +9,8 @@ import cn.meteor.beyondclouds.core.IErrorCode;
public enum SmsErrorCode implements IErrorCode {
SERVER_ERROR(2001, "发送短信时阿里的服务器出现了异常"),
CLIENT_ERROR(2002, "发送短信时自己的服务器出现了异常"),
SEND_FAILURE(2003, "短信发送失败");
SEND_FAILURE(2004, "短信发送失败"),
INVALID_MOBILE(2005, "手机号格式错误");
private long code;
private String msg;
......
package cn.meteor.beyondclouds.modules.user.api;
import cn.meteor.beyondclouds.core.annotation.Anonymous;
import cn.meteor.beyondclouds.core.annotation.CurrentSubject;
import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.bean.Subject;
import cn.meteor.beyondclouds.modules.user.bean.AuthenticationResult;
import cn.meteor.beyondclouds.modules.user.exception.AuthenticationServiceException;
import cn.meteor.beyondclouds.modules.user.exception.UserServiceException;
......@@ -14,6 +12,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
......@@ -50,7 +49,11 @@ public class AuthenticationApi {
@Anonymous
@ApiOperation(value = "短信验证登陆")
@PostMapping("/sms")
public Response<AuthenticationResult> smsAuth(@RequestBody @Valid @ApiParam("短信认证表单") SmsAuthFrom smsAuthFrom) throws UserServiceException {
public Response smsAuth(@RequestBody @Valid @ApiParam("短信认证表单") SmsAuthFrom smsAuthFrom, BindingResult bindingResult) throws UserServiceException {
if (bindingResult.hasErrors()) {
return Response.fieldError(bindingResult.getFieldError());
}
AuthenticationResult authenticationResult = null;
try {
authenticationResult = authenticationService.smsAuthentication(smsAuthFrom.getMobile(), smsAuthFrom.getVerifyCode());
......
......@@ -148,9 +148,13 @@ public class UserApi {
@PostMapping("/user/{userId}/follower")
public Response follower(@PathVariable(name = "userId") String userId, @CurrentSubject Subject subject){
try {
userFollowService.follow(userId, (String) subject.getId());
return Response.success();
} catch (UserServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
@ApiOperation(("取消关注"))
......
......@@ -12,7 +12,11 @@ public enum UserErrorCode implements IErrorCode {
* 手机号已经被注册//
*/
MOBILE_REGISTERED(1001, "该手机号已被注册"),
REG_VERIFY_CODE_ERROR(1002, "验证码错误");
REG_VERIFY_CODE_ERROR(1002, "验证码错误"),
CAN_NOT_FOLLOW_SELF(1003, "不能关注自己"),
FOLLOWED_USER_NOT_EXISTS(1004, "被关注用户不存在"),
FOLLOWER_USER_NOT_EXISTS(1005, "关注者不存在"),
ALREADY_FOLLOWED(1006, "已关注过该用户");
UserErrorCode(long code, String msg) {
this.code = code;
......
package cn.meteor.beyondclouds.modules.user.form;
import cn.meteor.beyondclouds.core.constant.RegexPatterns;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
/**
* @program: beyond-clouds
......@@ -17,9 +22,13 @@ import lombok.Data;
public class SmsAuthFrom {
@ApiModelProperty("手机号")
@NotNull(message = "手机号不能为空")
@Pattern(regexp = RegexPatterns.MOBILE, message = "手机号格式不正确")
private String mobile;
@ApiModelProperty("验证码")
@NotBlank(message = "验证码不能为空")
@NotBlank
private String verifyCode;
}
package cn.meteor.beyondclouds.modules.user.service;
import cn.meteor.beyondclouds.modules.user.entity.UserFollow;
import cn.meteor.beyondclouds.modules.user.exception.UserServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
......@@ -17,7 +18,7 @@ public interface IUserFollowService extends IService<UserFollow> {
* @param followedId 关注者的ID
* @param followerId 被关注者的ID
*/
void follow(String followedId, String followerId);
void follow(String followedId, String followerId) throws UserServiceException;
/**
......
......@@ -2,6 +2,8 @@ package cn.meteor.beyondclouds.modules.user.service.impl;
import cn.meteor.beyondclouds.modules.user.entity.User;
import cn.meteor.beyondclouds.modules.user.entity.UserFollow;
import cn.meteor.beyondclouds.modules.user.enums.UserErrorCode;
import cn.meteor.beyondclouds.modules.user.exception.UserServiceException;
import cn.meteor.beyondclouds.modules.user.mapper.IUserFollowMapper;
import cn.meteor.beyondclouds.modules.user.service.IUserFollowService;
import cn.meteor.beyondclouds.modules.user.service.IUserService;
......@@ -34,19 +36,36 @@ public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFo
* @param followerId 关注者的userId
*/
@Override
public void follow(String followedId, String followerId) {
public void follow(String followedId, String followerId) throws UserServiceException {
// 自己不能关注自己
if (followedId.equals(followerId)) {
throw new UserServiceException(UserErrorCode.CAN_NOT_FOLLOW_SELF);
}
UserFollow userFollow = new UserFollow();
userFollow.setFollowedId(followedId);
userFollow.setFollowerId(followerId);
QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>(userFollow);
if (null != getOne(userFollowQueryWrapper)) {
throw new UserServiceException(UserErrorCode.ALREADY_FOLLOWED);
}
//1.查询被关注用户的基本信息
User followedUser = userService.getById(followedId);
userFollow.setFollowedId(followedId);
if (null == followedUser) {
throw new UserServiceException(UserErrorCode.FOLLOWED_USER_NOT_EXISTS);
}
userFollow.setFollowedNick(followedUser.getNickName());
userFollow.setFollowedAvatar(followedUser.getUserAvatar());
//2.查询我的基本信息
User followerUser = userService.getById(followerId);
userFollow.setFollowerId(followerId);
if (null == followedUser) {
throw new UserServiceException(UserErrorCode.FOLLOWER_USER_NOT_EXISTS);
}
userFollow.setFollowerNick(followerUser.getNickName());
userFollow.setFollowerAvatar(followerUser.getUserAvatar());
......
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