引言 关于access_token

小程序发通知需要获取获取小程序全局唯一后台接口调用凭据(access_token)。调用绝大多数后台接口时都需使用 access_token,后面会进行使用redis的存取。

但是急于求成加上先去看了小马如何获取openId,打算使用前端发过来的code获取access_token,这确实是一个获取access_token的方法,照着这个去查找找到了微信小程序官网文档,按照里面的接口介绍,通过前端获取的code实现了获取。在此之间我并不知道access_token是会过期的,后面又了解到前端不能发起定时任务,在微信小程序官方文档中又看到了用AppID和AppSecret获取access_token


于是我们可以在自己定义的定时任务中实现在需要发送消息通知之前的10秒内(时间上可以接近一点)获取,同时,除了可以用定时任务来自动更新access_token,access_token的有效期为2h,也需要弄一个报错之后可以调用的主动方法获取最新的access_token并存进redis中。


于是我们发现,小程序发送消息通知需要很多的组成,比如OpenId,access_token,template_id(模板id)等,于是我们可以将其封装为一个专门的类。

@Data
public class WxMssVo {
    //小程序用户的OpenId
    private String touser;
    //模板id
    private String template_id;
    //跳转首页
    private String page;
    //接口凭证
    private String access_token;
    //请求路径
    private String request_url;
    //放置数据
    private HashMap<String, Object> map = new HashMap<>(3);

}

用小程序的AppID和AppSecret获取access_token(一般有效期为2min,需要与后面的定时任务结合使用,实现不间断的刷新获取最新可用的access_token)。

/**
     * 根据AppID和AppSecret获取最新可用的AccessToken
     * @return
     */
    public static String getAccessToken() throws IOException {
//        https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        String appid = PropUtils.getProp("APPID");
        String appsecret = PropUtils.getProp("APPSECRET");

        //构建url,用于向微信服务器请求用户的openId
        StringBuffer url = new StringBuffer("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&");
        url.append("appid=").append(appid)
                .append("&secret=").append(appsecret);

        //向微信的服务器发送Get请求
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url.toString());
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity result = httpResponse.getEntity();
        String resultStr = EntityUtils.toString(result);
        System.out.println(resultStr);
        JSONObject resultJsonObject = JSONUtil.parseObj(resultStr);
        String accessToken = (String) resultJsonObject.get("access_token");
        return accessToken;
    }

通过输出resultStr 可以得到一个{"access_token":"长字符串","expires_in":7200}的access_token信息

小程序的AppID和AppSecret在发布小程序的官方网页获取对应信息

这些都是不变的,我们可以将其存进resousce下的appconfig.properties

微信小程序开启ES6_微信小程序开启ES6

 通过书写一个PropUtil来获取

public class PropUtils {
    private static Properties properties;

    static {
        properties = new Properties();
    }

    public static String getProp(String key) throws IOException {
        //读取配置文件
        ClassPathResource classPathResource = new ClassPathResource("appconfig.properties");
        properties = PropertiesLoaderUtils.loadProperties(classPathResource);
        return properties.getProperty(key);
    }
}

发送模板信息

