diff --git a/austin-handler/pom.xml b/austin-handler/pom.xml index eb6ef31..94ca8bb 100644 --- a/austin-handler/pom.xml +++ b/austin-handler/pom.xml @@ -41,16 +41,7 @@ com.sun.mail javax.mail - - - com.github.binarywang - weixin-java-mp - - - - com.github.binarywang - weixin-java-miniapp - + diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/wechat/impl/OfficialAccountServiceImpl.java b/austin-handler/src/main/java/com/java3y/austin/handler/wechat/impl/OfficialAccountServiceImpl.java index a4359d2..6ce3703 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/wechat/impl/OfficialAccountServiceImpl.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/wechat/impl/OfficialAccountServiceImpl.java @@ -1,10 +1,10 @@ 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.handler.domain.wechat.WeChatOfficialParam; import com.java3y.austin.handler.wechat.OfficialAccountService; import com.java3y.austin.support.utils.AccountUtils; +import com.java3y.austin.support.utils.WxServiceUtils; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; @@ -31,8 +31,8 @@ public class OfficialAccountServiceImpl implements OfficialAccountService { @Override public List send(WeChatOfficialParam officialParam) throws Exception { - WeChatOfficialAccount officialAccount = accountUtils.getAccountById(officialParam.getSendAccount(),WeChatOfficialAccount.class); - WxMpService wxMpService = initService(officialAccount); + WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(officialParam.getSendAccount()); + WeChatOfficialAccount officialAccount = WxServiceUtils.accountHashMap.get(officialParam.getSendAccount()); List messages = assembleReq(officialParam, officialAccount); List messageIds = new ArrayList<>(messages.size()); for (WxMpTemplateMessage wxMpTemplateMessage : messages) { diff --git a/austin-support/pom.xml b/austin-support/pom.xml index f6dfb2b..0a20790 100644 --- a/austin-support/pom.xml +++ b/austin-support/pom.xml @@ -94,21 +94,31 @@ dynamic-tp-spring-boot-starter-apollo - - com.aliyun - alibaba-dingtalk-service-sdk - org.springframework.amqp spring-rabbit - com.alibaba.boot nacos-config-spring-boot-starter + + com.aliyun + alibaba-dingtalk-service-sdk + + + + com.github.binarywang + weixin-java-mp + + + + com.github.binarywang + weixin-java-miniapp + + \ No newline at end of file diff --git a/austin-support/src/main/java/com/java3y/austin/support/utils/WxServiceUtils.java b/austin-support/src/main/java/com/java3y/austin/support/utils/WxServiceUtils.java new file mode 100644 index 0000000..cc39aba --- /dev/null +++ b/austin-support/src/main/java/com/java3y/austin/support/utils/WxServiceUtils.java @@ -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 wxMpServiceMap = new HashMap<>(); + public static Map accountHashMap = new HashMap<>(); + + @Autowired + private ChannelAccountDao channelAccountDao; + + @PostConstruct + public void init() { + List 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; + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java index f1f5e0e..c6c393d 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java @@ -6,6 +6,7 @@ import com.java3y.austin.common.constant.AustinConstant; import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.support.domain.ChannelAccount; import com.java3y.austin.web.service.ChannelAccountService; +import com.java3y.austin.web.vo.amis.CommonAmisVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @@ -47,14 +48,12 @@ public class ChannelAccountController { @GetMapping("/queryByChannelType") @ApiOperation("/根据渠道标识查询相关的记录") public BasicResultVO query(Integer channelType) { - List channelAccounts = channelAccountService.queryByChannelType(channelType); + List result = new ArrayList<>(); - List> result = new ArrayList<>(); + List channelAccounts = channelAccountService.queryByChannelType(channelType); for (ChannelAccount channelAccount : channelAccounts) { - HashMap optionKV = new HashMap<>(); - optionKV.put("label", channelAccount.getName()); - optionKV.put("value", String.valueOf(channelAccount.getId())); - result.add(optionKV); + CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(channelAccount.getName()).value(String.valueOf(channelAccount.getId())).build(); + result.add(commonAmisVo); } return BasicResultVO.success(result); } diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java new file mode 100644 index 0000000..2fbef1d --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java @@ -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 result = new ArrayList<>(); + + WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(id); + List 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); + } + } + + + /** + * 根据账号Id和模板ID获取模板列表 + * + * @return + * @ + */ + @GetMapping("/template/detailTemplate") + @ApiOperation("/根据账号Id和模板ID获取模板列表") + public BasicResultVO queryDetailList(Long id, String wxTemplateId) { + try { + List result = new ArrayList<>(); + + WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(id); + List 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); + } + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/vo/amis/CommonAmisVo.java b/austin-web/src/main/java/com/java3y/austin/web/vo/amis/CommonAmisVo.java new file mode 100644 index 0000000..fe89b17 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/vo/amis/CommonAmisVo.java @@ -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; +}