增加用户注册功能
parent
dc871d5baf
commit
e3696a8216
|
@ -1,9 +1,14 @@
|
|||
package com.muyu.auth;
|
||||
|
||||
import com.muyu.common.handler.GlobalExceptionHandler;
|
||||
import com.muyu.common.remote.system.factory.RemoteUserInfoFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
|
@ -11,6 +16,8 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
|||
*/
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients(basePackages = "com.muyu.**")
|
||||
@Import(value = {RemoteUserInfoFactory.class, GlobalExceptionHandler.class})
|
||||
public class AuthApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthApplication.class);
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package com.muyu.auth.controller;
|
||||
|
||||
import com.muyu.auth.service.AuthRegService;
|
||||
import com.muyu.common.domain.request.auth.RegUserReq;
|
||||
import com.muyu.common.result.Result;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 注册
|
||||
* @Date 2024-1-11 下午 08:04
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/reg")
|
||||
public class AuthRegController {
|
||||
|
||||
private final AuthRegService authRegService;
|
||||
|
||||
public AuthRegController (AuthRegService authRegService) {
|
||||
this.authRegService = authRegService;
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public Result<String> reg(@RequestBody @Validated RegUserReq regUserReq){
|
||||
authRegService.reg(regUserReq.getUserName(), regUserReq.getPassword());
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.muyu.auth.service;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 注册业务
|
||||
* @Date 2024-1-11 下午 08:07
|
||||
*/
|
||||
public interface AuthRegService {
|
||||
|
||||
/**
|
||||
* 注册用户
|
||||
* @param userName 用户名称
|
||||
* @param password 用户密码
|
||||
*/
|
||||
void reg (String userName, String password);
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.muyu.auth.service.impl;
|
||||
|
||||
import com.muyu.auth.service.AuthRegService;
|
||||
import com.muyu.common.domain.request.user.UserAddReq;
|
||||
import com.muyu.common.remote.system.RemoteUserInfoService;
|
||||
import com.muyu.common.result.Result;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 注册用户实现类
|
||||
* @Date 2024-1-11 下午 08:07
|
||||
*/
|
||||
@Service
|
||||
public class AuthRegServiceImpl implements AuthRegService {
|
||||
|
||||
private final RemoteUserInfoService remoteUserInfoService;
|
||||
|
||||
public AuthRegServiceImpl (RemoteUserInfoService remoteUserInfoService) {
|
||||
this.remoteUserInfoService = remoteUserInfoService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册用户
|
||||
*
|
||||
* @param userName 用户名称
|
||||
* @param password 用户密码
|
||||
*/
|
||||
@Override
|
||||
public void reg (String userName, String password) {
|
||||
Result<String> addResult = remoteUserInfoService.add(
|
||||
UserAddReq.builder()
|
||||
.userName(userName)
|
||||
.password(password)
|
||||
.build()
|
||||
);
|
||||
Assert.isTrue(addResult.isSuccess(), "用户注册失败");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.muyu.common.constant;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 服务名称常量
|
||||
* @Date 2024-1-11 下午 07:43
|
||||
*/
|
||||
public class ServerNameConstants {
|
||||
|
||||
/**
|
||||
* 系统服务名称
|
||||
*/
|
||||
public final static String SYSTEM_NAME = "muyu-system";
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.common.domain.request.auth;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 注册用户
|
||||
* @Date 2024-1-11 下午 08:05
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RegUserReq {
|
||||
|
||||
@NotEmpty(message = "用户名称不可为空")
|
||||
@Pattern(regexp = "(?=.*[A-Za-z]\\d).{6,16}$", message = "用户名称不合法,因为6-16位的大小写英文和数字组成")
|
||||
private String userName;
|
||||
|
||||
@NotEmpty(message = "用户密码不可为空")
|
||||
@Pattern(regexp = "(?=.*[A-Za-z]\\d).{8,16}$", message = "用户密码不合法,因为8-16位的大小写英文和数字组成")
|
||||
private String password;
|
||||
}
|
|
@ -2,6 +2,7 @@ package com.muyu.common.handler;
|
|||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.muyu.common.result.Result;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
|
@ -13,12 +14,14 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||
* @description: 全局异常处理
|
||||
* @Date 2024-1-10 下午 08:49
|
||||
*/
|
||||
@Log4j2
|
||||
@RestControllerAdvice
|
||||
@Configuration
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||
public Result<String> runtimeException (MethodArgumentNotValidException exception) {
|
||||
log.error("请求异常:[{}]", exception.getMessage(), exception);
|
||||
return Result.error(
|
||||
JSONObject.toJSONString(
|
||||
exception.getBindingResult().getAllErrors()
|
||||
|
@ -28,4 +31,10 @@ public class GlobalExceptionHandler {
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = IllegalArgumentException.class)
|
||||
public Result<String> illegalArgumentExceptionHandler(IllegalArgumentException exception){
|
||||
log.error("请求异常:[{}]", exception.getMessage(), exception);
|
||||
return Result.error(exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package com.muyu.common.remote.system;
|
||||
|
||||
import com.muyu.common.constant.ServerNameConstants;
|
||||
import com.muyu.common.domain.request.user.UserAddReq;
|
||||
import com.muyu.common.remote.system.factory.RemoteUserInfoFactory;
|
||||
import com.muyu.common.result.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户远程调用
|
||||
* @Date 2024-1-11 下午 07:42
|
||||
*/
|
||||
@FeignClient(
|
||||
name = ServerNameConstants.SYSTEM_NAME,
|
||||
fallbackFactory = RemoteUserInfoFactory.class,
|
||||
path = "/user"
|
||||
)
|
||||
public interface RemoteUserInfoService {
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
* @return 方法结果集
|
||||
*/
|
||||
@PostMapping()
|
||||
public Result<String> add(@RequestBody @Validated UserAddReq userAddReq);
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.muyu.common.remote.system.factory;
|
||||
|
||||
import com.muyu.common.domain.request.user.UserAddReq;
|
||||
import com.muyu.common.remote.system.RemoteUserInfoService;
|
||||
import com.muyu.common.result.Result;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 远程用户调用熔断
|
||||
* @Date 2024-1-11 下午 07:46
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class RemoteUserInfoFactory implements FallbackFactory<RemoteUserInfoService> {
|
||||
@Override
|
||||
public RemoteUserInfoService create (Throwable cause) {
|
||||
return new RemoteUserInfoService(){
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
* @param userAddReq 请求对象
|
||||
* @return 方法结果集
|
||||
*/
|
||||
@Override
|
||||
public Result<String> add (UserAddReq userAddReq) {
|
||||
log.error("添加用户远程调用失败:[{}-{}]",userAddReq, cause.getMessage(), cause);
|
||||
return Result.error();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -50,4 +50,20 @@ public class Result<T> implements Serializable {
|
|||
apiResult.setMsg(msg);
|
||||
return apiResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功
|
||||
* @return 成功 true
|
||||
*/
|
||||
public boolean isSuccess(){
|
||||
return this.code == SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败
|
||||
* @return 失败 false
|
||||
*/
|
||||
public boolean isError(){
|
||||
return !isSuccess();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package com.muyu.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.domain.UserInfo;
|
||||
import com.muyu.system.mapper.UserInfoMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户业务实现类
|
||||
* @Date 2024-1-10 下午 07:42
|
||||
*/
|
||||
@Service
|
||||
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
|
||||
implements UserInfoService {
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.domain.UserInfo;
|
||||
import com.muyu.system.mapper.UserInfoMapper;
|
||||
import com.muyu.system.service.UserInfoService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户业务实现类
|
||||
* @Date 2024-1-10 下午 07:42
|
||||
*/
|
||||
@Service
|
||||
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
|
||||
implements UserInfoService {
|
||||
|
||||
@Override
|
||||
public boolean save (UserInfo entity) {
|
||||
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(UserInfo::getUserName, entity.getUserName());
|
||||
UserInfo userInfo = getOne(queryWrapper);
|
||||
Assert.isNull(userInfo, "用户已经存在");
|
||||
return super.save(entity);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue