package com.everyday.word.service.impl.user;
|
|
import com.everyday.word.dao.user.UserAuthMapper;
|
import com.everyday.word.dao.user.UserMapper;
|
import com.everyday.word.dto.QQUserInfo;
|
import com.everyday.word.entity.SystemEnum;
|
import com.everyday.word.entity.user.IdentityType;
|
import com.everyday.word.entity.user.User;
|
import com.everyday.word.entity.user.UserAuth;
|
import com.everyday.word.exception.user.UserException;
|
import com.everyday.word.factory.user.UserAuthFactory;
|
import com.everyday.word.service.inter.user.UserService;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.entity.wx.WeiXinUser;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* @author hxh
|
* @title: UserServiceImpl
|
* @description: 用户服务实现
|
* @date 2025/2/11 13:14
|
*/
|
@Service
|
public class UserServiceImpl implements UserService {
|
@Resource
|
private UserMapper userMapper;
|
@Resource
|
private UserAuthMapper userAuthMapper;
|
|
@Override
|
public User loginByWX(WeiXinUser user, SystemEnum system) {
|
UserAuth userAuth = selectUserAuth(IdentityType.WECHAT, user.getOpenid(), system);
|
if (userAuth != null) {
|
return selectUser(userAuth.getUserId());
|
}
|
return null;
|
}
|
|
@Override
|
public User loginByQQ(String openId, SystemEnum system) {
|
UserAuth userAuth = selectUserAuth(IdentityType.QQ, openId, system);
|
if (userAuth != null) {
|
return selectUser(userAuth.getUserId());
|
}
|
return null;
|
}
|
|
@Override
|
public User loginByPhone(String phone, SystemEnum system) {
|
UserAuth userAuth = selectUserAuth(IdentityType.PHONE, phone, system);
|
if (userAuth != null) {
|
return selectUser(userAuth.getUserId());
|
}
|
return null;
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void register(UserAuth authInfo) throws UserException {
|
if (authInfo.getIdentityType() == null || StringUtil.isNullOrEmpty(authInfo.getIdentifier())) {
|
throw new UserException("数据缺失");
|
}
|
if (selectUserAuth(authInfo.getIdentityType(), authInfo.getIdentifier(), authInfo.getSystem()) != null) {
|
throw new UserException("授权数据已存在");
|
}
|
// 新增用户
|
User user = new User();
|
user.setNickName(authInfo.getNickName());
|
user.setPortrait(authInfo.getPortrait());
|
user.setCreateTime(new Date());
|
userMapper.insertSelective(user);
|
authInfo.setUserId(user.getId());
|
authInfo.setCreateTime(new Date());
|
userAuthMapper.insertSelective(authInfo);
|
}
|
|
@Override
|
public UserAuth selectUserAuth(IdentityType type, String identifier, SystemEnum system) {
|
UserAuthMapper.DaoQuery daoQuery = new UserAuthMapper.DaoQuery();
|
daoQuery.identityType = type;
|
daoQuery.identifier = identifier;
|
daoQuery.system = system;
|
daoQuery.count = 1;
|
List<UserAuth> authList = userAuthMapper.list(daoQuery);
|
if (authList.size() < 1) {
|
return null;
|
}
|
return authList.get(0);
|
}
|
|
@Override
|
public UserAuth selectUserAuth(Long uid, IdentityType type) {
|
UserAuthMapper.DaoQuery daoQuery = new UserAuthMapper.DaoQuery();
|
daoQuery.identityType = type;
|
daoQuery.userId = uid;
|
daoQuery.count = 1;
|
List<UserAuth> authList = userAuthMapper.list(daoQuery);
|
if (authList.size() < 1) {
|
return null;
|
}
|
return authList.get(0);
|
}
|
|
@Override
|
public List<UserAuth> listUserAuth(Long uid) {
|
UserAuthMapper.DaoQuery daoQuery = new UserAuthMapper.DaoQuery();
|
daoQuery.userId = uid;
|
daoQuery.count = Integer.MAX_VALUE;
|
return userAuthMapper.list(daoQuery);
|
}
|
|
@Override
|
public User selectUser(Long id) {
|
return userMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public void bindWechat(Long userId, WeiXinUser weiXinUser) throws UserException {
|
User user = selectUser(userId);
|
if (user == null) {
|
throw new UserException("用户不存在");
|
}
|
UserAuth userAuth = UserAuthFactory.create(weiXinUser);
|
if (selectUserAuth(userAuth.getIdentityType(), userAuth.getIdentifier(), user.getSystem()) != null) {
|
throw new UserException("微信已被绑定");
|
}
|
bind(userId, userAuth);
|
}
|
|
@Override
|
public void bindQQ(Long userId, QQUserInfo qqUserInfo) throws UserException {
|
User user = selectUser(userId);
|
if (user == null) {
|
throw new UserException("用户不存在");
|
}
|
UserAuth userAuth = UserAuthFactory.create(qqUserInfo);
|
if (selectUserAuth(userAuth.getIdentityType(), userAuth.getIdentifier(), user.getSystem()) != null) {
|
throw new UserException("QQ已被绑定");
|
}
|
bind(userId, userAuth);
|
}
|
|
@Override
|
public void bindPhone(Long userId, String phone) throws UserException {
|
User user = selectUser(userId);
|
if (user == null) {
|
throw new UserException("用户不存在");
|
}
|
UserAuth userAuth = UserAuthFactory.create(phone);
|
if (selectUserAuth(userAuth.getIdentityType(), userAuth.getIdentifier(), user.getSystem()) != null) {
|
throw new UserException("手机号已被绑定");
|
}
|
bind(userId, userAuth);
|
}
|
|
private void bind(Long userId, UserAuth userAuth) {
|
// 查询是否为更换绑定
|
UserAuth oldAuth = selectUserAuth(userId, userAuth.getIdentityType());
|
if (oldAuth != null) {
|
// 更换绑定
|
userAuth.setId(oldAuth.getId());
|
userAuth.setCreateTime(oldAuth.getCreateTime());
|
userAuth.setUpdateTime(new Date());
|
userAuthMapper.updateByPrimaryKeySelective(userAuth);
|
} else {
|
// 添加绑定
|
userAuth.setUserId(userId);
|
userAuth.setCreateTime(new Date());
|
userAuthMapper.insertSelective(userAuth);
|
}
|
|
}
|
|
}
|