spring boot 发送微信小程序订阅消息

news2024/9/21 14:54:15

首先我们需要订阅一个消息:

订阅教程本文章并未提起,感兴趣的同学自行百度。

我们可以看到订阅消息中【消息内容】有很多参数,我们在发送消息时就需要将这些参数进行填充,当然填充的时候要注意格式,如果格式不对还是会报错。 

教程开始:

1、创建一个实体类,用来填充对应的数据

import lombok.Data;

import java.util.Date;

@Data
public class LeaveResultMsg {

    /**
     * 请假结果通知 -- 模板ID
     */
    public static String RESULTID = "Qd0*************T8k";


    /**
     * 请假人OpenId
     */
    private String openId;

    /**
     * 请假人名称
     */
    private String name;

    /**
     * 请假开始时间
     */
    private Date startTime;

    /**
     * 请假结束时间
     */
    private Date endTime;

    /**
     * 审核人
     */
    private String examine;

    /**
     * 请假结果(同意,不同意)
     */
    private String status;

    /**
     * 请假类型
     */
    private String type;


}

2、实现类

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.api.service.ApiWeChatUtilsService;
import org.dromara.common.api.utils.DateUtil;
import org.dromara.school.domain.LeaveResultMsg;
import org.dromara.school.service.ISubscribeMsgService;
import org.dromara.system.service.ISysConfigService;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


