给微信小程序发送消息

article/2025/8/16 1:37:59

给微信小程序发送消息

所用技术:后台:jeecgboot 数据库:oracle 前台:vue2.0

首先登陆微信公众平台

链接: https://mp.weixin.qq.com/

1.登陆之后每个公司 都会有自己得相关参数例如 appid之类得 后面代码里时要用到的

在这里插入图片描述

2.然后先择一个适合自己的模板,也可以自己去申请一个但是微信审核时间比较长所以我直接挑选一个比较适合我自己的

在这里插入图片描述

3.发送消息主体代码

1.我的需要用到的参数是写在配置文件里的

	@Value("${WXxcx.appid}")private String appid;//公众平台的企业appid(微信小程序的)@Value("${WXxcx.secret}")private String secret;//公众平台的企业secret小程序密钥@Value("${WXgzh.appid}")private String appidgzh;//公众号的appid@Value("${WXgzh.template_id}")private String template_id;//模板id
这里解释下:content是前台通过微信提供的接口获取到的传给后台的 ,就类似于微信需要的临时token一样。processname是我的业务需求是个用户名,传值可以根据自己业务需求定义
/*** 推送* @param content = appid* processname 流程名称* @return*/@ResponseBody@RequestMapping(value = "/testPush",  method = RequestMethod.POST,produces = "application/json;charset=UTF-8")public String push(@RequestParam(name = "content", required = true) String content,@RequestParam(name = "processname", required = true) String processname) {//获取需要推送的用户openidString openid= content;//获取用户tokenString token = getAccessToken();//封装单独的 方法String resultStatus = "0";//0:成功,1:失败try {//小程序统一消息推送String path = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + token;//封装参数JSONObject jsonData = new JSONObject();//小程序用户的openidjsonData.put("touser", openid);JSONObject jsonObject = new JSONObject();//公众号APPIDjsonObject.put("appid", appidgzh);//公众号模板IDjsonObject.put("template_id", template_id);//公众号模板消息所要跳转的url//jsonObject.put("url","https://blog.csdn.net/qq_46122292/article/details/124961251");//公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系JSONObject miniprogram = new JSONObject();//小程序APPIDminiprogram.put("appid",appid);//跳转到小程序的页面路径//miniprogram.put("pagepath","pages/login/login");jsonObject.put("miniprogram", miniprogram);//公众号消息数据封装JSONObject data = new JSONObject();Date time = new Date();//获取当前时间SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String strdate = dateformat.format(time);//此处的参数key,需要对照模板中的key来设置remark//data.put("first",getValue("你好"));data.put("keyword1",getValue(processname));data.put("keyword2",getValue("审批中"));data.put("keyword3",getValue(strdate));//data.put("remark",getValue("你好"));jsonObject.put("data", data);jsonData.put("mp_template_msg", jsonObject);System.out.println("请求参数:"+jsonData);String s = HttpUtils.doPost(path, jsonData.toString(),token);System.out.println("返回结果:"+s);resultStatus="发送成功!";} catch (Exception e) {//log.error("微信公众号发送消息失败!",e.getMessage());resultStatus="发送消息失败!";}return resultStatus;}

4.获取token是微信提供的接口只需要用appid 跟secret 两个参数请求就会返回token

/*** @Description: 微信公众号推送消息* @Author: 测试* @Date:   2022-08* @Version: V1.0*/
@Api(tags="微信公众号推送消息")
@RestController
@RequestMapping("/system/weiXinPushMessage")
public class WeiXinPushMessageController {@Value("${WXxcx.appid}")private String appid;//公众平台的企业appid(微信小程序的)@Value("${WXxcx.secret}")private String secret;//公众平台的企业secret小程序密钥@Value("${WXgzh.appid}")private String appidgzh;//公众号的appid@Value("${WXgzh.template_id}")private String template_id;//模板id/*** 获取token* @return*/public String getAccessToken() {RestTemplate restTemplate = new RestTemplate();Map<String, String> params = new HashMap<>();params.put("APPID", appid);params.put("APPSECRET", secret);params.put("grant_type", "client_credential");String tokenUrl="https://api.weixin.qq.com/cgi-bin/token?appid={APPID}&secret={APPSECRET}&grant_type={grant_type}";ResponseEntity<String> responseEntity = restTemplate.getForEntity(tokenUrl, String.class, params);String body = responseEntity.getBody();JSONObject object = JSONObject.fromObject(body);String access_Token = object.getString("access_token");System.err.println("access_Token:"+access_Token);return access_Token;}/*** 推送* @param content* processname 流程名称* @return*/@ResponseBody@RequestMapping(value = "/testPush",  method = RequestMethod.POST,produces = "application/json;charset=UTF-8")public String push(@RequestParam(name = "content", required = true) String content,@RequestParam(name = "processname", required = true) String processname) {//获取需要推送的用户openidString openid= content;//获取用户tokenString token = getAccessToken();String resultStatus = "0";//0:成功,1:失败try {//小程序统一消息推送String path = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + token;//封装参数JSONObject jsonData = new JSONObject();//小程序用户的openidjsonData.put("touser", openid);JSONObject jsonObject = new JSONObject();//公众号APPIDjsonObject.put("appid", appidgzh);//公众号模板IDjsonObject.put("template_id", template_id);//公众号模板消息所要跳转的url//jsonObject.put("url", "https://blog.csdn.net/qq_46122292/article/details/124961251");//公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系JSONObject miniprogram = new JSONObject();//小程序APPIDminiprogram.put("appid",appid);//跳转到小程序的页面路径//miniprogram.put("pagepath","pages/login/login");jsonObject.put("miniprogram", miniprogram);//公众号消息数据封装JSONObject data = new JSONObject();Date time = new Date();//获取当前时间SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String strdate = dateformat.format(time);//此处的参数key,需要对照模板中的key来设置remark//data.put("first",getValue("你好"));data.put("keyword1",getValue(processname));data.put("keyword2",getValue("审批中"));data.put("keyword3",getValue(strdate));//data.put("remark",getValue("你好"));jsonObject.put("data", data);jsonData.put("mp_template_msg", jsonObject);System.out.println("请求参数:"+jsonData);//请求头String s = HttpUtils.doPost(path, jsonData.toString(),token);System.out.println("返回结果:"+s);resultStatus="发送成功!";} catch (Exception e) {//log.error("微信公众号发送消息失败!",e.getMessage());resultStatus="发送消息失败!";}return resultStatus;}/*** 获取data,模板的字体颜色* @param value* @return*/private JSONObject getValue(String value) {// TODO Auto-generated method stubJSONObject json = new JSONObject();json.put("value", value);json.put("color", "#173177");return json;}}

5.最后这个JSONObject 方法只是定义模板的字体颜色跟样式 直接粘过去用就可以 不需要改什么

6.请求头设置

public static String doPost(String url,String json,String token) {String retStr = "";// 创建HttpClientBuilderHttpClientBuilder httpClientBuilder = HttpClientBuilder.create();// HttpClientCloseableHttpClient closeableHttpClient = httpClientBuilder.build();HttpPost httpPost = new HttpPost(url);//  设置请求和传输超时时间RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();httpPost.setConfig(requestConfig);try {//httpPost.setHeader("token", token);StringEntity data = new StringEntity(json,Charset.forName("UTF-8"));data.setContentEncoding("UTF-8");data.setContentType("application/json");httpPost.setEntity(data);CloseableHttpResponse response = closeableHttpClient.execute(httpPost);HttpEntity httpEntity = response.getEntity();if (httpEntity != null) {// 打印响应内容retStr = EntityUtils.toString(httpEntity, "UTF-8");}// 释放资源closeableHttpClient.close();} catch (Exception e) {System.out.println(e);}return retStr;}

此代码本人亲测有效,有什么问题 还请各路大神指点。


http://chatgpt.dhexx.cn/article/YQttFrIe.shtml

相关文章

微信小程序通过公众号服务号发送消息

一、基础概念: 准备条件&#xff1a; 1、公众号和小程序必须在同一个公司主体下。 2、在公众号后台需要对小程序进程绑定操作。 公众号提供了两种消息&#xff0c;一种是订阅消息&#xff0c;一种是模板消息。 订阅消息需要用户主动订阅&#xff0c;然后才能接收消息&#x…

六、微信小程序发布流程

目录 1、上传2、提交审核 1、上传 当我们写完代码后 点击微信开发者工具的上传 要么是体验版 要么是1.0.1最新版 直接点击确定或上传即可 【前提是你本人必须是小程序的管理员或项目成员】 上传成功会有提示弹窗 2、提交审核 在版本管理中我们能看到上传的小程序版本 在这里我…

微信小程序发布全流程

1.在微信公众平台注册 我选的是用QQ邮箱注册的&#xff0c;因为我的163邮箱和微信绑定的&#xff0c;不能使用注册。 登陆进来就是这个页面啦。 2.npm打包&#xff08;假设您的电脑已经安装了nodeJS&#xff09; 选中您的项目&#xff0c;然后到项目的根目录下 执行npm ins…

微信小程序申请+开发+发布流程

框架用的Taro&#xff0c;开发工具用的是VSCode微信开发者工具&#xff0c;VSCode主要用来编码&#xff0c;微信开发者工具主要用来看和最后上传那么一下&#xff0c;其他没什么好说的。放张图&#xff0c;大家自行体会&#xff1a; 利用taro发布web程序如下&#xff1a; 过程整…

微信小程序发布流程

1.授权 登录微信公众平台微信公众平台&#xff0c;给相应人员授权&#xff1b;最好同时添加体验成员&#xff08;用于验证测试&#xff09;。 2.开发工具及代码 下载官方的微信开发者工具&#xff0c;使用微信扫一扫登录&#xff1b;同时下载最新的项目代码&#xff0c;导入…

信号与系统(四)

注&#xff1a;本博客是基于奥本海姆的《信号与系统》第二版编写&#xff0c;主要是为了自己考研&#xff0c;准备专业课。 (转载&#xff1a;https://blog.csdn.net/Explorer_day/article/details/77177542) 一、基本系统性质 1、记忆系统与无记忆系统 ①无记忆系统 如果对…

信号与系统 | 信号与系统概述 | 信号的基本概念和分类

文章目录 常见的概念信号的描述信号的分类确定信号和随机信号连续信号和离散信号周期信号和非周期信号能量信号和功率信号一维信号和多维信号因果信号和非因果信号 信号的matlab表示与绘图 常见的概念 消息&#xff0c;来自外界的各种报道统称为消息信息&#xff0c;消息中有意…

城市轨道交通信号系统学习笔记(四)信号机

信号机 <font face"楷体" size5>一、地面信号机的设置</font><font face"楷体" size5>二、信号显示</font> 城市轨道交通采用与铁路相同的色灯信号机&#xff0c;但设置位置和信号显示不同于铁路&#xff0c;信号显示距离也有自己…

信号与系统(一)

注&#xff1a;本博客是基于奥本海姆的《信号与系统》第二版编写&#xff0c;主要是为了自己考研&#xff0c;准备专业课。 (转载&#xff1a;https://blog.csdn.net/Explorer_day/article/details/76910002) 一、连续时间和离散时间信号 1、信号的定义 ①在物理上&#xf…

操作系统:信号

文章目录 一 信号概念1. 信号简介2. 信号/事件 产生方式3. 信号处理方式 二 信号实现1. 不可靠信号2. 可靠与不可靠信号类型3. 被中断的系统调用4. 信号函数4.1 信号发送函数 kill/raise4.2 定时发送信号函数 alarm4.3 信号捕获等待函数 pause 5. 信号集5.1 信号集来由5.2 信号…

信号与系统—信号的描述与分类

信号的描述和分类 信号的描述 函数表示 信号通常是时间变量t的函数&#xff0c;所以对于某一类信号可以用时间函数来表示&#xff0c;如 c ( u ) { 0 &#xff0c; t < 0 e − t τ &#xff0c; t ≥ 0 c(u)\begin{cases} 0&#xff0c;t<0\\ e^{-\frac{t}{\tau}}&…

操作系统——信号

信号的概念 信号的产生 信号的注册 信号的注销 自定义信号处理方式 信号的捕捉流程 信号阻塞 信号的概念&#xff1a; 1.信号是一个软件中断 2.信号的种类&#xff1a; kill -l命令的时候&#xff0c;我们会看到62种信号 1~31 &#xff1a; 非可靠信号&#xff0c; 信号有可能…

信号与系统(典型信号)

1.信号的描述与信号的分类 1.1基于信号维度分类 一维信号&#xff08;声音&#xff09;、二维信号&#xff08;图像&#xff09;、三维信号&#xff08;视频&#xff09;、四维信号&#xff08;VR中看到的信号&#xff09;、…N维。 信号与系统中只讨论一维信号 1.2一维信号…

信号与系统1-概述

目录&#xff1a; 一、概述 1、信号与系统概念 1&#xff09;信号 2&#xff09;系统 2、信号的描述、分类与典型信号 1&#xff09;描述方式 2&#xff09;信号的分类 &#xff08;1&#xff09;确定信号/随机信号 &#xff08;2&#xff09;周期信号/非周期信号 &#xff0…

【信号与系统】(四)信号与系统概述——系统的概念及分类

文章目录 第一章 信号与系统概述1.4 系统的概念及分类1.4.1 系统定义与典型系统举例1.4.1.1 系统定义1.4.1.2 系统模型1.4.1.3 系统的状态1.4.1.4 典型系统举例 1.4.2 系统分类1.4.2.1 连续系统与离散系统1.4.2.2 动态系统与即时系统1.4.2.3 单输入单输出系统与多输入多输出系统…

信号系统 | 信号的概念与常用信号

信号的本质是函数&#xff0c;因此信号处理方法具有普适性&#xff0c;应用领域非常广泛。本篇介绍信号的基本概念以及一些典型的信号&#xff0c;主要参考奥本海姆的经典教材《信号与系统》&#xff0c;微信搜索公众号PurePlay&#xff0c;后台回复Oppenheim即可获取中文PDF。…

【信号与系统】(二)信号与系统概述——基本信号

文章目录 第一章 信号与系统概述1.2基本信号1.2.1 阶跃函数1.2.2 冲激函数1.2.3 冲激函数的广义函数定义1.2.4 冲激函数的取样性质1.2.4.1 f ( t ) f(t) f(t)乘以 δ ( t ) δ(t) δ(t)1.2.4.2 f ( t ) f(t) f(t)乘以 δ ( t − a ) δ(t-a) δ(t−a) 1.2.5 冲激函数的导数1.…

【信号与系统|吴大正】1:信号与系统概述

信号与系统概述 写在前面&#xff1a; 自己也感觉最近基础类课程开了好多坑…因为发现未来研究领域跟数字信号处理也脱离不了关系&#xff0c;打算从《信号系统与线性分析》开始慢慢往后补&#xff1b;大二下期的时候学习过了这门课&#xff0c;但其实现在回过头来知识也不剩多…

城市轨道交通信号系统学习笔记(二)信号系统的组成

信号系统的组成 一、列车自动控制&#xff08;ATC&#xff09;系统 二、车联段联锁设备 城市轨道交通信号系统通常由列车自动控制&#xff08;Automatic Train Control&#xff0c;ATC&#xff09;系统和车辆段/停车场信号控制系统两大部分组成。用于列车进路控制、列车间隔控制…

信号与系统——基本概念

目录 基础知识 信号与系统&#xff08;Signals and Systems&#xff09; 信号 信号的定义 信号的分类 系统 基础知识 信号理论主要包括&#xff1a;信号分析&#xff0c;信号传输&#xff0c;信号综合&#xff1b;系统理论主要包括&#xff1a;系统分析和系统综合 信号…