From 6a4f8cdea5a15ead44f45a5440022cea684621e5 Mon Sep 17 00:00:00 2001 From: czk Date: Wed, 27 Oct 2021 09:42:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E6=B6=88=E6=81=AF=E7=BE=A4?= =?UTF-8?q?=E5=8F=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/NoticeController.java | 30 ++++++++ .../pojo/vo/TemplateDataVO.java | 23 ++++++ .../pojo/vo/WxMsgVO.java | 26 +++++++ .../service/NoticeService.java | 5 ++ .../service/impl/NoticeServiceImpl.java | 71 ++++++++++++++++++- .../util/HttpClientUtil.java | 4 ++ 6 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/mobile_education_platform/pojo/vo/TemplateDataVO.java create mode 100644 src/main/java/com/mobile_education_platform/pojo/vo/WxMsgVO.java diff --git a/src/main/java/com/mobile_education_platform/controller/NoticeController.java b/src/main/java/com/mobile_education_platform/controller/NoticeController.java index 53c4cd6..8897e54 100644 --- a/src/main/java/com/mobile_education_platform/controller/NoticeController.java +++ b/src/main/java/com/mobile_education_platform/controller/NoticeController.java @@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mobile_education_platform.common.R; import com.mobile_education_platform.pojo.*; import com.mobile_education_platform.pojo.Class; +import com.mobile_education_platform.pojo.vo.TemplateDataVO; +import com.mobile_education_platform.pojo.vo.WxMsgVO; import com.mobile_education_platform.service.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -15,6 +17,7 @@ import org.apache.commons.collections.map.LinkedMap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; @@ -55,6 +58,9 @@ public class NoticeController { @Autowired public ClassService classService; + @Autowired + public LoginService loginService; + @ApiOperation(value = "系统消息-查询接口", notes = "查询本人参与的逻辑删除字段不为0的班级的通知,以学期为条件来排序查询。") @RequestMapping(value = "/notices/{schoolSemester}/{id}", method = RequestMethod.GET) public R inNotice(@PathVariable("id") String id, @@ -195,5 +201,29 @@ public class NoticeController { } return data; } + + @ApiOperation("订阅消息群发") + @ApiImplicitParam(name = "用户openId",value = "openId",paramType = "string") + @RequestMapping(value = "/publishMsg",method = RequestMethod.GET) + public R publishMsg(String openId) throws IOException { + if (openId==null || "".equals(openId)){ + return R.error("请检查openId是否有误!"); + } + Map token = loginService.getAccessToken(); + String accessToken = (String)token.get("access_token"); + String url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken; + WxMsgVO msgVo = new WxMsgVO(); + msgVo.setAccess_token(accessToken); + msgVo.setTemplate_id("tkHjDhvUgu5kf9X_DFLsZt3IeHwjgH_YuBOYCUtvCKk"); + msgVo.setRequest_url(url); + msgVo.setTouser(openId); + List dataList=new ArrayList<>(); + dataList.add(new TemplateDataVO("教师")); + dataList.add(new TemplateDataVO(new Date().toString())); + dataList.add(new TemplateDataVO("该教师发布了新通知,请及时查看。")); + msgVo.setParams(dataList); + String s = noticeService.sendSubscribeMessage(msgVo); + return R.ok("发布成功!"); + } } diff --git a/src/main/java/com/mobile_education_platform/pojo/vo/TemplateDataVO.java b/src/main/java/com/mobile_education_platform/pojo/vo/TemplateDataVO.java new file mode 100644 index 0000000..0d4cb9e --- /dev/null +++ b/src/main/java/com/mobile_education_platform/pojo/vo/TemplateDataVO.java @@ -0,0 +1,23 @@ +package com.mobile_education_platform.pojo.vo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 用于微信小程序发送模板信息时的data + * @Author: ChenZikang + * @Date: 2021/10/27 8:45 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class TemplateDataVO { + private String key; + private String value; + private String color = "#ffffff"; + + public TemplateDataVO(String value) { + this.value = value; + } +} diff --git a/src/main/java/com/mobile_education_platform/pojo/vo/WxMsgVO.java b/src/main/java/com/mobile_education_platform/pojo/vo/WxMsgVO.java new file mode 100644 index 0000000..54b7c55 --- /dev/null +++ b/src/main/java/com/mobile_education_platform/pojo/vo/WxMsgVO.java @@ -0,0 +1,26 @@ +package com.mobile_education_platform.pojo.vo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +/** + * 封装,用于微信小程序发送模板信息时组装请求 + * @Author: ChenZikang + * @Date: 2021/10/27 8:43 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class WxMsgVO { + private String touser; + private String template_id; + private String page; + private String form_id; + private String access_token; + private String request_url; + private List params = new ArrayList<>(); +} \ No newline at end of file diff --git a/src/main/java/com/mobile_education_platform/service/NoticeService.java b/src/main/java/com/mobile_education_platform/service/NoticeService.java index 21a0d9b..b05fe17 100644 --- a/src/main/java/com/mobile_education_platform/service/NoticeService.java +++ b/src/main/java/com/mobile_education_platform/service/NoticeService.java @@ -3,8 +3,11 @@ package com.mobile_education_platform.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mobile_education_platform.pojo.Notice; import com.baomidou.mybatisplus.extension.service.IService; +import com.mobile_education_platform.pojo.vo.WxMsgVO; +import java.io.IOException; import java.util.List; +import java.util.Map; /** *

@@ -35,4 +38,6 @@ public interface NoticeService extends IService { * @return Page对象 */ Page getNoticePage(int pageNum,int pageCount,String classId); + + String sendSubscribeMessage(WxMsgVO vo) throws IOException; } diff --git a/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java b/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java index 85b6f0d..41a6b19 100644 --- a/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java +++ b/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java @@ -1,15 +1,25 @@ package com.mobile_education_platform.service.impl; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mobile_education_platform.pojo.Notice; import com.mobile_education_platform.mapper.NoticeMapper; +import com.mobile_education_platform.pojo.vo.WxMsgVO; +import com.mobile_education_platform.service.LoginService; import com.mobile_education_platform.service.NoticeService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import org.aspectj.weaver.ast.Not; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.List; +import java.util.Map; /** *

@@ -22,6 +32,9 @@ import java.util.List; @Service public class NoticeServiceImpl extends ServiceImpl implements NoticeService { + @Autowired + public LoginService loginService; + /** * 功能: 根据通知ID的List,来得到通知对象List * @author: 陈梓康 @@ -51,4 +64,60 @@ public class NoticeServiceImpl extends ServiceImpl impleme queryWrapper.eq("class_id",classId); return baseMapper.selectPage(page,queryWrapper); } + + @Override + public String sendSubscribeMessage(WxMsgVO vo) throws IOException { + String info=""; + try { + //创建连接 + URL url = new URL(vo.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(); + obj.put("touser",vo.getTouser()); + obj.put("template_id",vo.getTemplate_id()); + JSONObject jsonObject = new JSONObject(); + + for (int i = 0; i < vo.getParams().size(); i++) { + JSONObject dataInfo = new JSONObject(); + dataInfo.put("value", vo.getParams().get(i).getValue()); + dataInfo.put("color", vo.getParams().get(i).getColor()); + jsonObject.put("keyword" + (i + 1), dataInfo); + } + + obj.put("data", jsonObject); + 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 (IOException e) { + e.printStackTrace(); + } + + return info; + } } diff --git a/src/main/java/com/mobile_education_platform/util/HttpClientUtil.java b/src/main/java/com/mobile_education_platform/util/HttpClientUtil.java index 0a73985..199f4d3 100644 --- a/src/main/java/com/mobile_education_platform/util/HttpClientUtil.java +++ b/src/main/java/com/mobile_education_platform/util/HttpClientUtil.java @@ -1,14 +1,18 @@ package com.mobile_education_platform.util; +import com.alibaba.fastjson.JSONObject; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Component; import java.io.IOException; +import java.net.URL; /** * @Description: 请求工具类 -- Gitee