@Service
@RequiredArgsConstructor
public class SubscribeMsgService implements ISubscribeMsgService {
    private final static String SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";

//	private final WeChatTokenUtil weChatTokenUtil;
    private final ISysConfigService configService;
    private final ApiWeChatUtilsService apiWeChatUtilsService;
    /**
     * 请假结果通知
     * @param msg
     * @throws Exception
     */
    public void leaveResultW(LeaveResultMsg msg){
        try {
            System.out.println(msg.toString());
            Map<String, Object> params = baseParams(msg.getOpenId());
            params.put("template_id", LeaveResultMsg.RESULTID);
            Map<String, Object> data = new HashMap<String, Object>();
            // 传入转换后的 UTF-8 编码字节数组
            data.put("thing2", Collections.singletonMap("value", new String(msg.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)));
            data.put("time3", Collections.singletonMap("value", DateUtil.getStringDateFormat(msg.getStartTime())));
            data.put("time4",Collections.singletonMap("value", DateUtil.getStringDateFormat(msg.getEndTime())));
            data.put("thing7", Collections.singletonMap("value",new String(msg.getExamine().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)));
            data.put("phrase1",Collections.singletonMap("value", new String(msg.getStatus().getBytes(StandardCharsets.UTF_8),StandardCharsets.UTF_8)));
            params.put("data", data);
            System.err.println(params);
            String post;
            try {
                post = HttpUtil.post(SEND_URL + apiWeChatUtilsService.getAccessTokenByRedis(0), JSONUtil.toJsonStr(params));
            }catch (Exception e) {
                post = HttpUtil.post(SEND_URL + apiWeChatUtilsService.getAccessTokenByRedis(1), JSONUtil.toJsonStr(params));
            }
//			String post = HttpUtil.post(SEND_URL + weChatTokenUtil.getAccessToken(), JSONUtil.toJsonStr(params));
            System.err.println(post);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

关联到的其他方法:

3、组装参数

@Service
@RequiredArgsConstructor
public class SubscribeMsgService implements ISubscribeMsgService {
        /**
         * 组装基础参数
         *
         * @param openId
         * @return
         */
        private Map<String, Object> baseParams(String openId) {
            Map<String, Object> map = new HashMap<String, Object>();
            String touser = openId;
            String page = "/pages/home/index";
            // 微信-小程序订阅跳转版本,developer:开发版,trial:体验版,formal:正式版
            String miniprogramState = configService.selectConfigByKey("***.version"); //小程序版本
            map.put("touser", touser);
            map.put("page", page);
            map.put("miniprogram_state", miniprogramState);
            return map;
        }
}

4、时间转换

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {   
 /**
     * 获取当前时间,转换成String(yyyy-MM-dd HH:mm:ss)
     * @return yyyyMMddHHmmss
     */
    public static String getStringDateFormat(Date date){
        //Thu May 11 11:08:15 GMT+08:00 2023
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取String类型的时间
        String str = df.format(date);
        return str;
    }
}

5、获取token

package org.dromara.system.dubbo;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.api.service.ApiWeChatUtilsService;
import org.dromara.common.api.utils.StringUtil;
import org.dromara.common.api.utils.https.HttpClientUtil;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.exception.base.BaseException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * 字典 业务层处理
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ApiWeChatUtilsServiceImpl implements ApiWeChatUtilsService {

    @Autowired
    ISysConfigService sysConfigService;

    /**
     * 0正常获取,1可能token失效了,重新获取(线上线下冲突)
     * @return
     */
    @Override
    public String getAccessTokenByRedis(int state)  {
        String key = "wx.access_token";
        String accessToken;
        if (state==0){
            accessToken = RedisUtils.getCacheObject(key);
            if (StringUtil.isEmpty(accessToken)) {
                accessToken = getAccessTokenByAddress();
                if (!StringUtil.isEmpty(accessToken))
                    RedisUtils.setCacheObject(key, accessToken, Duration.ofMinutes(120));
            }
        }else {
            accessToken = getAccessTokenByAddress();
            if (!StringUtil.isEmpty(accessToken))
                RedisUtils.setCacheObject(key, accessToken, Duration.ofMinutes(120));
        }
        return accessToken;
    }

    private String getAccessTokenByAddress() {
        String appid = sysConfigService.selectConfigByKey("***.appid");
        String secret =sysConfigService.selectConfigByKey("***.secret");
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
        String result = HttpUtil.get(url);
        HashMap<String, String> resultMap = (HashMap<String, String>) JSONUtil.parse(result).toBean(HashMap.class);
        String accessToken = resultMap.get("access_token");
        if (StringUtil.isEmpty(accessToken))
            throw new BaseException(resultMap.get("errmsg"));
        return accessToken;
    }

    /**
     * 获取手机号
     * @param code
     * @return
     */
    @Override
    public String getPhoneByCode(String code) {
        String accessToken = getAccessTokenByRedis(0);
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;

        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<Map<String, String>> httpEntity;
        if (code.contains("code")) {
            httpEntity = new HttpEntity(code,headers);
        }else {
            Map<String, String> params = new HashMap<>();
            params.put("code", code);
            httpEntity = new HttpEntity(params,headers);
        }

        ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class, new Object[0]);

        HashMap<String, Object> resultMap = (HashMap<String, Object>) JSONUtil.parse(response.getBody()).toBean(HashMap.class);
        int errcode = ((Integer) resultMap.get("errcode")).intValue();
        if (errcode != 0) {
            if (errcode == 40001){
                //可能会出现多个服务器重复获取,导致accessToken失效的情况
                accessToken = getAccessTokenByRedis(1);
                url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
                response = restTemplate.postForEntity(url, httpEntity, Object.class, new Object[0]);
                resultMap = (HashMap<String, Object>) JSONUtil.parse(response.getBody()).toBean(HashMap.class);
                errcode = ((Integer) resultMap.get("errcode")).intValue();
                if (errcode != 0){
                    throw new ServiceException("获取手机号出错,报错类型------>{}",errcode);
                }
            }else {
                throw new ServiceException("获取手机号出错,报错类型------>{}",errcode);
            }
        }
        HashMap<String, Object> phoneInfo = (HashMap<String, Object>) JSONUtil.parse(resultMap.get("phone_info")).toBean(HashMap.class);
        String phone = (String) phoneInfo.get("purePhoneNumber");
        return phone;
    }


    /**
     * 获取手机号2
     * @param code
     * @return
     */
    public String getPhoneByCode2(String code) {
        String accessToken = getAccessTokenByRedis(0);

        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
//        String jsonStr = "{\"code\":\"" + code + "\"}";
        String httpOrgCreateTestRtn = HttpClientUtil.doPost(url, code, "utf-8");
        System.out.println("获取到的手机号----------->"+httpOrgCreateTestRtn);
        return httpOrgCreateTestRtn;
    }



    /**
     * 获取openid
     * @param code
     * @return
     */
    @Override
    public String getOpenidByCode(String code) {
        String appid = sysConfigService.selectConfigByKey("weChat.mini.appid");
        String secret =sysConfigService.selectConfigByKey("weChat.mini.secret");
        //https://api.weixin.qq.com/sns/jscode2session
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret +
            "&js_code=" + code + "&grant_type=authorization_code";
        String result = HttpUtil.get(url);
        HashMap<String, String> resultMap = (HashMap<String, String>) JSONUtil.parse(result).toBean(HashMap.class);
        String openId = resultMap.get("openid");
//        System.out.println("获取到的openId----------->"+openId);
        if (StringUtils.isNotEmpty(openId)) {
            return openId;
        } else {
            throw new ServiceException("用户信息获取错误,请稍候重试",Integer.valueOf(resultMap.get("errcode")) );
        }
    }


}

以上用到的config类是配置类,配置的内容是由你申请小程序时候得到的数据。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2037399.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

python字符串方法,格式化字符串format,字符串的编码和解码,字符串的拼接

字符串–不可变序列 1.字符串方法 2.格式化字符串format&#xff0c;通过格式化字符串解决不同数据类型链接时候报错的问题例如用连接字符串和int就会报错 ①占位符需要注意格式print ( ’ 结果:%s %d ’ % (user_inp,ans))&#xff0c;注意需要给输出语句“”,后边需要有%。…

HarmonyOS应用开发者高级认证(一)

1、依次点击A、B、C、D四个按钮&#xff0c;其中不会触发UI刷新的是&#xff1a; 答案&#xff1a; Button("C").onClick(() > {this.nameList[0].name "Jim"})分析&#xff1a;直接更新非一级数据不会触发UI刷新 2、如果要实现Row组件内的子元素均匀…

wish怎么提升店铺流量?自养号测评防关联技术全解析

在电商领域&#xff0c;Wish作为一家知名的跨境电商平台&#xff0c;店铺流量的提升对于商家来说至关重要&#xff0c;如若店铺流量不够的话排名也会比较靠后&#xff0c;这时候卖家就需要做好相关运营政策去提升流量。 一、Wish怎么提升店铺流量&#xff1f; 1、优化商品信息…

C语言——结构体、共用体、枚举、位运算

C语言——结构体、共用体、枚举、位运算 结构体共用体枚举位运算 结构体 如果将复杂的复杂的数据类型组织成一个组合项&#xff0c;在一个组合项中包含若干个类型不同&#xff08;当然也可以相同&#xff09;的数据项。 C语言允许用户自己指定这样一种数据结构&#xff0c;它称…

Sentinel 滑动时间窗口源码分析

前言&#xff1a; Sentinel 的一个重要功能就是限流&#xff0c;对于限流来说有多种的限流算法&#xff0c;比如滑动时间窗口算法、漏桶算法、令牌桶算法等&#xff0c;Sentinel 对这几种算法都有具体的实现&#xff0c;如果我们对某一个资源设置了一个流控规则&#xff0c;并…

【upload]-ini-[SUCTF 2019]CheckIn-笔记

上传图片木马文件后看到&#xff0c;检查的文件内容&#xff0c;包含<? 一句话木马提示 检查的文件格式 用如下图片木马&#xff0c;加上GIF89a绕过图片和<?检查 GIF89a <script languagephp>eval($_POST[cmd])</script> .user.ini实际上就是一个可以由用…

交换机VLAN配置中Tagged与Untagged端口的差异和应用区别

VLAN&#xff08;虚拟局域网&#xff09;是一种将局域网设备从逻辑上划分为不同虚拟工作组的技术。它打破了传统局域网在物理位置上的限制&#xff0c;允许网络管理员根据功能、部门或安全需求等因素&#xff0c;将同一物理网络中的设备划分到不同的逻辑网络中。每个VLAN都像一…

使用Copilot辅助编程:我如何减少加班并提高工作效率

当我聘用了一个高级工程师给我写代码&#xff0c;我再也不加班了&#xff01; 很多医生朋友说写代码很难&#xff0c;学不会python。在这个AI时代&#xff0c;作为智慧的顶尖人类&#xff0c;你还在百度搜代码真的是out了。 学会站在巨人的肩膀上&#xff0c;让AI替你搬砖&am…

工业互联网边缘计算实训室解决方案

一、引言 随着物联网&#xff08;IoT&#xff09;、5G通信技术的快速发展&#xff0c;工业互联网已成为推动制造业转型升级的重要力量。边缘计算作为云计算的延伸和补充&#xff0c;在实时数据分析、降低数据传输延迟、提升处理效率及增强数据安全性方面展现出巨大潜力。在此背…

C语言——查漏补缺

前言 本篇博客主要记录一些C语言的遗漏点&#xff0c;完成查漏补缺的工作&#xff0c;如果读者感兴趣&#xff0c;可以看看下面的内容。都是一些小点&#xff0c;下面进入正文部分。 1. 字符汇聚 编写代码&#xff0c;演示多个字符从两端移动&#xff0c;向中间汇聚 #inclu…

无人机可以用来追黄蜂吗?

哈哈&#xff0c;这个问题真是挺有趣的&#xff01;不过&#xff0c;从实际应用和安全性角度来考虑&#xff0c;使用无人机来追黄蜂可能并不是一个好主意。 首先&#xff0c;黄蜂通常对突然出现的移动物体非常敏感&#xff0c;尤其是像无人机这样的“不明飞行物”。如果无人机…

【网站项目】SpringBoot679牙科诊所管理系统

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

Qt 窗口:对话框详解

目录 对话框 1. 对话框的基本概念 2. 对话框的内存释放问题 3. 自定义对话框界面 3.1 使用纯代码的方式定义 3.2 使用图形化的方式定义 4. 对话框的分类 4.1 模态对话框 4.2 非模态对话框 4.3 混合属性对话框 5. Qt 内置对话框 5.1 消息对话框 QMessageBox 示例1&…

生活生鲜超市小程序系统开发方案

生活生鲜超市小程序系统是集商品浏览、在线下单、支付结算、物流配送、会员管理等功能于一体。是为了满足现代消费者对新鲜食品购买的便利性与即时性需求。 适用于&#xff1a;生鲜超市、百货、连锁、水果、批发、便民、果蔬、食品、食材、鲜果、特产、海鲜等店铺。一、目标用户…

IP基础知识以及IP地址分类(A类 B类 C类 D类 E类)

IP地址是什么&#xff1f; IP 地址是互联网协议特有的一种地址&#xff0c;它是 IP 协议提供的一种统一的地址格式&#xff0c;为互联网上的每一个网络和每一台主机分配一个逻辑地址&#xff0c;以此来屏蔽物理地址的差异。 MAC和IP 在⽹络数据包传输中&#xff0c;源IP地址…

蜂鸣器(51单片机)

一、蜂鸣器介绍 1.蜂鸣器 2.蜂鸣器电路 3.芯片图示 二、蜂鸣器功能实现 1.蜂鸣器提示音代码 蜂鸣器函数 播放提示音功能实现 2.蜂鸣器播放音乐

Scrapy框架进行数据采集详细实现

摘要 本项目是python课程的课程项目&#xff0c;在简要学习完python和爬虫相关的Scrapy框架后&#xff0c;基于这两者的运用最终完成了对于北京链家网站新房页面的信息进行爬取&#xff0c;并将爬取的数据存放于excel之中&#xff0c;可使用excel或者wps进行查看。 1 引言 1…

论文分享 | Fuzz4All: 基于大语言模型的通用模糊测试

大语言模型是当前最受关注的研究热点&#xff0c;基于其生成和理解能力&#xff0c;对现有领域在提升性能和效果上做更多尝试。分享一篇发表于2024年ICSE会议的论文Fuzz4All&#xff0c;它组合多个大语言模型以非常轻量且黑盒的方式&#xff0c;实现了一种跨语言和软件的通用模…

【数学分析笔记】第1章第2节:映射与函数(2)

1. 集合与映射 1.12 函数&#xff08;实函数&#xff09; 函数是映射的一种特殊情况&#xff0c; f : X ⟶ Y f:\textbf{X}\longrightarrow \textbf{Y} f:X⟶Y x ⟼ y f ( x ) x\longmapsto yf(x) x⟼yf(x) 如果 X ⊂ R , Y R \textbf{X}\subset\mathbb{R},\textbf{Y}\ma…

OpenCV的Hello World

按照前文的步骤&#xff0c;我们已经在Windows机器上把OpenCV源代码编译成了DLL。接下来的问题自然是&#xff0c;我们怎么在自己的项目中使用OpenCV&#xff1f;我们将从零开始编写第一个OpenCV “Hello World”程序。通过本文的练习&#xff0c;大家将掌握&#xff1a; 在自…