zoukankan      html  css  js  c++  java
  • 微信服务号发送模板消息

    package org.liufeng.course.servlet;
    
    
    
    import java.io.IOException;
    
    
    
    import javax.servlet.ServletException;
    
    import javax.servlet.http.HttpServlet;
    
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    
    import javax.servlet.http.HttpSession;
    
    
    
    import net.sf.json.JSONArray;
    
    import net.sf.json.JSONObject;
    
    import net.sf.json.JSONString;
    
    
    
    import org.liufeng.course.pojo.Token;
    
    import org.liufeng.course.util.AdvancedUtil;
    
    import org.liufeng.course.util.CommonUtil;
    
    import org.liufeng.course.util.UserConfig;
    
    import org.slf4j.Logger;
    
    import org.slf4j.LoggerFactory;
    
    
    
    /**
    
     * 发送模板消息
    
     * @author Administrator
    
     */
    
    public class TemplateServlet extends HttpServlet {
    
    
    
        private static final long serialVersionUID = 3493133031468141822L;
    
        
    
        private static Logger log = LoggerFactory.getLogger(TemplateServlet.class);
    
        
    
        //接收后端推送消息
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            request.setCharacterEncoding("utf-8");
    
            response.setCharacterEncoding("utf-8");
    
    
    
            HttpSession session = request.getSession();
    
            //获取请求参数
    
            String openId = (String)request.getParameter("openId");
    
            String data = (String)request.getParameter("data");
    
            String type = (String)request.getParameter("type");
    
            //获取服务器地址
    
            String url = request.getRequestURL().toString();
    
            url = url.substring(0, url.lastIndexOf("/")+1);
    
            //获取新的token
    
            Token token = CommonUtil.getToken(UserConfig.APPID, UserConfig.SECRET);
    
            if(token!=null){
    
                session.setAttribute("access_token", token.getAccessToken());
    
                send(openId,token.getAccessToken(),url,data,type);
    
            }else{
    
                log.info("获取access_token异常");
    
            }
    
        }
    
        
    
        /**
    
         * 发送模板消息接口
    
         * @param openId 用户的openid
    
         * @param access_token 调用接口凭证
    
         * @param url 服务器地址
    
         * @param data 模板数据
    
         * @param type 模板类型(1隐患 2任务 3审批 4质量排查)
    
         */
    
        public void send(String openId, String access_token, String url, String data, String type){
    
            //拼模板请求参数
    
            String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
    
            String param = "{"touser":"TOUSER","template_id":"TEMPLATE_ID","
    
                    + ""url":"URL","data":DATA}";
    
            param = param.replace("TOUSER", openId);//接收者openid
    
            param = param.replace("URL", url+"showTemplateServlet?title="+JSONObject.fromObject(data).getString("first"));//模板跳转链接
    
            
    
            switch (type) {
    
                case "1"://隐患提醒
    
                    param = param.replace("TEMPLATE_ID", UserConfig.YINHUAN_TEMPLATE_ID);//模板ID
    
                    break;
    
                case "2"://任务指派
    
                    param = param.replace("TEMPLATE_ID", UserConfig.RENWU_TEMPLATE_ID);//模板ID
    
                    break;
    
                case "3"://流程待审批
    
                    param = param.replace("TEMPLATE_ID", UserConfig.SHENPI_TEMPLATE_ID);//模板ID
    
                    break;
    
                case "4"://质量排查
    
                    param = param.replace("TEMPLATE_ID", UserConfig.PAICHA_TEMPLATE_ID);//模板ID
    
                    break;
    
                default:
    
                    log.info("推送消息的类型不存在");
    
                    break;
    
            }
    
            param = param.replace("DATA", data);
    
            
    
    //        param = param.replace("DATA", "{"first":{"value":"隐患提醒"},"keyword1":{"value":"C 机电"},"keyword2":{"value":"3-5#层运大巷内无交通管控信号。"},"keyword3":{"value":"已联系厂家制作,预计10月底完工。"},"keyword4":{"value":"2017-07-03"},"keyword5":{"value":"孙荣廷(机二队)"},"remark":{"value":"该隐患暂未销号"}}");//模板数据
    
            
    
            System.out.println(requestUrl);
    
            System.out.println(param);
    
            //发送模板消息
    
    //        JSONObject jsonObject = CommonUtil.sendPost(requestUrl, param);
    
            JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", param);
    
            int errorCode     = jsonObject.getInt("errcode");
    
            String errorMsg = jsonObject.getString("errmsg");
    
            String msgid = jsonObject.getString("msgid");
    
            log.info("已调用模板消息接口 errcode:"+errorCode+", errmsg:"+errorMsg+", msgid:"+msgid);
    
        }
    
    
    
    }
  • 相关阅读:
    ES6
    JavaScript中的Function
    正则表达式
    小程序---电影商城---娱乐---电影列表
    小程序---电影商城---第三方组件 vant(vant weapp)
    Nginx 日志格式
    隐藏响应中的server和X-Powered-By
    个人常用的正则表达式(偶尔更新)
    笔记-VUE滚动加载更多数据
    thinkphp5.1+ 使用 Redis 缓存
  • 原文地址:https://www.cnblogs.com/zh-1721342390/p/9764960.html
Copyright © 2011-2022 走看看