LoginService.java 7.15 KB
package cn.com.glsx.app.modules.service;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.com.glsx.app.common.enums.AuthType;
import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.common.exception.AppServerException;
import cn.com.glsx.app.modules.converter.UserConverter;
import cn.com.glsx.app.modules.entity.*;
import cn.com.glsx.app.modules.mapper.*;
import cn.com.glsx.app.modules.model.*;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser;
import com.glsx.plat.core.web.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class LoginService {

    @Resource
    private UserAuthRelMapper authRelMapper;

    @Resource
    private UserLocalAuthMapper localAuthMapper;

    @Resource
    private UserThirdAuthMapper thirdAuthMapper;

    @Resource
    private UserWechatAccountMapper wechatAccountMapper;

    @Resource
    private UserQQAccountMapper qqAccountMapper;

    @Autowired
    private AuthFeignClient authFeignClient;

    @Transactional(rollbackFor = Exception.class)
    public AppUser localAuth(LoginByAccountBO loginBO) {
        //内部登录转成appUser返回前端
        if (loginBO.getLoginType() == 1) {
            //密码登录
            R<SimpleUser> userR = authFeignClient.login(loginBO.getAccount(), loginBO.getCode());
            SimpleUser simpleUser = userR.getData(SimpleUser.class);
            if (!userR.isSuccess() || simpleUser == null) {
                throw new AppServerException(userR.getMessage());
            }
            UserAuthRel userAuthRel = authRelMapper.selectLocalAuthRelByUserId(simpleUser.getId());

            UserLocalAuth localAuth = localAuthMapper.selectById(userAuthRel.getAuthId());
            if (localAuth == null) {
                throw new AppServerException("请先开通登录权限");
            }
            return UserConverter.INSTANCE.su2au(simpleUser);
        } else if (loginBO.getLoginType() == 2) {
            //验证码校验
            //smsService.verifyCode(loginBO.getAccount(), loginBO.getCode());
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    public UserAuthRel qqAuth(QQOAuth2AccessToken accessToken) {
        UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(accessToken.getOpenId());
        if (thirdAuth == null) {
            thirdAuth = new UserThirdAuth();
            thirdAuth.setOpenid(accessToken.getOpenId());
            thirdAuth.setAccessToken(accessToken.getAccessToken());
            thirdAuth.setLoginType(LoginType.QQ.getType());
            thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
        }

        UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId());
        if (qqAccount == null) {
            qqAccount = new UserQQAccount();
            qqAccount.setAuthId(thirdAuth.getId());
            qqAccount.setUnionId(accessToken.getUnionId());
            qqAccountMapper.insertUseGeneratedKeys(qqAccount);
        }

        UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
        if (authRel == null) {
            authRel = new UserAuthRel();
            authRel.setAuthId(thirdAuth.getId());
            authRel.setAuthType(AuthType.THIRD.getType());
            authRelMapper.insertUseGeneratedKeys(authRel);
        }
        return authRel;
    }

    @Transactional(rollbackFor = Exception.class)
    public UserAuthRel qqLogin(QQLoginBO loginBO) {
        UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(loginBO.getOpenId());
        if (thirdAuth == null) {
            thirdAuth = new UserThirdAuth();
            thirdAuth.setOpenid(loginBO.getOpenId());
            thirdAuth.setLoginType(LoginType.QQ.getType());
            thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
        }

        UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId());
        if (qqAccount == null) {
            qqAccount = new UserQQAccount();
            qqAccount.setAuthId(thirdAuth.getId());
            qqAccountMapper.insertUseGeneratedKeys(qqAccount);
        }

        UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
        if (authRel == null) {
            authRel = new UserAuthRel();
            authRel.setAuthId(thirdAuth.getId());
            authRel.setAuthType(AuthType.THIRD.getType());
            authRelMapper.insertUseGeneratedKeys(authRel);
        }
        return authRel;
    }

    @Transactional(rollbackFor = Exception.class)
    public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo) {
        UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(sessionInfo.getOpenid());
        if (thirdAuth == null) {
            thirdAuth = new UserThirdAuth();
            thirdAuth.setOpenid(sessionInfo.getOpenid());
            thirdAuth.setAccessToken(sessionInfo.getSessionKey());
            thirdAuth.setLoginType(LoginType.WECHAT.getType());
            thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
        }

        UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId());
        if (wechatAccount == null) {
            wechatAccount = new UserWechatAccount();
            wechatAccount.setAuthId(thirdAuth.getId());
            wechatAccount.setUnionId(sessionInfo.getUnionid());
            wechatAccountMapper.insertUseGeneratedKeys(wechatAccount);
        }

        UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
        if (authRel == null) {
            authRel = new UserAuthRel();
            authRel.setAuthId(thirdAuth.getId());
            authRel.setAuthType(AuthType.THIRD.getType());
            authRelMapper.insertUseGeneratedKeys(authRel);
        }
        return authRel;
    }

    @Transactional(rollbackFor = Exception.class)
    public UserAuthRel wechatLogin(WechatLoginBO loginBO) {
        UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(loginBO.getOpenId());
        if (thirdAuth == null) {
            thirdAuth = new UserThirdAuth();
            thirdAuth.setOpenid(loginBO.getOpenId());
            thirdAuth.setLoginType(LoginType.WECHAT.getType());
            thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
        }

        UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId());
        if (wechatAccount == null) {
            wechatAccount = new UserWechatAccount();
            wechatAccount.setAuthId(thirdAuth.getId());
            wechatAccount.setUnionId(loginBO.getUnionId());
            wechatAccountMapper.insertUseGeneratedKeys(wechatAccount);
        }

        UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
        if (authRel == null) {
            authRel = new UserAuthRel();
            authRel.setAuthId(thirdAuth.getId());
            authRel.setAuthType(AuthType.THIRD.getType());
            authRelMapper.insertUseGeneratedKeys(authRel);
        }
        return authRel;
    }

}