栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微信公众号跳转小程序失败 new WxMpTemplateMessage.MiniProgram

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信公众号跳转小程序失败 new WxMpTemplateMessage.MiniProgram

公司去年营收不太好,最近开始疯狂裁员,近一个月大概裁了6-8个前端,4-5个产品。(开发一共也就60~70人)
公司福利待遇本来就不好,过年后又开始压榨员工了,晚上九点下班、周六加班成为常有的事了。
同班同学搞安卓,11K,965不加班,对比起来就emo了。。还遇到个3-5年的脑残前端,对个接口都对不好,态度极其恶劣。也就随便吐槽一点,多学习,争取早点跳槽吧。

回到正题

项目中有一个微信消息推送的功能。
微信开发文档-模板消息
使用的是GitHub上大神封装的SDK

遇到的问题1:微信公众号的accessToken有效期为7200秒,一天只能获取2000次,项目中并没有引入redis,那么如果避免频繁获取accessToken? 解决办法:将配置信息存储在WxMpConfigStorage类中,再注入到Spring。

官方示例:

@Test(invocationCount = 5, threadPoolSize = 3)
  public void testSendTemplateMsg() throws WxErrorException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    TestConfigStorage configStorage = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
    WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
      .toUser(configStorage.getOpenid())
      .templateId(configStorage.getTemplateId())
      .url(" ")
      .build();

    templateMessage.addData(new WxMpTemplateData("first", dateFormat.format(new Date()), "#FF00FF"))
      .addData(new WxMpTemplateData("remark", RandomStringUtils.randomAlphanumeric(100), "#FF00FF"));
    String msgId = this.wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
    Assert.assertNotNull(msgId);
    System.out.println(msgId);
  }

自己的代码实现:

@Component
public class WechatMpConfig {
	// WechatAccountConfig实际上是一个配置信息类
	// 主要作用是从nacos上读取配置信息
    @Autowired
    private WechatAccountConfig wxAccountConfig;

    
    @Bean
    public WxMpService wxMpService(WxMpConfigStorage wxConfigProvider) {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxConfigProvider);
        return wxMpService;
    }

    
    @Bean
    public WxMpConfigStorage wxConfigProvider() {
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        // 公众号的appId
        wxMpConfigStorage.setAppId(wxAccountConfig.getAppId());
        // 公众号appSecret
        wxMpConfigStorage.setSecret(wxAccountConfig.getSecret());
        // 公众号Token
        wxMpConfigStorage.setToken(wxAccountConfig.getToken());
        return wxMpConfigStorage;
    }
}

发送消息时

    @Resource
    private WxMpService wxMpService;
    // 发送消息的配置信息、内容自行替换掉
    public void send(){
        WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder().toUser(openId).templateId(templateId).miniProgram(new WxMpTemplateMessage.MiniProgram(basicConfig.getMiniWxAppId(), "pages/index/auth?push=" + urlEncode, false)).build();
        wxMpTemplateMessage.addData(new WxMpTemplateData("first", "尊敬的家长,您的孩子有了新的家校成绩", "#EC320")).addData(new WxMpTemplateData("keyword1", achievementMsgDTO.getClassName(), "#272A6C")).addData(new WxMpTemplateData("keyword2", achievementMsgDTO.getTestName(), "#272A6C")).addData(new WxMpTemplateData("remark", "xxxx", "#EC320"));
        // 直接通过wxMpService来发送消息,无需关注accessToken
        String result = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
    }    
问题2:微信公众号发送消息模板,报40013错误。如果不附带小程序的参数是正常推送的。附带了小程序参数的话,说是appid错误。(检查后确认小程序的appid是无误的)。 解决办法:检查公众号和小程序是否进行关联

当时Google出的解决办法是,到微信公众号后台检查公众号是否关联了小程序。我到微信公众号后台检查了,并且确定了关联了小程序。最后和产品确定了才发现,原来没有关联。(因为我们有2个小程序,名字和图标都是十分相似的。要和产品即时沟通!!)

问题3:消息模板附带小程序参数确实是可以发送了,但是无法跳转到小程序的指定页面。 解决办法:

官方示例:

public class WxMpTemplateMessageTest {
  @Test
  public void testToJson() {
    WxMpTemplateMessage tm = WxMpTemplateMessage.builder()
      .toUser("OPENID")
      .templateId("ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY")
      .miniProgram(new WxMpTemplateMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar",true))
      .url("http://weixin.qq.com/download")
      .build();

    tm.addData(
      new WxMpTemplateData("first", "haahah", "#FF00FF"));
    tm.addData(
      new WxMpTemplateData("remark", "heihei", "#FF00FF"));

    assertEquals(tm.toJson(), "{"touser":"OPENID","template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY","url":"http://weixin.qq.com/download","miniprogram":{"appid":"xiaochengxuappid12345","pagepath":"index?foo=bar"},"data":{"first":{"value":"haahah","color":"#FF00FF"},"remark":{"value":"heihei","color":"#FF00FF"}}}");
  }

}

我也的确是按照官方示例进行编写的啊,new WxMpTemplateMessage.MiniProgram中也设为true了。但是就是无法跳转到指定页面,点开MiniProgram的源码,发现了猫腻

这个usePath并不是代表是否开启页面跳转,而是代表发送给微信时,参数名称的不同!!官方中使用的时

可能是我的不细心导致遇到那么多问题吧。。最近也被老大刁的有点多。。sad。。。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755175.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号