parent
a72971bd5b
commit
6a9bce2955
@ -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 2022年05月06日 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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据账号Id和模板ID获取模板列表
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue