1
0
Fork 0

增加用户注册功能

main
DongZeLiang 2024-01-11 20:53:00 +08:00
parent dc871d5baf
commit e3696a8216
12 changed files with 257 additions and 17 deletions

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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(), "用户注册失败");
}
}

View File

@ -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";
}

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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();
}
};
}
}

View File

@ -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();
}
}

View File

@ -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 {
}

View File

@ -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);
}
}