From 47ea3909ac497d0c73e9eec46a3c22b676067f82 Mon Sep 17 00:00:00 2001 From: czk Date: Tue, 30 Nov 2021 15:58:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=91=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pojo/vo/WxMsgVO.java | 54 ++++++------------- .../service/impl/NoticeServiceImpl.java | 8 +-- ...bileEducationPlatformApplicationTests.java | 18 ++++++- 3 files changed, 38 insertions(+), 42 deletions(-) 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 index 904a5a6..b54d16c 100644 --- a/src/main/java/com/mobile_education_platform/pojo/vo/WxMsgVO.java +++ b/src/main/java/com/mobile_education_platform/pojo/vo/WxMsgVO.java @@ -5,6 +5,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -21,7 +22,7 @@ public class WxMsgVO { private String touser; private String template_id; private String page; - private Map data = new HashMap<>(); + private Map> data = new HashMap<>(); private String miniprogram_state = "formal"; private String lang = "zh_CN"; private String form_id; @@ -36,47 +37,24 @@ public class WxMsgVO { msgVO.setTemplate_id(TEMPLATE_ID); msgVO.setRequest_url(url); msgVO.setTouser(openId); - Map detail=new HashMap<>(); - detail.put("thing1","该老师发布了通知,请及时查看!"); - detail.put("thing2","家庭作业"); + Map> detail=new HashMap<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日"); String date = dateFormat.format(new Date()); - detail.put("time4","老师"); - detail.put("thing1",date); - return msgVO; - } - - public static WxMsgVO getSchoolNoticeMsgVo(String accessToken, String url, String openId){ - WxMsgVO msgVO = new WxMsgVO(); - msgVO.setAccess_token(accessToken); - msgVO.setTemplate_id(TEMPLATE_ID); - msgVO.setRequest_url(url); - msgVO.setTouser(openId); - Map detail=new HashMap<>(); - detail.put("thing1","学校发布了通知,请及时查看!"); - detail.put("thing2","学校通知"); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日"); - String date = dateFormat.format(new Date()); - detail.put("time4","校方"); - detail.put("thing1",date); + Map thing1= new HashMap<>(); + thing1.put("value","该教师发布了通知请及时查看"); + Map thing2= new HashMap<>(); + thing2.put("value","通知"); + Map thing4= new HashMap<>(); + thing4.put("value","教师"); + Map time3= new HashMap<>(); + time3.put("value",date); + detail.put("thing1",thing1); + detail.put("thing2",thing2); + detail.put("thing4",thing4); + detail.put("time3",time3); + System.out.println(Arrays.toString(new Map[]{detail})); msgVO.setData(detail); return msgVO; } - public static WxMsgVO getSchoolBulletinMsgVo(String accessToken, String url, String openId){ - WxMsgVO msgVO = new WxMsgVO(); - msgVO.setAccess_token(accessToken); - msgVO.setTemplate_id(TEMPLATE_ID); - msgVO.setRequest_url(url); - msgVO.setTouser(openId); - Map detail=new HashMap<>(); - detail.put("thing1","学校发布了公告,请及时查看!"); - detail.put("thing2","学校公告"); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日"); - String date = dateFormat.format(new Date()); - detail.put("time4","校方"); - detail.put("thing1",date); - msgVO.setData(detail); - return msgVO; - } } \ No newline at end of file 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 9e96168..920ef06 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 @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -98,12 +99,13 @@ public class NoticeServiceImpl extends ServiceImpl impleme obj.put("template_id",vo.getTemplate_id()); JSONObject jsonObject = new JSONObject(); - for(Map.Entry entry:vo.getData().entrySet()){ - JSONObject dataInfo=new JSONObject(); - dataInfo.put(entry.getKey(),entry.getValue()); + for(Map.Entry> entry:vo.getData().entrySet()){ + jsonObject.put(entry.getKey(),entry.getValue()); } + System.out.println(jsonObject); obj.put("data", jsonObject); + System.out.println(obj); out.write(obj.toString().getBytes()); out.flush(); out.close(); diff --git a/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java b/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java index 1cf187f..be8f2ce 100644 --- a/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java +++ b/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java @@ -1,13 +1,29 @@ package com.mobile_education_platform; +import com.mobile_education_platform.pojo.vo.WxMsgVO; +import com.mobile_education_platform.service.LoginService; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + @SpringBootTest class MobileEducationPlatformApplicationTests { + @Autowired + private LoginService loginService; + @Test - void contextLoads() { + void contextLoads() throws IOException { + Map data=new HashMap<>(); + 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 = WxMsgVO.getHomeWorkMsgVo(accessToken, url, "o8fr25H9CBgPXu6BRIrGNe2aK6kE"); + System.out.println(msgVo); } } -- Gitee