微信小程序通过公众号(服务号)推送通知或提醒步骤及代码(一,获取推送前所需信息) | 您所在的位置:网站首页 › 关注公众号给佣金的群里当托会被抓吗 › 微信小程序通过公众号(服务号)推送通知或提醒步骤及代码(一,获取推送前所需信息) |
提前准备:微信小程序,微信公众号(服务号),微信开放平台 微信小程序不用说啦,肯定要有的。 微信公众号(服务号)是需要有企业资质才可以申请的,个人无法申请,认证费300RMB 微信开放平台申请,这个需要企业资质,认证费用300RMB 开发前关系绑定: 在公众号后台绑定小程序,原因:公众号绑定了小程序可以再直接跳转到小程序中,位置再公众号后台左侧菜单栏 -> 小程序 -> 小程序管理中。一个公众号可以绑定多个小程序。 公众号白名单配置和服务器配置 白名单配置: 在公众号后台进行配置,开发 -> 基本配置 -> IP白名单。把部署的服务器IP配置进来就可以 服务器配置: 在公众号后台进行配置,开发 -> 基本配置 -> 服务器配置 官方文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html 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 实验室设备网 版权所有 |