瀏覽代碼

公众号用户批量绑定优化,新增定时任务。

skyline 1 年之前
父節點
當前提交
f182fe67e9

+ 32 - 0
miniapp/src/main/java/com/kym/miniapp/jobs/MpUserRelationJob.java

@@ -0,0 +1,32 @@
+package com.kym.miniapp.jobs;
+
+import com.kym.service.miniapp.MpRelationService;
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author skyline
+ * 公众号用户关联
+ * 2024-08-07
+ */
+@Component
+@Slf4j
+public class MpUserRelationJob {
+
+    private final MpRelationService mpRelationService;
+
+    public MpUserRelationJob(MpRelationService mpRelationService) {
+        this.mpRelationService = mpRelationService;
+    }
+
+    // 每天凌晨1点执行一次
+    @Scheduled(cron = "0 0 1 * * ?")
+    public void executeMpUserRelationJob() throws WxErrorException {
+        log.info("公众号用户关联处理启动...");
+        mpRelationService.batchBindMpUser();
+        log.info("公众号用户关联结束...");
+    }
+
+}

+ 14 - 21
service/src/main/java/com/kym/service/miniapp/impl/MpRelationServiceImpl.java

@@ -17,6 +17,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * <p>
@@ -58,33 +59,25 @@ public class MpRelationServiceImpl extends MyBaseServiceImpl<MpRelationMapper, M
             }
             // 通过unionid获取公众号openid
             var mpRelationList = new ArrayList<MpRelation>();
-            wxUserList.getOpenids().forEach(openid -> {
-                try {
-                    WxMpUser mpUser = wxMpService.getUserService().userInfo(openid, lang);
-                    MpRelation mpRelation = new MpRelation();
-                    mpRelation.setId(IDGenerator.INS().nextId());
-                    mpRelation
-                            .setMpOpenid(mpUser.getOpenId())
-                            .setUnionid(mpUser.getUnionId())
-                            .setSubscribe(mpUser.getSubscribe())
-                            .setSubscribeScene(mpUser.getSubscribeScene());
-                    mpRelationList.add(mpRelation);
-                } catch (WxErrorException e) {
-                    throw new RuntimeException(e);
-                }
+            List<WxMpUser> mpUserList = wxMpService.getUserService().userInfoList(wxUserList.getOpenids());
+            mpUserList.forEach(mpUser -> {
+                MpRelation mpRelation = new MpRelation()
+                        .setMpOpenid(mpUser.getOpenId())
+                        .setUnionid(mpUser.getUnionId())
+                        .setSubscribe(mpUser.getSubscribe())
+                        .setSubscribeScene(mpUser.getSubscribeScene());
+                mpRelationList.add(mpRelation);
             });
 
             // 批量replace
             replaceBatch(mpRelationList);
 
-            var mpRelations = mpRelationList.stream().filter(mpRelation -> mpRelation.getUnionid() != null && !mpRelation.getUnionid().isEmpty()).toList();
+            var mpRelations = mpRelationList.stream().filter(mpRelation -> CommUtil.isNotEmptyAndNull(mpRelation.getUnionid())).toList();
             var userList = userService.lambdaQuery().in(User::getUnionid, mpRelations.stream().map(MpRelation::getUnionid).toList()).list();
-            userList.forEach(user -> {
-                mpRelations.stream().filter(mpRelation -> mpRelation.getUnionid().equals(user.getUnionid())).findFirst().ifPresent(mpRelation -> {
-                    mpRelation.setOpenid(user.getOpenid());
-                    mpRelation.setUserId(user.getId());
-                });
-            });
+            userList.forEach(user -> mpRelations.stream().filter(mpRelation -> mpRelation.getUnionid().equals(user.getUnionid())).forEach(mpRelation -> {
+                mpRelation.setOpenid(user.getOpenid());
+                mpRelation.setUserId(user.getId());
+            }));
             updateBatchByQueryWrapper(mpRelations, mpRelation ->
                     new QueryWrapper<MpRelation>().eq("unionid", mpRelation.getUnionid()));
         }