diff --git a/src/main/java/com/mobile_education_platform/aspect/SendNoticeAspect.java b/src/main/java/com/mobile_education_platform/aspect/SendNoticeAspect.java new file mode 100644 index 0000000000000000000000000000000000000000..4ec9a34385459e3a366ec9fdec586771b9b825ca --- /dev/null +++ b/src/main/java/com/mobile_education_platform/aspect/SendNoticeAspect.java @@ -0,0 +1,102 @@ +package com.mobile_education_platform.aspect; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.mobile_education_platform.common.R; +import com.mobile_education_platform.controller.NoticeController; +import com.mobile_education_platform.pojo.ClassNotice; +import com.mobile_education_platform.pojo.Notice; +import com.mobile_education_platform.pojo.StudentClass; +import com.mobile_education_platform.pojo.vo.WxMsgVO; +import com.mobile_education_platform.service.*; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import lombok.SneakyThrows; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * 功能: 发布通知后对用户的转发,由于微信小程序只能单体发送,故使用AOP + * + * @Author 陈梓康 + * @create 2021/11/4 23:37 + */ +@Component +@Aspect +public class SendNoticeAspect { + + @Autowired + public StudentService studentService; + + @Autowired + public LoginService loginService; + + @Autowired + public NoticeService noticeService; + + @Autowired + public StudentClassService studentClassService; + + @Autowired + public UserService userService; + + private Logger logger = LoggerFactory.getLogger(SendNoticeAspect.class); + + @Pointcut("execution(* com.mobile_education_platform.controller.NoticeController.addNotice(..))") + public void sendHomeworkNotice() { + } + + @After("sendHomeworkNotice()") + public void send(JoinPoint joinPoint) throws IOException { + //获取参数,随后获取wechatID进行转发 + new Thread(){ + @SneakyThrows + @Override + public void run() { + List classIds=(List)joinPoint.getArgs()[1]; + int count=0; + for(String classId:classIds){ + List studentClassList = studentClassService.listByClassId(classId); + List studentIds = new ArrayList<>(); + for (StudentClass studentClass : studentClassList) { + studentIds.add(studentClass.getStudentId()); + } + //调用群发功能,需要根据学生们的student_id来获取wechat_id进行单发(通知只支持一对一发送) + List numbers = studentService.listNumberById(studentIds); + List studentWechatIds= userService.listWechatIdByUserNumber(numbers); + for (String wechatId:studentWechatIds){ + count++; + publishMsg(wechatId); + } + } + logger.info("已完成群发。转发人数为:"+count); + } + }.start(); + } + + public void publishMsg(String openId) throws IOException { + if (openId==null || "".equals(openId)){ + logger.info("该用户openId为:"+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 = WxMsgVO.getHomeWorkMsgVo(accessToken, url, openId); + String result = noticeService.sendSubscribeMessage(msgVo); + JSONObject sendResult= JSON.parseObject(result); + Integer errcode = (Integer) sendResult.get("errcode"); + logger.info("该用户openId为:"+openId+",错误码为"+errcode); + } + +} \ No newline at end of file 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 0f82156a61f98041a84c5da55836de96baf79e73..9a7d402f0a7f3542cccbe9d38c622c00384e6f7a 100644 --- a/src/main/java/com/mobile_education_platform/controller/NoticeController.java +++ b/src/main/java/com/mobile_education_platform/controller/NoticeController.java @@ -14,6 +14,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -164,6 +165,7 @@ public class NoticeController { }) public R addNotice(@RequestBody Notice notice, @RequestParam List classIds) throws IOException { + notice.setIsDeleted(1); boolean saveNotice = noticeService.save(notice); if (!saveNotice) { @@ -179,18 +181,19 @@ public class NoticeController { if (!saveClassNotice) { return R.error("请检查班级是否有误!"); } - List studentClassList = studentClassService.listByClassId(classId); - List studentIds = new ArrayList<>(); - for (StudentClass studentClass : studentClassList) { - studentIds.add(studentClass.getStudentId()); - } - //调用群发功能,需要根据学生们的student_id来获取wechat_id进行单发(通知只支持一对一发送) - List studentWechatIds= userService.listWechatIdByUserNumber(studentIds); - for (String wechatId:studentWechatIds){ - publishMsg(wechatId); - } +// List studentClassList = studentClassService.listByClassId(classId); +// List studentIds = new ArrayList<>(); +// for (StudentClass studentClass : studentClassList) { +// studentIds.add(studentClass.getStudentId()); +// } +// 调用群发功能,需要根据学生们的student_id来获取wechat_id进行单发(通知只支持一对一发送) +// List numbers = studentService.listNumberById(studentIds); +// List studentWechatIds= userService.listWechatIdByUserNumber(numbers); +// for (String wechatId:studentWechatIds){ +// publishMsg(wechatId); +// } } - return R.ok("新发布通知已完成群发。"); + return R.ok("通知发布成功!后台正在转发至对应班级。"); } /** 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 764bc23d813c4f8f0c20e7ea76472b537135087e..37602f6855afc190153b937854e55148ae9401e0 100644 --- a/src/main/java/com/mobile_education_platform/service/NoticeService.java +++ b/src/main/java/com/mobile_education_platform/service/NoticeService.java @@ -66,4 +66,6 @@ public interface NoticeService extends IService { * @param id 通知ID */ Integer updateDeletedState(String id); + + public String sendSubscribeMessagetest(Map send,String sendUrl) throws IOException; } diff --git a/src/main/java/com/mobile_education_platform/service/StudentService.java b/src/main/java/com/mobile_education_platform/service/StudentService.java index a4c23cf0ba7d3b67bb34c7fc7b8d9e0dc5792216..bb9c63984ac25ba425541edde2e593f48a0c0341 100644 --- a/src/main/java/com/mobile_education_platform/service/StudentService.java +++ b/src/main/java/com/mobile_education_platform/service/StudentService.java @@ -3,6 +3,8 @@ package com.mobile_education_platform.service; import com.mobile_education_platform.pojo.Student; import com.baomidou.mybatisplus.extension.service.IService; +import java.util.List; + /** *

* 服务类 @@ -22,4 +24,12 @@ public interface StudentService extends IService { */ String getIdByNumber(String number); + /** + * 功能: 根据List学生ID来获取List学生编号 + * @author: 陈梓康 + * @Created: 2021/11/6 13:47 + * @param studentIds + * @return List学生编号 + */ + List listNumberById(List studentIds); } 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 72f3f4e39702d48adf11c4bcbea28a8b8b4e6178..58ef300c2da5cfd2ae9fec365496de4a1fcb8b2c 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 @@ -159,4 +159,60 @@ public class NoticeServiceImpl extends ServiceImpl impleme updateWrapper.eq("id",id); return baseMapper.update(notice, updateWrapper); } + + @Override + public String sendSubscribeMessagetest(Map send,String sendUrl) throws IOException { + String info=""; + try { + //创建连接 + URL url = new URL(sendUrl); + 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",send.get("tourser")); + obj.put("template_id",send.get("template_id")); + JSONObject jsonObject = new JSONObject(); + + Map data = (Map)send.get("data"); + + for (Map.Entry entry : data.entrySet()){ + JSONObject dataInfo=new JSONObject(); + dataInfo.put(entry.getKey(),entry.getValue()); + } + + 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/service/impl/StudentServiceImpl.java b/src/main/java/com/mobile_education_platform/service/impl/StudentServiceImpl.java index 4ed0e513f6bfd19bddf82c87457e0ef920ad3610..7491de926d80a9b3f0a78fccd8d04cbbc5dd769a 100644 --- a/src/main/java/com/mobile_education_platform/service/impl/StudentServiceImpl.java +++ b/src/main/java/com/mobile_education_platform/service/impl/StudentServiceImpl.java @@ -8,6 +8,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import sun.misc.BASE64Decoder; +import java.util.ArrayList; +import java.util.List; + /** *

* 服务实现类 @@ -32,4 +35,23 @@ public class StudentServiceImpl extends ServiceImpl impl Student student = baseMapper.selectOne(queryWrapper); return student.getId(); } + + /** + * 功能: 根据List学生ID来获取List学生编号 + * @author: 陈梓康 + * @Created: 2021/11/6 13:47 + * @param studentIds + * @return List学生编号 + */ + @Override + public List listNumberById(List studentIds) { + List numbers=new ArrayList<>(); + for (String studentId:studentIds){ + QueryWrapper queryWrapper=new QueryWrapper<>(); + queryWrapper.eq("id",studentId); + Student student = baseMapper.selectOne(queryWrapper); + numbers.add(student.getNumber()); + } + return numbers; + } }