| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package com.chkj.pdata.task;
- import com.chkj.pdata.entity.AccessToken;
- import com.chkj.pdata.entity.MemReturnFromWeixin;
- import com.chkj.pdata.util.DBUtils;
- import com.chkj.pdata.util.FileUtil;
- import com.chkj.pdata.util.HttpRequestUtils;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.http.ResponseEntity;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- public class BakMember {
- final String BaseURL = "https://open.wecard.qq.com/cgi-bin/";
- FileUtil myWriter = new FileUtil();
- /**
- * 成员备份任务
- */
- public void member_bak_task() {
- Map<String, Object> map;
- List<String> card_numbers = new LinkedList<>();
- // 查询没有推送的记录
- String selectSql = "SELECT `学工号` as card_number_cy FROM `成员列表` WHERE `手机(完整手机号仅限超级管理员导出)` LIKE '%**%'" +
- " OR `证件号码(完整证件号码仅限超级管理员导出)` LIKE '%******%' LIMIT 1000";
- // String selectSql = "SELECT `学工号` as card_number_cy FROM `成员列表` WHERE `更新手机和身份证状态`=0 LIMIT 1000";
- // 返回查询的成员列表
- map = DBUtils.getAll(selectSql);
- ResultSet rs = (ResultSet) map.get("rs");
- PreparedStatement stmt = (PreparedStatement) map.get("stmt");
- Connection conn = (Connection) map.get("conn");
- try {
- while (rs.next()) {
- card_numbers.add(rs.getString("card_number_cy"));
- }
- if (card_numbers.size() > 0) {
- // 获取 access_token
- String access_token = getAccessToken();
- if (!"".equals(access_token)) {
- // 推送成员信息
- pullChengyuan(card_numbers, access_token);
- } else {
- myWriter.writeToFile("查询【成员列表】获取 access_token 失败!", "abnormal");
- }
- } else {
- System.out.println("1");
- // myWriter.writeToFile("没有要更新的 手机 和 身份证 的记录!", "bak");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (JsonProcessingException e) {
- throw new RuntimeException(e);
- } finally {
- DBUtils.closeResultSet(rs);
- DBUtils.closeStatement(stmt);
- DBUtils.closeConnection(conn);
- }
- }
- /**
- * 获取 access_token
- *
- * @return 返回 access_token
- * @throws JsonProcessingException 异常
- */
- public String getAccessToken() throws JsonProcessingException {
- // 获取 access_token 接口
- final String URL = BaseURL + "oauth2/token";
- // 查询参数
- Map<String, String> queryParams = new HashMap<>();
- // 请求参数
- Map<String, String> requestBody = new HashMap<>();
- requestBody.put("app_key", "505888BB58CE4DF0");
- requestBody.put("app_secret", "6102F58C83240854331AF196861E6E5F");
- requestBody.put("grant_type", "client_credentials");
- requestBody.put("scope", "base");
- requestBody.put("ocode", "1015730314");
- // 携带参数访问接口
- ResponseEntity<String> stringResponseEntity = HttpRequestUtils.httpPost(URL, requestBody, queryParams);
- // 判断是否返回null
- if (stringResponseEntity == null) {
- return "";
- }
- // String token_type = "";
- String access_token;
- // String ocode = null;
- // int expires_in = 0;
- String body = stringResponseEntity.getBody();
- String code = stringResponseEntity.getStatusCode().toString();
- ObjectMapper objectMapper = new ObjectMapper(); // 创建ObjectMapper对象
- AccessToken accessToken = objectMapper.readValue(body, AccessToken.class); // 将JSON字符串转换成User对象
- if (code.contains("200") && code.contains("OK")) {
- // token_type = accessToken.getToken_type();
- access_token = accessToken.getAccess_token();
- // ocode = accessToken.getOcode();
- // expires_in = accessToken.getExpires_in();
- } else {
- access_token = "";
- myWriter.writeToFile("错误状态码," + code.split(" ")[0], "");
- myWriter.writeToFile("错误信息," + body, "");
- }
- // 返回access_token
- return access_token;
- }
- /**
- * 拉取成员
- *
- * @param card_numbers 成员列表
- * @param access_token access_token
- */
- private void pullChengyuan(List<String> card_numbers, String access_token) {
- // 查询参数
- Map<String, String> queryParams = new HashMap<>();
- // 请求参数
- Map<String, List<String>> requestBody = new HashMap<>();
- requestBody.put("card_numbers", card_numbers);
- // 创建用户接口
- final String URL = BaseURL + "user/get-user-by-card-numbers?access_token=" + access_token;
- // 调用批量获取成员接口
- ResponseEntity<String> stringResponseEntity = HttpRequestUtils.httpPost(URL, requestBody, queryParams);
- // 判断是否返回null
- if (stringResponseEntity == null) {
- return;
- }
- // 获取响应体
- String body = stringResponseEntity.getBody();
- // 获取响应码
- String code = stringResponseEntity.getStatusCode().toString();
- // 判断响应码是否是成功
- if (code.contains("200") && code.contains("OK")) {
- try {
- ObjectMapper objectMapper = new ObjectMapper(); // 创建ObjectMapper对象
- MemReturnFromWeixin memReturnFromWeixin = objectMapper.readValue(body, MemReturnFromWeixin.class);
- // 如果用户信息列表数组长度不等于0,说明有该成员信息
- if (memReturnFromWeixin.getUserlist().length != 0) {
- for (int i = 0; i < memReturnFromWeixin.getUserlist().length; i++) {
- String objectString = memReturnFromWeixin.getUserlist()[i].toString();
- String[] arr = objectString.substring(1, objectString.length() - 1).split(", ");
- String card_number = "", name = "", id_card = "", telephone = "";
- for (String s : arr) {
- String[] split = s.split("=");
- if (split[0].equals("card_number")) {
- if (split.length > 1)
- card_number = split[1];
- }
- if (split[0].equals("name")) {
- if (split.length > 1)
- name = split[1];
- }
- if (split[0].equals("id_card")) {
- if (split.length > 1)
- id_card = split[1];
- }
- if (split[0].equals("telephone")) {
- if (split.length > 1)
- telephone = split[1];
- }
- }
- if (card_number.isEmpty() || name.isEmpty()) {
- System.out.println("学工号 或 姓名 为空!");
- } else {
- String condition = "";
- if (!"".equals(id_card))
- condition += "`证件号码(完整证件号码仅限超级管理员导出)`='" + id_card + "',";
- if (!"".equals(telephone))
- condition += "`手机(完整手机号仅限超级管理员导出)`='" + telephone + "',";
- condition += "`更新手机和身份证状态`=" + 1 + ",";
- String update_sql = "UPDATE `成员列表` SET " + condition.substring(0, condition.length() - 1)
- + " WHERE `学工号` like '%" + card_number + "%' AND `姓名` like '%" + name + "%'";
- int num = DBUtils.update(update_sql);
- if (num > 0) {
- myWriter.writeToFile(card_number + " " + name + "身份证和手机 更新成功!", "bak");
- } else {
- myWriter.writeToFile(card_number + " " + name + "身份证和手机 更新失败!", "bak");
- }
- }
- }
- }
- } catch (JsonProcessingException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- }
|