admin
2025-02-11 8cc47cfe4c6d6b48e62cf00f6cbd0951ec57c264
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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);
        }
 
    }
 
}