refactor: 微信登录功能代码修改

This commit is contained in:
wangtaocs
2024-11-29 15:39:43 +08:00
parent d24dafc1fb
commit f04e21e0e5
10 changed files with 17 additions and 10 deletions

View File

@@ -405,7 +405,7 @@ CREATE TABLE `sys_user` (
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
`update_by` bigint NULL DEFAULT NULL COMMENT '修改人ID',
`is_deleted` tinyint(1) NULL DEFAULT 0 COMMENT '逻辑删除标识(0-未删除 1-已删除)',
`open_id` char(28) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '微信 openid',
`open_id` char(28) DEFAULT NULL COMMENT '微信 openid',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `login_name`(`username` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户信息表' ROW_FORMAT = DYNAMIC;

View File

@@ -367,7 +367,7 @@
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
`update_by` bigint NULL DEFAULT NULL COMMENT '修改人ID',
`is_deleted` tinyint(1) NULL DEFAULT 0 COMMENT '逻辑删除标识(0-未删除 1-已删除)',
`open_id` char(28) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '微信 openid',
`open_id` char(28) DEFAULT NULL COMMENT '微信 openid',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `login_name`(`username` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户信息表' ROW_FORMAT = DYNAMIC;

View File

@@ -38,5 +38,5 @@ public interface SecurityConstants {
/**
* 微信登录路径
*/
String WX_LOGIN_PATH = "/api/v1/auth/wechatLogin";
String WECHAT_LOGIN_PATH = "/api/v1/auth/wechat-login";
}

View File

@@ -57,7 +57,7 @@ public class SecurityConfig {
requestMatcherRegistry
.requestMatchers(
SecurityConstants.LOGIN_PATH,
SecurityConstants.WX_LOGIN_PATH)
SecurityConstants.WECHAT_LOGIN_PATH)
.permitAll()
.anyRequest().authenticated()
)

View File

@@ -11,8 +11,9 @@ import org.springframework.context.annotation.Configuration;
/**
* 配置微信 appId 和 appSecret
*
* @author wangtao
* @date 2024/11/26 17:28
* @since 2024/11/26 17:28
*/
@Setter
@ConfigurationProperties(prefix = "wechat.miniapp")

View File

@@ -63,7 +63,7 @@ public class AuthController {
}
@Operation(summary = "微信登录")
@PostMapping("/wechatLogin")
@PostMapping("/wechat-login")
@Log(value = "微信登录", module = LogModuleEnum.LOGIN)
public Result<AuthTokenResponse> wechatLogin(
@Parameter(description = "微信授权码", example = "code") @RequestParam String code

View File

@@ -42,7 +42,8 @@ public interface AuthService {
AuthTokenResponse refreshToken(RefreshTokenRequest request);
/**
* 微信登录
* 微信小程序登录
*
* @param code 微信登录code
* @return 登录结果
*/

View File

@@ -158,6 +158,12 @@ public class AuthServiceImpl implements AuthService {
return tokenService.refreshToken(refreshToken);
}
/**
* 微信小程序登录
*
* @param code 微信登录code
* @return 访问令牌
*/
@Override
public AuthTokenResponse wechatLogin(String code) {
// 1. 通过code获取微信access_token
@@ -179,7 +185,6 @@ public class AuthServiceImpl implements AuthService {
// 2. 根据openId查询用户信息如果不存在则注册新用户
User user = userService.getUserByOpenId(openId);
if (Objects.isNull(user)) {
String name = "微信用户" + IdUtil.simpleUUID();
UserForm newUser = new UserForm();
@@ -188,11 +193,9 @@ public class AuthServiceImpl implements AuthService {
newUser.setUsername(name);
boolean result = userService.saveUser(newUser);
if (!result) {
throw new BusinessException("微信用户注册失败");
}
}
user = userService.getUserByOpenId(openId);
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(user.getUsername().toLowerCase().trim(), SystemConstants.DEFAULT_PASSWORD);
// 执行用户认证

View File

@@ -161,6 +161,7 @@ public interface UserService extends IService<User> {
/**
* 根据openId获取用户信息
*
* @param openId openId
* @return {@link User}
*/

View File

@@ -448,6 +448,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
/**
* 根据openId获取用户信息
*
* @param openId openId
* @return {@link User}
*/