|
|
@@ -1,16 +1,23 @@
|
|
|
package com.cyksj.web.controller.user;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.cyksj.common.util.J11HttpC;
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.config.WeChatConfig;
|
|
|
import com.cyksj.dto.Result;
|
|
|
import com.cyksj.enums.GatewayResponse;
|
|
|
import com.cyksj.mapper.OrderDonMapper;
|
|
|
import com.cyksj.mapper.UserTransferLogMapper;
|
|
|
+import com.cyksj.mapper.WxSyncLogMapper;
|
|
|
import com.cyksj.mapper.manage.coupon.CouponUserMapper;
|
|
|
import com.cyksj.mapper.market.task.UserBindPhoneMapper;
|
|
|
-import com.cyksj.model.dto.GzhUnionidUserinfo;
|
|
|
-import com.cyksj.model.dto.UserWhoami;
|
|
|
+import com.cyksj.model.dto.*;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.model.request.ExchangeCodeTicket;
|
|
|
import com.cyksj.model.views.RenewalView;
|
|
|
@@ -20,11 +27,18 @@ import com.cyksj.service.relation.GroupRelationFrontService;
|
|
|
import com.cyksj.service.user.UserService;
|
|
|
import com.cyksj.service.wechat.WeChatService;
|
|
|
import com.cyksj.web.util.StpUserUtil;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.net.http.HttpRequest;
|
|
|
+import java.net.http.HttpResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author chan
|
|
|
@@ -106,47 +120,203 @@ public class UserController {
|
|
|
|
|
|
private final UserTransferLogMapper userTransferLogMapper;
|
|
|
|
|
|
- /**
|
|
|
- * 获取微信用户信息
|
|
|
- */
|
|
|
- @GetMapping("/get/wx/user")
|
|
|
- public void syncUser() {
|
|
|
- List<User> users = userService.list(Wrappers.lambdaQuery(User.class)
|
|
|
- .ne(User::getUnionid, ""));
|
|
|
- String appId = "wxd58016eee53b6558";
|
|
|
- users.forEach(user->{
|
|
|
- try {
|
|
|
- String accessToken = "64_rPJYk5-812w8P9pMMn-X4JYkuxg-NFHp6TNWwdFl1KgTkEhwPXGPrSl624mmD5WebnG0Ll37-iQ8bOnUQtOE82DLj1pkRC1zvEEnXhEQh_wbEwak-ijroBeSqpcZMOaAJAMZB";
|
|
|
- GzhUnionidUserinfo userInfo2 = weChatService.getUserInfo2(accessToken, user.getOpenId());
|
|
|
- if ("ADD_SCENE_ACCOUNT_MIGRATION".equals(userInfo2.getSubscribeScene())) {
|
|
|
- log.info(user.getOpenId() + "公众号迁移");
|
|
|
- }
|
|
|
- if (userInfo2 == null) {
|
|
|
- log.info(user.getOpenId() + "失联");
|
|
|
- }
|
|
|
- if (StrUtil.isEmpty(userInfo2.getUnionid())) {
|
|
|
- log.info(user.getOpenId() + "未关注新公众号");
|
|
|
- return;
|
|
|
- }
|
|
|
- if (user.getUnionid().equals(userInfo2.getUnionid())) {
|
|
|
- log.info("新旧公众号unionid相同");
|
|
|
- return;
|
|
|
- } else {
|
|
|
- user.setUnionid(userInfo2.getUnionid());
|
|
|
- userService.updateById(user);
|
|
|
-
|
|
|
- UserTransferLog log = new UserTransferLog();
|
|
|
- log.setAppId(appId);
|
|
|
- log.setUserId(user.getId());
|
|
|
- log.setOldUnionid(user.getUnionid());
|
|
|
- log.setType("手动同步");
|
|
|
- log.setNewUnionid(userInfo2.getUnionid());
|
|
|
-
|
|
|
- userTransferLogMapper.insert(log);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("获取accesstoken错误");
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ private final WxSyncLogMapper wxSyncLogMapper;
|
|
|
+
|
|
|
+ private final WeChatConfig weChatConfig;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取微信用户信息, 迁移后通过openid同步用户unionid
|
|
|
+ */
|
|
|
+ @GetMapping("/get/wx/user")
|
|
|
+ public void syncUser() {
|
|
|
+ String oldAppId = weChatConfig.getOldAppid();
|
|
|
+ String newAppId = weChatConfig.getAppid();
|
|
|
+ LambdaQueryWrapper<WxSyncLog> wrapper = Wrappers.lambdaQuery(WxSyncLog.class)
|
|
|
+ .ne(WxSyncLog::getNewOpenid, "")
|
|
|
+ .in(WxSyncLog ::getUserId,List.of(9909,11201))
|
|
|
+ .orderByAsc(WxSyncLog::getId);
|
|
|
+ Integer limit = 12000;
|
|
|
+ Integer count = wxSyncLogMapper.selectCount(wrapper);
|
|
|
+ int num = (count / limit) + 1;
|
|
|
+ for (int i = 0; i < num; i++) {
|
|
|
+ Integer start = i + 1;
|
|
|
+ Page<WxSyncLog> wxSyncLogs = wxSyncLogMapper.selectPage(new Page<>(start, limit),wrapper);
|
|
|
+ List<WxSyncLog> records = wxSyncLogs.getRecords();
|
|
|
+ List<UserTransferLog> userTransferLogs = new ArrayList<>(records.size());
|
|
|
+ records.forEach(syncUser -> {
|
|
|
+ try {
|
|
|
+ String accessToken = weChatService.getAccessToken();
|
|
|
+ String oriOpenid = syncUser.getOriOpenid();
|
|
|
+ String newOpenId = syncUser.getNewOpenid();
|
|
|
+ GzhUnionidUserinfo userInfo2 = weChatService.getUserInfo2(accessToken, newOpenId);
|
|
|
+ if (userInfo2 == null) {
|
|
|
+ log.info("newopenid:{}未关注新公众号", newOpenId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if ("ADD_SCENE_ACCOUNT_MIGRATION".equals(userInfo2.getSubscribeScene())) {
|
|
|
+ log.info("旧oriOpenid:{}->新newOpenid:{}公众号迁移", oriOpenid, newOpenId);
|
|
|
+ }
|
|
|
+ if (userInfo2 == null) {
|
|
|
+ log.info("该newopenid:{}不是该新公众号的用户", newOpenId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ User user = userService.getOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getOpenId, oriOpenid)
|
|
|
+ .select(User::getId,
|
|
|
+ User::getUnionid,
|
|
|
+ User::getOpenId).last("limit 1"));
|
|
|
+ if (user == null) {
|
|
|
+ log.info("该newopenid:{}是新公众号的新用户,同步过来的数据不存在这种情况", newOpenId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(userInfo2.getUnionid())) {
|
|
|
+ log.info("该用户未未关注新公众号,等待在新公众号授权重置其uinonid");
|
|
|
+ }
|
|
|
+ if (user.getUnionid() != null && user.getUnionid().equals(userInfo2.getUnionid())) {
|
|
|
+ log.info("用户id:{},新旧公众号unionid相同", user.getId());
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ String unionid = user.getUnionid();
|
|
|
+ user.setOpenId(newOpenId);
|
|
|
+ if (StrUtil.isNotEmpty(userInfo2.getUnionid())) {
|
|
|
+ user.setUnionid(userInfo2.getUnionid());
|
|
|
+ }
|
|
|
+// userService.updateById(user);
|
|
|
+
|
|
|
+ UserTransferLog log = new UserTransferLog();
|
|
|
+ log.setOldAppId(oldAppId);
|
|
|
+ log.setNewAppId(newAppId);
|
|
|
+ log.setUserId(user.getId());
|
|
|
+ log.setOldUnionid(unionid);
|
|
|
+ log.setType("手动同步");
|
|
|
+ log.setNewUnionid(userInfo2.getUnionid());
|
|
|
+ userTransferLogs.add(log);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("获取accesstoken错误");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ List<List<UserTransferLog>> logsList = Lists.partition(userTransferLogs, 100);
|
|
|
+ logsList.forEach(insertLogList->{
|
|
|
+ userTransferLogMapper.batchInsert(insertLogList);
|
|
|
+ });
|
|
|
+ userTransferLogs.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步旧的公众号openid->新公众号
|
|
|
+ */
|
|
|
+ @GetMapping("/sync/old/wx/user")
|
|
|
+ public void test() throws Exception {
|
|
|
+ String accessToken = "+ \"64_F1RV0DPgP6silfzERCbrckClRYLk2-HsP2mklVETA_t2FRlnlnN0wpBcFcy5rBqMv5VPpwwNsSM47Y1uuuoEDkgtWQLtBEUH3EgtnNmMAZYa8brtKvf_GRTJiPcHKFbAIAQTO\"";
|
|
|
+ String syncUrl = "https://api.weixin.qq.com/cgi-bin/changeopenid?access_token=" + weChatService.getAccessToken();
|
|
|
+// UserWxDto userWxDto = getUserWxDto("");
|
|
|
+// syncWxUser(userWxDto, syncUrl);
|
|
|
+ Integer limit = 12000;
|
|
|
+ WxSyncLog wxSyncLog = wxSyncLogMapper.selectOne(Wrappers.lambdaQuery(WxSyncLog.class)
|
|
|
+ .orderByDesc(WxSyncLog::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ String oriOpenId = wxSyncLog.getOriOpenid();
|
|
|
+ Date finalCreateTime = userService.getOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getOpenId, oriOpenId).last("limit 1")).getCreatedTime();
|
|
|
+ LambdaQueryWrapper<User> select = Wrappers.lambdaQuery(User.class)
|
|
|
+ .ne(User::getOpenId, "")
|
|
|
+ .ge(User::getCreatedTime, finalCreateTime)
|
|
|
+ .ne(User::getOpenId, oriOpenId)
|
|
|
+ .orderByAsc(User::getId)
|
|
|
+ .select(User::getOpenId);
|
|
|
+ int count = userService.count(select);
|
|
|
+ int num = (count / limit) + 1;
|
|
|
+ for (int i = 0; i < num; i++) {
|
|
|
+ Integer start = i + 1;
|
|
|
+ Page<User> page = userService.page(new Page<>(start, limit), select.select(User::getOpenId, User::getId));
|
|
|
+ List<User> records = page.getRecords();
|
|
|
+ syncWxUser(records, syncUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void syncWxUser(List<User> users, String syncUrl) throws Exception {
|
|
|
+ List<String> openidList = users.stream().map(User::getOpenId).collect(Collectors.toList());
|
|
|
+ Map<String, List<User>> openidMap = users.stream().collect(Collectors.groupingBy(User::getOpenId));
|
|
|
+ if (CollUtil.isNotEmpty(openidList)) {
|
|
|
+ List<List<String>> partition = Lists.partition(openidList, 100);
|
|
|
+ partition.forEach(openids -> {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.putOpt("from_appid", weChatConfig.getOldAppid());
|
|
|
+ jsonObject.putOpt("openid_list", openids);
|
|
|
+ try {
|
|
|
+ HttpResponse<String> send = J11HttpC.custom()
|
|
|
+ .ofPost()
|
|
|
+ .url(syncUrl)
|
|
|
+ .body(HttpRequest.BodyPublishers.ofString(jsonObject.toString()))
|
|
|
+ .send(HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ JSONObject json = Jsons.parseObject(send.body(), JSONObject.class);
|
|
|
+ List<WxOpenIdSyncResultDto> result_list = Jsons.parseList(json.get("result_list"), WxOpenIdSyncResultDto.class);
|
|
|
+ result_list.forEach(result -> {
|
|
|
+ WxSyncLog wxSyncLog = new WxSyncLog();
|
|
|
+ wxSyncLog.setFromAppid(weChatConfig.getOldAppid());
|
|
|
+ wxSyncLog.setToAppid(weChatConfig.getAppid());
|
|
|
+ wxSyncLog.setUserId(openidMap.get(result.getOri_openid()).get(0).getId());
|
|
|
+ wxSyncLog.setOriOpenid(result.getOri_openid());
|
|
|
+ wxSyncLog.setNewOpenid(result.getNew_openid());
|
|
|
+ wxSyncLog.setErrMsg(result.getErr_msg());
|
|
|
+ wxSyncLogMapper.insert(wxSyncLog);
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("公众号迁移错误,error_msg:", e.getMessage());
|
|
|
+ }
|
|
|
+// List<User> user = userService.list(Wrappers.lambdaQuery(User.class)
|
|
|
+// .in(User::getOpenId, openids));
|
|
|
+// if (user.isEmpty()) {
|
|
|
+// log.info("ss");
|
|
|
+// }
|
|
|
+// if (user.size() != openids.size()) {
|
|
|
+// log.info("不相等");
|
|
|
+// List<String> collect = user.stream().map(User::getOpenId).collect(Collectors.toList());
|
|
|
+// log.info("本地用户大小:{},微信关注用户数:{}", user.size(), openids.size());
|
|
|
+// openids.removeAll(collect);
|
|
|
+// try {
|
|
|
+// log.info("剩余:{}", openids);
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// });
|
|
|
+// });
|
|
|
+// if (StrUtil.isNotBlank(userWxDto.getNext_openid())) {
|
|
|
+// userWxDto = getUserWxDto(userWxDto.getNext_openid());
|
|
|
+// syncWxUser(userWxDto, syncUrl);
|
|
|
+// }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private UserWxDto getUserWxDto(String nxt_openid) throws Exception {
|
|
|
+ String accessToken = "64_F1RV0DPgP6silfzERCbrckClRYLk2-HsP2mklVETA_t2FRlnlnN0wpBcFcy5rBqMv5VPpwwNsSM47Y1uuuoEDkgtWQLtBEUH3EgtnNmMAZYa8brtKvf_GRTJiPcHKFbAIAQTO";
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s";
|
|
|
+ HttpResponse<String> response = J11HttpC.custom()
|
|
|
+ .ofGet()
|
|
|
+ .url(String.format(url, accessToken, nxt_openid))
|
|
|
+ .send(HttpResponse.BodyHandlers.ofString());
|
|
|
+ return Jsons.parseObject(response.body(), UserWxDto.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/test")
|
|
|
+ public void tes2t() throws Exception {
|
|
|
+// List<String> strings = List.of("oXxcY6I5boH-4sSBf3eL5lARrGP0", "oXxcY6FY-PFSRohN-NuiIb_XKHXM","oXxcY6A5GdZIPbZBj4CFkXcNWVjU");
|
|
|
+// strings.forEach(opneid->{
|
|
|
+// try {
|
|
|
+// GzhUnionidUserinfo userInfo2 = weChatService.getUserInfo2(weChatService.getAccessToken(), opneid);
|
|
|
+// System.out.println(Jsons.toJson(userInfo2));
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// });
|
|
|
+ AuthToken authToken = weChatService.getAuthToken("081UY51w3Q7AWZ2dlc0w3EJPFp4UY51j");
|
|
|
+ GzhOAuth2UserInfo userInfo = weChatService.getUserInfo(authToken.getAccessToken(), authToken.getOpenid());
|
|
|
+ System.out.println("s");
|
|
|
+ }
|
|
|
}
|