欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信源代码在哪里看(微信小程序代码大全)

程序员文章站 2023-11-20 23:20:04
我们在做小程序开发时,消息推送是不可避免的。今天就来教大家如何实现小程序消息推送的后台和前台开发。源码会在文章末尾贴出来。其实我之前有写过一篇:《springboot实现微信消息推送,java实现小程...

我们在做小程序开发时,消息推送是不可避免的。今天就来教大家如何实现小程序消息推送的后台和前台开发。源码会在文章末尾贴出来。

其实我之前有写过一篇:《springboot实现微信消息推送,java实现小程序推送,含小程序端实现代码》 但是有同学反应这篇文章里的代码太繁琐,接入也比较麻烦。今天就来给大家写个精简版的,基本上只需要几行代码,就能实现小程序模版消息推送功能。

老规矩先看效果图

微信源代码在哪里看(微信小程序代码大全)

这是我们最终推送给用户的模版消息。这是用户手机微信上显示的推送消息截图。

本节知识点

1,java开发推送后台

2,springboot实现推送功能

3,小程序获取用户openid

4,小程序获取fromid用来推送

先来看后台推送功能的实现

只有下面一个简单的pushcontroller类,就可以实现小程序消息的推送

微信源代码在哪里看(微信小程序代码大全)

再来看下pushcontroller类,你没看错,实现小程序消息推送,就需要下面这几行代码就可以实现了。

微信源代码在哪里看(微信小程序代码大全)

由于本推送代码是用springboot来实现的,下面就来简单的讲下。我我们需要注意的几点内容。

1,需要在pom.xml引入一个三方类库(推送的三方类库)

微信源代码在哪里看(微信小程序代码大全)

pom.xml的完整代码如下

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <parent>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-parent</artifactid>
 <version>2.1.5.release</version>
 <relativepath/> <!-- lookup parent from repository -->
 </parent>
 <groupid>com.qcl</groupid>
 <artifactid>wxapppush</artifactid>
 <version>0.0.1-snapshot</version>
 <name>wxapppush</name>
 <description>demo project for spring boot</description>
 <properties>
 <java.version>1.8</java.version>
 </properties>
 <dependencies>
 <dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-web</artifactid>
 </dependency>
 <dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-test</artifactid>
 <scope>test</scope>
 </dependency>
 <!--微信小程序模版推送-->
 <dependency>
 <groupid>com.github.binarywang</groupid>
 <artifactid>weixin-java-miniapp</artifactid>
 <version>3.4.0</version>
 </dependency>
 </dependencies>
 <build>
 <plugins>
 <plugin>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-maven-plugin</artifactid>
 </plugin>
 </plugins>
 </build>
</project>

其实到这里我们java后台的推送功能,就已经实现了。我们只需要运行springboot项目,就可以实现推送了。

下面贴出完整的pushcontroller.java类。里面注释很详细了。

package com.qcl.wxapppush;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.restcontroller;
import java.util.arraylist;
import java.util.list;
import cn.binarywang.wx.miniapp.api.wxmaservice;
import cn.binarywang.wx.miniapp.api.impl.wxmaserviceimpl;
import cn.binarywang.wx.miniapp.bean.wxmatemplatedata;
import cn.binarywang.wx.miniapp.bean.wxmatemplatemessage;
import cn.binarywang.wx.miniapp.config.wxmainmemoryconfig;
import me.chanjar.weixin.common.error.wxerrorexception;
/**
 * created by qcl on 2019-05-20
 * :2501902696
 * desc: 微信小程序模版推送实现
 */
