Преглед изворни кода

微信小程序推送信息修改

junfeitang пре 3 година
родитељ
комит
b1235cc8d0

+ 12 - 0
boman-api/boman-api-wechat/src/main/java/com/boman/wechat/api/RemoteWechatService.java

@@ -3,11 +3,14 @@ package com.boman.wechat.api;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.domain.constant.ServiceNameConstants;
 import com.boman.domain.dto.AjaxResult;
+import com.boman.domain.dto.WxMsgDto;
 import com.boman.domain.form.LoginBody;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 
+import java.util.Map;
+
 @FeignClient(contextId = "remoteObjService", value = ServiceNameConstants.WECHAT_SERVICE)
 public interface RemoteWechatService {
 
@@ -19,4 +22,13 @@ public interface RemoteWechatService {
      */
     @PostMapping("/wechat/p/c/wechatInfo")
     AjaxResult getWechatInfo(@RequestBody LoginBody loginBody);
+
+    /**
+     * 发送消息
+     *
+     * @param dto
+     * @return
+     */
+    @PostMapping("/wechat/p/c/pushMsg")
+    public Map<String, Object> pushMsg(@RequestBody WxMsgDto dto);
 }

+ 1 - 1
boman-api/boman-domain/src/main/java/com.boman.domain/dto/WxMsgDto.java

@@ -9,7 +9,7 @@ public class WxMsgDto {
 
     private String touser;//用户openid
     private String template_id;//模版id
-    private String page = "pages/index/index";//默认跳到小程序首页
+    private String page = "pages/indexView/addressee/index";//默认跳到小程序首页
     private String emphasis_keyword = "title.DATA";//放大那个推送字段
     private Map<String, TemplateData> data;//推送文字
     private List<Long> ids;

+ 6 - 0
boman-web-core/pom.xml

@@ -106,6 +106,12 @@
             <artifactId>boman-api-jflow</artifactId>
             <version>2.5.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>com.boman</groupId>
+            <artifactId>boman-api-wechat</artifactId>
+            <version>2.5.0-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
 
     </dependencies>
 

+ 39 - 17
boman-web-core/src/main/java/com/boman/web/core/service/TableServiceCmdService.java

@@ -32,13 +32,13 @@ import com.boman.web.core.service.update.IBaseUpdateService;
 import com.boman.web.core.utils.ColumnUtils;
 import com.boman.web.core.utils.IdUtils;
 import com.boman.web.core.utils.JSONObjectUtils;
+import com.boman.wechat.api.RemoteWechatService;
 import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
 import org.apache.commons.lang3.BooleanUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.aop.framework.AopContext;
-import org.springframework.aop.support.AopUtils;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
@@ -92,6 +92,9 @@ public class TableServiceCmdService {
     private RemoteMenuService remoteMenuService;
     @Resource
     private RemoteJflowProcessService remoteJflowProcessService;
+    @Resource
+    private RemoteWechatService remoteWechatService;
+
 
     private static final Logger LOGGER = LoggerFactory.getLogger(TableServiceCmdService.class);
 
@@ -114,7 +117,7 @@ public class TableServiceCmdService {
         //判断时候有定制新增接口
         List<JSONObject> listData = new ArrayList<>();
         listData.add(context.getCommitData());
-        isCustomized(context.getTableName(),listData,"trigger_create");
+        isCustomized(context.getTableName(), listData, "trigger_create");
 
         AjaxResult result;
         // 新增
@@ -169,6 +172,8 @@ public class TableServiceCmdService {
         List<AjaxResult> resultList = new ArrayList<>(result.size());
         Map<String, Long> tableNameIdMap = new HashMap<>(result.size());
         AjaxResult ajaxResult;
+        List<Long> receiveUserIds = new ArrayList<>();
+        Map<String, Object> messageInfo = new HashMap<>();
         for (int i = 0; i < result.size(); i++) {
             FormDataDto formDataDto = result.get(i);
             boolean optPrimaryTable = i == 0;
@@ -188,8 +193,25 @@ public class TableServiceCmdService {
 
             tableNameIdMap.put(tableName, fkId);
             resultList.add(ajaxResult);
-        }
 
+            if (dto.getTable().equals("boman_message")) {
+                if (optPrimaryTable) {
+                    messageInfo.put("thing1", formDataDto.getFixedData().getString("message_title"));
+                    messageInfo.put("thing2", formDataDto.getFixedData().getString("send_user_name"));
+                } else {
+                    receiveUserIds.add(formDataDto.getFixedData().getLong("receive_user_id"));
+                }
+            }
+
+        }
+        // 获取发文相关信息
+        if (dto.getTable().equals("boman_message")) {
+            WxMsgDto wxMsgDto = new WxMsgDto();
+            wxMsgDto.setIds(receiveUserIds);
+            wxMsgDto.setParams(messageInfo);
+            LOGGER.info("收文人id:{}, 发送数据: {}", receiveUserIds, messageInfo);
+            remoteWechatService.pushMsg(wxMsgDto);
+        }
         return AjaxResult.success(resultList);
     }
 
@@ -576,7 +598,7 @@ public class TableServiceCmdService {
                 // dateTime
                 if (NEED_CONVERT_DATE_LIST.contains(columnType)) {
                     assert value instanceof Timestamp;
-                    column.setColumnValue(getStrDate((Timestamp)value));
+                    column.setColumnValue(getStrDate((Timestamp) value));
                 }
                 // fk
                 if (isNotEmpty(column.getForeignKey())) {
@@ -586,7 +608,7 @@ public class TableServiceCmdService {
                 }
                 // annex
                 if (HTML_IMAGE_UPLOAD.equalsIgnoreCase(htmlType) || HTML_FILE_UPLOAD.equalsIgnoreCase(htmlType)) {
-                    column.setAnnex(getAnnex((String)value));
+                    column.setAnnex(getAnnex((String) value));
                 }
 
                 children.add(column);
@@ -622,7 +644,7 @@ public class TableServiceCmdService {
         GenTable genTable = getTableFromRedisByTableName(RedisKey.TABLE_INFO, dto.getTable());
 
         JSONObject jsonObject = selectService.selectById(dto.getTable(), IdUtils.getPkName(genTable.getColumns()), dto.getObjId(), dto.getOther());
-        if(jsonObject == null) {
+        if (jsonObject == null) {
             return AjaxResult.error("没有表单数据", jsonObject);
         }
         List<GenTableColumn> columns = genTable.getColumns();
@@ -648,7 +670,7 @@ public class TableServiceCmdService {
         }
         JSONObject result = new JSONObject();
         result.put("childs", addcolums);
-        result.put("size",columns.size());
+        result.put("size", columns.size());
 
         JSONArray adc = new JSONArray();
         adc.add(result);
@@ -876,9 +898,9 @@ public class TableServiceCmdService {
         for (JSONObject commitDatum : commitData) {
             String dbStatus = getStatusFromFormData(commitDatum, beforeList);
             // 只能是未提交的状态下,才能调用提交
-            if(StringUtils.isNotEmpty(condition.getSubmitSource())) {
+            if (StringUtils.isNotEmpty(condition.getSubmitSource())) {
                 handlerSubmit(tableName, pkName, commitDatum);
-            }else {
+            } else {
                 if (isSubmit) {
                     // 只能是未提交的状态下,才能调用提交, 下面是不允许
                     if (!SubmitConstant.NOT_SUBMIT.equals(dbStatus)) {
@@ -913,7 +935,7 @@ public class TableServiceCmdService {
         String tableName = requireNonNull(genTable.getTableName(), "tableName = [" + genTable.getTableName() + "] 此表不存在");
 
         List<JSONObject> commitData = condition.getCommitData();
-        for(JSONObject commitDatum : commitData) {
+        for (JSONObject commitDatum : commitData) {
             Long foreignKey = Long.valueOf(commitDatum.get("foreignKey").toString());
             String foreignKeyName = (String) commitDatum.get("foreignKeyName");
             this.executeData(tableName, foreignKeyName, foreignKey, (Map<String, Object>) commitDatum.get("updateData"));
@@ -925,10 +947,10 @@ public class TableServiceCmdService {
     /**
      * 功能描述: 修改数据
      *
-     * @param tableName   tableName
+     * @param tableName      tableName
      * @param foreignKeyName 外键名
-     * @param foreignKey  外键值
-     * @param commitDatum 返回给前台的数据
+     * @param foreignKey     外键值
+     * @param commitDatum    返回给前台的数据
      */
     private void executeData(String tableName, String foreignKeyName, Long foreignKey, Map<String, Object> commitDatum) {
         int effective = submitService.executeData(tableName, foreignKeyName, foreignKey, new JSONObject(commitDatum));
@@ -1127,7 +1149,7 @@ public class TableServiceCmdService {
     }
 
     private void addOtherColumn(List<GenTableColumn> columns, String other) {
-        if(StringUtils.isEmpty(other)) {
+        if (StringUtils.isEmpty(other)) {
             return;
         }
 
@@ -1143,7 +1165,7 @@ public class TableServiceCmdService {
 
         String dictType = column.getDictType();
         String columnName = column.getColumnName();
-        if(StringUtils.isEmpty(other)) {
+        if (StringUtils.isEmpty(other)) {
             jo.put("valuedata", jsonObject.get(columnName));
             return;
         }
@@ -1153,7 +1175,7 @@ public class TableServiceCmdService {
             List<SysDictData> sysDictData = column.getSysDictData();
             //获取到字段的值
             String value = String.valueOf(jsonObject.get(columnName));
-            if (sysDictData != null && sysDictData.size() > 0){
+            if (sysDictData != null && sysDictData.size() > 0) {
                 for (SysDictData sysDictDatum : sysDictData) {
                     if (sysDictDatum.getDictValue().equals(value)) {
                         jo.put("valuedata", sysDictDatum.getDictLabel());
@@ -1161,7 +1183,7 @@ public class TableServiceCmdService {
                     }
                 }
             }
-        }else{
+        } else {
             jo.put("valuedata", jsonObject.get(columnName));
         }
     }

+ 22 - 1
boman-web-core/src/main/java/com/boman/web/core/service/save/BaseSaveServiceImpl.java

@@ -5,10 +5,12 @@ import com.boman.common.redis.service.RedisService;
 import com.boman.domain.GenTableColumn;
 import com.boman.domain.constant.FormDataConstant;
 import com.boman.domain.constant.TableNameConst;
+import com.boman.domain.dto.WxMsgDto;
 import com.boman.web.core.domain.RowResult;
 import com.boman.web.core.mapper.StandardMapper;
 import com.boman.web.core.utils.ColumnUtils;
 import com.boman.web.core.utils.IdUtils;
+import com.boman.wechat.api.RemoteWechatService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -17,7 +19,9 @@ import org.springframework.stereotype.Component;
 import javax.annotation.Resource;
 import java.sql.Timestamp;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import static com.boman.common.core.utils.obj.ObjectUtils.*;
 import static com.boman.web.core.utils.ColumnUtils.*;
@@ -36,6 +40,9 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
     private StandardMapper mapper;
     @Resource
     private RedisService redisService;
+    @Resource
+    private RemoteWechatService remoteWechatService;
+
     /**
      * 功能描述: 保存一行
      *
@@ -85,6 +92,8 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
         List<RowResult> rowResults = new ArrayList<>(dataList.size());
         List<GenTableColumn> columns = ColumnUtils.getColumnList(tableName);
         String pkName = IdUtils.getPkName(columns);
+        List<Long> receiveUserIds = new ArrayList<>();
+        Map<String, Object> messageInfo = new HashMap<>();
         for (JSONObject commitData : dataList) {
             Long maxId = IdUtils.getMaxId(tableName, pkName);
             packUpdateByAndTime(columns, commitData, new Timestamp(System.currentTimeMillis()), true);
@@ -94,8 +103,20 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
             buildBillRule(tableName, commitData, columns);
             commitData.put(pkName, maxId);
             rowResults.add(insertRow(tableName, commitData));
+            if (tableName.equals("urge_read_message")) {
+                receiveUserIds.add(commitData.getLong("receive_user_id"));
+            }
+        }
+        messageInfo.put("thing1", dataList.get(0).getString("message_title"));
+        messageInfo.put("thing2", dataList.get(0).getString("send_user_name"));
+        // 获取发文相关信息
+        if (tableName.equals("urge_read_message")) {
+            WxMsgDto wxMsgDto = new WxMsgDto();
+            wxMsgDto.setIds(receiveUserIds);
+            wxMsgDto.setParams(messageInfo);
+            LOGGER.info("收文人id:{}, 发送数据: {}", receiveUserIds, messageInfo);
+            remoteWechatService.pushMsg(wxMsgDto);
         }
-
         return rowResults;
     }
 

+ 1 - 7
boman-wechat/src/main/java/com/boman/wechat/controller/CheckPushMsgAuthController.java

@@ -26,9 +26,6 @@ public class CheckPushMsgAuthController {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(CheckPushMsgAuthController.class);
 
-    @Autowired
-    private WxPushService wxPushService;
-
     @GetMapping
     public String pushMsg(HttpServletRequest request, HttpServletResponse response) {
         // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
@@ -61,8 +58,5 @@ public class CheckPushMsgAuthController {
         return echostr;
     }
 
-    @PostMapping("/pushMsg")
-    public Map<String, Object> pushMsg(@RequestBody WxMsgDto dto) {
-       return wxPushService.pushToUser(dto.getIds(), dto.getParams());
-    }
+
 }

+ 18 - 0
boman-wechat/src/main/java/com/boman/wechat/controller/WechatInfoController.java

@@ -1,12 +1,16 @@
 package com.boman.wechat.controller;
 
 import com.boman.domain.dto.AjaxResult;
+import com.boman.domain.dto.WxMsgDto;
 import com.boman.domain.form.LoginBody;
 import com.boman.wechat.service.WechatService;
+import com.boman.wechat.service.WxPushService;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.Map;
+
 /**
  * 微信登录验证
  *
@@ -16,6 +20,9 @@ import org.springframework.web.bind.annotation.*;
 @RestController
 public class WechatInfoController {
 
+    @Autowired
+    private WxPushService wxPushService;
+
     @Autowired
     private WechatService wechatService;
 
@@ -30,4 +37,15 @@ public class WechatInfoController {
     public AjaxResult getTmpIds() {
         return wechatService.getTmpIds();
     }
+
+    /**
+     * 发送消息
+     *
+     * @param dto
+     * @return
+     */
+    @PostMapping("/pushMsg")
+    public Map<String, Object> pushMsg(@RequestBody WxMsgDto dto) {
+        return wxPushService.pushToUser(dto);
+    }
 }

+ 3 - 1
boman-wechat/src/main/java/com/boman/wechat/service/WxPushService.java

@@ -1,10 +1,12 @@
 package com.boman.wechat.service;
 
+import com.boman.domain.dto.WxMsgDto;
+
 import java.util.List;
 import java.util.Map;
 
 public interface WxPushService {
 
-    public Map<String, Object> pushToUser(List<Long> userIds, Map<String, Object> params);
+    public Map<String, Object> pushToUser(WxMsgDto dto);
 
 }

+ 4 - 3
boman-wechat/src/main/java/com/boman/wechat/service/impl/WxPushServiceImpl.java

@@ -36,8 +36,9 @@ public class WxPushServiceImpl implements WxPushService {
      * 微信小程序推送给用户
      */
     @Override
-    public Map<String, Object> pushToUser(List<Long> userIds, Map<String, Object> params) {
-        String templateId = properties.getTemplateId();
+    public Map<String, Object> pushToUser(WxMsgDto dto) {
+        List<Long> userIds = dto.getIds();
+        Map<String, Object> params = dto.getParams();
         Map<String, Object> pushResult = new HashMap<>();
 
         //获取access_token
@@ -65,7 +66,7 @@ public class WxPushServiceImpl implements WxPushService {
             //拼接推送的模版
 //            WxMsgDto wxMssVo = new WxMsgDto();
             wxMssVo.setTouser(openId);//用户openid
-            wxMssVo.setTemplate_id(templateId);//模版id
+            wxMssVo.setTemplate_id(properties.getTemplateId());//模版id
             Map<String, TemplateData> msgMap = new HashMap<>(5);
             for(String key : params.keySet()) {
                 TemplateData templateData = new TemplateData();

+ 1 - 1
boman-wechat/src/main/resources/application.properties

@@ -4,4 +4,4 @@ auth.wechat.access_token_url=https://api.weixin.qq.com/cgi-bin/token?grant_type=
 auth.wechat.appId=wxf556d2b0c34da8cf
 auth.wechat.secret=8f90a52972268f82d5017a2ac29650c4
 auth.wechat.grantType=authorization_code
-auth.wechat.template_id=lLRrKgsb4UJ_zHJSeEnIH0QNWcyERFRB9igmd6nZgRc
+auth.wechat.template_id=Dhunm8Bqy6lRDfYgJxYQnhKV6JxNIhFoov2MOJ0c6Dw

+ 10 - 0
boman-wechat/src/main/resources/bootstrap.yml

@@ -17,3 +17,13 @@ spring:
         shared-configs:
           - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
         namespace: d8110874-ad03-4826-80bc-ff00126c1644  #潜山教育局
+
+auth:
+  wechat:
+    sessionHost: https://api.weixin.qq.com/sns/jscode2session
+    msg_url: https://api.weixin.qq.com/cgi-bin/message/subscribe/send
+    access_token_url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential
+    appId: wxf556d2b0c34da8cf
+    secret: 8f90a52972268f82d5017a2ac29650c4
+    grantType: authorization_code
+    template_id: Dhunm8Bqy6lRDfYgJxYQnhKV6JxNIhFoov2MOJ0c6Dw,VwSWWdD8a_L4DFKkTGMLUF1W0XQSLd3Bdfvk