♻️ Refactoring code. pigx-mp-biz 适配最新V5 UI

This commit is contained in:
lbw committed 2023-04-07 10:17:51 +08:00
1 parent 106627660e
commit 1ddacb086e
44 files changed
+158 -31

No files matched your search

File mode changed.
File mode changed.
File mode changed.
@@ -15,12 +15,16 @@ import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.constant.WxMpEventConstants;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.scheduling.annotation.Async;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
@@ -83,7 +87,13 @@ public class WxMpInitConfigRunner {
private final RedisTemplate redisTemplate;
@PostConstruct
@Async
@Order
@EventListener({ WebServerInitializedEvent.class })
public void WebServerInit() {
this.initServices();
}
public void initServices() {
// 获取全部租户 遍历所有租户对应的公众号列表
List<WxAccount> accountList = new ArrayList<>();
@@ -16,6 +16,8 @@
*/
package com.pig4cloud.pigx.mp.controller;
import cn.hutool.core.util.StrUtil;
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.pig4cloud.pigx.common.core.constant.CacheConstants;
@@ -54,7 +56,10 @@ public class WxAccountController {
*/
@GetMapping("/page")
public R getWxAccountPage(Page page, WxAccount wxAccount) {
return R.ok(wxAccountService.page(page, Wrappers.query(wxAccount)));
LambdaQueryWrapper<WxAccount> wrapper = Wrappers.<WxAccount>lambdaQuery()
.like(StrUtil.isNotBlank(wxAccount.getName()), WxAccount::getName, wxAccount.getName())
.like(StrUtil.isNotBlank(wxAccount.getAccount()), WxAccount::getAccount, wxAccount.getAccount());
return R.ok(wxAccountService.page(page, wrapper));
}
/**
@@ -126,8 +131,10 @@ public class WxAccountController {
* @return
*/
@GetMapping("/list")
public R list() {
return R.ok(wxAccountService.list());
public R list(String name) {
LambdaQueryWrapper<WxAccount> wrapper = Wrappers.<WxAccount>lambdaQuery().like(StrUtil.isNotBlank(name),
WxAccount::getName, name);
return R.ok(wxAccountService.list(wrapper));
}
/**
@@ -94,4 +94,20 @@ public class WxAccountFansController {
return R.ok(wxAccountFansService.syncAccountFans(appId));
}
/**
* 取消拉黑
* @param ids
* @param appId
* @return
*/
@PostMapping("/unblack/{appId}")
public R unblack(@RequestBody Long[] ids, @PathVariable String appId) {
return R.ok(wxAccountFansService.unblack(ids, appId));
}
@PostMapping("/black/{appId}")
public R black(@RequestBody Long[] ids, @PathVariable String appId) {
return R.ok(wxAccountFansService.black(ids, appId));
}
}
@@ -1,10 +1,13 @@
package com.pig4cloud.pigx.mp.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.mp.entity.WxAccountTag;
import com.pig4cloud.pigx.mp.entity.dto.WxAccountTagDeleteDTO;
import com.pig4cloud.pigx.mp.service.WxAccountTagService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -33,8 +36,12 @@ public class WxAccountTagController {
* @return
*/
@GetMapping("/page")
public R<IPage<WxAccountTag>> getWxAccountTagPage(Page page, WxAccountTag wxAccountTag) {
return R.ok(wxAccountTagService.page(page, Wrappers.query(wxAccountTag)));
public R<Page<WxAccountTag>> getWxAccountTagPage(Page<WxAccountTag> page, WxAccountTag wxAccountTag) {
LambdaQueryWrapper<WxAccountTag> wrapper = Wrappers.<WxAccountTag>lambdaQuery()
.like(StrUtil.isNotBlank(wxAccountTag.getTag()), WxAccountTag::getTag, wxAccountTag.getTag())
.eq(StrUtil.isNotBlank(wxAccountTag.getWxAccountAppid()), WxAccountTag::getWxAccountAppid,
wxAccountTag.getWxAccountAppid());
return R.ok(wxAccountTagService.page(page, wrapper));
}
/**
@@ -76,8 +83,15 @@ public class WxAccountTagController {
*/
@DeleteMapping
@PreAuthorize("@pms.hasPermission('mp_wx_account_tag_del')")
public R<Boolean> removeAccountTagById(@RequestBody WxAccountTag wxAccountTag) {
return R.ok(wxAccountTagService.removeAccountTagById(wxAccountTag));
public R<Boolean> removeAccountTagById(@RequestBody WxAccountTagDeleteDTO deleteDTO) {
deleteDTO.getIds().forEach(item -> {
WxAccountTag wxAccountTag = new WxAccountTag();
wxAccountTag.setId(item);
wxAccountTag.setWxAccountAppid(deleteDTO.getWxAccountAppid());
wxAccountTagService.removeAccountTagById(wxAccountTag);
});
return R.ok();
}
/**
@@ -67,7 +67,6 @@ public class WxAutoReplyController {
@PutMapping
@PreAuthorize("@pms.hasPermission('mp_wxautoreply_edit')")
public R updateById(@RequestBody WxAutoReply wxAutoReply) {
this.jude(wxAutoReply);
return R.ok(wxAutoReplyService.updateById(wxAutoReply));
}
@@ -78,7 +77,7 @@ public class WxAutoReplyController {
*/
@DeleteMapping("/{id}")
@PreAuthorize("@pms.hasPermission('mp_wxautoreply_del')")
public R removeById(@PathVariable String id) {
public R removeById(@PathVariable Long id) {
return R.ok(wxAutoReplyService.removeById(id));
}
@@ -16,6 +16,8 @@
*/
package com.pig4cloud.pigx.mp.controller;
import cn.hutool.core.util.StrUtil;
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.pig4cloud.pigx.common.core.util.R;
@@ -47,7 +49,11 @@ public class WxFansMsgController {
*/
@GetMapping("/page")
public R getWxMsgPage(Page page, WxMsg msg) {
return R.ok(wxMsgService.page(page, Wrappers.query(msg)));
LambdaQueryWrapper<WxMsg> wrapper = Wrappers.<WxMsg>lambdaQuery()
.eq(StrUtil.isNotBlank(msg.getAppId()), WxMsg::getAppId, msg.getAppId())
.like(StrUtil.isNotBlank(msg.getNickName()), WxMsg::getNickName, msg.getNickName())
.eq(StrUtil.isNotBlank(msg.getType()), WxMsg::getType, msg.getType());
return R.ok(wxMsgService.page(page, wrapper));
}
/**
@@ -32,9 +32,11 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* 微信素材
@@ -56,18 +58,21 @@ public class WxMaterialController {
*/
@PostMapping("/materialFileUpload")
@PreAuthorize("@pms.hasPermission('mp_wxmaterial_add')")
public R materialFileUpload(@RequestParam("file") MultipartFile mulFile, @RequestParam String appId,
public R materialFileUpload(@RequestPart("file") MultipartFile mulFile, @RequestParam String appId,
@RequestParam("title") String title, @RequestParam("introduction") String introduction,
@RequestParam("mediaType") String mediaType) {
try {
String originalFilename = new String(
Objects.requireNonNull(mulFile.getOriginalFilename()).getBytes(StandardCharsets.ISO_8859_1),
StandardCharsets.UTF_8);
WxMpMaterial material = new WxMpMaterial();
material.setName(mulFile.getOriginalFilename());
material.setName(originalFilename);
if (WxConsts.MediaFileType.VIDEO.equals(mediaType)) {
material.setVideoTitle(title);
material.setVideoIntroduction(introduction);
}
File file = FileUtil.newFile(FileUtil.getTmpDirPath() + mulFile.getOriginalFilename());
File file = FileUtil.newFile(FileUtil.getTmpDirPath() + originalFilename);
mulFile.transferTo(file);
material.setFile(file);
File mode changed.
File mode changed.
@@ -116,6 +116,11 @@ public class WxAccountFans extends Model<WxAccountFans> {
*/
private Long[] tagIds;
/**
* 是否被拉黑
*/
private Integer isBlack;
/**
* 创建时间
*/
File mode changed.
File mode changed.
@@ -0,0 +1,14 @@
package com.pig4cloud.pigx.mp.entity.dto;
import lombok.Data;
import java.util.List;
@Data
public class WxAccountTagDeleteDTO {
private List<Long> ids;
private String wxAccountAppid;
}
File mode changed.
File mode changed.
File mode changed.
File mode changed.
File mode changed.
@@ -1,5 +1,6 @@
package com.pig4cloud.pigx.mp.handler;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -167,12 +168,10 @@ public class MsgHandler extends AbstractHandler {
}
if (WxConsts.KefuMsgType.NEWS.equals(wxAutoReply.getRepType())) {
List<WxMpXmlOutNewsMessage.Item> list = new ArrayList<>();
JSONObject content = JSONUtil.parseObj(wxAutoReply.getContent());
List<JSONObject> jsonObjectList = content.getJSONArray("articles").toList(JSONObject.class);
WxMpXmlOutNewsMessage.Item t;
for (JSONObject jsonObject : jsonObjectList) {
t = new WxMpXmlOutNewsMessage.Item();
JSONArray jsonArray = JSONUtil.parseArray(wxAutoReply.getContent());
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
WxMpXmlOutNewsMessage.Item t = new WxMpXmlOutNewsMessage.Item();
t.setTitle(jsonObject.getStr("title"));
t.setDescription(jsonObject.getStr("digest"));
t.setPicUrl(jsonObject.getStr("thumbUrl"));
File mode changed.
File mode changed.
File mode changed.
File mode changed.
File mode changed.
File mode changed.
File mode changed.
File mode changed.
File mode changed.
@@ -51,4 +51,8 @@ public interface WxAccountFansService extends IService<WxAccountFans> {
*/
Boolean updateFans(WxAccountFans wxAccountFans);
Boolean unblack(Long[] ids, String appId);
Boolean black(Long[] ids, String appId);
}
File mode changed.
File mode changed.
@@ -22,10 +22,12 @@ 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.conditions.query.LambdaQueryWrapper;
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;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.data.tenant.TenantBroker;
import com.pig4cloud.pigx.mp.config.WxMpInitConfigRunner;
import com.pig4cloud.pigx.mp.entity.WxAccount;
@@ -40,9 +42,11 @@ import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService;
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.WxMpUserBlacklistGetResult;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
@@ -54,7 +58,9 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author lengleng
@@ -80,13 +86,21 @@ public class WxAccountFansServiceImpl extends ServiceImpl<WxAccountFansMapper, W
* @param wxAccountFans 查询条件
* @return
*/
@SneakyThrows
@Override
public IPage getFansWithTagPage(Page page, WxAccountFans wxAccountFans) {
// 查询当前公众号的标签
List<WxAccountTag> wxAccountTags = wxAccountTagMapper.selectList(Wrappers.emptyWrapper());
// 翻页查询粉丝
IPage<WxAccountFans> fansPage = baseMapper.selectPage(page, Wrappers.query(wxAccountFans));
LambdaQueryWrapper<WxAccountFans> wrapper = Wrappers.<WxAccountFans>lambdaQuery()
.eq(StrUtil.isNotBlank(wxAccountFans.getWxAccountAppid()), WxAccountFans::getWxAccountAppid,
wxAccountFans.getWxAccountAppid())
.like(StrUtil.isNotBlank(wxAccountFans.getNickname()), WxAccountFans::getNickname,
wxAccountFans.getNickname());
IPage<WxAccountFans> fansPage = baseMapper.selectPage(page, wrapper);
List<WxAccountFansVo> voList = fansPage.getRecords().stream().map(fans -> {//
WxAccountFansVo vo = new WxAccountFansVo();
BeanUtils.copyProperties(fans, vo);
@@ -144,6 +158,40 @@ public class WxAccountFansServiceImpl extends ServiceImpl<WxAccountFansMapper, W
return Boolean.TRUE;
}
@SneakyThrows
@Override
public Boolean unblack(Long[] ids, String appId) {
List<WxAccountFans> wxAccountFans = this.listByIds(CollUtil.toList(ids));
WxMpService wxMpService = WxMpInitConfigRunner.getMpServices().get(appId);
WxMpUserBlacklistService blackListService = wxMpService.getBlackListService();
List<String> collect = wxAccountFans.stream().map(WxAccountFans::getOpenid).collect(Collectors.toList());
blackListService.pullFromBlacklist(collect);
// 更新数据
List<WxAccountFans> fansList = wxAccountFans.stream().peek(item -> {
item.setIsBlack(CommonConstants.SUCCESS);
}).collect(Collectors.toList());
this.updateBatchById(fansList);
return Boolean.TRUE;
}
@SneakyThrows
@Override
public Boolean black(Long[] ids, String appId) {
List<WxAccountFans> wxAccountFans = this.listByIds(CollUtil.toList(ids));
WxMpService wxMpService = WxMpInitConfigRunner.getMpServices().get(appId);
WxMpUserBlacklistService blackListService = wxMpService.getBlackListService();
List<String> collect = wxAccountFans.stream().map(WxAccountFans::getOpenid).collect(Collectors.toList());
blackListService.pushToBlacklist(collect);
// 更新数据
List<WxAccountFans> fansList = wxAccountFans.stream().peek(item -> {
item.setIsBlack(CommonConstants.FAIL);
}).collect(Collectors.toList());
this.updateBatchById(fansList);
return Boolean.TRUE;
}
/**
* 获取公众号粉丝,生产建议异步
* @param appId
@@ -230,6 +278,7 @@ public class WxAccountFansServiceImpl extends ServiceImpl<WxAccountFansMapper, W
wxAccountFans.setWxAccountId(wxAccount.getId());
wxAccountFans.setWxAccountAppid(wxAccount.getAppid());
wxAccountFans.setWxAccountName(wxAccount.getName());
wxAccountFans.setIsBlack(CommonConstants.SUCCESS);
return wxAccountFans;
}
@@ -104,7 +104,7 @@ public class WxAccountTagServiceImpl extends ServiceImpl<WxAccountTagMapper, WxA
List<WxUserTag> wxUserTags = wxMpService.getUserTagService().tagGet();
// 删除旧数据
baseMapper.delete(Wrappers.lambdaQuery());
baseMapper.delete(Wrappers.<WxAccountTag>lambdaQuery().eq(WxAccountTag::getWxAccountAppid, appId));
WxAccount wxAccount = accountMapper.selectOne(Wrappers.<WxAccount>lambdaQuery().eq(WxAccount::getAppid, appId));
for (WxUserTag wxUserTag : wxUserTags) {
@@ -112,15 +112,14 @@ public class WxMsgServiceImpl extends ServiceImpl<WxMsgMapper, WxMsg> implements
if (WxConsts.KefuMsgType.NEWS.equals(wxMsg.getRepType())) {
List<WxMpKefuMessage.WxArticle> list = new ArrayList<>();
JSONArray articles = JSONUtil.parseObj(wxMsg.getContent()).getJSONArray("articles");
WxMpKefuMessage.WxArticle t;
for (Object object : articles) {
JSONObject obj = JSONUtil.parseObj(JSONUtil.toJsonStr(object));
t = new WxMpKefuMessage.WxArticle();
t.setTitle(obj.getStr("title"));
t.setDescription(obj.getStr("digest"));
t.setPicUrl(obj.getStr("thumbUrl"));
t.setUrl(obj.getStr("url"));
JSONArray jsonArray = JSONUtil.parseArray(wxMsg.getContent());
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
WxMpKefuMessage.WxArticle t = new WxMpKefuMessage.WxArticle();
t.setTitle(jsonObject.getStr("title"));
t.setDescription(jsonObject.getStr("digest"));
t.setPicUrl(jsonObject.getStr("thumbUrl"));
t.setUrl(jsonObject.getStr("url"));
list.add(t);
}
wxMsg.setRepName(wxMsg.getRepName());
File mode changed.
File mode changed.
File mode changed.