admin
2021-03-06 7804263c6061aef813f0db27cb3046f746572606
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.yeshi.buwan.service.imp;
 
import com.yeshi.buwan.dao.user.LoginUserDao;
import com.yeshi.buwan.dao.user.LoginUserExtraDao;
import com.yeshi.buwan.domain.user.LoginUser;
import com.yeshi.buwan.domain.user.LoginUserExtra;
import com.yeshi.buwan.exception.user.LoginUserException;
import com.yeshi.buwan.exception.PPTVException;
import com.yeshi.buwan.pptv.PPTVApiUtil;
import com.yeshi.buwan.pptv.PPTVUtil;
import com.yeshi.buwan.service.inter.LoginUserService;
import org.springframework.stereotype.Service;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.entity.wx.WeiXinUser;
 
import javax.annotation.Resource;
import java.util.Date;
 
@Service
public class LoginUserServiceImpl implements LoginUserService {
 
    @Resource
    private LoginUserDao loginUserDao;
 
    @Resource
    private LoginUserExtraDao loginUserExtraDao;
 
 
    @Override
    public void initExtra(LoginUserExtra extra) {
        if (extra.getCreateTime() == null)
            extra.setCreateTime(new Date());
        extra.setPptvUid(PPTVUtil.getPPTVUid(extra.getId()));
        loginUserExtraDao.save(extra);
    }
 
    @Override
    public LoginUser getLoginUser(String id) {
        return loginUserDao.find(LoginUser.class, id);
    }
 
    @Override
    public LoginUser getLoginUserByOpenId(String systemId, String openid) {
        return loginUserDao.findOne("from LoginUser u where u.systemId=? and u.openid=?", systemId, openid);
    }
 
    @Override
    public LoginUser getLoginUserByEmail(String systemId, String email) {
        return loginUserDao.findOne("from LoginUser u where u.systemId=? and u.email=?", systemId, email);
    }
 
    @Override
    public LoginUser getLoginUserByQQOpenId(String systemId, String openId) {
        return loginUserDao.findOne("from LoginUser u where u.systemId=? and u.qqOpenId=?", systemId, openId);
    }
 
    @Override
    public LoginUser getLoginUserByWxUnionId(String systemId, String unionId) {
        return loginUserDao.findOne("from LoginUser u where u.systemId=? and u.wxUnionId=?", systemId, unionId);
    }
 
    @Override
    public LoginUser getLoginUserByPhone(String systemId, String phone) {
        return loginUserDao.findOne("from LoginUser u where u.systemId=? and u.phone=?", systemId, phone);
    }
 
    @Override
    public void bindPhone(String loginUid, String phone) throws LoginUserException {
        LoginUser user = getLoginUser(loginUid);
        if (user == null) {
            throw new LoginUserException("用户不存在");
        }
 
        LoginUser other = getLoginUserByPhone(user.getSystemId(), phone);
        if (other != null) {
            throw new LoginUserException("手机号已被其他账户绑定");
        }
        user.setPhone(phone);
        loginUserDao.update(user);
    }
 
    @Override
    public void bindQQ(String loginUid, String openId, String nickName, String portrait) throws LoginUserException {
 
        LoginUser user = getLoginUser(loginUid);
        if (user == null) {
            throw new LoginUserException("用户不存在");
        }
 
        LoginUser other = getLoginUserByQQOpenId(user.getSystemId(), openId);
        if (other != null && !other.getId().equalsIgnoreCase(loginUid)) {
            throw new LoginUserException("QQ已被其他账户绑定");
        }
        LoginUser update = new LoginUser();
        update.setId(loginUid);
        update.setQqOpenId(openId);
        loginUserDao.updateSelective(update);
 
        LoginUserExtra extra = loginUserExtraDao.get(loginUid);
        if (extra == null) {
            extra = new LoginUserExtra();
            extra.setId(loginUid);
        }
        extra.setQqNickName(nickName);
        extra.setQqOpenId(openId);
        extra.setQqPortrait(portrait);
        if (extra.getId() == null) {
            initExtra(extra);
        } else {
            loginUserExtraDao.save(extra);
        }
 
    }
 
