✨ Introducing new features. close #3505 【公众号】公众号支持更改备注及用户的标签

This commit is contained in:
lbw committed 2022-01-05 22:43:58 +08:00
1 parent 9e70c551cc
commit c9ccc6c888
7 files changed
+186 -15

No files matched your search

@@ -0,0 +1,56 @@
package com.pig4cloud.pigx.common.data.handler;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Mybatis数组,符串互转
* <p>
* MappedJdbcTypes 数据库中的数据类型 MappedTypes java中的的数据类型
*
* @author xuzihui
* @date 2019-11-20
*/
@MappedTypes(value = { Long[].class })
@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class JsonLongArrayTypeHandler extends BaseTypeHandler<Long[]> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Long[] parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, ArrayUtil.join(parameter, StrUtil.COMMA));
}
@Override
@SneakyThrows
public Long[] getNullableResult(ResultSet rs, String columnName) {
String reString = rs.getString(columnName);
return Convert.toLongArray(reString);
}
@Override
@SneakyThrows
public Long[] getNullableResult(ResultSet rs, int columnIndex) {
String reString = rs.getString(columnIndex);
return Convert.toLongArray(reString);
}
@Override
@SneakyThrows
public Long[] getNullableResult(CallableStatement cs, int columnIndex) {
String reString = cs.getString(columnIndex);
return Convert.toLongArray(reString);
}
}
@@ -16,7 +16,6 @@
*/
package com.pig4cloud.pigx.mp.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
@@ -47,7 +46,7 @@ public class WxAccountFansController {
*/
@GetMapping("/page")
public R getWxAccountFansPage(Page page, WxAccountFans wxAccountFans) {
return R.ok(wxAccountFansService.page(page, Wrappers.query(wxAccountFans)));
return R.ok(wxAccountFansService.getFansWithTagPage(page, wxAccountFans));
}
/**
@@ -60,18 +59,6 @@ public class WxAccountFansController {
return R.ok(wxAccountFansService.getById(id));
}
/**
* 新增微信公众号粉丝
* @param wxAccountFans 微信公众号粉丝
* @return R
*/
@SysLog("新增微信公众号粉丝")
@PostMapping
@PreAuthorize("@pms.hasPermission('mp_wxaccountfans_add')")
public R save(@RequestBody WxAccountFans wxAccountFans) {
return R.ok(wxAccountFansService.save(wxAccountFans));
}
/**
* 修改微信公众号粉丝
* @param wxAccountFans 微信公众号粉丝
@@ -81,7 +68,7 @@ public class WxAccountFansController {
@PutMapping
@PreAuthorize("@pms.hasPermission('mp_wxaccountfans_edit')")
public R updateById(@RequestBody WxAccountFans wxAccountFans) {
return R.ok(wxAccountFansService.updateById(wxAccountFans));
return R.ok(wxAccountFansService.updateFans(wxAccountFans));
}
/**
@@ -111,6 +111,11 @@ public class WxAccountFans extends Model<WxAccountFans> {
*/
private String wxAccountName;
/**
* 标签ID列表
*/
private Long[] tagIds;
/**
* 创建时间
*/
@@ -0,0 +1,23 @@
package com.pig4cloud.pigx.mp.entity.vo;
import com.pig4cloud.pigx.mp.entity.WxAccountFans;
import com.pig4cloud.pigx.mp.entity.WxAccountTag;
import lombok.Data;
import java.util.List;
/**
* 粉丝Vo 对象
*
* @author lengleng
* @date 2022/1/5
*/
@Data
public class WxAccountFansVo extends WxAccountFans {
/**
* 标签名称列表
*/
private List<WxAccountTag> tagList;
}
@@ -75,6 +75,7 @@ public class SubscribeHandler extends AbstractHandler {
wxAccountFans.setLanguage(wxMpUser.getLanguage());
wxAccountFans.setHeadimgUrl(wxMpUser.getHeadImgUrl());
wxAccountFans.setRemark(wxMpUser.getRemark());
wxAccountFans.setTagIds(wxAccountFans.getTagIds());
WxAccount wxAccount = wxAccountMapper
.selectOne(Wrappers.<WxAccount>lambdaQuery().eq(WxAccount::getAppid, WxMpContextHolder.getAppId()));
@@ -16,6 +16,8 @@
*/
package com.pig4cloud.pigx.mp.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.mp.entity.WxAccountFans;
@@ -34,4 +36,19 @@ public interface WxAccountFansService extends IService<WxAccountFans> {
*/
Boolean syncAccountFans(String appId);
/**
* 分页查询粉丝
* @param page 粉丝
* @param wxAccountFans 查询条件
* @return
*/
IPage getFansWithTagPage(Page page, WxAccountFans wxAccountFans);
/**
* 更新粉丝信息
* @param wxAccountFans 信息
* @return
*/
Boolean updateFans(WxAccountFans wxAccountFans);
}
@@ -18,9 +18,11 @@ package com.pig4cloud.pigx.mp.service.impl;
import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -28,17 +30,24 @@ import com.pig4cloud.pigx.common.data.tenant.TenantBroker;
import com.pig4cloud.pigx.mp.config.WxMpInitConfigRunner;
import com.pig4cloud.pigx.mp.entity.WxAccount;
import com.pig4cloud.pigx.mp.entity.WxAccountFans;
import com.pig4cloud.pigx.mp.entity.WxAccountTag;
import com.pig4cloud.pigx.mp.entity.vo.WxAccountFansVo;
import com.pig4cloud.pigx.mp.mapper.WxAccountFansMapper;
import com.pig4cloud.pigx.mp.mapper.WxAccountMapper;
import com.pig4cloud.pigx.mp.mapper.WxAccountTagMapper;
import com.pig4cloud.pigx.mp.service.WxAccountFansService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.api.WxMpUserTagService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.time.LocalDateTime;
@@ -61,8 +70,80 @@ public class WxAccountFansServiceImpl extends ServiceImpl<WxAccountFansMapper, W
private static final int SIZE = 100;
private final WxAccountTagMapper wxAccountTagMapper;
private final WxAccountMapper wxAccountMapper;
/**
* 分页查询粉丝
* @param page 粉丝
* @param wxAccountFans 查询条件
* @return
*/
@Override
public IPage getFansWithTagPage(Page page, WxAccountFans wxAccountFans) {
// 查询当前公众号的标签
List<WxAccountTag> wxAccountTags = wxAccountTagMapper.selectList(Wrappers.emptyWrapper());
// 翻页查询粉丝
IPage<WxAccountFans> fansPage = baseMapper.selectPage(page, Wrappers.query(wxAccountFans));
List<WxAccountFansVo> voList = fansPage.getRecords().stream().map(fans -> {//
WxAccountFansVo vo = new WxAccountFansVo();
BeanUtils.copyProperties(fans, vo);
// 赋值 TAG_NAME
if (ArrayUtil.isNotEmpty(fans.getTagIds())) {
List<WxAccountTag> tagList = wxAccountTags.stream()
.filter(tag -> tag.getWxAccountAppid().endsWith(fans.getWxAccountAppid()) //
&& ArrayUtil.contains(fans.getTagIds(), tag.getTagId()))
.collect(Collectors.toList());
vo.setTagList(tagList);
}
return vo;
}).collect(Collectors.toList());
page.setRecords(voList);
page.setTotal(fansPage.getTotal());
return page;
}
/**
* 更新粉丝信息
* @param wxAccountFans 信息
* @return
*/
@Override
@SneakyThrows
@Transactional(rollbackFor = Exception.class)
public Boolean updateFans(WxAccountFans wxAccountFans) {
baseMapper.updateById(wxAccountFans);
// 获取操作微信接口类
WxMpService wxMpService = WxMpInitConfigRunner.getMpServices().get(wxAccountFans.getWxAccountAppid());
if (StrUtil.isNotBlank(wxAccountFans.getRemark())) {
WxMpUserService wxMpUserService = wxMpService.getUserService();
wxMpUserService.userUpdateRemark(wxAccountFans.getOpenid(), wxAccountFans.getRemark());
}
// 更新用户标签
WxMpUserTagService userTagService = wxMpService.getUserTagService();
if (ArrayUtil.isNotEmpty(wxAccountFans.getTagIds())) {
// 移除原有标签
List<Long> oldTag = userTagService.userTagList(wxAccountFans.getOpenid());
for (Long tagId : oldTag) {
userTagService.batchUntagging(tagId, new String[] { wxAccountFans.getOpenid() });
}
// 添加新标签
for (Long tagId : wxAccountFans.getTagIds()) {
userTagService.batchTagging(tagId, new String[] { wxAccountFans.getOpenid() });
}
}
return Boolean.TRUE;
}
/**
* 获取公众号粉丝,生产建议异步
* @param appId
@@ -145,6 +226,7 @@ public class WxAccountFansServiceImpl extends ServiceImpl<WxAccountFansMapper, W
wxAccountFans.setLanguage(wxMpUser.getLanguage());
wxAccountFans.setHeadimgUrl(wxMpUser.getHeadImgUrl());
wxAccountFans.setRemark(wxMpUser.getRemark());
wxAccountFans.setTagIds(wxAccountFans.getTagIds());
wxAccountFans.setWxAccountId(wxAccount.getId());
wxAccountFans.setWxAccountAppid(wxAccount.getAppid());
wxAccountFans.setWxAccountName(wxAccount.getName());