@restcontroller
public class pushcontroller {
 @getmapping("/push")
 public string push(@requestparam string openid, @requestparam string formid) {
 //1,配置小程序信息
 wxmainmemoryconfig wxconfig = new wxmainmemoryconfig();
 wxconfig.setappid("wx7c54942dfc87f4d8");//小程序appid
 wxconfig.setsecret("5873a729c365b65ab42bb5fc82d2ed49");//小程序appsecret
 wxmaservice wxmaservice = new wxmaserviceimpl();
 wxmaservice.setwxmaconfig(wxconfig);
 //2,设置模版信息(keyword1:类型,keyword2:内容)
 list<wxmatemplatedata> templatedatalist = new arraylist<>(2);
 wxmatemplatedata data1 = new wxmatemplatedata("keyword1", "获取老师微信");
 wxmatemplatedata data2 = new wxmatemplatedata("keyword2", "2501902696");
 templatedatalist.add(data1);
 templatedatalist.add(data2);
 //3,设置推送消息
 wxmatemplatemessage templatemessage = wxmatemplatemessage.builder()
 .touser(openid)//要推送的用户openid
 .formid(formid)//收集到的formid
 .templateid("edzcu__qiz64xx19daokg0taf5aaodmhuhprf6cad4a")//推送的模版id(在小程序后台设置)
 .data(templatedatalist)//模版信息
 .page("pages/index/index")//要跳转到小程序那个页面
 .build();
 //4,发起推送
 try { wxmaservice.getmsgservice().sendtemplatemsg(templatemessage);
 } catch (wxerrorexception e) {
 system.out.println("推送失败:" + e.getmessage());
 return e.getmessage();
 }
 return "推送成功";
 }
}

看代码我们可以知道,我们需要做一些配置,需要下面信息

1,小程序appid

2,小程序appsecret(密匙)

3,小程序推送模版id

4,用户的openid

5,用户的formid(一个formid只能用一次)

下面就是小程序部分,来教大家如何获取上面所需的5个信息。

1,appid和appsecret的获取(登录小程序管理后台)

微信源代码在哪里看(微信小程序代码大全)

2,推送模版id

微信源代码在哪里看(微信小程序代码大全)

3,用户openid的获取,可以看下面的这篇文章,也可以看源码,这里不做具体讲解

小程序开发如何获取用户openid

4,获取formid

微信源代码在哪里看(微信小程序代码大全)

看官方文档,可以知道我们的formid有效期是7天,并且一个form_id只能使用一次,所以我们小程序端所需要做的就是尽可能的多拿些formid,然后传个后台,让后台存到数据库中,这样7天有效期内,想怎么用就怎么用了。所以接下来要讲的就是小程序开发怎么尽可能多的拿到formid了

微信源代码在哪里看(微信小程序代码大全)

看下官方提供的,只有在表单提交时把report-submit设为true时才能拿到formid,比如这样

 <form report-submit='true' >
 <button form-type='submit'>获取formid</button>
 </form>

所以我们就要在这里下功夫了,既然只能在form组件获取,我们能不能把我们小程序里用到最多的地方用form来伪装呢。

下面简单写个获取formid和openid的完整示例,方便大家学习

效果图

微信源代码在哪里看(微信小程序代码大全)

我们要做的就是点击获取formid按钮,可以获取到用户的formid和openid,正常我们开发时,是需要把openid和formid传给后台的,这里简单起见,我们直接用获取到的formid和openid实现推送功能

下面来看小程序端的实现代码

1,index.wxml

微信源代码在哪里看(微信小程序代码大全)

2,index.js

微信源代码在哪里看(微信小程序代码大全)

到这里我们小程序端的代码也实现了,接下来测试下推送。

formid: 6ee9ce80c1ed4a2f887fccddf87686eb
openid o3dol0uusu1urbjk0nj4jd1lrre0
微信源代码在哪里看(微信小程序代码大全)

可以看到我们用了上面获取到的openid和formid做了一次推送,显示推送成功

微信源代码在哪里看(微信小程序代码大全)
微信源代码在哪里看(微信小程序代码大全)

到这里我们小程序消息推送的后台和小程序端都讲完了。

这里有两点需要大家注意

1,推送的openid和formid必须对应。

2,一个formid只能用一次,多次使用会报一下错误。

{"errcode":41029,"errmsg":"form id used count reach limit hint: [ssun8a09984113]"}

编程小石头,码农一枚,非著名全栈开发人员。分享自己的一些经验,学习心得,希望后来人少走弯路,少填坑。

这里就不单独贴出源码下载链接了,大家感兴趣的话,可以私信我,或者在底部留言,我会把源码下载链接贴在留言区。