    @Override
    public void bindWX(String loginUid, WeiXinUser weiXinUser) throws LoginUserException {
        LoginUser user = getLoginUser(loginUid);
        if (user == null) {
            throw new LoginUserException("用户不存在");
        }
 
        LoginUser other = getLoginUserByWxUnionId(user.getSystemId(), weiXinUser.getUnionid());
        if (other != null && !other.getId().equalsIgnoreCase(loginUid)) {
            throw new LoginUserException("微信已被其他账户绑定");
        }
        LoginUser update = new LoginUser();
        update.setId(loginUid);
        update.setWxOpenId(weiXinUser.getOpenid());
        update.setWxUnionId(weiXinUser.getUnionid());
        loginUserDao.updateSelective(update);
 
        LoginUserExtra extra = null;//loginUserExtraDao.get(loginUid);
        if (extra == null) {
            extra = new LoginUserExtra();
            extra.setId(loginUid);
        }
        extra.setWxNickName(weiXinUser.getNickname());
        extra.setWxOpenId(weiXinUser.getOpenid());
        extra.setWxUnionId(weiXinUser.getUnionid());
        extra.setWxPortrait(weiXinUser.getHeadimgurl());
        extra.setWxSex(weiXinUser.getSex());
        if (loginUserExtraDao.get(loginUid) == null) {
            initExtra(extra);
        } else {
            extra.setUpdateTime(new Date());
            loginUserExtraDao.updateSelective(extra);
        }
 
        System.out.println("12213");
 
    }
 
    @Override
    public LoginUserExtra getExtra(String uid) {
        return loginUserExtraDao.get(uid);
    }
 
    @Override
    public LoginUserExtra initExtra(String uid) {
        LoginUser user = getLoginUser(uid);
        if (user == null)
            return null;
        LoginUserExtra extra = new LoginUserExtra();
        extra.setId(uid);
        extra.setCreateTime(new Date());
        //之前的用户邮箱登录
        if (user.getLoginType() == LoginUser.LOGIN_TYPE_QQ) {
            user.setQqOpenId(user.getOpenid());
            extra.setQqOpenId(user.getOpenid());
            extra.setQqPortrait(user.getPortrait());
        } else if (user.getLoginType() == LoginUser.LOGIN_TYPE_EMAIL) {
            user.setEmail(user.getOpenid());
            extra.setEmail(user.getOpenid());
        } else if (user.getLoginType() == LoginUser.LOGIN_TYPE_WX) {
            user.setWxOpenId(user.getOpenid());
            extra.setWxOpenId(user.getOpenid());
            extra.setWxPortrait(user.getPortrait());
        }
 
        extra.setBirthday(user.getBirthday());
        extra.setSex(user.getSex());
        extra.setSign(user.getSign());
        extra.setIpinfo(user.getIpinfo());
        extra.setDevice(user.getDevice());
        extra.setPptvUid(PPTVUtil.getPPTVUid(uid));
        //TODO 初始化附加信息
        loginUserDao.update(user);
        loginUserExtraDao.save(extra);
        return extra;
    }
 
    @Override
    public LoginUserExtra initPPTVUid(String uid) {
        LoginUserExtra extra = loginUserExtraDao.get(uid);
        if (extra != null) {
            LoginUserExtra update = new LoginUserExtra(uid);
            update.setPptvUid(PPTVUtil.getPPTVUid(uid));
            loginUserExtraDao.updateSelective(update);
            extra.setPptvUid(update.getPptvUid());
        }
        return extra;
    }
 
    @Override
    public void updateSelectiveByPrimaryKey(LoginUser loginUser) {
        loginUserDao.updateSelective(loginUser);
    }
 
    @Override
    public void updateSelectiveByPrimaryKey(LoginUserExtra extra) {
        loginUserExtraDao.updateSelective(extra);
    }
 
    @Override
    public String updatePPTVOpenId(String uid) throws PPTVException {
        LoginUserExtra extra = getExtra(uid);
        if (extra == null) {
            extra = initExtra(uid);
        }
        String code = PPTVUtil.getPPTVCode(extra.getPptvUid());
 
        boolean login = PPTVApiUtil.login(code);
        if (!login) {
            throw new PPTVException(101, "登录失败");
        }
 
        String openId = PPTVApiUtil.getOpenId(extra.getPptvUid());
        if (StringUtil.isNullOrEmpty(openId)) {
            throw new PPTVException(102, "openId获取失败");
        }
        LoginUserExtra update = new LoginUserExtra();
        update.setId(uid);
        update.setPptvOpenId(openId);
        updateSelectiveByPrimaryKey(update);
        return openId;
    }
}