微信小程序通过公众号(服务号)推送通知或提醒步骤及代码(一,获取推送前所需信息) 您所在的位置:网站首页 关注公众号给佣金的群里当托会被抓吗 微信小程序通过公众号(服务号)推送通知或提醒步骤及代码(一,获取推送前所需信息)

微信小程序通过公众号(服务号)推送通知或提醒步骤及代码(一,获取推送前所需信息)

2023-08-27 11:47| 来源: 网络整理| 查看: 265

提前准备:微信小程序,微信公众号(服务号),微信开放平台 微信小程序不用说啦,肯定要有的。 微信公众号(服务号)是需要有企业资质才可以申请的,个人无法申请,认证费300RMB 微信开放平台申请,这个需要企业资质,认证费用300RMB

开发前关系绑定: 在公众号后台绑定小程序,原因:公众号绑定了小程序可以再直接跳转到小程序中,位置再公众号后台左侧菜单栏 -> 小程序 -> 小程序管理中。一个公众号可以绑定多个小程序。在这里插入图片描述 把小程序和公众号都绑定到微信开放平台,原因:为了获取unionid,unionid是什么?简单来说,就是开放平台用于区分是否是同一用户的标识,你把多个小程序,公众号,第三方平台等都绑定到开放平台上,取到的unionid都是一样的,这样就可以知道多个不同程序之间的用户是否是同一用户。 我们这里获取unionid就是为了知道使用我们小程序的销售和关注了公众号的销售是不是同一个销售,从而给该销售发送提醒通知,我们在小程序端获取销售的openId和unionid,在公众号端通过关注也获取销售的openId和unionid,其中openId是不同的,unionid是相同的。我们就可以通过小程序的unionid找到公众号的openid。通过openId就知道给谁发送通知。(openId就是小程序或公众号的唯一标识) 在这里插入图片描述

公众号白名单配置和服务器配置

白名单配置: 在公众号后台进行配置,开发 -> 基本配置 -> IP白名单。把部署的服务器IP配置进来就可以 在公众号后台进行配置

服务器配置: 在公众号后台进行配置,开发 -> 基本配置 -> 服务器配置 官方文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html 在这里插入图片描述 URL: 用户微信服务器调用进行验证和发送信息使用。 Token: 可以随便写,但是要和代码中一致,才可以验证过去 验证服务器配置接口: 要先把接口写好放到服务器中,服务器配置才可以成功。

//这个token要与公众平台服务器配置填写的token一致 private final static String TOKEN = "xxxxxx"; //该接口就是在公众号后台配置的服务器URL @GetMapping("/mp/serverCheck") public void doGet(HttpServletRequest request, HttpServletResponse response) throws AesException, IOException { // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 String signature = request.getParameter("signature"); // 时间戳 String timestamp = request.getParameter("timestamp"); // 随机数 String nonce = request.getParameter("nonce"); // 随机字符串 String echostr = request.getParameter("echostr"); log.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr); if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) { throw new IllegalArgumentException("请求参数非法,请核实!"); } // 将token、timestamp、nonce三个参数进行字典序排序 2)将三个参数字符串拼接成一个字符串进行sha1加密 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 String signatureCheck = getSHA1(TOKEN, timestamp, nonce); log.info("\n加密后的signatureCheck = {}", signatureCheck); if (signatureCheck.equals(signature)) { log.info("\n接入成功"); PrintWriter out = response.getWriter(); //原样返回echostr参数 out.print(echostr); out.flush(); out.close(); } else { throw new AesException(AesException.ValidateSignatureError); } }

SHA1加密方法

/** * 用SHA1算法验证Token * * @param token 票据 * @param timestamp 时间戳 * @param nonce 随机字符串 * @return 安全签名 * @throws AesException */ public static String getSHA1(String token, String timestamp, String nonce) throws AesException { try { String[] array = new String[]{token, timestamp, nonce}; StringBuffer sb = new StringBuffer(); // 字符串排序 Arrays.sort(array); for (int i = 0; i shaHex = Integer.toHexString(digest[i] & 0xFF); if (shaHex.length() e.printStackTrace(); throw new AesException(AesException.ComputeSignatureError); } }

获取公众号用户的openId 和 unionId

我这里就不写获取小程序的openId 和 unionId和用户信息了,获取小程序的代码有点杂,是以前写的,不会的可以上网找一下如何获取,如果不绑定开放平台时获取不到unionId的,可以看一下官方文档https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html

下面是获取公众号的openId和unionId代码:根据关注的用户进行获取 这个接口就是服务器配置的URL,上面验证的时候也使用过一次,不过验证时get请求,这次是post请求。 Controller层

@Autowired private WeChatMPService weChatMPService; @ApiOperation(value = "处理微信服务器发来的消息", notes = "处理微信服务器发来的消息") @PostMapping("/mp/serverCheck") public String doPost(HttpServletRequest request, HttpServletResponse response) { // 调用核心服务类接收处理请求 return weChatMPService.processRequest(request); }

Service接口层

String processRequest(HttpServletRequest request, String projectId);

ServiceImpl实现类

import com.minapp.management.config.WeChatContant; import com.minapp.management.service.TdSysMpStaffLoginService; import com.minapp.management.service.WeChatMPService; import com.minapp.management.utils.WeChatUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Map; /** * @ClassName: WeChatServiceImpl * @Description: 可以结合官网的api看是什么意思 ↓ 消息管理 -> 接受事件推送 * https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html * @Authror: XQD * @Date: 2021/1/4 15:42 */ @Slf4j @Service public class WeChatMPServiceImpl implements WeChatMPService { @Resource private TdSysMpStaffLoginService mpStaffLoginService; @Override public String processRequest(HttpServletRequest request) { // xml格式的消息数据 String respXml = null; // 默认返回的文本消息内容 String respContent; try { // 调用parseXml方法解析请求消息 Map requestMap = WeChatUtil.parseXml(request); // 消息类型 String msgType = requestMap.get(WeChatContant.MsgType); log.info("\n消息类型:{}", msgType); String mes = null; // 文本消息 if (msgType.equals("text")) { respContent = "您发送的是文本消息!"; respXml = WeChatUtil.sendTextMsg(requestMap, respContent); } // 图片消息 else if (msgType.equals("image")) { respContent = "您发送的是图片消息!"; respXml = WeChatUtil.sendTextMsg(requestMap, respContent); } // 语音消息 else if (msgType.equals("voice")) { respContent = "您发送的是语音消息!"; respXml = WeChatUtil.sendTextMsg(requestMap, respContent); } // 视频消息 else if (msgType.equals("video")) { respContent = "您发送的是视频消息!"; respXml = WeChatUtil.sendTextMsg(requestMap, respContent); } // 地理位置消息 else if (msgType.equals("location")) { respContent = "您发送的是地理位置消息!"; respXml = WeChatUtil.sendTextMsg(requestMap, respContent); } //


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有