|
|
@@ -0,0 +1,96 @@
|
|
|
+package com.kym.service.miniapp.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.kym.common.utils.CommUtil;
|
|
|
+import com.kym.common.utils.IDGenerator;
|
|
|
+import com.kym.entity.miniapp.MpRelation;
|
|
|
+import com.kym.entity.miniapp.User;
|
|
|
+import com.kym.mapper.miniapp.MpRelationMapper;
|
|
|
+import com.kym.service.miniapp.MpRelationService;
|
|
|
+import com.kym.service.miniapp.UserService;
|
|
|
+import com.kym.service.mybatisplus.MyBaseServiceImpl;
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
+import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
|
|
+import me.chanjar.weixin.mp.bean.result.WxMpUserList;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 微信公众号用户关联表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author skyline
|
|
|
+ * @since 2024-07-31
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class MpRelationServiceImpl extends MyBaseServiceImpl<MpRelationMapper, MpRelation> implements MpRelationService {
|
|
|
+
|
|
|
+ private final UserService userService;
|
|
|
+ private final WxMpService wxMpService;
|
|
|
+
|
|
|
+
|
|
|
+ public MpRelationServiceImpl(UserService userService, WxMpService wxMpService) {
|
|
|
+ this.userService = userService;
|
|
|
+ this.wxMpService = wxMpService;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公众号用户关联小程序用户
|
|
|
+ *
|
|
|
+ * @throws WxErrorException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void bindMpUser() throws WxErrorException {
|
|
|
+ String nextOpenid = null;
|
|
|
+ for (; ; ) {
|
|
|
+ // 获取公众号所有关注者列表
|
|
|
+ WxMpUserList wxUserList = wxMpService.getUserService().userList(nextOpenid);
|
|
|
+ if (CommUtil.isEmptyOrNull(wxUserList.getNextOpenid())) {
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ nextOpenid = wxUserList.getNextOpenid();
|
|
|
+ }
|
|
|
+ // 通过unionid获取公众号openid
|
|
|
+ String lang = "zh_CN";
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // todo 优化成批量replace
|
|
|
+ // saveBatch(mpRelationList);
|
|
|
+
|
|
|
+ replaceBatch(mpRelationList);
|
|
|
+
|
|
|
+ var mpRelations = mpRelationList.stream().filter(mpRelation -> mpRelation.getUnionid() != null && !mpRelation.getUnionid().isEmpty()).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());
|
|
|
+ });
|
|
|
+ });
|
|
|
+ updateBatchByQueryWrapper(mpRelations, mpRelation ->
|
|
|
+ new QueryWrapper<MpRelation>().eq("unionid", mpRelation.getUnionid()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|