微信小程序-订阅消息 接入,调试中。

master
3y 3 years ago
parent a72971bd5b
commit 6a9bce2955

@ -1,5 +1,9 @@
package com.java3y.austin.common.constant; package com.java3y.austin.common.constant;
/**
* @author 3y
*/
public class CommonConstant { public class CommonConstant {
public final static String PERIOD = "."; public final static String PERIOD = ".";

@ -4,6 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/**
* @author 3y
*/
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor

@ -18,5 +18,16 @@ public class MiniProgramContentModel extends ContentModel {
/** /**
* *
*/ */
Map<String, String> map; Map<String, String> miniProgramParam;
/**
* Id
*/
private String templateId;
/**
*
*/
private String page;
} }

@ -12,10 +12,12 @@ import lombok.ToString;
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum MessageType { public enum MessageType {
NOTICE(10,"通知类消息","notice"), NOTICE(10,"通知类消息","notice"),
MARKETING(20,"营销类消息","marketing"), MARKETING(20,"营销类消息","marketing"),
AUTH_CODE(30,"验证码消息","auth_code") AUTH_CODE(30,"验证码消息","auth_code")
; ;
/** /**
* *
*/ */

@ -1,38 +0,0 @@
package com.java3y.austin.handler.domain.wechat;
import lombok.Builder;
import lombok.Data;
import java.util.Map;
import java.util.Set;
/**
* @author sunql
* @date 20220506 15:56
*
*
*/
@Data
@Builder
public class WeChatMiniProgramParam {
/**
* Id
*/
private Long messageTemplateId;
/**
*
*/
private Integer sendAccount;
/**
* openid
*/
private Set<String> openIds;
/**
* { "key1": { "value": any }, "key2": { "value": any } }
*/
private Map<String, String> data;
}

@ -14,6 +14,7 @@ public interface FlowControlService {
* *
* @param taskInfo * @param taskInfo
* @param flowControlParam * @param flowControlParam
* @return
*/ */
Double flowControl(TaskInfo taskInfo, FlowControlParam flowControlParam); Double flowControl(TaskInfo taskInfo, FlowControlParam flowControlParam);

@ -1,17 +1,15 @@
package com.java3y.austin.handler.flowcontrol.annotations; package com.java3y.austin.handler.flowcontrol.annotations;
import com.java3y.austin.handler.enums.RateLimitStrategy; import com.java3y.austin.handler.enums.RateLimitStrategy;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.lang.annotation.*;
/** /**
* *
* Created by TOM * Created by TOM
* On 2022/7/21 17:03 * On 2022/7/21 17:03
* @author TOM
*/ */
@Target({ElementType.TYPE}) @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)

@ -10,9 +10,10 @@ import com.java3y.austin.handler.flowcontrol.annotations.LocalRateLimit;
/** /**
* Created by TOM * Created by TOM
* On 2022/7/21 17:05 * On 2022/7/21 17:05
* @author TOM
*/ */
@LocalRateLimit(rateLimitStrategy = RateLimitStrategy.REQUEST_RATE_LIMIT) @LocalRateLimit(rateLimitStrategy = RateLimitStrategy.REQUEST_RATE_LIMIT)
public class RequestRateLimitService implements FlowControlService { public class RequestRateLimitServiceImpl implements FlowControlService {
/** /**
* *

@ -10,9 +10,10 @@ import com.java3y.austin.handler.flowcontrol.annotations.LocalRateLimit;
/** /**
* Created by TOM * Created by TOM
* On 2022/7/21 17:14 * On 2022/7/21 17:14
* @author TOM
*/ */
@LocalRateLimit(rateLimitStrategy = RateLimitStrategy.SEND_USER_NUM_RATE_LIMIT) @LocalRateLimit(rateLimitStrategy = RateLimitStrategy.SEND_USER_NUM_RATE_LIMIT)
public class SendUserNumRateLimitService implements FlowControlService { public class SendUserNumRateLimitServiceImpl implements FlowControlService {
/** /**
* *

@ -1,19 +1,25 @@
package com.java3y.austin.handler.handler.impl; package com.java3y.austin.handler.handler.impl;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.java3y.austin.common.domain.TaskInfo; import com.java3y.austin.common.domain.TaskInfo;
import com.java3y.austin.common.dto.model.MiniProgramContentModel; import com.java3y.austin.common.dto.model.MiniProgramContentModel;
import com.java3y.austin.common.enums.ChannelType; import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.handler.domain.wechat.WeChatMiniProgramParam;
import com.java3y.austin.handler.handler.BaseHandler; import com.java3y.austin.handler.handler.BaseHandler;
import com.java3y.austin.handler.handler.Handler; import com.java3y.austin.handler.handler.Handler;
import com.java3y.austin.handler.wechat.MiniProgramAccountService;
import com.java3y.austin.support.domain.MessageTemplate; import com.java3y.austin.support.domain.MessageTemplate;
import com.java3y.austin.support.utils.WxServiceUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* @author sunql * @author sunql
* *
@ -21,9 +27,8 @@ import org.springframework.stereotype.Component;
@Component @Component
@Slf4j @Slf4j
public class MiniProgramAccountHandler extends BaseHandler implements Handler { public class MiniProgramAccountHandler extends BaseHandler implements Handler {
@Autowired @Autowired
private MiniProgramAccountService miniProgramAccountService; private WxServiceUtils wxServiceUtils;
public MiniProgramAccountHandler() { public MiniProgramAccountHandler() {
channelCode = ChannelType.MINI_PROGRAM.getCode(); channelCode = ChannelType.MINI_PROGRAM.getCode();
@ -31,35 +36,47 @@ public class MiniProgramAccountHandler extends BaseHandler implements Handler {
@Override @Override
public boolean handler(TaskInfo taskInfo) { public boolean handler(TaskInfo taskInfo) {
WeChatMiniProgramParam miniProgramParam = buildMiniProgramParam(taskInfo); MiniProgramContentModel contentModel = (MiniProgramContentModel) taskInfo.getContentModel();
WxMaSubscribeService wxMaSubscribeService = wxServiceUtils.getMiniProgramServiceMap().get(taskInfo.getSendAccount().longValue());
List<WxMaSubscribeMessage> wxMaSubscribeMessages = assembleReq(taskInfo.getReceiver(), contentModel);
for (WxMaSubscribeMessage message : wxMaSubscribeMessages) {
try { try {
miniProgramAccountService.send(miniProgramParam); wxMaSubscribeService.sendSubscribeMsg(message);
} catch (Exception e) { } catch (Exception e) {
log.error("MiniProgramAccountHandler#handler fail:{},params:{}", log.info("MiniProgramAccountHandler#handler fail! param:{},e:{}", JSON.toJSONString(taskInfo), Throwables.getStackTraceAsString(e));
Throwables.getStackTraceAsString(e), JSON.toJSONString(taskInfo)); }
return false;
} }
return true; return true;
} }
/** /**
* taskInfo *
*
* @param taskInfo
* @return
*/ */
private WeChatMiniProgramParam buildMiniProgramParam(TaskInfo taskInfo) { private List<WxMaSubscribeMessage> assembleReq(Set<String> receiver, MiniProgramContentModel contentModel) {
// 小程序订阅消息可以关联到系统业务,通过接口查询。 List<WxMaSubscribeMessage> messageList = new ArrayList<>(receiver.size());
WeChatMiniProgramParam miniProgramParam = WeChatMiniProgramParam.builder() for (String openId : receiver) {
.openIds(taskInfo.getReceiver()) WxMaSubscribeMessage subscribeMessage = WxMaSubscribeMessage.builder()
.messageTemplateId(taskInfo.getMessageTemplateId()) .toUser(openId)
.sendAccount(taskInfo.getSendAccount()) .data(getWxMaTemplateData(contentModel.getMiniProgramParam()))
.templateId(contentModel.getTemplateId())
.page(contentModel.getPage())
.build(); .build();
messageList.add(subscribeMessage);
}
return messageList;
}
MiniProgramContentModel contentModel = (MiniProgramContentModel) taskInfo.getContentModel(); /**
miniProgramParam.setData(contentModel.getMap()); *
return miniProgramParam; *
* @returnp
*/
private List<WxMaSubscribeMessage.MsgData> getWxMaTemplateData(Map<String, String> data) {
List<WxMaSubscribeMessage.MsgData> templateDataList = new ArrayList<>(data.size());
data.forEach((k, v) -> templateDataList.add(new WxMaSubscribeMessage.MsgData(k, v)));
return templateDataList;
} }
@Override @Override
public void recall(MessageTemplate messageTemplate) { public void recall(MessageTemplate messageTemplate) {

@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@ -28,6 +29,9 @@ import java.util.Set;
@Slf4j @Slf4j
public class OfficialAccountHandler extends BaseHandler implements Handler { public class OfficialAccountHandler extends BaseHandler implements Handler {
@Autowired
private WxServiceUtils wxServiceUtils;
public OfficialAccountHandler() { public OfficialAccountHandler() {
channelCode = ChannelType.OFFICIAL_ACCOUNT.getCode(); channelCode = ChannelType.OFFICIAL_ACCOUNT.getCode();
} }
@ -36,7 +40,7 @@ public class OfficialAccountHandler extends BaseHandler implements Handler {
public boolean handler(TaskInfo taskInfo) { public boolean handler(TaskInfo taskInfo) {
try { try {
OfficialAccountsContentModel contentModel = (OfficialAccountsContentModel) taskInfo.getContentModel(); OfficialAccountsContentModel contentModel = (OfficialAccountsContentModel) taskInfo.getContentModel();
WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(taskInfo.getSendAccount().longValue()); WxMpService wxMpService = wxServiceUtils.getOfficialAccountServiceMap().get(taskInfo.getSendAccount().longValue());
List<WxMpTemplateMessage> messages = assembleReq(taskInfo.getReceiver(), contentModel); List<WxMpTemplateMessage> messages = assembleReq(taskInfo.getReceiver(), contentModel);
for (WxMpTemplateMessage message : messages) { for (WxMpTemplateMessage message : messages) {
try { try {

@ -41,6 +41,7 @@ public class SmsPullReceiptStarterImpl implements ReceiptMessageStater {
/** /**
* *
*/ */
@Override
public void start() { public void start() {
try { try {
List<ChannelAccount> channelAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.SMS.getCode()); List<ChannelAccount> channelAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.SMS.getCode());

@ -1,19 +0,0 @@
package com.java3y.austin.handler.wechat;
import com.java3y.austin.handler.domain.wechat.WeChatMiniProgramParam;
/**
* @author sunql
*/
public interface MiniProgramAccountService {
/**
*
*
* @param miniProgramParam
* @return
* @throws Exception
*/
void send(WeChatMiniProgramParam miniProgramParam) throws Exception;
}

@ -1,91 +0,0 @@
package com.java3y.austin.handler.wechat.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaSubscribeServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.WeChatMiniProgramAccount;
import com.java3y.austin.handler.domain.wechat.WeChatMiniProgramParam;
import com.java3y.austin.handler.wechat.MiniProgramAccountService;
import com.java3y.austin.support.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author sunql
* @date 20220506 16:41
*/
@Service
@Slf4j
public class MiniProgramAccountServiceImpl implements MiniProgramAccountService {
@Autowired
private AccountUtils accountUtils;
@Override
public void send(WeChatMiniProgramParam miniProgramParam) throws Exception {
WeChatMiniProgramAccount miniProgramAccount = accountUtils.getAccountById(miniProgramParam.getSendAccount(),
WeChatMiniProgramAccount.class);
WxMaSubscribeService wxMaSubscribeService = initService(miniProgramAccount);
List<WxMaSubscribeMessage> subscribeMessageList = assembleReq(miniProgramParam, miniProgramAccount);
for (WxMaSubscribeMessage subscribeMessage : subscribeMessageList) {
wxMaSubscribeService.sendSubscribeMsg(subscribeMessage);
}
}
/**
*
*/
private List<WxMaSubscribeMessage> assembleReq(WeChatMiniProgramParam miniProgramParam, WeChatMiniProgramAccount miniProgramAccount) {
Set<String> receiver = miniProgramParam.getOpenIds();
List<WxMaSubscribeMessage> messageList = new ArrayList<>(receiver.size());
// 构建微信小程序订阅消息
for (String openId : receiver) {
WxMaSubscribeMessage subscribeMessage = WxMaSubscribeMessage.builder()
.toUser(openId)
.data(getWxMTemplateData(miniProgramParam.getData()))
.miniprogramState(miniProgramAccount.getMiniProgramState())
.templateId(miniProgramAccount.getTemplateId())
.page(miniProgramAccount.getPage())
.build();
messageList.add(subscribeMessage);
}
return messageList;
}
/**
*
*
* @returnp
*/
private List<WxMaSubscribeMessage.MsgData> getWxMTemplateData(Map<String, String> data) {
List<WxMaSubscribeMessage.MsgData> templateDataList = new ArrayList<>(data.size());
data.forEach((k, v) -> templateDataList.add(new WxMaSubscribeMessage.MsgData(k, v)));
return templateDataList;
}
/**
*
*
* @return
*/
private WxMaSubscribeServiceImpl initService(WeChatMiniProgramAccount miniProgramAccount) {
WxMaService wxMaService = new WxMaServiceImpl();
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
wxMaConfig.setAppid(miniProgramAccount.getAppId());
wxMaConfig.setSecret(miniProgramAccount.getAppSecret());
wxMaService.setWxMaConfig(wxMaConfig);
return new WxMaSubscribeServiceImpl(wxMaService);
}
}

@ -11,7 +11,7 @@ import org.springframework.stereotype.Service;
/** /**
* @Autor xzcawl * @author xzcawl
* @Date 2022/7/15 17:29 * @Date 2022/7/15 17:29
*/ */
@Slf4j @Slf4j

@ -1,12 +1,19 @@
package com.java3y.austin.support.utils; package com.java3y.austin.support.utils;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaSubscribeServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.java3y.austin.common.constant.CommonConstant; import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.dto.account.WeChatMiniProgramAccount;
import com.java3y.austin.common.dto.account.WeChatOfficialAccount; import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
import com.java3y.austin.common.enums.ChannelType; import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.support.dao.ChannelAccountDao; import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount; import com.java3y.austin.support.domain.ChannelAccount;
import lombok.Data;
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;
@ -26,21 +33,40 @@ import java.util.Map;
*/ */
@Component @Component
@Slf4j @Slf4j
@Data
public class WxServiceUtils { public class WxServiceUtils {
public static Map<Long, WxMpService> wxMpServiceMap = new HashMap<>(); private Map<Long, WxMpService> officialAccountServiceMap = new HashMap<>();
public static Map<Long, WeChatOfficialAccount> accountHashMap = new HashMap<>(); private Map<Long, WxMaSubscribeService> miniProgramServiceMap = new HashMap<>();
private Map<Long, WeChatOfficialAccount> officialAccountHashMap = new HashMap<>();
private Map<Long, WeChatMiniProgramAccount> miniProgramHashMap = new HashMap<>();
@Autowired @Autowired
private ChannelAccountDao channelAccountDao; private ChannelAccountDao channelAccountDao;
@PostConstruct @PostConstruct
public void init() { public void init() {
initOfficialAccount();
initMiniProgram();
}
private void initMiniProgram() {
List<ChannelAccount> miniProgram = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.MINI_PROGRAM.getCode());
for (ChannelAccount channelAccount : miniProgram) {
WeChatMiniProgramAccount weChatMiniProgramAccount = JSON.parseObject(channelAccount.getAccountConfig(), WeChatMiniProgramAccount.class);
miniProgramServiceMap.put(channelAccount.getId(), initMiniProgramService(weChatMiniProgramAccount));
miniProgramHashMap.put(channelAccount.getId(), weChatMiniProgramAccount);
}
}
private void initOfficialAccount() {
List<ChannelAccount> officialAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.OFFICIAL_ACCOUNT.getCode()); List<ChannelAccount> officialAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.OFFICIAL_ACCOUNT.getCode());
for (ChannelAccount channelAccount : officialAccountList) { for (ChannelAccount channelAccount : officialAccountList) {
WeChatOfficialAccount weChatOfficialAccount = JSON.parseObject(channelAccount.getAccountConfig(), WeChatOfficialAccount.class); WeChatOfficialAccount weChatOfficialAccount = JSON.parseObject(channelAccount.getAccountConfig(), WeChatOfficialAccount.class);
wxMpServiceMap.put(channelAccount.getId(), initService(weChatOfficialAccount)); officialAccountServiceMap.put(channelAccount.getId(), initOfficialAccountService(weChatOfficialAccount));
accountHashMap.put(channelAccount.getId(), weChatOfficialAccount); officialAccountHashMap.put(channelAccount.getId(), weChatOfficialAccount);
} }
} }
@ -49,7 +75,7 @@ public class WxServiceUtils {
* *
* @return * @return
*/ */
public WxMpService initService(WeChatOfficialAccount officialAccount) { public WxMpService initOfficialAccountService(WeChatOfficialAccount officialAccount) {
WxMpService wxMpService = new WxMpServiceImpl(); WxMpService wxMpService = new WxMpServiceImpl();
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
config.setAppId(officialAccount.getAppId()); config.setAppId(officialAccount.getAppId());
@ -57,4 +83,18 @@ public class WxServiceUtils {
wxMpService.setWxMpConfigStorage(config); wxMpService.setWxMpConfigStorage(config);
return wxMpService; return wxMpService;
} }
/**
*
*
* @return
*/
private WxMaSubscribeServiceImpl initMiniProgramService(WeChatMiniProgramAccount miniProgramAccount) {
WxMaService wxMaService = new WxMaServiceImpl();
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
wxMaConfig.setAppid(miniProgramAccount.getAppId());
wxMaConfig.setSecret(miniProgramAccount.getAppSecret());
wxMaService.setWxMaConfig(wxMaConfig);
return new WxMaSubscribeServiceImpl(wxMaService);
}
} }

@ -0,0 +1,79 @@
package com.java3y.austin.web.controller;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
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.utils.Convert4Amis;
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.common.bean.subscribemsg.TemplateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author 3y
*/
@Slf4j
@RestController
@RequestMapping("/miniProgram")
@Api("微信服务号")
@CrossOrigin(origins = {AustinConstant.ORIGIN_VALUE}, allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.PUT, RequestMethod.POST, RequestMethod.GET})
public class MiniProgramController {
@Autowired
private WxServiceUtils wxServiceUtils;
@GetMapping("/template/list")
@ApiOperation("/根据账号Id获取模板列表")
public BasicResultVO queryList(Long id) {
try {
List<CommonAmisVo> result = new ArrayList<>();
WxMaSubscribeService wxMaSubscribeService = wxServiceUtils.getMiniProgramServiceMap().get(id);
List<TemplateInfo> templateList = wxMaSubscribeService.getTemplateList();
for (TemplateInfo templateInfo : templateList) {
CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(templateInfo.getTitle()).value(templateInfo.getPriTmplId()).build();
result.add(commonAmisVo);
}
return BasicResultVO.success(result);
} catch (Exception e) {
log.error("MiniProgramController#queryList fail:{}", Throwables.getStackTraceAsString(e));
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR);
}
}
/**
* IdID
*
* @return
*/
@PostMapping("/detailTemplate")
@ApiOperation("/根据账号Id和模板ID获取模板列表")
public BasicResultVO queryDetailList(Long id, String wxTemplateId) {
if (id == null || wxTemplateId == null) {
return BasicResultVO.success(RespStatusEnum.CLIENT_BAD_PARAMETERS);
}
try {
WxMaSubscribeService wxMaSubscribeService = wxServiceUtils.getMiniProgramServiceMap().get(id);
List<TemplateInfo> templateList = wxMaSubscribeService.getTemplateList();
CommonAmisVo wxMpTemplateParam = Convert4Amis.getWxMaTemplateParam(wxTemplateId, templateList);
return BasicResultVO.success(wxMpTemplateParam);
} catch (Exception e) {
log.error("MiniProgramController#queryDetailList fail:{}", Throwables.getStackTraceAsString(e));
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR);
}
}
}

@ -13,6 +13,7 @@ import io.swagger.annotations.ApiOperation;
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.bean.template.WxMpTemplate; import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,6 +32,8 @@ import java.util.List;
, allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.PUT, RequestMethod.POST, RequestMethod.GET}) , allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.PUT, RequestMethod.POST, RequestMethod.GET})
public class OfficialAccountController { public class OfficialAccountController {
@Autowired
private WxServiceUtils wxServiceUtils;
/** /**
* @param id Id * @param id Id
@ -41,8 +44,8 @@ public class OfficialAccountController {
public BasicResultVO queryList(Long id) { public BasicResultVO queryList(Long id) {
try { try {
List<CommonAmisVo> result = new ArrayList<>(); List<CommonAmisVo> result = new ArrayList<>();
WxMpService wxMpService = wxServiceUtils.getOfficialAccountServiceMap().get(id);
WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(id);
List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate(); List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
for (WxMpTemplate wxMpTemplate : allPrivateTemplate) { for (WxMpTemplate wxMpTemplate : allPrivateTemplate) {
CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(wxMpTemplate.getTitle()).value(wxMpTemplate.getTemplateId()).build(); CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(wxMpTemplate.getTitle()).value(wxMpTemplate.getTemplateId()).build();
@ -60,7 +63,6 @@ public class OfficialAccountController {
* IdID * IdID
* *
* @return * @return
* @
*/ */
@PostMapping("/detailTemplate") @PostMapping("/detailTemplate")
@ApiOperation("/根据账号Id和模板ID获取模板列表") @ApiOperation("/根据账号Id和模板ID获取模板列表")
@ -69,16 +71,15 @@ public class OfficialAccountController {
return BasicResultVO.success(RespStatusEnum.CLIENT_BAD_PARAMETERS); return BasicResultVO.success(RespStatusEnum.CLIENT_BAD_PARAMETERS);
} }
try { try {
WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(id); WxMpService wxMpService = wxServiceUtils.getOfficialAccountServiceMap().get(id);
List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate(); List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
CommonAmisVo wxMpTemplateParam = Convert4Amis.getWxMpTemplateParam(wxTemplateId, allPrivateTemplate); CommonAmisVo wxMpTemplateParam = Convert4Amis.getWxMpTemplateParam(wxTemplateId, allPrivateTemplate);
return BasicResultVO.success(wxMpTemplateParam); return BasicResultVO.success(wxMpTemplateParam);
} catch (Exception e) { } catch (Exception e) {
log.error("OfficialAccountController#queryList fail:{}", Throwables.getStackTraceAsString(e)); log.error("OfficialAccountController#queryDetailList fail:{}", Throwables.getStackTraceAsString(e));
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR); return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR);
} }
} }
} }

@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.java3y.austin.web.vo.amis.CommonAmisVo; import com.java3y.austin.web.vo.amis.CommonAmisVo;
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate; import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -24,12 +25,19 @@ import java.util.stream.Collectors;
*/ */
public class Convert4Amis { public class Convert4Amis {
// 标识忽略 /**
public static final int ignoreTg = 0; *
// 标识已读取到'$'字符 */
public static final int startTg = 1; public static final int IGNORE_TG = 0;
// 标识已读取到'{'字符 /**
public static final int readTg = 2; * '$'
*/
public static final int START_TG = 1;
/**
* '{'
*/
public static final int READ_TG = 2;
/** /**
* (json * (json
* () * ()
@ -45,7 +53,7 @@ public class Convert4Amis {
/** /**
* (JSONObjectJSONArray) * (JSONObjectJSONArray)
*/ */
private static final List<String> PARSE_JSON_OBJ_TO_ARRAY = Arrays.asList("officialAccountParam"); private static final List<String> PARSE_JSON_OBJ_TO_ARRAY = Arrays.asList("officialAccountParam","miniProgramParam");
/** /**
* OA * OA
@ -128,6 +136,7 @@ public class Convert4Amis {
/** /**
* amis * amis
* *
*
* @param wxTemplateId * @param wxTemplateId
* @param allPrivateTemplate * @param allPrivateTemplate
* @return * @return
@ -209,31 +218,31 @@ public class Convert4Amis {
// 存储占位符 位置信息集合 // 存储占位符 位置信息集合
List<String> placeholderList = new ArrayList<>(); List<String> placeholderList = new ArrayList<>();
// 当前标识 // 当前标识
int modeTg = ignoreTg; int modeTg = IGNORE_TG;
for (int m = 0; m < textChars.length; m++) { for (int m = 0; m < textChars.length; m++) {
char c = textChars[m]; char c = textChars[m];
textSofar.append(c); textSofar.append(c);
switch (c) { switch (c) {
case '{': { case '{': {
modeTg = startTg; modeTg = START_TG;
sb.append(c); sb.append(c);
} }
break; break;
case '$': { case '$': {
if (modeTg == startTg) { if (modeTg == START_TG) {
sb.append(c); sb.append(c);
modeTg = readTg; modeTg = READ_TG;
} else { } else {
if (modeTg == readTg) { if (modeTg == READ_TG) {
sb = new StringBuilder(); sb = new StringBuilder();
modeTg = ignoreTg; modeTg = IGNORE_TG;
} }
} }
} }
break; break;
case '}': { case '}': {
if (modeTg == readTg) { if (modeTg == READ_TG) {
modeTg = ignoreTg; modeTg = IGNORE_TG;
sb.append(c); sb.append(c);
String str = sb.toString(); String str = sb.toString();
if (StrUtil.isNotEmpty(str)) { if (StrUtil.isNotEmpty(str)) {
@ -241,16 +250,17 @@ public class Convert4Amis {
textSofar = new StringBuilder(); textSofar = new StringBuilder();
} }
sb = new StringBuilder(); sb = new StringBuilder();
} else if (modeTg == startTg) { } else if (modeTg == START_TG) {
modeTg = ignoreTg; modeTg = IGNORE_TG;
sb = new StringBuilder(); sb = new StringBuilder();
} }
break;
} }
default: { default: {
if (modeTg == readTg) { if (modeTg == READ_TG) {
sb.append(c); sb.append(c);
} else if (modeTg == startTg) { } else if (modeTg == START_TG) {
modeTg = ignoreTg; modeTg = IGNORE_TG;
sb = new StringBuilder(); sb = new StringBuilder();
} }
} }
@ -260,4 +270,39 @@ public class Convert4Amis {
return result; return result;
} }
/**
* amis
*
*
* @param wxTemplateId
* @param templateList
* @return
*/
public static CommonAmisVo getWxMaTemplateParam(String wxTemplateId, List<TemplateInfo> templateList) {
CommonAmisVo officialAccountParam = null;
for (TemplateInfo templateInfo : templateList) {
if (wxTemplateId.equals(templateInfo.getPriTmplId())) {
String[] data = templateInfo.getContent().split(StrUtil.LF);
officialAccountParam = CommonAmisVo.builder()
.type("input-table")
.name("miniProgramParam")
.addable(true)
.editable(true)
.needConfirm(false)
.build();
List<CommonAmisVo.ColumnsDTO> columnsDTOS = new ArrayList<>();
for (String datum : data) {
String name = datum.substring(datum.indexOf("{{") + 2, datum.indexOf("."));
String label = datum.split(":")[0];
CommonAmisVo.ColumnsDTO columnsDTO = CommonAmisVo.ColumnsDTO.builder()
.name(name).type("input-text").required(true).quickEdit(true).label(label).build();
columnsDTOS.add(columnsDTO);
}
officialAccountParam.setColumns(columnsDTOS);
}
}
return officialAccountParam;
}
} }

@ -11,6 +11,8 @@ import java.util.List;
/** /**
* amis * amis
*
* @author 3y
*/ */
@Data @Data
@Builder @Builder

Loading…
Cancel
Save