| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package com.cyksj.service.user.impl;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.config.WeChatConfig;
- import com.cyksj.mapper.UserMapper;
- import com.cyksj.mapper.UserTransferLogMapper;
- import com.cyksj.mapper.WxSyncLogMapper;
- import com.cyksj.model.dto.GzhOAuth2UserInfo;
- import com.cyksj.model.dto.UserWhoami;
- import com.cyksj.model.entity.User;
- import com.cyksj.model.entity.UserTransferLog;
- import com.cyksj.model.entity.WxSyncLog;
- import com.cyksj.service.distribute.DistributeService;
- import com.cyksj.service.user.UserService;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.util.Assert;
- import java.util.Optional;
- /**
- * @author chan
- * @date 2021/10/10 下午3:27
- */
- @Slf4j
- @Service
- @RequiredArgsConstructor
- public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
- private final WeChatConfig weChatConfig;
- private final UserTransferLogMapper userTransferLogMapper;
- private final WxSyncLogMapper wxSyncLogMapper;
- private final DistributeService distributeService;
- @Override
- public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType, Long pid) {
- final String unionid = info.getUnionid();
- if (StrUtil.isEmpty(unionid)) {
- throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面");
- }
- WxSyncLog wxSyncLog = wxSyncLogMapper.selectOne(Wrappers.lambdaQuery(WxSyncLog.class)
- .eq(WxSyncLog::getToAppid, weChatConfig.getAppid())
- .eq(WxSyncLog::getNewOpenid, info.getOpenid()).last("limit 1"));
- User user = getSyncUser(info.getOpenid(), info.getUnionid(), wxSyncLog);
- user = Optional.ofNullable(user).orElseGet(() -> new User()
- .setUnionid(unionid));
- user.setNickname(info.getNickname())
- .setSex(info.getSex())
- .setCountry(info.getCountry())
- .setProvince(info.getProvince())
- .setCity(info.getCity())
- .setHeadimgurl(info.getHeadimgurl());
- if (!user.getUnionid().equals(unionid)) {
- UserTransferLog log = new UserTransferLog();
- if (wxSyncLog != null) {
- log.setType("公众号迁移");
- } else {
- log.setType("绑定开发者账号");
- }
- log.setOldAppId(weChatConfig.getOldAppid());
- log.setNewAppId(weChatConfig.getAppid());
- log.setUserId(user.getId());
- log.setOldUnionid(user.getUnionid());
- log.setNewUnionid(info.getUnionid());
- userTransferLogMapper.insert(log);
- }
- user.setUnionid(unionid);
- user.setOpenId(info.getOpenid());
- Boolean isNewUser = false;
- if (user.getId() == null) {
- isNewUser = true;
- user.setPopularizeId(pid);
- }
- this.saveOrUpdate(user);
- if (sharedId != null && isNewUser) {
- distributeService.saveDistributionInvitation(sharedId, user.getId());
- }
- return user;
- }
- @Override
- public UserWhoami whoami(long userId) {
- User user = this.getById(userId);
- Assert.notNull(user,"业务处理失败,请联系客服");
- UserWhoami userWhoami = new UserWhoami();
- BeanUtil.copyProperties(user, userWhoami);
- return userWhoami;
- }
- @Override
- public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) {
- User user;
- //迁移公众号之后同步授权后信息
- //是否是迁移用户
- if (wxSyncLog != null) {
- user = this.getOne(Wrappers.lambdaQuery(User.class)
- .ne(User::getUnionid, unionId)
- .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1"));
- if (user != null) {
- user.setOpenId(wxSyncLog.getNewOpenid());
- } else {
- // 查询用户是否存在
- user = Optional.ofNullable(
- this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
- .orderByAsc(User::getId)
- .last("limit 1")))
- .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
- .orderByAsc(User::getId)
- .last("limit 1")));
- }
- } else {
- // 查询用户是否存在
- user = Optional.ofNullable(
- this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
- .orderByAsc(User::getId)
- .last("limit 1")))
- .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
- .orderByAsc(User::getId)
- .last("limit 1")));
- }
- return user;
- }
- }
|