diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/PassportController.java similarity index 96% rename from user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java rename to user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/PassportController.java index d79cf0624..27a25282a 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/PassportController.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.user.application.controller.users; +package cn.iocoder.mall.user.application.controller; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.application.convert.PassportConvert; @@ -19,7 +19,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("users/passport") +@RequestMapping("user/passport") @Api("Passport 模块") public class PassportController { diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/UserController.java similarity index 90% rename from user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java rename to user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/UserController.java index 9e1c83899..f6ad37d30 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/UserController.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.user.application.controller.users; +package cn.iocoder.mall.user.application.controller; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder; @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("users/user") +@RequestMapping("/user") @Api("用户模块") public class UserController { diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/UserService.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/UserService.java index 149cff1b5..bbd479d59 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/UserService.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/service/api/UserService.java @@ -1,5 +1,19 @@ package cn.iocoder.mall.user.service.api; +import cn.iocoder.common.framework.exception.ServiceException; +import cn.iocoder.mall.user.service.api.bo.UserBO; + public interface UserService { + /** + * 创建用户。一般在用户注册时,调用该方法 + * + * TODO 芋艿,此处要传递一些用户注册时的相关信息,例如说 ip、ua、客户端来源等等。用于数据分析、风控等等。 + * + * @param mobile 手机号 + * @param code 手机验证码 + * @return 用户 + */ + UserBO createUser(String mobile, String code) throws ServiceException; + } \ No newline at end of file diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/dataobject/UserDO.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/dataobject/UserDO.java index b51d2b8bd..45bcb6e7d 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/dataobject/UserDO.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/dataobject/UserDO.java @@ -1,13 +1,13 @@ package cn.iocoder.mall.user.dataobject; -import cn.iocoder.common.framework.dataobject.BaseDO; +import java.util.Date; /** * 用户实体,存储用户基本数据。 * * idx_mobile 唯一索引 */ -public class UserDO extends BaseDO { +public class UserDO { /** * 用户编号 @@ -18,13 +18,9 @@ public class UserDO extends BaseDO { */ private String mobile; /** - * 昵称 + * 创建时间 */ - private String nickname; - /** - * 头像 - */ - private String avatar; + private Date createTime; public Long getId() { return id; @@ -44,22 +40,12 @@ public class UserDO extends BaseDO { return this; } - public String getNickname() { - return nickname; + public Date getCreateTime() { + return createTime; } - public UserDO setNickname(String nickname) { - this.nickname = nickname; + public UserDO setCreateTime(Date createTime) { + this.createTime = createTime; return this; } - - public String getAvatar() { - return avatar; - } - - public UserDO setAvatar(String avatar) { - this.avatar = avatar; - return this; - } - } \ No newline at end of file diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java index f85161458..f17b54866 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/OAuth2ServiceImpl.java @@ -61,11 +61,7 @@ public class OAuth2ServiceImpl implements OAuth2Service { // 获取用户 UserDO userDO = userService.getUser(mobile); if (userDO == null) { // 用户不存在,则进行创建用户 - CommonResult createResult = userService.createUser(mobile); - if (createResult.isError()) { - return CommonResult.error(createResult); - } - userDO = createResult.getData(); + userDO = userService.createUser(mobile); Assert.notNull(userDO, "创建用户必然成功"); } // 创建刷新令牌 diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/UserServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/UserServiceImpl.java index adbdf7e34..bd9066caa 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/UserServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/service/UserServiceImpl.java @@ -1,13 +1,14 @@ package cn.iocoder.mall.user.service; import cn.iocoder.common.framework.util.ServiceExceptionUtil; -import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.user.convert.UserConvert; import cn.iocoder.mall.user.dao.UserMapper; import cn.iocoder.mall.user.dao.UserRegisterMapper; import cn.iocoder.mall.user.dataobject.UserDO; import cn.iocoder.mall.user.dataobject.UserRegisterDO; import cn.iocoder.mall.user.service.api.UserService; import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum; +import cn.iocoder.mall.user.service.api.bo.UserBO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; @@ -31,21 +32,38 @@ public class UserServiceImpl implements UserService { return userMapper.selectByMobile(mobile); } + @Override @Transactional - public CommonResult createUser(String mobile) { + public UserBO createUser(String mobile, String code) { // TODO 芋艿,校验手机格式 + // 校验手机号的最后一个手机验证码是否有效 + mobileCodeService.validLastMobileCode(mobile, code); // 校验用户是否已经存在 if (getUser(mobile) != null) { - return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode()); } // 创建用户 - UserDO userDO = new UserDO().setMobile(mobile); - userDO.setCreateTime(new Date()); + UserDO userDO = new UserDO().setMobile(mobile).setCreateTime(new Date()); userMapper.insert(userDO); // 插入注册信息 createUserRegister(userDO); // 转换返回 - return CommonResult.success(userDO); + return UserConvert.INSTANCE.convert(userDO); + } + + @Transactional + public UserDO createUser(String mobile) { + // 校验用户是否已经存在 + if (getUser(mobile) != null) { + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode()); + } + // 创建用户 + UserDO userDO = new UserDO().setMobile(mobile).setCreateTime(new Date()); + userMapper.insert(userDO); + // 插入注册信息 + createUserRegister(userDO); + // 转换返回 + return userDO; } private void createUserRegister(UserDO userDO) {