|
@@ -0,0 +1,109 @@
|
|
|
+package com.example.nngkxxdp.program.util;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.example.nngkxxdp.program.entity.AppletUser;
|
|
|
+import com.example.nngkxxdp.program.entity.DTO.PushMessageDTO;
|
|
|
+import com.example.nngkxxdp.program.entity.Event;
|
|
|
+import com.example.nngkxxdp.program.entity.EventPushRecord;
|
|
|
+import com.example.nngkxxdp.program.service.AppletUserService;
|
|
|
+import com.example.nngkxxdp.program.service.EventPushRecordService;
|
|
|
+import com.example.nngkxxdp.program.service.EventService;
|
|
|
+import com.example.nngkxxdp.util.RedisUtil;
|
|
|
+import com.example.nngkxxdp.util.SendUtil;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@AllArgsConstructor
|
|
|
+@Component
|
|
|
+public class WxMessageUtil {
|
|
|
+
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+ private AppletUserService appletUserService;
|
|
|
+ private EventService eventService;
|
|
|
+ private EventPushRecordService eventPushRecordService;
|
|
|
+
|
|
|
+ private final String GETTEMPLATE_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=%s";
|
|
|
+ private final String SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取订阅消息模板列表
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, Object> getMsgTemplate() {
|
|
|
+ String templateResult = HttpRequest.get(String.format(GETTEMPLATE_URL, redisUtil.get("access_token"))).execute().body();
|
|
|
+ JSONObject templateData = JSONUtil.parseObj(templateResult);
|
|
|
+ if (templateData.getInt("errcode") != 0) {
|
|
|
+ return SendUtil.send(false, templateData.getStr("errmsg"));
|
|
|
+ }
|
|
|
+ return SendUtil.send(true, "", templateData.getJSONArray("data"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送订阅消息
|
|
|
+ *
|
|
|
+ * @param pushMessageDTO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, Object> pushMessage(PushMessageDTO pushMessageDTO) {
|
|
|
+ //保存事件
|
|
|
+ Event event = new Event();
|
|
|
+ event.setEventTitle(pushMessageDTO.getEventTitle());
|
|
|
+ event.setTemplateId(pushMessageDTO.getTemplateId());
|
|
|
+ event.setTemplateContent(pushMessageDTO.getContent().toString());
|
|
|
+ event.setLinkUrl(pushMessageDTO.getUrlLink());
|
|
|
+ event.setCreateTime(DateUtil.now());
|
|
|
+ eventService.add(event);
|
|
|
+ //推送消息 每次查2000条出来,行数小于2000停止循环
|
|
|
+ ThreadUtil.newExecutor().execute(() -> {
|
|
|
+ int page = 1;
|
|
|
+ int size = 2000;
|
|
|
+ List<AppletUser> appletUserList;
|
|
|
+ do {
|
|
|
+ appletUserList = appletUserService.queryPage((page - 1) * size, size);
|
|
|
+ this.push(event.getId(), appletUserList, pushMessageDTO);
|
|
|
+ ++page;
|
|
|
+ } while (appletUserList.size() == size);
|
|
|
+ });
|
|
|
+ return SendUtil.send(true, "", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void push(Integer eventId, List<AppletUser> appletUserList, PushMessageDTO pushMessageDTO) {
|
|
|
+ appletUserList.forEach(appletUser -> {
|
|
|
+ //封装参数
|
|
|
+ JSONObject params = JSONUtil.createObj();
|
|
|
+ params.put("template_id", pushMessageDTO.getTemplateId());
|
|
|
+ params.put("page", "/pages/index/index");
|
|
|
+ params.put("touser", appletUser.getOpenid());
|
|
|
+ JSONObject data = JSONUtil.createObj();
|
|
|
+ //封装content参数
|
|
|
+ pushMessageDTO.getContent().forEach(field -> {
|
|
|
+ data.set(field.getKey(), JSONUtil.createObj().set("value", field.getValue()));
|
|
|
+ });
|
|
|
+ params.set("data", data);
|
|
|
+ params.set("miniprogram_state", "trial");
|
|
|
+ params.set("lang", "zh_CN");
|
|
|
+ String sendResult = HttpRequest.post(String.format(SEND_URL, redisUtil.get("access_token"))).body(params.toString()).execute().body();
|
|
|
+ JSONObject result = JSONUtil.parseObj(sendResult);
|
|
|
+ //保存推送记录
|
|
|
+ EventPushRecord eventPushRecord = new EventPushRecord();
|
|
|
+ eventPushRecord.setEventId(eventId);
|
|
|
+ eventPushRecord.setUserNickname(appletUser.getNickName());
|
|
|
+ eventPushRecord.setName(appletUser.getName());
|
|
|
+ eventPushRecord.setUserPhone(appletUser.getPhone());
|
|
|
+ eventPushRecord.setUserOpenid(appletUser.getOpenid());
|
|
|
+ eventPushRecord.setPushTime(DateUtil.now());
|
|
|
+ eventPushRecord.setErrCode(result.getInt("errcode"));
|
|
|
+ eventPushRecord.setErrMsg(result.getStr("errmsg"));
|
|
|
+ eventPushRecord.setPushStatus(0 == result.getInt("errcode") ? "success" : "fail");
|
|
|
+ eventPushRecordService.add(eventPushRecord);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|