浏览代码

小程序发送消息问题

zh 3 年之前
父节点
当前提交
0526c6219e

+ 2 - 2
boman-modules/boman-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -23,8 +23,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		<result property="updateBy"     column="update_by"    />
 		<result property="updateTime"   column="update_time"  />
 		<result property="remark"       column="remark"       />
-		<result property="open_id"       column="openId"       />
-		<result property="union_id"       column="unionId"       />
+		<result property="openId"       column="open_id"       />
+		<result property="unionId"       column="union_id"       />
 		<association property="dept"    column="dept_id" javaType="com.boman.domain.SysDept" resultMap="deptResult" />
 		<collection  property="roles"   javaType="java.util.List"        resultMap="RoleResult" />
 	</resultMap>

+ 2 - 1
boman-wechat/src/main/java/com/boman/wechat/controller/PushMsgController.java

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.PrintWriter;
+import java.util.Map;
 import java.util.Objects;
 
 import static com.boman.wechat.utils.CheckAuthUtils.*;
@@ -56,7 +57,7 @@ public class PushMsgController {
     }
 
     @PostMapping("/pushMsg")
-    public String pushMsg(@RequestBody WxMsgDto dto) {
+    public Map<String, Object> pushMsg(@RequestBody WxMsgDto dto) {
        return wxPushService.pushToUser(dto.getIds(), dto.getParams());
     }
 }

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

@@ -5,6 +5,6 @@ import java.util.Map;
 
 public interface WxPushService {
 
-    public String pushToUser(List<Long> userIds, Map<String, Object> params);
+    public Map<String, Object> pushToUser(List<Long> userIds, Map<String, Object> params);
 
 }

+ 26 - 16
boman-wechat/src/main/java/com/boman/wechat/service/impl/WxPushServiceImpl.java

@@ -1,5 +1,6 @@
 package com.boman.wechat.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.utils.StringUtils;
 import com.boman.domain.SysUser;
@@ -37,29 +38,35 @@ public class WxPushServiceImpl implements WxPushService {
      * 微信小程序推送给用户
      */
     @Override
-    public String pushToUser(List<Long> userIds, Map<String, Object> params) {
+    public Map<String, Object> pushToUser(List<Long> userIds, Map<String, Object> params) {
         String templateId = properties.getTemplateId();
+        Map<String, Object> pushResult = new HashMap<>();
 
         //获取access_token
         String accessToken = getAccessToken(properties.getAppId(), properties.getSecret());
         if(StringUtils.isEmpty(accessToken)) {
-            return "获取access_token失败";
+            pushResult.put("msg", "获取access_token失败");
+            return pushResult;
         }
 
         String url = properties.getMsgUrl() + "?access_token=" + accessToken;
         AjaxResult result = remoteUserService.selectUserByIds(userIds);
-        List<SysUser> users = (List<SysUser>) result.get("data");
+        List<Map<String, Object>> users = (List<Map<String, Object>>) result.get("data");
         if(users == null || users.size() <= 0) {
-            return "推送失败,没有选择推送人员";
+            pushResult.put("msg", "推送失败,没有选择推送人员");
+            return pushResult;
         }
-        List<WxMsgDto> vos = new ArrayList<>();
-        for(SysUser user : users) {
-            if(StringUtils.isEmpty(user.getOpenId())) {
+
+        WxMsgDto wxMssVo = new WxMsgDto();
+        Map<String, String> responseResult = new HashMap<>();
+        for(Map user : users) {
+            String openId = (String) user.get("openId");
+            if(StringUtils.isEmpty(openId)) {
                 continue;
             }
             //拼接推送的模版
-            WxMsgDto wxMssVo = new WxMsgDto();
-            wxMssVo.setTouser(user.getOpenId());//用户openid
+//            WxMsgDto wxMssVo = new WxMsgDto();
+            wxMssVo.setTouser(openId);//用户openid
             wxMssVo.setTemplate_id(templateId);//模版id
             Map<String, TemplateData> msgMap = new HashMap<>(5);
             for(String key : params.keySet()) {
@@ -68,15 +75,18 @@ public class WxPushServiceImpl implements WxPushService {
                 msgMap.put(key, templateData);
             }
             wxMssVo.setData(msgMap);
-            vos.add(wxMssVo);
+            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class);
+            JSONObject resultJsonObject = JSON.parseObject(responseEntity.getBody());
+            if((Integer) resultJsonObject.get("errcode") != 0) {
+                responseResult.put(user.get("id").toString(), "用户 " + user.get("nickName") + " 推送失败," + resultJsonObject.get("errmsg").toString());
+            }
+            logger.info("result {}", responseEntity.getBody());
         }
-
-        if(vos.size() == 0) {
-            return "没有可发送的用户,请确认所选用户是否订阅消息!";
+        if(responseResult.size() > 0) {
+            pushResult.put("msg", "有用户推送失败!");
+            pushResult.put("data", responseResult);
         }
-        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, vos, String.class);
-        logger.error("小程序推送结果={}", responseEntity.getBody());
-        return responseEntity.getBody();
+        return pushResult;
     }
 
     /*

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

@@ -1,5 +1,5 @@
 auth.wechat.sessionHost=https://api.weixin.qq.com/sns/jscode2session
-auth.wechat.msg_url=https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send
+auth.wechat.msg_url=https://api.weixin.qq.com/cgi-bin/message/subscribe/send
 auth.wechat.access_token_url=https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential
 auth.wechat.appId=wxf556d2b0c34da8cf
 auth.wechat.secret=8f90a52972268f82d5017a2ac29650c4