Browse Source

添加 微信消息推送

DESKTOP-USV654P\pc 1 year ago
parent
commit
127cebc4e8

+ 25 - 19
src/main/java/com/xjrsoft/common/utils/WeChatUtil.java

@@ -2,6 +2,7 @@ package com.xjrsoft.common.utils;
 
 
 import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.xjrsoft.common.exception.MyException;
 import com.xjrsoft.module.organization.dto.WeChatDepartDto;
@@ -52,13 +53,17 @@ public class WeChatUtil {
     //获取通讯录或发消息token
     public String getToken(Integer type) {
         HashMap<String, Object> paramMap = new HashMap<>();
-        paramMap.put("corpid", appKey);
-        if(type==1) {
+        String url = "https://api.weixin.qq.com/cgi-bin/token";
+        if (type == 1) {
+            url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
             paramMap.put("corpsecret", appSecret);
-        }else if(type==2){
-            paramMap.put("corpsecret", appSecret1);
+            paramMap.put("corpid", appKey);
+        } else if (type == 2) {
+            paramMap.put("grant_type", "client_credential");
+            paramMap.put("appid", appKey);
+            paramMap.put("secret", appSecret1);
         }
-        String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken", paramMap);
+        String result = HttpUtil.get(url, paramMap);
         JSONObject jsonObject = JSONObject.parseObject(result);
         String token = jsonObject.get("access_token").toString();
         return token;
@@ -93,18 +98,19 @@ public class WeChatUtil {
         return weChatUserDtos;
     }
 
-//    //发送消息给企业微信用户
-//    public Boolean sendMessage(XjrOaMessage xjrOaMessage, String userId) {
-//        String token = this.getToken(2);
-//        JSONObject object = new JSONObject();
-//        object.put("touser", userId);
-//        object.put("msgtype", "text");
-//        object.put("agentid", agentid);
-//        JSONObject text = new JSONObject();
-//        text.put("content", xjrOaMessage.getContent());
-//        object.put("text",text);
-//        String result = HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token, JSONObject.toJSONString(object));
-//        System.out.printf(result);
-//        return true;
-//    }
+    //公众号发送文本消息给用户
+    public Boolean sendMessage(String content, String userId) {
+        String token = this.getToken(2);
+        JSONObject object = new JSONObject();
+        JSONArray touser=new JSONArray();
+        touser.add(userId);
+        object.put("touser", touser);
+        object.put("msgtype", "text");
+        JSONObject text = new JSONObject();
+        text.put("content", content);
+        object.put("text",text);
+        String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token="+token, JSONObject.toJSONString(object));
+        System.out.printf(result);
+        return true;
+    }
 }

+ 5 - 2
src/main/java/com/xjrsoft/module/oa/dto/AddNewsRelationDto.java

@@ -6,8 +6,11 @@ import lombok.Data;
 @Data
 public class AddNewsRelationDto {
 
-    @ApiModelProperty("部门ID")
-    private Long deptId;
+    @ApiModelProperty("关系类型 1=部门 2=人员")
+    private Integer relationType;
+
+    @ApiModelProperty("关系ID")
+    private Long relationId;
 
     @ApiModelProperty("新闻主键")
     private Long newsId;

+ 1 - 1
src/main/java/com/xjrsoft/module/oa/entity/News.java

@@ -119,5 +119,5 @@ public class News extends AuditEntity implements Serializable {
     @ApiModelProperty("附件子表")
     @TableField(exist = false)
     @EntityMapping(thisField = "id", joinField = "newsId")
-    private List<NewsAppendix> newsAppendixList;
+    private List<NewsAppendix> appendixList;
 }

+ 8 - 1
src/main/java/com/xjrsoft/module/oa/service/INewsService.java

@@ -1,6 +1,7 @@
 package com.xjrsoft.module.oa.service;
 
 import com.github.yulichang.base.MPJBaseService;
+import com.xjrsoft.module.oa.dto.AddNewsDto;
 import com.xjrsoft.module.oa.entity.News;
 
 import java.util.List;
@@ -14,7 +15,13 @@ import java.util.List;
  * @since 2022-06-16
  */
 public interface INewsService extends MPJBaseService<News> {
-
+    /**
+     * 添加新闻
+     *
+     * @param addNewsDto
+     * @return
+     */
+    boolean add(AddNewsDto addNewsDto);
     /**
      * 删除新闻 以及 消息推送
      *

+ 41 - 0
src/main/java/com/xjrsoft/module/oa/service/impl/NewsServiceImpl.java

@@ -1,9 +1,19 @@
 package com.xjrsoft.module.oa.service.impl;
 
+import cn.hutool.core.bean.BeanUtil;
 import com.github.yulichang.base.MPJBaseServiceImpl;
+import com.xjrsoft.module.oa.dto.AddNewsAppendixDto;
+import com.xjrsoft.module.oa.dto.AddNewsDto;
+import com.xjrsoft.module.oa.dto.AddNewsRelationDto;
 import com.xjrsoft.module.oa.entity.News;
+import com.xjrsoft.module.oa.entity.NewsAppendix;
+import com.xjrsoft.module.oa.entity.NewsRelation;
+import com.xjrsoft.module.oa.mapper.NewsAppendixMapper;
 import com.xjrsoft.module.oa.mapper.NewsMapper;
+import com.xjrsoft.module.oa.mapper.NewsRelationMapper;
 import com.xjrsoft.module.oa.service.INewsService;
+import com.xjrsoft.module.student.entity.BaseStudentContact;
+import com.xjrsoft.module.student.entity.BaseStudentUser;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -25,6 +35,37 @@ public class NewsServiceImpl extends MPJBaseServiceImpl<NewsMapper, News> implem
     private final NewsMapper newsMapper;
 
 
+    private final NewsAppendixMapper newsAppendixMapper;
+    private final NewsRelationMapper newsRelationMapper;
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean add(AddNewsDto addNewsDto) {
+
+        News news = BeanUtil.toBean(addNewsDto, News.class);
+        newsMapper.insert(news);
+
+        for (AddNewsAppendixDto appendixDto : addNewsDto.getAppendixList()) {
+            NewsAppendix newsAppendix =BeanUtil.toBean(appendixDto, NewsAppendix.class);
+            newsAppendix.setNewsId(news.getId());
+            newsAppendixMapper.insert(newsAppendix);
+        }
+
+        for (AddNewsRelationDto relationDto : addNewsDto.getRelationList()) {
+            NewsRelation newsRelation = new NewsRelation();// BeanUtil.toBean(relationDto, NewsRelation.class);
+            newsRelation.setNewsId(news.getId());
+            newsRelation.setReadMark(0);
+            if (relationDto.getRelationType() == 1) {
+                // 获取部门下的人员添加
+            } else {
+                newsRelation.setUserId(relationDto.getRelationId());
+                newsRelationMapper.insert(newsRelation);
+            }
+        }
+
+        return true;
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean delete(List<Long> ids) {

+ 9 - 0
src/main/java/com/xjrsoft/module/organization/controller/WechatController.java

@@ -12,6 +12,7 @@ import com.xjrsoft.common.utils.RedisUtil;
 import com.xjrsoft.common.utils.VoToColumnUtil;
 import com.xjrsoft.module.organization.dto.WeChatDepartPageDto;
 import com.xjrsoft.module.organization.dto.WeChatPageDto;
+import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
 import com.xjrsoft.module.organization.entity.Department;
 import com.xjrsoft.module.organization.entity.User;
 import com.xjrsoft.module.organization.service.IDepartmentService;
@@ -70,4 +71,12 @@ public class WechatController {
         return  R.ok(departmentService.Ipage(dto));
     }
 
+    @PostMapping("/send-message")
+    @ApiOperation(value="微信公众号发送消息")
+    public R sendMessage(WeChatSendMessageDto dto){
+
+        WeChatService.sendMessage(dto);
+        return R.ok();
+    }
+
 }

+ 9 - 0
src/main/java/com/xjrsoft/module/organization/dto/WeChatSendMessageDto.java

@@ -0,0 +1,9 @@
+package com.xjrsoft.module.organization.dto;
+
+import lombok.Data;
+
+@Data
+public class WeChatSendMessageDto {
+    private String content;
+    private String userId;
+}

+ 2 - 1
src/main/java/com/xjrsoft/module/organization/service/WeChatService.java

@@ -1,9 +1,10 @@
 package com.xjrsoft.module.organization.service;
 
 
+import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
 
 public interface WeChatService {
 
-
+    void sendMessage(WeChatSendMessageDto dto);
     boolean updateInfo();
 }

+ 6 - 0
src/main/java/com/xjrsoft/module/organization/service/impl/WeChatServiceImgl.java

@@ -11,6 +11,7 @@ import com.xjrsoft.common.enums.EnabledMark;
 import com.xjrsoft.common.exception.MyException;
 import com.xjrsoft.common.utils.WeChatUtil;
 import com.xjrsoft.module.organization.dto.WeChatDepartDto;
+import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
 import com.xjrsoft.module.organization.dto.WeChatUserDto;
 import com.xjrsoft.module.organization.entity.Department;
 import com.xjrsoft.module.organization.entity.User;
@@ -45,6 +46,11 @@ public class WeChatServiceImgl implements WeChatService {
         return this.updatedepart();
     }
 
+    @Override
+    public  void sendMessage(WeChatSendMessageDto dto){
+        weChatUtil.sendMessage(dto.getContent(),dto.getUserId());
+    }
+
     private boolean updatedepart() {
 //        List<Department> departmentList = departmentService.list(Wrappers.<Department>query().lambda().eq(Department::getEnabledMark, EnabledMark.ENABLED.getCode()).eq(Department::getDeleteMark, DeleteMark.NODELETE.getCode()));
 //        ArrayList<Department> updateDeparts = new ArrayList<>();