微信小程序发送订阅消息

package com.shucha.signalnotification.biz.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shucha.signalnotification.biz.constants.Constants;
import com.shucha.signalnotification.biz.dto.h5dto.MessageDTO.TroubleMessageDTO;
import com.shucha.signalnotification.biz.model.Subscribe;
import com.shucha.signalnotification.biz.model.UserInfo;
import com.shucha.signalnotification.biz.service.MessageService;
import com.shucha.signalnotification.biz.service.SubscribeService;
import com.shucha.signalnotification.biz.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tqf
 * @Description
 * @Version 1.0
 * @since 2021-04-13 18:31
 */
@Slf4j
@Service
public class MessageServiceImpl  implements MessageService {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SubscribeService subscribeService;
    @Override
    public String sendMessage(Long transportationUnitId, TroubleMessageDTO troubleMessageDTO) {
        // 获取符合订阅条件的该部门下的
        List<UserInfo> userInfos = userInfoService.listSubscribe(transportationUnitId, troubleMessageDTO.getTemplateId());
        List<String> wxcodes = userInfos.stream().map(UserInfo::getWxCode).collect(Collectors.toList());
        String msg = "";
        // 根据模板id查询那些用户订阅了的
        if(wxcodes.size()>0) {
            for (String wxcode: wxcodes) {
                JSONObject body = new JSONObject();
                // 用户的openid
                body.set("touser", wxcode);
                // 订阅消息模板ID
                body.set("template_id", troubleMessageDTO.getTemplateId());
                // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
                body.set("miniprogram_state", "developer");
                // 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
                // body.set("lang", "zh_CN");
                // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
                body.set("page", troubleMessageDTO.getPage());

                JSONObject json = new JSONObject();
                json.set("thing11", new JSONObject().set("value", troubleMessageDTO.getEquipmentLocation()));
                json.set("thing1", new JSONObject().set("value", troubleMessageDTO.getFaultType()));
                json.set("thing2", new JSONObject().set("value", troubleMessageDTO.getFaultDescribe()));
                json.set("thing3", new JSONObject().set("value", troubleMessageDTO.getGeographicPeople()));
                json.set("time5", new JSONObject().set("value",troubleMessageDTO.getGeographicTime()));
                body.set("data", json);
                // 发送
                // String accessToken = "44_K5uuxtk-9MJKElbYnS1IzGk2zTDK9R_EiW58xKDNsNg3_XbEIeotYlWLryi_FNUzG9DE3FT6lwJR4YiAZBd3LlbEkUJKJcoXQJKJXJhtVHAFzlIY_Jp38asy0OXdHCCgtCEAL1cNrRDQaEN3HJDcADAUWQ";
                // String accessToken = WxConfig.getOaAccessToken();
                msg = HttpUtil.post(Constants.OA_TEMPLATE_MSG_SEND.replace("${ACCESS_TOKEN}",troubleMessageDTO.getAccessToken()), body.toString());
                log.info("msg="+msg);
                JSONObject jsonObj = new JSONObject(msg);
                String errcode = jsonObj.get("errcode").toString();
                switch (errcode) {
                    // 用户拒绝接受消息,如果用户之前曾经订阅过,则表示用户取消了订阅关系
                    case "43101":
                        subscribeService.update(Wrappers.<Subscribe>lambdaUpdate()
                                .eq(Subscribe::getWxOpenid, wxcode)
                                .set(Subscribe::getStatus, "reject")
                                .set(Subscribe::getUpdateTime, new Date()));
                        break;
                    default:
                    break;
                }
            }
        }
        return msg;
    }
}

// 使用到的参数
 /**
     * 微信故障单消息模板id
     */
    public static final String WX_TROUBLE_MSG_TEMPLATE_ID = "订阅消息模板ID";
    public static final String WX_TROUBLE_MSG_JUMP_PATH = "pages/details/details?index=1&id=";

    /**
     * 发送订阅消息地址
     */
    public static final String OA_TEMPLATE_MSG_SEND = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${ACCESS_TOKEN}";
HttpUtil是使用的微信小程序的jar
<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.0</version>
            <scope>compile</scope>
        </dependency>

 

上一篇:Java RestTemplate 请求参数字符串中有大括号{}的请求正确方法


下一篇:Java构造和解析Json数据的两种方法详解二