微信公众号推送消息

推送的时候出现错误:
me.chanjar.weixin.common.error.WxErrorException: {“errcode”:40029,“errmsg”:"invalid code, hints: [ r
原因是:对应使用的微信公众号的appid和appsecret的配置信息不是当前访问的公众号的appid和appsecret。

等到appid和appsecret都配对上了还是报这个错,原因是没有添加白名单

代码:

微信公众号推送消息_第1张图片

<!--微信模版消息推送三方sdk-->
	  <dependency>
		  <groupId>com.github.binarywang</groupId>
		  <artifactId>weixin-java-mp</artifactId>
		  <version>3.3.0</version>
	  </dependency>
package com.fcar.kj.manager.bsj.wxPush;

import com.fcar.kj.bsj.common.def.wxPushVO.OrderGenerationVO;
import com.fcar.kj.bsj.common.def.wxPushVO.OrderReminderVO;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 微信推送管理
 *
 * @author 李晨亮
 * @date 2020-08-18 15:09
 **/
@Component
@Slf4j
public class WxPushMessageUtil {

    /**
     * 订单生成推送消息
     *
     * @param orderGenerationVO 推送的消息
     * @param openId            微信
     */
    @GetMapping("/push")
    public void push(OrderGenerationVO orderGenerationVO, String openId) {
        //1,配置
        WxMpService wxMpService = new WxMpServiceImpl();
        getAppConfig(wxMpService);

        //2,推送消息
        //要推送的用户openid
        //模版id
        // .url("https://30paotui.com/")//点击模版消息要访问的网址
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId("模板id")
                .url(orderGenerationVO.getUrl())
                .build();
        templateMessage.addData(new WxMpTemplateData("first", orderGenerationVO.getOrderDetails()));
        templateMessage.addData(new WxMpTemplateData("keyword1", orderGenerationVO.getOrderTime()));
        templateMessage.addData(new WxMpTemplateData("keyword2", orderGenerationVO.getTradeName()));
        templateMessage.addData(new WxMpTemplateData("keyword3", orderGenerationVO.getOrderNumber()));
        templateMessage.addData(new WxMpTemplateData("remark", orderGenerationVO.getRemark()));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败:" + e.getMessage());
            e.printStackTrace();
        }

    }

    /**
     * 新订单提醒推送
     *
     * @param orderReminderVO 推送的消息
     * @param openId          微信
     */
    @GetMapping("/push")
    public void push(OrderReminderVO orderReminderVO, String openId) {
        //1,配置
        WxMpService wxMpService = new WxMpServiceImpl();
        getAppConfig(wxMpService);

        //推送消息(“remark”,推送的字段要看温馨模板对应的字段才能显示)
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId("模板id")
                .url(orderReminderVO.getUrl())
                .build();
        templateMessage.addData(new WxMpTemplateData("remark", orderReminderVO.getRemark()));
        templateMessage.addData(new WxMpTemplateData("first", orderReminderVO.getOrderDetails()));
        templateMessage.addData(new WxMpTemplateData("keyword1", orderReminderVO.getOrderType()));
        templateMessage.addData(new WxMpTemplateData("keyword2", orderReminderVO.getPeople()));
        templateMessage.addData(new WxMpTemplateData("keyword3", orderReminderVO.getPhone()));
        templateMessage.addData(new WxMpTemplateData("keyword4", orderReminderVO.getCustomerPhone()));
        templateMessage.addData(new WxMpTemplateData("keyword5", orderReminderVO.getUrl()));

        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败:" + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 获取公众号配置
     *
     * @param wxMpService
     */
    public static void getAppConfig(WxMpService wxMpService) {
    //AppId、Secret 公众号的配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx245fa386e0b34a02");
        wxStorage.setSecret("1243214324");
        wxStorage.setToken("wxszkj886");
        wxMpService.setWxMpConfigStorage(wxStorage);
    }

}

你可能感兴趣的:(微信公众号推送消息)