微信服务号推送:获取模板列表

pull/26/head
3y 2 years ago
parent 65059e8cf2
commit d87b501a1b

@ -41,16 +41,7 @@
<groupId>com.sun.mail</groupId> <groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId> <artifactId>javax.mail</artifactId>
</dependency> </dependency>
<!--微信发送模板信息-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
</dependency>
<!--微信小程序第三方SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
<!--企业微信发送消息--> <!--企业微信发送消息-->
<dependency> <dependency>

@ -1,10 +1,10 @@
package com.java3y.austin.handler.wechat.impl; package com.java3y.austin.handler.wechat.impl;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.WeChatOfficialAccount; import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
import com.java3y.austin.handler.domain.wechat.WeChatOfficialParam; import com.java3y.austin.handler.domain.wechat.WeChatOfficialParam;
import com.java3y.austin.handler.wechat.OfficialAccountService; import com.java3y.austin.handler.wechat.OfficialAccountService;
import com.java3y.austin.support.utils.AccountUtils; import com.java3y.austin.support.utils.AccountUtils;
import com.java3y.austin.support.utils.WxServiceUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
@ -31,8 +31,8 @@ public class OfficialAccountServiceImpl implements OfficialAccountService {
@Override @Override
public List<String> send(WeChatOfficialParam officialParam) throws Exception { public List<String> send(WeChatOfficialParam officialParam) throws Exception {
WeChatOfficialAccount officialAccount = accountUtils.getAccountById(officialParam.getSendAccount(),WeChatOfficialAccount.class); WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(officialParam.getSendAccount());
WxMpService wxMpService = initService(officialAccount); WeChatOfficialAccount officialAccount = WxServiceUtils.accountHashMap.get(officialParam.getSendAccount());
List<WxMpTemplateMessage> messages = assembleReq(officialParam, officialAccount); List<WxMpTemplateMessage> messages = assembleReq(officialParam, officialAccount);
List<String> messageIds = new ArrayList<>(messages.size()); List<String> messageIds = new ArrayList<>(messages.size());
for (WxMpTemplateMessage wxMpTemplateMessage : messages) { for (WxMpTemplateMessage wxMpTemplateMessage : messages) {

@ -94,21 +94,31 @@
<artifactId>dynamic-tp-spring-boot-starter-apollo</artifactId> <artifactId>dynamic-tp-spring-boot-starter-apollo</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.amqp</groupId> <groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId> <artifactId>spring-rabbit</artifactId>
</dependency> </dependency>
<!--nacos-->
<dependency> <dependency>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId> <artifactId>nacos-config-spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -0,0 +1,60 @@
package com.java3y.austin.support.utils;
import com.alibaba.fastjson.JSON;
import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* /
*
* @author 3y
*/
@Component
@Slf4j
public class WxServiceUtils {
public static Map<Long, WxMpService> wxMpServiceMap = new HashMap<>();
public static Map<Long, WeChatOfficialAccount> accountHashMap = new HashMap<>();
@Autowired
private ChannelAccountDao channelAccountDao;
@PostConstruct
public void init() {
List<ChannelAccount> officialAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.OFFICIAL_ACCOUNT.getCode());
for (ChannelAccount channelAccount : officialAccountList) {
WeChatOfficialAccount weChatOfficialAccount = JSON.parseObject(channelAccount.getAccountConfig(), WeChatOfficialAccount.class);
wxMpServiceMap.put(channelAccount.getId(), initService(weChatOfficialAccount));
accountHashMap.put(channelAccount.getId(), weChatOfficialAccount);
}
}
/**
*
*
* @return
*/
public WxMpService initService(WeChatOfficialAccount officialAccount) {
WxMpService wxMpService = new WxMpServiceImpl();
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
config.setAppId(officialAccount.getAppId());
config.setSecret(officialAccount.getSecret());
wxMpService.setWxMpConfigStorage(config);
return wxMpService;
}
}

@ -6,6 +6,7 @@ import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.common.vo.BasicResultVO;
import com.java3y.austin.support.domain.ChannelAccount; import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.web.service.ChannelAccountService; import com.java3y.austin.web.service.ChannelAccountService;
import com.java3y.austin.web.vo.amis.CommonAmisVo;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -47,14 +48,12 @@ public class ChannelAccountController {
@GetMapping("/queryByChannelType") @GetMapping("/queryByChannelType")
@ApiOperation("/根据渠道标识查询相关的记录") @ApiOperation("/根据渠道标识查询相关的记录")
public BasicResultVO query(Integer channelType) { public BasicResultVO query(Integer channelType) {
List<ChannelAccount> channelAccounts = channelAccountService.queryByChannelType(channelType); List<CommonAmisVo> result = new ArrayList<>();
List<Map<String, String>> result = new ArrayList<>(); List<ChannelAccount> channelAccounts = channelAccountService.queryByChannelType(channelType);
for (ChannelAccount channelAccount : channelAccounts) { for (ChannelAccount channelAccount : channelAccounts) {
HashMap<String, String> optionKV = new HashMap<>(); CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(channelAccount.getName()).value(String.valueOf(channelAccount.getId())).build();
optionKV.put("label", channelAccount.getName()); result.add(commonAmisVo);
optionKV.put("value", String.valueOf(channelAccount.getId()));
result.add(optionKV);
} }
return BasicResultVO.success(result); return BasicResultVO.success(result);
} }

@ -0,0 +1,100 @@
package com.java3y.austin.web.controller;
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.common.vo.BasicResultVO;
import com.java3y.austin.support.utils.WxServiceUtils;
import com.java3y.austin.web.vo.amis.CommonAmisVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author 3y
*/
@Slf4j
@RestController
@RequestMapping("/officialAccount")
@Api("微信服务号")
@CrossOrigin(origins = {AustinConstant.ORIGIN_VALUE, "https://aisuda.bce.baidu.com"}, allowCredentials = "true", allowedHeaders = "*")
public class OfficialAccountController {
/**
* @param id Id
* @return
*/
@GetMapping("/template/list")
@ApiOperation("/根据账号Id获取模板列表")
public BasicResultVO queryList(Long id) {
try {
List<CommonAmisVo> result = new ArrayList<>();
WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(id);
List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
for (WxMpTemplate wxMpTemplate : allPrivateTemplate) {
CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(wxMpTemplate.getTitle()).value(wxMpTemplate.getTemplateId()).build();
result.add(commonAmisVo);
}
return BasicResultVO.success(result);
} catch (Exception e) {
log.error("OfficialAccountController#queryList fail:{}", Throwables.getStackTraceAsString(e));
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR);
}
}
/**
* IdID
*
* @return
* @
*/
@GetMapping("/template/detailTemplate")
@ApiOperation("/根据账号Id和模板ID获取模板列表")
public BasicResultVO queryDetailList(Long id, String wxTemplateId) {
try {
List<CommonAmisVo> result = new ArrayList<>();
WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(id);
List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
for (WxMpTemplate wxMpTemplate : allPrivateTemplate) {
if (wxTemplateId.equals(wxMpTemplate.getTemplateId())) {
String[] data = wxMpTemplate.getContent().split(StrUtil.LF);
for (String datum : data) {
String name = datum.substring(datum.indexOf("{{") + 2, datum.indexOf("."));
CommonAmisVo commonAmisVo = CommonAmisVo.builder()
.name(name)
.type("text")
.required(true)
.build();
if (datum.contains("first")) {
commonAmisVo.setLabel("名字");
} else if (datum.contains("remark")) {
commonAmisVo.setLabel("备注");
} else {
commonAmisVo.setLabel(datum.split("")[0]);
}
result.add(commonAmisVo);
}
}
}
return BasicResultVO.success(result);
} catch (Exception e) {
log.error("OfficialAccountController#queryList fail:{}", Throwables.getStackTraceAsString(e));
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR);
}
}
}

@ -0,0 +1,27 @@
package com.java3y.austin.web.vo.amis;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* amis
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommonAmisVo {
private String type;
private String label;
private String value;
private String name;
private boolean required;
}
Loading…
Cancel
Save