Forráskód Böngészése

微信公众号消息推送

dingsixi 2 hete
szülő
commit
3f743c3f8f

+ 161 - 0
nckd-fi/src/main/java/nckd/fi/er/message/SendMsgToWeChatOA.java

@@ -0,0 +1,161 @@
+package nckd.fi.er.message;
+
+import com.alibaba.fastjson.JSONObject;
+import kd.bos.dataentity.entity.DynamicObjectCollection;
+import kd.bos.exception.KDBizException;
+import kd.bos.logging.Log;
+import kd.bos.logging.LogFactory;
+import kd.bos.orm.query.QCP;
+import kd.bos.orm.query.QFilter;
+import kd.bos.servicehelper.QueryServiceHelper;
+import kd.bos.servicehelper.parameter.SystemParamServiceHelper;
+import kd.bos.workflow.engine.msg.AbstractMessageServiceHandler;
+import kd.bos.workflow.engine.msg.ctx.MessageContext;
+import kd.bos.workflow.engine.msg.info.MessageInfo;
+import kd.bos.workflow.engine.msg.info.ToDoInfo;
+import nckd.base.common.utils.HttpUtils;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * @description:TODO
+ * @author: dingsixi
+ * @create: 2025/12/27 0:09
+ */
+public class SendMsgToWeChatOA extends AbstractMessageServiceHandler {
+    private Log log = LogFactory.getLog(SendMsgToWeChatOA.class);
+    @Override
+    public void createToDo(MessageContext messageContext, ToDoInfo toDoInfo) {
+    }
+
+    @Override
+    public void dealToDo(MessageContext messageContext, ToDoInfo toDoInfo) {
+    }
+
+    @Override
+    public void deleteToDo(MessageContext messageContext, ToDoInfo toDoInfo) {
+    }
+    //重写sendMessage,给微信公众号发消息
+    @Override
+    public void sendMessage(MessageContext ctx, MessageInfo message) {
+        super.sendMessage(ctx, message);
+        String mobUrl = message.getMobContentUrl();
+        String contentUrl = message.getContentUrl();
+        Long id = message.getId();
+        //消息标题
+        String title = message.getMessageTitle().getLocaleValue_zh_CN();
+        //消息内容
+        String content = message.getMessageContent().getLocaleValue_zh_CN();
+//        Map<String, Object> publicParamWhole = SystemParamServiceHelper.loadPublicParametersFromCache();
+        //微信获取token地址 设置为系统参数
+        String url = "https://api.weixin.qq.com/cgi-bin/stable_token?";
+        //公众号appid 设置为系统参数
+        String appid = "wx75dd6e337f49f16c";
+        //公众号密码 设置为系统参数
+        String appsecret = "582ec148de67eebb9d41da86a6a03844";
+        //公众号模板消息 模板id 设置为系统参数
+        String templateid = "-MwmQae_NQ31EAmuYHC3n-aKMk9Qo6I1B1Kd1dSCqho";
+        String accessToken = "";
+        //请求token接口参数
+        Map<String,Object> data = new HashMap<>();
+        data.put("appid",appid);
+        data.put("secret",appsecret);
+        data.put("grant_type","client_credential");
+        //获取token
+        String responseStr = null;
+        try {
+            responseStr = HttpUtils.postJson(url,data);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        //是否推送成功
+        if(!StringUtils.isEmpty(responseStr)){
+            JSONObject jsonObject = JSONObject.parseObject(responseStr);
+            accessToken = jsonObject.getString("access_token");
+            if(StringUtils.isEmpty(accessToken)){
+                String errmsg = jsonObject.getString("errmsg");
+            }
+        }
+        //当前消息发送人用户id
+        List<Long> userIds = message.getUserIds();
+        List<Long> errUserIds = new ArrayList<Long>();
+        if(!StringUtils.isEmpty(accessToken)){
+            //微信公众号模板消息接口url 配置成参数
+            url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
+            url = url+accessToken;
+            //模板消息接口请求参数
+            Map<String,Object> requestJson = new HashMap<>();
+            requestJson.put("template_id",templateid);
+            requestJson.put("url",mobUrl);
+            requestJson.put("client_msg_id",id);
+            // 修改成实际得审批消息数据
+            String dataStr = "{\n" +
+                    "                   \"first\": {\n" +
+                    "                       \"value\":\"恭喜你审批通过\"\n" +
+                    "                   },\n" +
+                    "                   \"keyword1\":{\n" +
+                    "                       \"value\":\"丁思喜\"\n" +
+                    "                   },\n" +
+                    "                   \"keyword2\": {\n" +
+                    "                       \"value\":\"审批通过\"\n" +
+                    "                   },\n" +
+                    "                   \"keyword3\": {\n" +
+                    "                       \"value\":\"20251227\"\n" +
+                    "                   },\n" +
+                    "                   \"remark\":{\n" +
+                    "                       \"value\":\"消息测试\"\n" +
+                    "                   }\n" +
+                    "           }\n";
+            Map map = JSONObject.parseObject(dataStr, Map.class);
+            requestJson.put("data",map);
+            //获取当前用户id对应得微信公众号openid,每个用户都需要发送
+            for(Long userId:userIds){
+                //只有通过微信公众号单点登陆过的用户,苍穹平台才会留存openid,否则无法正常推送消息
+                String openid = getUserOpenId(userId);
+                if(StringUtils.isEmpty(openid)){
+                    continue;
+                }
+                requestJson.put("touser",openid);
+                String response = null;
+                try {
+                    response = HttpUtils.postJson(url,  requestJson);
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+                log.info("公众号:"+response);
+                if(!StringUtils.isEmpty(response)){
+                    errUserIds.add(userId);
+                }else{
+                    JSONObject json = JSONObject.parseObject(response);
+                    Integer errcode = json.getInteger("errcode");
+                    if(errcode!=0){
+                        errUserIds.add(userId);
+                    }
+                }
+            }
+            if(errUserIds.size()>0){
+                message.setUserIds(errUserIds);
+                throw new KDBizException("推送公众号消息失败,请联系管理员!");
+            }
+
+        }else{
+            throw new KDBizException("推送公众号消息失败,获取token失败!");
+        }
+
+
+    }
+    //根据用户id获取微信openid
+    private String getUserOpenId(Long userId) {
+        String openid = "";
+        //普通微信表里存了1
+        QFilter filter = new QFilter("imtype", QCP.equals,"1");
+        filter = filter.and(new QFilter("userid", QCP.equals,userId));
+        DynamicObjectCollection openids = QueryServiceHelper.query("bas_immapping", "openid", new QFilter[]{filter});
+        if(openids.size()>0){
+            openid = openids.get(0).getString("openid");
+        }
+        return openid;
+    }
+}