admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
src/main/resources/code/service/app/src/main/java/com/ks/app/service/impl/user/UserAccountServiceImpl.java
@@ -1,11 +1,13 @@
package com.ks.app.service.impl.user;
import com.ks.app.dto.user.LoginInfoDTO;
import com.ks.app.entity.SystemEnum;
import com.ks.app.entity.config.SystemConfigKey;
import com.ks.app.entity.user.UserInfo;
import com.ks.app.entity.user.UserLoginRecord;
import com.ks.app.entity.user.WXUserInfo;
import com.ks.app.exception.user.LoginException;
import com.ks.app.exception.user.UserAccountException;
import com.ks.app.service.inter.config.SystemConfigService;
import com.ks.app.service.inter.user.*;
import com.ks.app.service.manager.VerifyCodeManager;
@@ -62,7 +64,7 @@
                } else {
                    //验证码登录
                    if (!verifyCodeManager.isPhoneCodeRight(loginInfo.getSystem(), loginInfo.getPhone(), loginInfo.getVcode())) {
                        throw new LoginException("验证码出错");
                        throw new LoginException("验证码错误");
                    }
                }
                //执行登录操作
@@ -99,8 +101,11 @@
                //保存微信用户信息
                WXUserInfo wxUserInfo = null;
                try {
                    wxUserInfo = wxUserInfoService.save(WXUserInfo.create(weiXinUser));
                    WXUserInfo temp = WXUserInfo.create(weiXinUser);
                    temp.setSystem(loginInfo.getSystem());
                    wxUserInfo = wxUserInfoService.save(temp);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new LoginException("保存微信授权信息失败");
                }
                wxUserInfo.setSystem(loginInfo.getSystem());
@@ -111,7 +116,11 @@
                    newUser.setWxUser(wxUserInfo);
                    newUser.setSystem(loginInfo.getSystem());
                    newUser.setNickName(wxUserInfo.getNickName());
                    newUser.setPortrait(wxUserInfo.getHeadImgUrl());
                    if (StringUtil.isNullOrEmpty(wxUserInfo.getHeadImgUrl())) {
                        newUser.setPortrait(systemConfigService.getValueCache(loginInfo.getSystem(), SystemConfigKey.defaultPortrait));
                    } else {
                        newUser.setPortrait(wxUserInfo.getHeadImgUrl());
                    }
                    try {
                        userInfoService.add(newUser);
                    } catch (Exception e) {
@@ -146,7 +155,7 @@
        }
        loginSuccess(user.getId(), loginInfo);
        return null;
        return user;
    }
    @Override
@@ -157,6 +166,77 @@
        userInfoService.update(update);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void bindWX(Long uid, String code) throws UserAccountException {
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserAccountException(UserAccountException.CODE_USER_ERROR, "用户不存在");
        }
        SystemEnum system = user.getSystem();
        String appId = systemConfigService.getValueCache(system, SystemConfigKey.wxAppId);
        String appSecret = systemConfigService.getValueCache(system, SystemConfigKey.wxAppSecret);
        WeiXinUser weiXinUser = WXAppLoginUtil.getWeiXinUser(code, appId, appSecret);
        if (weiXinUser == null) {
            throw new UserAccountException(UserAccountException.CODE_ERROR_WX_INFO, "微信授权信息获取失败");
        }
        //保存微信用户信息
        WXUserInfo wxUserInfo = null;
        try {
            WXUserInfo temp = WXUserInfo.create(weiXinUser);
            temp.setSystem(system);
            wxUserInfo = wxUserInfoService.save(temp);
        } catch (Exception e) {
            throw new UserAccountException(UserAccountException.CODE_ERROR_WX_INFO, "保存微信授权信息失败");
        }
        UserInfo u = userInfoService.selectByWXIdAndSystemAndStatus(wxUserInfo.getId(), system, UserInfo.STATUS_NORMAL);
        if (u != null) {
            throw new UserAccountException(UserAccountException.CODE_ALREAD_BIND, "微信已绑定其他账号");
        }
        //绑定到用户
        UserInfo update = new UserInfo();
        update.setId(uid);
        update.setWxUser(wxUserInfo);
        userInfoService.update(update);
    }
    @Override
    public void bindPhone(Long uid, String phone, String vcode, String token) throws UserAccountException {
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserAccountException(UserAccountException.CODE_USER_ERROR, "用户不存在");
        }
        SystemEnum system = user.getSystem();
        if (!StringUtil.isNullOrEmpty(token)) {
            //一键登录
            //获取电话号码
            String mobile = AliyunOneKeyLoginUtil.getMobile(token, UUID.randomUUID().toString(), systemConfigService.getValueCache(system, SystemConfigKey.aliyunOneKeyAuthAcessKey), systemConfigService.getValueCache(system, SystemConfigKey.aliyunOneKeyAuthAcessSecret));
            if (StringUtil.isNullOrEmpty(mobile)) {
                throw new UserAccountException(UserAccountException.CODE_ERROR_PHONE, "电话号码获取失败");
            }
            phone = mobile;
        } else {
            //验证码登录
            if (!verifyCodeManager.isPhoneCodeRight(system, phone, vcode)) {
                throw new UserAccountException(UserAccountException.CODE_ERROR_PHONE, "验证码错误");
            }
        }
        UserInfo u = userInfoService.selectByPhoneAndSystemAndStatus(phone, system, UserInfo.STATUS_NORMAL);
        if (u != null) {
            throw new UserAccountException(UserAccountException.CODE_ALREAD_BIND, "手机号已绑定其他账号");
        }
        //绑定到用户
        UserInfo update = new UserInfo();
        update.setId(uid);
        update.setPhone(phone);
        userInfoService.update(update);
    }
    /**
     * @return void