Commit 0f30d2f1 authored by 李健华's avatar 李健华

OneID一体化-企业微信获取unionid

parent ac5d090d
package cn.quantgroup.xyqb.model.webchat;
import lombok.Data;
import java.io.Serializable;
@Data
public class ContactInfoResponse implements Serializable {
private static final long serialVersionUID = -1L;
private String externalUserid;
private String name;
private Integer type;
private String unionid;
}
package cn.quantgroup.xyqb.model.webchat;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
/**
*
* 获取部门下成员的客户信息
*/
@Data
public class ContactListResponse implements Serializable {
private static final long serialVersionUID = -1L;
private Object followInfo;
private ContactInfoResponse externalContact;
}
package cn.quantgroup.xyqb.model.webchat;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
*
* 获取部门下成员的客户列表
*/
@Data
public class CustomerListResponse implements Serializable {
private static final long serialVersionUID = -1L;
private Integer errcode;
private String errmsg;
private List<ContactListResponse> external_contact_list;
private String nextCursor;
}
package cn.quantgroup.xyqb.model.webchat;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
*
* 获取部门下成员信息
*/
@Data
public class DepartmentInfoResponse implements Serializable {
private static final long serialVersionUID = -1L;
private String userid;
private String name;
}
package cn.quantgroup.xyqb.model.webchat;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
*
* 获取部门下成员列表
*/
@Data
public class DepartmentListResponse implements Serializable {
private static final long serialVersionUID = -1L;
private Integer errcode;
private String errmsg;
private List<DepartmentInfoResponse> userlist;
}
......@@ -7,7 +7,7 @@ import cn.quantgroup.xyqb.model.webchat.AccessTokenResponse;
public interface IWechatFollowService {
AccessTokenResponse getToken();
JsonResult executeWechatFollowStatus(String nextOpenId);
JsonResult executeWechatFollowStatus(String nextOpenId, Integer period);
void executeTask();
}
......@@ -5,17 +5,18 @@ import cn.quantgroup.xyqb.entity.WechatUserInfo;
import cn.quantgroup.xyqb.event.WechatBindEvent;
import cn.quantgroup.xyqb.exception.WechatRelateUserException;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.model.webchat.AccessTokenResponse;
import cn.quantgroup.xyqb.model.webchat.WechatEventMsg;
import cn.quantgroup.xyqb.model.webchat.WechatUserListResponse;
import cn.quantgroup.xyqb.model.webchat.*;
import cn.quantgroup.xyqb.repository.IWeChatInfoRelationRepository;
import cn.quantgroup.xyqb.repository.IWeChatUserRepository;
import cn.quantgroup.xyqb.service.http.IHttpService;
import cn.quantgroup.xyqb.service.wechat.IWechatFollowService;
import cn.quantgroup.xyqb.service.wechat.IWechatService;
import cn.quantgroup.xyqb.util.GcodeUtils;
import cn.quantgroup.xyqb.util.ValidationUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -23,16 +24,18 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* Created by Miraculous on 2017/1/19.
......@@ -54,6 +57,10 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
private String departmentUserListUrl;
private String customerInfoListBatchUrl;
private Integer departmentId = 2;
@Resource
private IHttpService httpService;
......@@ -70,6 +77,17 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
@Autowired
private JdbcTemplate jdbcTemplate;
// 公众号
private final String WECHAT_ACCOUNT = "wechatAccount";
//企业微信
private final String ENTERPRISE_WECHAT = "enterpriseWechat";
// 公众号类型
private final String WECHAT_XYQB = "xyqb";
private List<String> unionIdList;
@PostConstruct
private void init() {
// 公众号
......@@ -79,7 +97,9 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
qyAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwd6e3a8c6bc4f3763&corpsecret=5SvwrDrf0tapQZnKLcmDJ9nprQ7zEard_mzXCqNm06Y";
// 成员列表
departmentUserListUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=%s&department_id=%s&fetch_child=1";
//
//批量获取客户详情
customerInfoListBatchUrl = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token=%s";
unionIdList = new ArrayList<>();
}
......@@ -104,26 +124,29 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
}
@Override
public JsonResult executeWechatFollowStatus(String nextOpenId) {
public JsonResult executeWechatFollowStatus(String nextOpenId, Integer period) {
AccessTokenResponse token = getToken();
String userListUrlFormat = String.format(userListUrl, token.getAccessToken(), nextOpenId);
try {
String response = httpService.get(userListUrlFormat);
if (response != null) {
WechatUserListResponse wechatUserListResponse = JSONObject.parseObject(response, WechatUserListResponse.class);
if (wechatUserListResponse.getErrcode() == null) {
if (wechatUserListResponse.getErrcode() == null && wechatUserListResponse.getCount() > 0) {
nextOpenId = wechatUserListResponse.getNext_openid();
List<String> opendIdList = wechatUserListResponse.getData().get("openid");
if (!opendIdList.isEmpty()) {
// 更新指定OpenId 关注状态
weChatInfoRelationRepository.updateIsFollowWechatInOpenIdList(opendIdList);
String[] openIds = opendIdList.toArray(new String[0]);
String openIdStrs = String.join("\',\'", openIds);
openIdStrs = "\'" + openIdStrs + "\'";
// 添加公众号数据
String sql = String.format("INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `period`) SELECT `user_id`, `open_id`, `union_id`, '%s', %s from wechat_userinfo where app_name='%s' and `open_id` IN (%s)", WECHAT_ACCOUNT, period, WECHAT_XYQB, openIdStrs);
jdbcTemplate.update(sql);
}
if (nextOpenId != "") {
executeWechatFollowStatus(nextOpenId);
executeWechatFollowStatus(nextOpenId, period);
}
return JsonResult.buildSuccessResult("", opendIdList);
} else {
log.error("excuateWechatFollowStatus:errResponse--{}", wechatUserListResponse);
return null;
}
}
......@@ -139,14 +162,75 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
@Override
public void executeTask() {
// 初始化任务数据
initTask();
// initTask();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMdd");
Integer period = Integer.parseInt(simpleDateFormat.format(new Date()));
// 微信公众号关注数据更新
// executeWechatFollowStatus("");
executeWechatFollowStatus("", period);
// 企业微信关注数据更新
executeEnterpriseFollowStatus("");
executeEnterpriseFollowStatus("", period);
}
private JsonResult executeEnterpriseFollowStatus(String s, Integer period) {
try {
String response = httpService.get(qyAccessTokenUrl);
if (StringUtils.isEmpty(response)) {
return null;
}
AccessTokenResponse accessTokenResponse = JSONObject.parseObject(response,
AccessTokenResponse.class);
if (accessTokenResponse.getAccessToken() != null) {
String deparmentList = String.format(departmentUserListUrl, accessTokenResponse.getAccessToken(), departmentId);
String departmentRep = httpService.get(deparmentList);
DepartmentListResponse departmentListResponse = JSONObject.parseObject(departmentRep, DepartmentListResponse.class);
if (!departmentListResponse.getUserlist().isEmpty()) {
List<String> userIdList = departmentListResponse.getUserlist().stream().map(DepartmentInfoResponse::getUserid).collect(Collectors.toList());
System.out.println(userIdList);
if (!userIdList.isEmpty()) {
// 批量查询客户详情
batchQueryCustomerDetailInfo(accessTokenResponse.getAccessToken(), userIdList, "", period);
}
}
System.out.println(departmentListResponse);
}
} catch (Exception e) {
log.error("同步企业微信数据失败--{}", e.getMessage());
return null;
}
return null;
}
private void executeEnterpriseFollowStatus(String s) {
private void batchQueryCustomerDetailInfo(String accessToken, List<String> userIdList, String cursor, Integer period) {
// 查询客户详情
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("userid_list", userIdList);
parameters.put("cursor", cursor);
parameters.put("limit", "100");
String customerInfoUrl = String.format(customerInfoListBatchUrl, accessToken);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.postForEntity(customerInfoUrl, parameters, String.class); //提交的body内容为user对象,请求的返回的body类型为String
CustomerListResponse customerListResponse = JSONObject.parseObject(responseEntity.getBody(), CustomerListResponse.class);
if (customerListResponse.getErrcode() == 0) {
// 获取所有unionId
List<String> unionIdAll = customerListResponse.getExternal_contact_list().stream().map(a -> a.getExternalContact().getUnionid()).collect(Collectors.toList());
if (!unionIdAll.isEmpty()) {
String[] unionIds = unionIdAll.toArray(new String[0]);
String unionIdStrs = String.join("\',\'", unionIds);
unionIdStrs = "\'" + unionIdStrs + "\'";
// String sql = String.format("INSERT INTO wechat_enterprise(`unionid`) values ('%s')", String.join(",", unionIdAll));
String sql = String.format("INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `period`) SELECT `user_id`, `open_id`, `union_id`, '%s', %s from wechat_userinfo where `union_id` IN (%s)", ENTERPRISE_WECHAT, period, unionIdStrs);
jdbcTemplate.update(sql);
}
// 根据unionid 添加现有指定信息
// 判断nextCursor不为空,继续获取更多客户
if (!"".equals(customerListResponse.getNextCursor())) {
batchQueryCustomerDetailInfo(accessToken, userIdList, customerListResponse.getNextCursor(), period);
}
}
}
private void initTask() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment