AppUserServiceImpl.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.flyer.foster.service.impl;
  2. import cn.dev33.satoken.session.SaSession;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import cn.hutool.core.bean.BeanUtil;
  5. import com.flyer.exception.BusinessException;
  6. import com.flyer.foster.consts.LoginDevice;
  7. import com.flyer.foster.consts.WechatConst;
  8. import com.flyer.foster.dto.app.AppUserQueryDTO;
  9. import com.flyer.foster.dto.app.AppUserRespDTO;
  10. import com.flyer.foster.entity.AppUser;
  11. import com.flyer.foster.entity.Garden;
  12. import com.flyer.foster.mapper.IAppUserMapper;
  13. import com.flyer.foster.pojo.Code2SessionResp;
  14. import com.flyer.foster.pojo.StpAppUtil;
  15. import com.flyer.foster.service.IAppUserService;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import com.flyer.foster.service.IGardenService;
  18. import com.flyer.foster.util.WeChatApiUtil;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import java.time.LocalDateTime;
  22. /**
  23. * <p>
  24. * 小程序用户 服务实现类
  25. * </p>
  26. *
  27. * @author flyer
  28. * @since 2024-05-09
  29. */
  30. @Service
  31. public class AppUserServiceImpl extends ServiceImpl<IAppUserMapper, AppUser> implements IAppUserService {
  32. /**
  33. * 默认用户名
  34. */
  35. public static final String DEFAUT_NAME = "微信用户";
  36. /**
  37. * 默认用户头像
  38. */
  39. public static final String DEFAULT_ICON = "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132";
  40. @Autowired
  41. private WeChatApiUtil weChatApiUtil;
  42. @Autowired
  43. private IAppUserService iAppUserService;
  44. @Autowired
  45. private IGardenService iGardenService;
  46. @Override
  47. public AppUserRespDTO login(AppUserQueryDTO dto) {
  48. // 查找用户信息
  49. Code2SessionResp code2Session = weChatApiUtil.getCode2Session(dto.getCode(), WechatConst.APP_ID, WechatConst.APP_SECRET);
  50. String openid = code2Session.getOpenid();
  51. // 根据gardenId获取租户id
  52. Garden garden = iGardenService.getById(dto.getGardenId());
  53. if (garden == null) {
  54. throw new BusinessException("无效的gardenId");
  55. }
  56. // 查找用户信息,存在更新,不存在新增
  57. AppUser appUser = iAppUserService.lambdaQuery().eq(AppUser::getOpenId, openid).one();
  58. if (appUser == null) {
  59. // 存入用户信息
  60. appUser = new AppUser();
  61. appUser.setUnionId(code2Session.getUnionid());
  62. appUser.setOpenId(code2Session.getOpenid());
  63. appUser.setName(DEFAUT_NAME);
  64. appUser.setIcon(DEFAULT_ICON);
  65. appUser.setTenantId(garden.getTenantId());
  66. appUser.setCreatedBy("kelei");
  67. appUser.setCreatedTime(LocalDateTime.now());
  68. appUser.setUpdatedBy("kelei");
  69. appUser.setUpdatedTime(LocalDateTime.now());
  70. appUser.setGardenId(dto.getGardenId());
  71. this.save(appUser);
  72. }
  73. StpAppUtil.login(appUser.getId(), LoginDevice.APP);
  74. SaSession tokenSession = StpAppUtil.getTokenSession();
  75. // 设置tenantId
  76. tokenSession.set("tenantId", appUser.getTenantId());
  77. AppUserRespDTO respDTO = new AppUserRespDTO();
  78. BeanUtil.copyProperties(appUser, respDTO);
  79. respDTO.setToken(StpAppUtil.getTokenValue());
  80. return respDTO;
  81. }
  82. }