文章目录
1、LoginService
package com.atguigu.tingshu.user.login;
import java.util.Map;
public interface LoginService {
Map<String, Object> login(String code);
}
2、WxLoginService 微信登录
@LoginTypeBean(LoginType.WX_LOGIN)
public class WxLoginService implements LoginService {
@Autowired
private UserInfoMapper userInfoMapper;
@Autowired
private WxMaService wxMaService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private KafkaService kafkaService;
@Override
public Map<String, Object> login(String code) {
Map<String, Object> map = new HashMap<>();
try {
// 调用微信接口获取用户信息
WxMaJscode2SessionResult sessionInfo = this.wxMaService.getUserService().getSessionInfo(code);
String openid = sessionInfo.getOpenid();
// 根据openId查询用户
UserInfo userInfo = this.userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getWxOpenId, openid));
if (userInfo == null){
// 隐式注册
userInfo = new UserInfo();
userInfo.setWxOpenId(openid);
userInfo.setNickname("这家伙太懒" + IdWorker.getIdStr());
userInfo.setAvatarUrl("http://img.touxiangwu.com/uploads/allimg/2022040509/bs5oegv1dc4.jpg");
this.userInfoMapper.insert(userInfo);
// 初始化账户信息
// userAccountFeignClient.initAccount(userInfo.getId());
this.kafkaService.sendMsg(KafkaConstant.QUEUE_USER_REGISTER, userInfo.getId().toString());
// int i = 1/0;
}
// 生成token,保存到redis同时响应给页面
String token = UUID.randomUUID().toString();
UserInfoVo userInfoVo = new UserInfoVo();
BeanUtils.copyProperties(userInfo, userInfoVo);
this.redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token, userInfoVo, RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
map.put("token", token);
return map;
} catch (WxErrorException e) {
throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
}
}
}
3、AlipayLoginService 支付宝登录
package com.atguigu.tingshu.user.login;
import java.util.Map;
@LoginTypeBean(LoginType.ALIPAY_LOGIN)
public class AlipayLoginService implements LoginService {
@Override
public Map<String, Object> login(String code) {
return null;
}
}
4、PhoneLoginService 手机号登录
package com.atguigu.tingshu.user.login;
import java.util.Map;
@LoginTypeBean(LoginType.PHONE_LOGIN)
public class PhoneLoginService implements LoginService {
@Override
public Map<String, Object> login(String code) {
return null;
}
}
5、AccountLoginService 账号登录
account在这里不是钱的那个账户,在这里指的是账号登录,也就是用户名和密码登录
package com.atguigu.tingshu.user.login;
import java.util.Map;
@LoginTypeBean(LoginType.ACCOUNT_LOGIN)
public class AccountLoginService implements LoginService {
@Override
public Map<String, Object> login(String code) {
return null;
}
}
6、LoginType 枚举类
package com.atguigu.tingshu.user.login;
public enum LoginType {
WX_LOGIN(1, "微信登录"),
ALIPAY_LOGIN(2, "支付宝登录"),
PHONE_LOGIN(3, "手机号登录"),
ACCOUNT_LOGIN(4, "账号登录"),
WB_LOGIN(5, "微博"),
;
public Integer type;
public String desc;
LoginType(Integer type, String desc) {
this.type = type;
this.desc = desc;
}
}
7、@LoginTypeBean
package com.atguigu.tingshu.user.login;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface LoginTypeBean {
LoginType value();
}
8、LoginClient
package com.atguigu.tingshu.user.login;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class LoginClient implements ApplicationContextAware {
private static final Map<Integer, LoginService> MAP = new ConcurrentHashMap<>();
/**
* 重写setApplicationContext方法,用于初始化登录服务类型的Bean
* 该方法通过检查带有LoginTypeBean注解的Bean,并将它们按照登录类型存储在一个静态映射中
* 这样可以在其他地方根据登录类型快速获取对应的登录服务Bean
*
* @param applicationContext 应用上下文,用于获取所有带有LoginTypeBean注解的Bean
* @throws BeansException 如果获取Bean的过程中发生异常
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 获取所有带有LoginTypeBean注解的Bean
Map<String, Object> beanMap = applicationContext.getBeansWithAnnotation(LoginTypeBean.class);
// 如果没有找到带有LoginTypeBean注解的Bean,则直接返回
if (CollectionUtils.isEmpty(beanMap)){
return;
}
// 提取所有带有LoginTypeBean注解的Bean
Collection<Object> beans = beanMap.values();
// 遍历每个Bean,根据LoginTypeBean注解的值,将Bean存储到MAP映射中
// 这里的MAP是预先定义好的,用于根据登录类型存储对应的LoginService接口实现类
beans.forEach(bean -> {
// 获取Bean上的LoginTypeBean注解
LoginTypeBean annotation = bean.getClass().getAnnotation(LoginTypeBean.class);
// 将Bean按照登录类型存储到MAP中
// 这里的(LoginService)是类型转换,确保存储的Bean是LoginService接口的实现类
MAP.put(annotation.value().type, (LoginService)bean);
});
}
/**
* 根据用户类型和授权码进行登录的方法
*
* 此方法主要用于根据不同类型的用户,通过提供的授权码进行登录验证它是如何工作的:
* 首先,它根据用户类型从一个预定义的映射中获取对应的登录策略,然后使用这个策略对提供的授权码进行登录验证
* 这种设计模式允许系统轻松地扩展和维护不同的登录策略,而不需要修改登录逻辑本身
*
* @param type 用户类型,用于从预定义的映射中选择登录策略
* @param code 授权码,用于登录验证的凭据
* @return 返回一个Map对象,包含登录结果和可能的用户信息
*/
public Map<String, Object> login(Integer type, String code){
return MAP.get(type).login(code);
}
}
9、WxLoginApiController
@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {
@Autowired
private UserInfoService userInfoService;
@Autowired
private LoginClient loginClient;
@GetMapping("wxLogin/{code}")
public Result<Map<String, Object>> wxLogin(@PathVariable String code){
//Map<String, Object> map = this.userInfoService.login(code);
Map<String, Object> map = this.loginClient.login(1, code);
return Result.ok(map);
}
}