⼩程序发送订阅消息
package com.shucha.signalnotification.biz.service.impl;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import lkit.Wrappers;
import com.shucha.stants.Constants;
import com.shucha.signalnotification.biz.dto.h5dto.MessageDTO.TroubleMessageDTO;
import com.shucha.del.Subscribe;
import com.shucha.del.UserInfo;
import com.shucha.signalnotification.biz.service.MessageService;
import com.shucha.signalnotification.biz.service.SubscribeService;
import com.shucha.signalnotification.biz.service.UserInfoService;
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, TemplateId());
List<String> wxcodes = userInfos.stream().map(UserInfo::getWxCode).List());
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", TemplateId());
// 跳转⼩程序类型: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", Page());
JSONObject json = new JSONObject();
json.set("thing11", new JSONObject().set("value", EquipmentLocation()));
json.set("thing1", new JSONObject().set("value", FaultType()));
json.set("thing2", new JSONObject().set("value", FaultDescribe()));
json.set("thing3", new JSONObject().set("value", GeographicPeople()));
json.set("time5", new JSONObject().set("value",GeographicTime()));
json.set("time5", new JSONObject().set("value",GeographicTime()));
body.set("data", json);
// 发送
// String accessToken = "44_K5uuxtk-9MJKElbYnS1IzGk2zTDK9R_EiW58xKDNsNg3_XbEIeotYlWLryi_FNUzG9DE3FT6lwJR4YiAZBd3LlbEkUJKJcoXQ // String accessToken = OaAccessToken();
msg = HttpUtil.post(Constants.OA_TEMPLATE_place("${ACCESS_TOKEN}",AccessToken()), String()); log.info("msg="+msg);
JSONObject jsonObj = new JSONObject(msg);
String errcode = ("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;service fault
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 = "api.weixin.qq/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>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论