123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package com.flyer.foster.service.impl;
- import cn.dev33.satoken.session.SaSession;
- import cn.dev33.satoken.stp.StpUtil;
- import cn.hutool.core.bean.BeanUtil;
- import com.flyer.exception.BusinessException;
- import com.flyer.foster.consts.LoginDevice;
- import com.flyer.foster.consts.WechatConst;
- import com.flyer.foster.dto.app.AppUserQueryDTO;
- import com.flyer.foster.dto.app.AppUserRespDTO;
- import com.flyer.foster.entity.AppUser;
- import com.flyer.foster.entity.Garden;
- import com.flyer.foster.mapper.IAppUserMapper;
- import com.flyer.foster.pojo.Code2SessionResp;
- import com.flyer.foster.pojo.StpAppUtil;
- import com.flyer.foster.service.IAppUserService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.flyer.foster.service.IGardenService;
- import com.flyer.foster.util.WeChatApiUtil;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.time.LocalDateTime;
- /**
- * <p>
- * 小程序用户 服务实现类
- * </p>
- *
- * @author flyer
- * @since 2024-05-09
- */
- @Service
- public class AppUserServiceImpl extends ServiceImpl<IAppUserMapper, AppUser> implements IAppUserService {
- /**
- * 默认用户名
- */
- public static final String DEFAUT_NAME = "微信用户";
- /**
- * 默认用户头像
- */
- public static final String DEFAULT_ICON = "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132";
- @Autowired
- private WeChatApiUtil weChatApiUtil;
- @Autowired
- private IAppUserService iAppUserService;
- @Autowired
- private IGardenService iGardenService;
- @Override
- public AppUserRespDTO login(AppUserQueryDTO dto) {
- // 查找用户信息
- Code2SessionResp code2Session = weChatApiUtil.getCode2Session(dto.getCode(), WechatConst.APP_ID, WechatConst.APP_SECRET);
- String openid = code2Session.getOpenid();
- // 根据gardenId获取租户id
- Garden garden = iGardenService.getById(dto.getGardenId());
- if (garden == null) {
- throw new BusinessException("无效的gardenId");
- }
- // 查找用户信息,存在更新,不存在新增
- AppUser appUser = iAppUserService.lambdaQuery().eq(AppUser::getOpenId, openid).one();
- if (appUser == null) {
- // 存入用户信息
- appUser = new AppUser();
- appUser.setUnionId(code2Session.getUnionid());
- appUser.setOpenId(code2Session.getOpenid());
- appUser.setName(DEFAUT_NAME);
- appUser.setIcon(DEFAULT_ICON);
- appUser.setTenantId(garden.getTenantId());
- appUser.setCreatedBy("kelei");
- appUser.setCreatedTime(LocalDateTime.now());
- appUser.setUpdatedBy("kelei");
- appUser.setUpdatedTime(LocalDateTime.now());
- appUser.setGardenId(dto.getGardenId());
- this.save(appUser);
- }
- StpAppUtil.login(appUser.getId(), LoginDevice.APP);
- SaSession tokenSession = StpAppUtil.getTokenSession();
- // 设置tenantId
- tokenSession.set("tenantId", appUser.getTenantId());
- AppUserRespDTO respDTO = new AppUserRespDTO();
- BeanUtil.copyProperties(appUser, respDTO);
- respDTO.setToken(StpAppUtil.getTokenValue());
- return respDTO;
- }
- }
|