微信小程序-Java实现订阅消息推送-基于SDK

孙凌
2023-12-01

SDK-gitee地址
SDK-github地址
阅读文档可知微信小程序所使用的sdk是weixin-java-miniapp
依赖如下

<dependency>
  <groupId>com.github.binarywang</groupId>
  <artifactId>weixin-java-miniapp</artifactId>
  <version>4.2.0</version>
</dependency>
wechat:
  appId: wxcbe3c54cjfkfsd4yfhe5
  appSecret: 3be9381e8ce370w0b0ailfc6120
  templateId:
    applyStatus: e-CqI4YdFaq671iRiqawEQxD6YNvW_7pc7LyEMb-yeQ
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatConfig {
    private String appId;

    private String appSecret;

    /**
     * 微信模版id
     */
    private Map<String, String> templateId;
}
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Configuration
public class WeChatMpConfig {

    @Resource
    private WechatConfig wechatConfig;
	
	// 设置wxMaService的实现类并交由Spring托管
    @Bean
    public WxMaService wxMaService(){
        WxMaServiceImpl wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaConfigMemory());
        return wxMaService;
    }

    @Bean
    public WxMaConfig wxMaConfigMemory(){
        WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
        wxMaConfig.setAppid(wechatConfig.getAppId());
        wxMaConfig.setSecret(wechatConfig.getAppSecret());
        return wxMaConfig;
    }
}
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.mkxcloud.xyd.config.WechatConfig;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WeChatService {
    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private WechatConfig wechatConfig;

    /**
     * 预约状态变更消息
     */
    public void applyStatus(){
        List<WxMaSubscribeMessage.MsgData> data = Arrays.asList(
                new WxMaSubscribeMessage.MsgData("time16","2020-08-20 13:00"),
                new WxMaSubscribeMessage.MsgData("thing34", "Q203"),
                new WxMaSubscribeMessage.MsgData("phone_number4", "18868812345"),
                new WxMaSubscribeMessage.MsgData("thing10", "123")
        );
        // 根据openid,模板id,以及封装好的模板数据即可发送消息
        WxMaSubscribeMessage build = WxMaSubscribeMessage.builder()
                .toUser("ocMijFYzZv5JtltRlqiRqbz5t70U")
                .templateId(wechatConfig.getTemplateId().get("applyStatus"))
                .data(data)
                .build();
        try {
            wxMaService.getMsgService().sendSubscribeMsg(build);
        } catch (WxErrorException e) {
            log.error("【微信订阅消息】发送失败, {}", e);
        }
    }
}
import com.mkxcloud.framework.common.result.Result;
import com.mkxcloud.xyd.service.WeChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wechat")
public class WeChatController {
    @Autowired
    private WeChatService weChatService;

    @PostMapping("send_msg")
    public Result sendTemplateMsg()  {
        weChatService.applyStatus();
        return Result.success();
    }

}

前端仅需要用户授权即可wx.requestSubscribeMessage官方文档

wx.requestSubscribeMessage({
  tmplIds: [''],
  success (res) { }
})
 类似资料: