ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

微信小程序发送订阅消息

2021-04-19 12:04:33  阅读:204  来源: 互联网

标签:订阅 set troubleMessageDTO 微信 new 发送 JSONObject import com


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>

 

标签:订阅,set,troubleMessageDTO,微信,new,发送,JSONObject,import,com
来源: https://blog.csdn.net/tanqingfu1/article/details/115861655

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有