parent
f4a698bc57
commit
220984c45b
@ -0,0 +1,18 @@
|
||||
package cn.iocoder.mall.system.biz.bo.user;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.ouath2.OAuth2AccessTokenBO;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* TODO 注释
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class UserAuthenticateBO {
|
||||
|
||||
private UserBO user;
|
||||
|
||||
private OAuth2AccessTokenBO token;
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package cn.iocoder.mall.system.biz.bo.user;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* TODO 注释
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class UserBO {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String nickname;
|
||||
/**
|
||||
* 头像
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package cn.iocoder.mall.system.biz.convert;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.ouath2.OAuth2AccessTokenBO;
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserAuthenticateBO;
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserBO;
|
||||
import cn.iocoder.mall.system.biz.dataobject.user.UserDO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface UserConvert {
|
||||
|
||||
UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
|
||||
|
||||
@Mapping(source = "userBO", target = "user")
|
||||
@Mapping(source = "accessTokenBO", target = "token")
|
||||
UserAuthenticateBO convert(UserBO userBO, OAuth2AccessTokenBO accessTokenBO);
|
||||
|
||||
UserBO convert(UserDO userDO);
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package cn.iocoder.mall.system.biz.dao.user;
|
||||
|
||||
import cn.iocoder.mall.system.biz.dataobject.user.UserDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserMapper extends BaseMapper<UserDO> {
|
||||
|
||||
default UserDO selectByAccountId(Integer accountId) {
|
||||
return selectOne(new QueryWrapper<UserDO>()
|
||||
.eq("account_id", accountId)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package cn.iocoder.mall.system.biz.dto.account;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
// TODO 注释
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AccountCreateDTO {
|
||||
|
||||
/**
|
||||
* 登陆账号
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
private String mobile;
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
private String email;
|
||||
/**
|
||||
* 密码
|
||||
*
|
||||
* // TODO 芋艿 暂时明文
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 创建 IP
|
||||
*/
|
||||
private String createIp;
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package cn.iocoder.mall.system.biz.dto.oatuh2;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
// TODO 注释
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class OAuth2MobileCodAuthenticateDTO {
|
||||
|
||||
private String mobile;
|
||||
private String code;
|
||||
|
||||
}
|
||||
@ -1,10 +1,13 @@
|
||||
package cn.iocoder.mall.system.biz.service.user;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserAuthenticateBO;
|
||||
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2MobileCodeAuthenticateDTO;
|
||||
|
||||
/**
|
||||
* 用户 Service 接口
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
|
||||
UserAuthenticateBO authenticate(OAuth2MobileCodeAuthenticateDTO authenticateDTO);
|
||||
|
||||
}
|
||||
|
||||
@ -1,8 +1,47 @@
|
||||
package cn.iocoder.mall.system.biz.service.user.impl;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.ouath2.OAuth2AccessTokenBO;
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserAuthenticateBO;
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserBO;
|
||||
import cn.iocoder.mall.system.biz.convert.UserConvert;
|
||||
import cn.iocoder.mall.system.biz.dao.user.UserMapper;
|
||||
import cn.iocoder.mall.system.biz.dataobject.user.UserDO;
|
||||
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2MobileCodeAuthenticateDTO;
|
||||
import cn.iocoder.mall.system.biz.service.oauth2.OAuth2Service;
|
||||
import cn.iocoder.mall.system.biz.service.user.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private OAuth2Service oAuth2Service;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public UserAuthenticateBO authenticate(OAuth2MobileCodeAuthenticateDTO authenticateDTO) {
|
||||
// 执行认证
|
||||
OAuth2AccessTokenBO accessTokenBO = oAuth2Service.authenticate(authenticateDTO);
|
||||
// 获得用户
|
||||
UserDO userDO = userMapper.selectById(accessTokenBO.getAccountId());
|
||||
if (userDO == null) {
|
||||
userDO = this.creatUser(accessTokenBO.getAccountId());
|
||||
}
|
||||
UserBO userBO = UserConvert.INSTANCE.convert(userDO);
|
||||
// 拼装返回
|
||||
return UserConvert.INSTANCE.convert(userBO, accessTokenBO);
|
||||
}
|
||||
|
||||
private UserDO creatUser(Integer accountId) {
|
||||
UserDO user = new UserDO();
|
||||
user.setAccountId(accountId);
|
||||
userMapper.insert(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
package cn.iocoder.mall.system.rest.convert.oauth2;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserAuthenticateBO;
|
||||
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2MobileCodeAuthenticateDTO;
|
||||
import cn.iocoder.mall.system.rest.request.oauth2.UsersOAuth2MobileCodeAuthenticateRequest;
|
||||
import cn.iocoder.mall.system.rest.response.user.UsersOAuth2AuthenticateResponse;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface UsersOAuth2Convert {
|
||||
|
||||
UsersOAuth2Convert INSTANCE = Mappers.getMapper(UsersOAuth2Convert.class);
|
||||
|
||||
OAuth2MobileCodeAuthenticateDTO convert(UsersOAuth2MobileCodeAuthenticateRequest request);
|
||||
|
||||
@Mapping(source = "token.id", target = "token.accessToken")
|
||||
UsersOAuth2AuthenticateResponse convert(UserAuthenticateBO userAuthenticateBO);
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package cn.iocoder.mall.system.rest.convert.user;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UsersUserConvert {
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package cn.iocoder.mall.system.rest.request.oauth2;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
@ApiModel("用户 - OAuth2 模块 - 手机验证码认证请求")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class UsersOAuth2MobileCodeAuthenticateRequest {
|
||||
|
||||
@ApiModelProperty(value = "手机号", required = true, example = "15601691300")
|
||||
@NotEmpty(message = "手机号不能为空")
|
||||
@Length(min = 11, max = 11, message = "账号长度为 11 位")
|
||||
@Pattern(regexp = "^[0-9]+$", message = "手机号必须都是数字")
|
||||
private String mobile;
|
||||
|
||||
@ApiModelProperty(value = "手机验证码", required = true, example = "1024")
|
||||
@NotEmpty(message = "手机验证码不能为空")
|
||||
@Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位")
|
||||
@Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字")
|
||||
private String code;
|
||||
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
package cn.iocoder.mall.system.rest.request;
|
||||
@ -0,0 +1,49 @@
|
||||
package cn.iocoder.mall.system.rest.response.oauth2;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("管理员 - OAuth2 模块 - 认证响应")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AdminsOAuth2AuthenticateResponse {
|
||||
|
||||
@Data
|
||||
public static class Token {
|
||||
|
||||
@ApiModelProperty(value = "access token", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String accessToken;
|
||||
|
||||
@ApiModelProperty(value = "refresh token", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String refreshToken;
|
||||
|
||||
@ApiModelProperty(value = "过期时间", required = true)
|
||||
private Date expiresTime;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Admin {
|
||||
|
||||
@ApiModelProperty(value = "管理员编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "真实名字", required = true, example = "小王")
|
||||
private String name;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 晚点测试下 swagger 的表现
|
||||
*/
|
||||
private Admin admin;
|
||||
/**
|
||||
* TODO 晚点测试下 swagger 的表现
|
||||
*/
|
||||
private Token token;
|
||||
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
package cn.iocoder.mall.user.api;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
|
||||
public interface MobileCodeService {
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
* @param mobile 手机号
|
||||
*/
|
||||
void send(String mobile) throws ServiceException;
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue