Search in sources :

Example 1 with ContactUser

use of com.jeecg.dingtalk.api.oauth2.vo.ContactUser in project jeecg-boot by jeecgboot.

the class ThirdAppDingtalkServiceImpl method oauth2Login.

/**
 * OAuth2登录,成功返回登录的SysUser,失败返回null
 */
public SysUser oauth2Login(String authCode) {
    ThirdAppTypeItemVo dtConfig = thirdAppConfig.getDingtalk();
    // 1. 根据免登授权码获取用户 AccessToken
    String userAccessToken = JdtOauth2API.getUserAccessToken(dtConfig.getClientId(), dtConfig.getClientSecret(), authCode);
    if (userAccessToken == null) {
        log.error("oauth2Login userAccessToken is null");
        return null;
    }
    // 2. 根据用户 AccessToken 获取当前用户的基本信息(不包括userId)
    ContactUser contactUser = JdtOauth2API.getContactUsers("me", userAccessToken);
    if (contactUser == null) {
        log.error("oauth2Login contactUser is null");
        return null;
    }
    String unionId = contactUser.getUnionId();
    // 3. 根据获取到的 unionId 换取用户 userId
    String accessToken = this.getAccessToken();
    if (accessToken == null) {
        log.error("oauth2Login accessToken is null");
        return null;
    }
    Response<String> getUserIdRes = JdtUserAPI.getUseridByUnionid(unionId, accessToken);
    if (!getUserIdRes.isSuccess()) {
        log.error("oauth2Login getUseridByUnionid failed: " + JSON.toJSONString(getUserIdRes));
        return null;
    }
    String appUserId = getUserIdRes.getResult();
    log.info("appUserId: " + appUserId);
    if (appUserId != null) {
        // 判断第三方用户表有没有这个人
        LambdaQueryWrapper<SysThirdAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysThirdAccount::getThirdUserUuid, appUserId);
        queryWrapper.or().eq(SysThirdAccount::getThirdUserId, appUserId);
        queryWrapper.eq(SysThirdAccount::getThirdType, THIRD_TYPE);
        SysThirdAccount thirdAccount = sysThirdAccountService.getOne(queryWrapper);
        if (thirdAccount != null) {
            return this.getSysUserByThird(thirdAccount, null, appUserId, accessToken);
        } else {
            // 直接创建新账号
            User appUser = JdtUserAPI.getUserById(appUserId, accessToken).getResult();
            ThirdLoginModel tlm = new ThirdLoginModel(THIRD_TYPE, appUser.getUserid(), appUser.getName(), appUser.getAvatar());
            thirdAccount = sysThirdAccountService.saveThirdUser(tlm);
            return this.getSysUserByThird(thirdAccount, appUser, null, null);
        }
    }
    return null;
}
Also used : ContactUser(com.jeecg.dingtalk.api.oauth2.vo.ContactUser) ContactUser(com.jeecg.dingtalk.api.oauth2.vo.ContactUser) User(com.jeecg.dingtalk.api.user.vo.User) ThirdAppTypeItemVo(org.jeecg.config.thirdapp.ThirdAppTypeItemVo) ThirdLoginModel(org.jeecg.modules.system.model.ThirdLoginModel) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)

Aggregations

LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 ContactUser (com.jeecg.dingtalk.api.oauth2.vo.ContactUser)1 User (com.jeecg.dingtalk.api.user.vo.User)1 ThirdAppTypeItemVo (org.jeecg.config.thirdapp.ThirdAppTypeItemVo)1 ThirdLoginModel (org.jeecg.modules.system.model.ThirdLoginModel)1