Search in sources :

Example 1 with AccessToken

use of com.jeecg.qywx.api.core.common.AccessToken in project jeecg-boot by jeecgboot.

the class ThirdAppWechatEnterpriseServiceImpl method getAppAccessToken.

/**
 * 获取APPToken,新版企业微信的秘钥是分开的
 */
public String getAppAccessToken() {
    String CORP_ID = thirdAppConfig.getWechatEnterprise().getClientId();
    String SECRET = thirdAppConfig.getWechatEnterprise().getAgentAppSecret();
    // 如果没有配置APP秘钥,就说明是老企业,可以通用秘钥
    if (oConvertUtils.isEmpty(SECRET)) {
        SECRET = thirdAppConfig.getWechatEnterprise().getClientSecret();
    }
    AccessToken accessToken = JwAccessTokenAPI.getAccessToken(CORP_ID, SECRET);
    if (accessToken != null) {
        return accessToken.getAccesstoken();
    }
    log.warn("获取AccessToken失败");
    return null;
}
Also used : AccessToken(com.jeecg.qywx.api.core.common.AccessToken)

Example 2 with AccessToken

use of com.jeecg.qywx.api.core.common.AccessToken in project jeecg-boot by jeecgboot.

the class ThirdAppWechatEnterpriseServiceImpl method getAccessToken.

@Override
public String getAccessToken() {
    String CORP_ID = thirdAppConfig.getWechatEnterprise().getClientId();
    String SECRET = thirdAppConfig.getWechatEnterprise().getClientSecret();
    AccessToken accessToken = JwAccessTokenAPI.getAccessToken(CORP_ID, SECRET);
    if (accessToken != null) {
        return accessToken.getAccesstoken();
    }
    log.warn("获取AccessToken失败");
    return null;
}
Also used : AccessToken(com.jeecg.qywx.api.core.common.AccessToken)

Example 3 with AccessToken

use of com.jeecg.qywx.api.core.common.AccessToken in project jeecg-boot by jeecgboot.

the class ThirdAppWechatEnterpriseServiceImpl method syncThirdAppUserToLocal.

@Override
public SyncInfoVo syncThirdAppUserToLocal() {
    SyncInfoVo syncInfo = new SyncInfoVo();
    String accessToken = this.getAccessToken();
    if (accessToken == null) {
        syncInfo.addFailInfo("accessToken获取失败!");
        return syncInfo;
    }
    // 获取企业微信所有的用户
    List<User> qwUsersList = JwUserAPI.getDetailUsersByDepartid("1", null, null, accessToken);
    if (qwUsersList == null) {
        syncInfo.addFailInfo("企业微信用户列表查询失败!");
        return syncInfo;
    }
    // 查询本地用户
    List<SysUser> sysUsersList = sysUserService.list();
    // 循环判断新用户和需要更新的用户
    for (User qwUser : qwUsersList) {
        /*
             * 判断是否同步过的逻辑:
             * 1. 查询 sys_third_account(第三方账号表)是否有数据,如果有代表已同步
             * 2. 本地表里没有,就先用手机号判断,不通过再用username判断。
             */
        SysThirdAccount sysThirdAccount = sysThirdAccountService.getOneByThirdUserId(qwUser.getUserid(), THIRD_TYPE);
        List<SysUser> collect = sysUsersList.stream().filter(user -> (qwUser.getMobile().equals(user.getPhone()) || qwUser.getUserid().equals(user.getUsername()))).collect(Collectors.toList());
        if (collect != null && collect.size() > 0) {
            SysUser sysUserTemp = collect.get(0);
            // 循环到此说明用户匹配成功,进行更新操作
            SysUser updateSysUser = this.qwUserToSysUser(qwUser, sysUserTemp);
            try {
                sysUserService.updateById(updateSysUser);
                String str = String.format("用户 %s(%s) 更新成功!", updateSysUser.getRealname(), updateSysUser.getUsername());
                syncInfo.addSuccessInfo(str);
            } catch (Exception e) {
                this.syncUserCollectErrInfo(e, qwUser, syncInfo);
            }
            this.thirdAccountSaveOrUpdate(sysThirdAccount, updateSysUser.getId(), qwUser.getUserid());
        // 更新完成,直接跳到下一次外部循环继续
        } else {
            // 没匹配到用户则走新增逻辑
            SysUser newSysUser = this.qwUserToSysUser(qwUser);
            try {
                sysUserService.save(newSysUser);
                String str = String.format("用户 %s(%s) 创建成功!", newSysUser.getRealname(), newSysUser.getUsername());
                syncInfo.addSuccessInfo(str);
            } catch (Exception e) {
                this.syncUserCollectErrInfo(e, qwUser, syncInfo);
            }
            this.thirdAccountSaveOrUpdate(sysThirdAccount, newSysUser.getId(), qwUser.getUserid());
        }
    }
    return syncInfo;
}
Also used : SyncInfoVo(org.jeecg.modules.system.vo.thirdapp.SyncInfoVo) ThirdLoginModel(org.jeecg.modules.system.model.ThirdLoginModel) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) org.jeecg.modules.system.entity(org.jeecg.modules.system.entity) Autowired(org.springframework.beans.factory.annotation.Autowired) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ArrayList(java.util.ArrayList) ThirdAppConfig(org.jeecg.config.thirdapp.ThirdAppConfig) JwDepartmentTreeVo(org.jeecg.modules.system.vo.thirdapp.JwDepartmentTreeVo) User(com.jeecg.qywx.api.user.vo.User) Service(org.springframework.stereotype.Service) org.jeecg.common.util.oConvertUtils(org.jeecg.common.util.oConvertUtils) TextEntity(com.jeecg.qywx.api.message.vo.TextEntity) org.jeecg.modules.system.service(org.jeecg.modules.system.service) AccessToken(com.jeecg.qywx.api.core.common.AccessToken) JwAccessTokenAPI(com.jeecg.qywx.api.base.JwAccessTokenAPI) JwMessageAPI(com.jeecg.qywx.api.message.JwMessageAPI) TextCard(com.jeecg.qywx.api.message.vo.TextCard) JwUserAPI(com.jeecg.qywx.api.user.JwUserAPI) JwDepartmentAPI(com.jeecg.qywx.api.department.JwDepartmentAPI) DepartMsgResponse(com.jeecg.qywx.api.department.vo.DepartMsgResponse) Department(com.jeecg.qywx.api.department.vo.Department) TextCardEntity(com.jeecg.qywx.api.message.vo.TextCardEntity) JwtUtil(org.jeecg.common.system.util.JwtUtil) PasswordUtil(org.jeecg.common.util.PasswordUtil) SysAnnouncementSendMapper(org.jeecg.modules.system.mapper.SysAnnouncementSendMapper) Collectors(java.util.stream.Collectors) RestUtil(org.jeecg.common.util.RestUtil) Slf4j(lombok.extern.slf4j.Slf4j) SpringContextUtils(org.jeecg.common.util.SpringContextUtils) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) List(java.util.List) Text(com.jeecg.qywx.api.message.vo.Text) JSONObject(com.alibaba.fastjson.JSONObject) SyncInfoVo(org.jeecg.modules.system.vo.thirdapp.SyncInfoVo) CommonConstant(org.jeecg.common.constant.CommonConstant) MessageDTO(org.jeecg.common.api.dto.message.MessageDTO) SysDepartTreeModel(org.jeecg.modules.system.model.SysDepartTreeModel) BeanUtils(org.springframework.beans.BeanUtils) User(com.jeecg.qywx.api.user.vo.User) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 4 with AccessToken

