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 904a5a6a82640b9c8e3b76741caa959a5c818b52..b54d16c3b37ca14512b18832d9b7a6aeb7023380 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 9e96168044c042996e7bdab47dccc51b53ae1658..920ef066299012e5525ab39c43fa0f2e9167b43f 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 1cf187f3ca1d7daea86ae1ed68e9af60fbc69407..be8f2ce13978ab572b845e375ab19b0362f3e8ad 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); } }