//发送模板消息
    public static String sendTemplateMessage(WxMssVo wxMssVo) {
        String info = "";
        try {
            //创建连接
            URL url = new URL(wxMssVo.getRequest_url());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Type", "utf-8");
            connection.connect();

            //POST请求
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            JSONObject obj = new JSONObject();

            //设置参数
            //不可缺失 缺失会出现invalid openid rid
            //putOpt 等效于当两个参数都为非空时;除此之外什么都不做。put(name, value)
            obj.putOpt("touser", wxMssVo.getTouser());
          //不可缺失 缺失出现invalid template_id
            obj.putOpt("template_id", wxMssVo.getTemplate_id());
            obj.putOpt("page", wxMssVo.getPage());

            JSONObject jsonObject = new JSONObject();

            //发送自定义数据
            Set<Map.Entry<String, Object>> entries = wxMssVo.getMap().entrySet();

            for (Map.Entry<String, Object>entry:entries){

                JSONObject dataInfo = new JSONObject();
                dataInfo.putOpt("value", entry.getValue());
                jsonObject.putOpt(entry.getKey(), dataInfo);
            }
            obj.putOpt("data", jsonObject);

            System.out.println(obj.toString());
            out.write(obj.toString().getBytes());
            out.flush();
            out.close();

            //读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                sb.append(lines);
            }
            info = sb.toString();
            System.out.println(sb);
            reader.close();
            // 断开连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

创建测试类

@Test
    public void publishModelMessage(){
        WxMssVo wxMssVo = new WxMssVo();
        //设置模板id
        wxMssVo.setTemplate_id("");
        String accessToken=null;
        try {
            //设置openId
            wxMssVo.setTouser("");
//            设置accessToken
            accessToken=WXUtils.getAccessToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        设置小程序 跳转首页
        wxMssVo.setPage("pages/index/index");

        wxMssVo.setRequest_url("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken);
//        将模板中的值一一赋值 发送到小程序的数据要转化为json格式 可以使用下面的方法直接拼接成JSONObject
      /*  JSONObject jsonObject = new JSONObject();
        JSONObject dataInfo = new JSONObject();
        dataInfo.putOpt("value", TimeUtils.getNowTime());
        jsonObject.putOpt("time5", dataInfo);
*/
//        使用map结合模板 后续再加遍历拼接成JSONObject
        HashMap<String, Object> map = new HashMap<>(3);
        //根据模板写值  
        map.put("time",TimeUtils.getNowTime());
        map.put("thing1","下班打卡");
        wxMssVo.setMap(map);
        WXUtils.sendTemplateMessage(wxMssVo);
    }

发送请求

没有权限用户,返回错误,需要每一次都设置权限(企业版才不需要)比较容易出现

微信小程序开启ES6_微信小程序开启ES6_02

小程序发送通知需要传递json数据,订阅号的模板对于每⼀个字段的类型都有特别要求,例如这个模板中的“通知”字段是叫time5.DATA,那么你在后端调取微信的时候也必须使用这个名字,而且对于类型也有一定的限制。

微信小程序开启ES6_json_03

首先,我们看看小程序模板大概的样子

微信小程序开启ES6_微信小程序开启ES6_04

这时,我们就需要设置成data->time5->value(替换为值)这样的json格式传值。

传值之后容易出现数据不合法,需要参考参数值内容限制说明

 参数类别

 参数说明

             参数值限制

   说明

  thing.DATA

   事务

           20个以内字符

  可汉字、数字、字母或符号组合

number.DATA

   数字

           32位以内数字

      只能数字,可带小数

 letter.DATA

   字母

            32位以内字母

               只能字母

 symbol.DATA

    符号

              5位以内符号

               只能符号

 character_string.DATA

   字符串

    32位以内数字、字母或符号

   可数字、字母或符号组合

 time.DATA

   时间

  24小时制时间格式(可以为年月日),支持时间段;两个时间点之间用~连接

(如2022-09-27 19:31:28或19:38)

date.DATA

    日期

  年月日格式(支持加24小时制时间和时间段),两个时间点之间用~连接

    如2022年9月27日,2022年9月27日 19:31

amount.DATA

    金额

一个货币符号+10位以内纯数字,可带小数,结尾可带元

 ¥8.8

phone_number.DATA

   电话号码

   17位以内数字、符号

如11122223333,+86-1111-11111111

car_number.DATA

 车牌

 8位以内,第一位与最后一位可为汉字,其余为字母或数字

车牌号 :如粤A8888Z桂

name.DATA

 姓名

 10字以内纯汉字或20个以内纯字母或符号

小明

phrase.DATA

汉字

5个字以内汉字

 点赞的都帅

成功结果

 

总结

在实现不同外部接口的调用时,应多去看相对应的使用文档,积极搜索相同需求下不同的实现方法,再搭配上业务需求,实现特定功能。下期将更新基于SchedulingConfigurer实现多定时任务来实现定时的发送下班请求。