use of com.jeecg.qywx.api.core.common.AccessToken in project kms by mahonelau.

the class ThirdAppWechatEnterpriseServiceImpl method getAccessToken.

@Override
public String getAccessToken() {
    String CORP_ID = thirdAppConfig.getWechatEnterprise().getClientId();
    String SECRET = thirdAppConfig.getWechatEnterprise().getClientSecret();
    AccessToken accessToken = JwAccessTokenAPI.getAccessToken(CORP_ID, SECRET);
    if (accessToken != null) {
        return accessToken.getAccesstoken();
    }
    log.warn("获取AccessToken失败");
    return null;
}
Also used : AccessToken(com.jeecg.qywx.api.core.common.AccessToken)

Example 5 with AccessToken

use of com.jeecg.qywx.api.core.common.AccessToken in project kykms by mahonelau.

the class ThirdAppWechatEnterpriseServiceImpl method syncThirdAppUserToLocal.

@Override
public SyncInfoVo syncThirdAppUserToLocal() {
    SyncInfoVo syncInfo = new SyncInfoVo();
    String accessToken = this.getAccessToken();
    if (accessToken == null) {
        syncInfo.addFailInfo("accessToken获取失败!");
        return syncInfo;
    }
    // 获取企业微信所有的用户
    List<User> qwUsersList = JwUserAPI.getDetailUsersByDepartid("1", null, null, accessToken);
    if (qwUsersList == null) {
        syncInfo.addFailInfo("企业微信用户列表查询失败!");
        return syncInfo;
    }
    // 查询本地用户
    List<SysUser> sysUsersList = sysUserService.list();
    // 循环判断新用户和需要更新的用户
    for (User qwUser : qwUsersList) {
        /*
             * 判断是否同步过的逻辑:
             * 1. 查询 sys_third_account(第三方账号表)是否有数据,如果有代表已同步
             * 2. 本地表里没有,就先用手机号判断,不通过再用username判断。
             */
        SysThirdAccount sysThirdAccount = sysThirdAccountService.getOneByThirdUserId(qwUser.getUserid(), ThirdAppConfig.WECHAT_ENTERPRISE.toLowerCase());
        List<SysUser> collect = sysUsersList.stream().filter(user -> (qwUser.getMobile().equals(user.getPhone()) || qwUser.getUserid().equals(user.getUsername()))).collect(Collectors.toList());
        if (collect != null && collect.size() > 0) {
            SysUser sysUserTemp = collect.get(0);
            // 循环到此说明用户匹配成功,进行更新操作
            SysUser updateSysUser = this.qwUserToSysUser(qwUser, sysUserTemp);
            try {
                sysUserService.updateById(updateSysUser);
                String str = String.format("用户 %s(%s) 更新成功!", updateSysUser.getRealname(), updateSysUser.getUsername());
                syncInfo.addSuccessInfo(str);
            } catch (Exception e) {
                this.syncUserCollectErrInfo(e, qwUser, syncInfo);
            }
            this.thirdAccountSaveOrUpdate(sysThirdAccount, updateSysUser.getId(), qwUser.getUserid());
        // 更新完成,直接跳到下一次外部循环继续
        } else {
            // 没匹配到用户则走新增逻辑
            SysUser newSysUser = this.qwUserToSysUser(qwUser);
            try {
                sysUserService.save(newSysUser);
                String str = String.format("用户 %s(%s) 创建成功!", newSysUser.getRealname(), newSysUser.getUsername());
                syncInfo.addSuccessInfo(str);
            } catch (Exception e) {
                this.syncUserCollectErrInfo(e, qwUser, syncInfo);
            }
            this.thirdAccountSaveOrUpdate(sysThirdAccount, newSysUser.getId(), qwUser.getUserid());
        }
    }
    return syncInfo;
}
Also used : SyncInfoVo(org.jeecg.modules.system.vo.thirdapp.SyncInfoVo) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) org.jeecg.modules.system.entity(org.jeecg.modules.system.entity) Autowired(org.springframework.beans.factory.annotation.Autowired) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ArrayList(java.util.ArrayList) ThirdAppConfig(org.jeecg.config.thirdapp.ThirdAppConfig) JwDepartmentTreeVo(org.jeecg.modules.system.vo.thirdapp.JwDepartmentTreeVo) User(com.jeecg.qywx.api.user.vo.User) Service(org.springframework.stereotype.Service) org.jeecg.common.util.oConvertUtils(org.jeecg.common.util.oConvertUtils) TextEntity(com.jeecg.qywx.api.message.vo.TextEntity) org.jeecg.modules.system.service(org.jeecg.modules.system.service) AccessToken(com.jeecg.qywx.api.core.common.AccessToken) JwAccessTokenAPI(com.jeecg.qywx.api.base.JwAccessTokenAPI) JwMessageAPI(com.jeecg.qywx.api.message.JwMessageAPI) TextCard(com.jeecg.qywx.api.message.vo.TextCard) JwUserAPI(com.jeecg.qywx.api.user.JwUserAPI) JwDepartmentAPI(com.jeecg.qywx.api.department.JwDepartmentAPI) DepartMsgResponse(com.jeecg.qywx.api.department.vo.DepartMsgResponse) Department(com.jeecg.qywx.api.department.vo.Department) TextCardEntity(com.jeecg.qywx.api.message.vo.TextCardEntity) JwtUtil(org.jeecg.common.system.util.JwtUtil) PasswordUtil(org.jeecg.common.util.PasswordUtil) SysAnnouncementSendMapper(org.jeecg.modules.system.mapper.SysAnnouncementSendMapper) Collectors(java.util.stream.Collectors) RestUtil(org.jeecg.common.util.RestUtil) Slf4j(lombok.extern.slf4j.Slf4j) SpringContextUtils(org.jeecg.common.util.SpringContextUtils) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) List(java.util.List) Text(com.jeecg.qywx.api.message.vo.Text) JSONObject(com.alibaba.fastjson.JSONObject) SyncInfoVo(org.jeecg.modules.system.vo.thirdapp.SyncInfoVo) CommonConstant(org.jeecg.common.constant.CommonConstant) MessageDTO(org.jeecg.common.api.dto.message.MessageDTO) SysDepartTreeModel(org.jeecg.modules.system.model.SysDepartTreeModel) BeanUtils(org.springframework.beans.BeanUtils) User(com.jeecg.qywx.api.user.vo.User) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Aggregations

AccessToken (com.jeecg.qywx.api.core.common.AccessToken)9 JSONObject (com.alibaba.fastjson.JSONObject)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 JwAccessTokenAPI (com.jeecg.qywx.api.base.JwAccessTokenAPI)3 JwDepartmentAPI (com.jeecg.qywx.api.department.JwDepartmentAPI)3 DepartMsgResponse (com.jeecg.qywx.api.department.vo.DepartMsgResponse)3 Department (com.jeecg.qywx.api.department.vo.Department)3 JwMessageAPI (com.jeecg.qywx.api.message.JwMessageAPI)3 Text (com.jeecg.qywx.api.message.vo.Text)3 TextCard (com.jeecg.qywx.api.message.vo.TextCard)3 TextCardEntity (com.jeecg.qywx.api.message.vo.TextCardEntity)3 TextEntity (com.jeecg.qywx.api.message.vo.TextEntity)3 JwUserAPI (com.jeecg.qywx.api.user.JwUserAPI)3 User (com.jeecg.qywx.api.user.vo.User)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 StringUtils (org.apache.commons.lang.StringUtils)3