微信推送_微信推送服务通知能给其他微信推送服务器 - CSDN
  • 微信推送图文消息 看看效果吧,写完后推送成功就是这样的。 发送这个东西分三步 第一步: 调用接口把图片传上去 public static JSONObject addImageMedia(File file){ String url="...

    微信推送图文消息

    看看效果吧,写完后推送成功就是这样的。

    发送这个东西分三步

    第一步:

               调用接口把图片传上去           

    public static JSONObject addImageMedia(File file){
            String url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+TOKEN+"&type=image";
            JSONObject jj= HttpClientUtil.httpPostFile(url,file);
            return jj;
        }

    返回结果

    {"media_id":"ieIHfih2axTFJEg72bfcAGp","url":"http://mmbiz.qpic.cn/mmbiz_png/AibYR0VkiaicsCMn2INRML3G17ItcBvPRJYzCEhExAG9/0?wx_fmt=png"}

    这个返回结果的「media_id」 是我们第二步要用的。

    第二步:

             创建媒体模板

    public static JSONObject addPermanentMediaArticle(){
            String PERMANENT_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s";
            PERMANENT_MEDIA_URL=String.format(PERMANENT_MEDIA_URL,TOKEN);
            JSONObject top=new JSONObject();
            JSONObject json=new JSONObject();
            json.put("title","我是测试图文");
            json.put("thumb_media_id","ieIHfih2axTFJEg72bfcAGp");
            json.put("author","haier");
            json.put("digest","我是描述");
            json.put("show_cover_pic",1);
            json.put("content","我是内容描述");
            json.put("content_source_url","http://baidu.com");
            JSONArray arr=new JSONArray();
            arr.add(json);
            top.put("articles",arr);
           JSONObject jj=  HttpClientUtil.httpPost(PERMANENT_MEDIA_URL,top,"template");
           return jj;
        }

    返回结果

    {"media_id":"ieIHfih2axTFJEg72bfcALl"}

    第三步:

            发送消息

    public static JSONObject sendImageTemplate(){
            String MEDIA_PRE_VIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s";
            MEDIA_PRE_VIEW_URL=String.format(MEDIA_PRE_VIEW_URL,TOKEN);
            JSONObject top=new JSONObject();
            JSONObject json=new JSONObject();
            top.put("touser","OPENID");
            top.put("msgtype","mpnews");
            json.put("media_id","ieIHfih2axTFJEg72bfcALl");
            top.put("mpnews",json);
            System.out.println(top.toJSONString());
            JSONObject jj=  HttpClientUtil.httpPost(MEDIA_PRE_VIEW_URL,top,"template");
            return jj;
        }

    返回结果:

    {"errcode":0,"errmsg":"preview success"}

    好了,这样就可以推送了。

    展开全文
  • 微信模板推送

    2017-08-07 14:26:02
    1.需求:微信服务号模板消息推送 2.流程: 1. 设计模板样式 2. 获得微信的全局调用凭证+接口 3. 设计库 4. 获得关注服务号的用户 5. 填充内容 6. 推送模板 7. 推送成功/失败返回结果设计模板登录公司...

    1.需求:微信服务号模板消息推送
    2.流程:
    1. 设计模板样式
    2. 获得微信的全局调用凭证+接口
    3. 设计库
    4. 获得关注服务号的用户
    5. 填充内容
    6. 推送模板
    7. 推送成功/失败返回结果

    设计模板

    登录公司或个人的微信公众平台-功能模板消息-模板库,这里有微信自己的设计好的模板库的样式,也可以自己设计,
    这里写图片描述
    本人用的是微信模板库的模板
    本人用的是微信模板库的模板

    获得微信的接口调用凭证+接口

    在微信服务号的开发者工具-开发者文档里有一则模板消息接口的文档,里面介绍了推送模板消息的用到的接口信息。
    模型消息接口使用文档
    1. 获取接口调用凭证access_token
    access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用 access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。
    接口调用请求说明:
    https请求方式: GET
    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    参数说明:
    这里写图片描述
    返回说明
    这里写图片描述
    2. 发送模板消息接口
    接口调用请求说明
    http请求方式: POST
    https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
    3. 获得关注用户id
    公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。
    接口调用请求说明
    http请求方式: GET(请使用https协议)
    https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
    参数说明:
    这里写图片描述

    后端操作

    我使用的是idea开发工具+spring boot+mybatis+mysql
    1. 创建项目
    这里写图片描述
    2. pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
      <packaging>war</packaging>
    
      <name>weixing</name>
      <groupId>com.weixing</groupId>
      <artifactId>weixing</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <shiro.version>1.3.2</shiro.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>1.2.0</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
    
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <scope>runtime</scope>
        </dependency>
    
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.0.27</version>
        </dependency>
    
        <dependency>
          <groupId>net.sf.json-lib</groupId>
          <artifactId>json-lib</artifactId>
          <version>2.4</version>
          <classifier>jdk15</classifier>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
        </dependency>
    
        <dependency>
          <groupId>me.chanjar</groupId>
          <artifactId>weixin-java-tools</artifactId>
          <version>1.0.2</version>
        </dependency>
    
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.1.0</version>
        </dependency>
    
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.3.2</version>
        </dependency>
    
        <dependency>
          <groupId>com.jfinal</groupId>
          <artifactId>jfinal-weixin</artifactId>
          <version>1.9</version>
        </dependency>
    
        <dependency>
          <groupId>com.jfinal</groupId>
          <artifactId>jfinal</artifactId>
          <version>3.1</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot </groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <optional>true </optional>
        </dependency>
    
        <!-- swagger依赖 -->
        <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger2</artifactId>
          <version>2.6.1</version>
        </dependency>
        <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
          <version>2.6.1</version>
        </dependency>
    
    
        <!-- 集成shiro 权限控制-->
        <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-core</artifactId>
          <version>${shiro.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-web</artifactId>
          <version>${shiro.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-spring</artifactId>
          <version>${shiro.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-ehcache</artifactId>
          <version>${shiro.version}</version>
        </dependency>
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
              <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.5.RELEASE</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
    
      </build>
    
    </project>
    
    1. application.properties配置文件
      在配置文件配置端口以及数据库信息
      这里写图片描述
    server.port=8081
    server.tomcat.max-threads=1000
    
    spring.output.ansi.enabled=DETECT
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/weixing?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=
    mybatis.mapper-locations=classpath:mybatis/*.xml
    mybatis.type-aliases-package=com.application.pojo
    
    1. 创建pojo
      这里写图片描述
      4.1AccessToken微信通用接口凭证
    package com.application.pojo;
    
    import java.util.Date;
    
    /**
     * 微信通用接口凭证
     * Created by 13592 on 2017/6/26.
     */
    public class AccessToken {
    
        //获取到的凭证
        private String token;
        //凭证有效时间,单位:秒
        private int expiresIn;
        //当前时间
        private Date time;
    
        public String getToken() {
            return token;
        }
    
        public void setToken(String token) {
            this.token = token;
        }
    
        public int getExpiresIn() {
            return expiresIn;
        }
    
        public void setExpiresIn(int expiresIn) {
            this.expiresIn = expiresIn;
        }
    
        public Date getTime() {
            return time;
        }
    
        public void setTime(Date time) {
            this.time = time;
        }
    }
    
    4.2UserList用户列表
    
    package com.application.pojo;
    
    import org.json.JSONArray;
    
    /**
     * 用户列表
     * Created by 13592 on 2017/6/27.
     */
    public class UserList {
        //关注该公众账号的总用户数
        private int total;
        //拉取的OPENID个数,最大值为10000
        private int count;
        //列表数据,OPENID的列表
        private JSONArray data;
        //拉取列表的最后一个用户的OPENID
        private String next_openid;
    
        public int getTotal() {
            return total;
        }
    
        public void setTotal(int total) {
            this.total = total;
        }
    
        public int getCount() {
            return count;
        }
    
        public void setCount(int count) {
            this.count = count;
        }
    
        public JSONArray getData() {
            return data;
        }
    
        public void setData(JSONArray data) {
            this.data = data;
        }
    
        public String getNext_openid() {
            return next_openid;
        }
    
        public void setNext_openid(String next_openid) {
            this.next_openid = next_openid;
        }
    }
    
    1. 常量类,用于配置接口以及一些固定的信息
      这里写图片描述
      5.1WechatApiUrlConstants微信常量API常量类
    package com.application.scheduling;
    
    /**
     * 微信常量API常量类
     * Created by 13592 on 2017/6/27.
     */
    public class WechatApiUrlConstants {
        //凭证获取
        public final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        //用户列表
        public final static String user_list_url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
        //用户信息
        public final static String user_data_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        //微信接口
        public final static String weixing_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        //第三方用户唯一凭证
        public final static String appid = "wx8aae499a2";
        //第三方用户唯一凭证密钥
        public final static String secret = "075c76ea076a8";
        //模板id
        public final static String template_id = "WmDf6MhB6lgUk";
        //详情连接
        public final static String url = "http://www.baidu.com/";
        //测试凭证
        public final static String ce_appid = "wxe50a7d9ecf";
        //测试密钥
        public final static String ce_secret = "33ed74e6e1";
        //测试模板id
        public final static String ce_template_id = "RY2_Ga_Epv7o59pYhpTclXw";
        //预览人员 燕之夜
        public final static String preview_yaner = "ow-b4GR-LyAixQbu1_9pZX-Y";
    
    1. 工具类-htt请求/调用接口/时间计算等
      这里写图片描述
      6.1MyX509TrustManager微信请求-信任管理器
    package com.application.util;
    
    import javax.net.ssl.X509TrustManager;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    /**
     * 微信请求-信任管理器
     * Created by 13592 on 2017/6/26.
     */
    public class MyX509TrustManager implements X509TrustManager{
    
        /**
         * 该方法检查客户端的证书,若不信任该证书则抛出异常。由于我们不需要对客户端进行认证,因此我们只需要执行默认的信任管理器的这个方法。
         * JSSE中,默认的信任管理器类为TrustManager。
         * @param x509Certificates
         * @param s
         * @throws CertificateException
         */
        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
        }
    
        /**
         * 该方法检查服务器的证书,若不信任该证书同样抛出异常。通过自己实现该方法,可以使之信任我们指定的任何证书。
         * 在实现该方法时,也可以简单的不做任何处理,即一个空的函数体,由于不会抛出异常,它就会信任任何证书。
         * @param x509Certificates
         * @param s
         * @throws CertificateException
         */
        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
        }
    
        /**
         * 返回受信任的X509证书数组。
         * @return
         */
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }
    
    6.2AccessTokenUtil获取微信APPID和secret工具类
    
    package com.application.util;
    
    
    import com.application.pojo.AccessToken;
    import com.application.pojo.UserData;
    import com.application.pojo.UserList;
    
    /**
     * 获取微信APPID和secret工具类
     * Created by 13592 on 2017/6/27.
     */
    public class AccessTokenUtil {
        public static AccessToken accessToken = new AccessToken();
        public static UserData userData = new UserData();
        public static UserList userList = new UserList();
    
    }
    
    6.3CommonUtil通用工具类/配置类
    
    package com.application.util;
    
    import com.application.from.MessageFrom;
    import com.application.pojo.AccessToken;
    import com.application.pojo.UserData;
    import com.application.pojo.UserList;
    import com.application.scheduling.WechatApiUrlConstants;
    import net.sf.json.JSONObject;
    
    import org.json.JSONArray;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.annotation.Configuration;
    
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManager;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ConnectException;
    import java.net.URL;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;
    
    /**
     * 通用工具类
     * Created by 13592 on 2017/6/26.
     */
    @Configuration
    public class CommonUtil {
    
        private static Logger log = LoggerFactory.getLogger(CommonUtil.class);
        /**
         * 发送https请求
         *
         * @param requestUrl 请求地址
         * @param requestMethod 请求方式(GET、POST)
         * @param outputStr 提交的数据
         * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
         */
        public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
            JSONObject jsonObject = null;
            try {
                // 创建SSLContext对象,并使用我们指定的信任管理器初始化
                TrustManager[] tm = { new MyX509TrustManager() };
                SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
                sslContext.init(null, tm, new java.security.SecureRandom());
                // 从上述SSLContext对象中得到SSLSocketFactory对象
                SSLSocketFactory ssf = sslContext.getSocketFactory();
                URL url = new URL(requestUrl);
                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setSSLSocketFactory(ssf);
    
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                // 设置请求方式(GET/POST)
                conn.setRequestMethod(requestMethod);
                // 当outputStr不为null时向输出流写数据
                if (null != outputStr) {
                    OutputStream outputStream = conn.getOutputStream();
                    // 注意编码格式
                    outputStream.write(outputStr.getBytes("UTF-8"));
                    outputStream.close();
                }
                // 从输入流读取返回内容
                InputStream inputStream = conn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String str = null;
                StringBuffer buffer = new StringBuffer();
                while ((str = bufferedReader.readLine()) != null) {
                    buffer.append(str);
                }
    
                // 释放资源
                bufferedReader.close();
                inputStreamReader.close();
                inputStream.close();
                inputStream = null;
                conn.disconnect();
                jsonObject = JSONObject.fromObject(buffer.toString());
            } catch (ConnectException ce) {
                log.error("连接超时:{}", ce);
            } catch (Exception e) {
                log.error("https请求异常:{}", e);
            }
            return jsonObject;
            }
    
        /**
         * 获取接口访问凭证
         *
         * @param appid 凭证
         * @param appsecret 密钥
         * @return
         */
        public static AccessToken getToken(String appid, String appsecret) {
            String requestUrl = WechatApiUrlConstants.token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
            // 发起GET请求获取凭证
            JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);
            if (null != jsonObject) {
                try {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    AccessTokenUtil.accessToken.setToken(jsonObject.getString("access_token"));
                    AccessTokenUtil.accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
                    AccessTokenUtil.accessToken.setTime(dateFormat.parse(dateFormat.format(new Date())));
                    if(null != AccessTokenUtil.accessToken){
                        log.info("获取access_token成功,有效时长{}秒 token:{}", AccessTokenUtil.accessToken.getExpiresIn(), AccessTokenUtil.accessToken.getToken());
                    }
                } catch (Exception e) {
                    AccessTokenUtil.accessToken = null;
    
                    log.error("{}", e);
                    log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
                }
            }
            return AccessTokenUtil.accessToken;
        }
    
        /**
         * 获取用户信息
         * @param open 用户id
         * @param token 调用接口凭证
         * @return
         */
        public static UserData getUser(List<String> open, String token){
            for (int i = 0 ; i < open.size() ; i ++){
                String requestUrl = WechatApiUrlConstants.user_data_url.replace("ACCESS_TOKEN",token).replace("OPENID",open.get(i));
                //发起GET请求获取凭证
                JSONObject jsonObject = httpsRequest(requestUrl,"GET",null);
                if (null != jsonObject){
                    try {
                        AccessTokenUtil.userData.setSubscribe(jsonObject.getInt("subscribe"));
                        AccessTokenUtil.userData.setOpenid(jsonObject.getString("openid"));
                        AccessTokenUtil.userData.setNickname(jsonObject.getString("nickname"));
                        AccessTokenUtil.userData.setSex(jsonObject.getInt("sex"));
                        AccessTokenUtil.userData.setLanguage(jsonObject.getString("language"));
                        AccessTokenUtil.userData.setCity(jsonObject.getString("city"));
                        AccessTokenUtil.userData.setProvince(jsonObject.getString("province"));
                        AccessTokenUtil.userData.setCountry(jsonObject.getString("country"));
                        AccessTokenUtil.userData.setHeadimgurl(jsonObject.getString("headimgurl"));
                        AccessTokenUtil.userData.setSubscribe_time(jsonObject.getString("subscribe_time"));
                        AccessTokenUtil.userData.setUnionid(jsonObject.getString("unionid"));
                        AccessTokenUtil.userData.setRemark(jsonObject.getString("remark"));
                        AccessTokenUtil.userData.setGroupid(jsonObject.getInt("groupid"));
                        AccessTokenUtil.userData.setTagid_list(jsonObject.get("tagid_list"));
                        if (null != AccessTokenUtil.userData){
                            log.info("获取用户信息成功, token:{}",open.get(i), jsonObject.getString("nickname"));
                            System.out.println("用户id:"+open.get(i)+"用户姓名:"+jsonObject.getString("nickname"));
                        }else{
                            log.error("获取用户信息失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
    
            return AccessTokenUtil.userData;
        }
    
        /**
         * 获取关注用户列表
         * @param token 调用接口凭证
         * @param openid 第一个拉取的OPENID
         * @return
         */
        public static UserList getUserList(String token,String openid){
            String requestUrl = WechatApiUrlConstants.user_list_url.replace("ACCESS_TOKEN",token).replace("NEXT_OPENID","");
            //发起GET请求获取凭证
            JSONObject jsonObject = httpsRequest(requestUrl,"GET",null);
            if (null != jsonObject){
                try {
                    AccessTokenUtil.userList.setCount(jsonObject.getInt("count"));
                    AccessTokenUtil.userList.setNext_openid(jsonObject.getString("next_openid"));
                    AccessTokenUtil.userList.setTotal(jsonObject.getInt("total"));
                    JSONObject json1 = jsonObject.getJSONObject("data");
                    JSONArray json2 = new JSONArray(json1.get("openid").toString());
                    AccessTokenUtil.userList.setData(json2);
                    if (null != AccessTokenUtil.userList)
                        log.info("获取用户列表成功, token:{}", AccessTokenUtil.userList.getCount(), AccessTokenUtil.userList.getTotal());
                    else{
                        log.error("获取用户列表失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
    
            }
            return AccessTokenUtil.userList;
        }
    
        /**
         * 获取token的时间差
         * @param time 获取时间
         * @return
         */
        public static Long dateDiff(Date time){
            Date curretTime = new Date(System.currentTimeMillis());
            long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
            long nh = 1000 * 60 * 60;// 一小时的毫秒数
            long nm = 1000 * 60;// 一分钟的毫秒数
            long ns = 1000;// 一秒钟的毫秒数
            long diff;
            long day = 0;
            long hour = 0;
            long min = 0;
            long sec = 0;
            try {
                diff = curretTime.getTime() - time.getTime();
                day = diff / nd;// 计算差多少天
                hour = diff % nd / nh + day * 24;// 计算差多少小时
                min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
                sec = diff % nd % nh % nm / ns;// 计算差多少秒
                // 输出结果
                System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时"
                        + (min - day * 24 * 60) + "分钟" + sec + "秒。");
                System.out.println("hour=" + hour + ",min=" + min);
            } catch (Exception e) {
                e.printStackTrace();
            }
             return hour;
        }
    
    
        /**
         * 预览推送模板
         * @param mf 模板信息
         * @param openid 预览人员
         * @param url 推送接口
         */
        public static void reviewUser(MessageFrom mf, String openid,String url){
            JSONObject json = new JSONObject();
            json.put("touser", openid);//接收者wxName
            json.put("template_id", WechatApiUrlConstants.ce_template_id);//消息模板
            json.put("url", WechatApiUrlConstants.url);//填写url可查看详情
            JSONObject dd = new JSONObject();
            JSONObject dd2 = new JSONObject();
            dd2.put("value", mf.getTitle_name());//消息提示
            dd2.put("color", "#173177");
            dd.put("first", dd2);
            JSONObject cc2 = new JSONObject();
            cc2.put("value", mf.getVulnerability_levels());//漏洞等级
            cc2.put("color", "#173177");
            dd.put("keyword1", cc2);
            JSONObject ee2 = new JSONObject();
            ee2.put("value", mf.getVulnerability_time());//漏洞时间
            ee2.put("color", "#173177");
            dd.put("keyword2", ee2);
            JSONObject gg2 = new JSONObject();
            gg2.put("value", mf.getVulnerabiliry_Overview());
            gg2.put("color", "#173177");
            dd.put("remark", gg2);
            json.put("data", dd);
            System.out.println(json.toString());
            JSONObject js = CommonUtil.httpsRequest(url, "POST", json.toString());
            System.out.println("js=="+js);
    
        }
    
        /**
         * 全推送模板
         * @param mf 模板信息
         * @param openidList 全关注用户
         * @param url 推送接口
         */
        public static void push(MessageFrom mf,List<String> openidList,String url){
            for (int i = 0 ; i < openidList.size(); i ++){
                JSONObject json = new JSONObject();
                json.put("touser", openidList.get(i));//接收者wxName
                json.put("template_id", WechatApiUrlConstants.ce_template_id);//消息模板
                json.put("url", WechatApiUrlConstants.url);//填写url可查看详情
                JSONObject dd = new JSONObject();
                JSONObject dd2 = new JSONObject();
                dd2.put("value", mf.getTitle_name());//消息提示
                dd2.put("color", "#173177");
                dd.put("first", dd2);
                JSONObject cc2 = new JSONObject();
                cc2.put("value", mf.getVulnerability_levels());//漏洞等级
                cc2.put("color", "#173177");
                dd.put("keyword1", cc2);
                JSONObject ee2 = new JSONObject();
                ee2.put("value", mf.getVulnerability_time());//漏洞时间
                ee2.put("color", "#173177");
                dd.put("keyword2", ee2);
                JSONObject gg2 = new JSONObject();
                gg2.put("value", mf.getVulnerabiliry_Overview());
                gg2.put("color", "#173177");
                dd.put("remark", gg2);
                json.put("data", dd);
                System.out.println(json.toString());
                JSONObject js = CommonUtil.httpsRequest(url, "POST", json.toString());
                System.out.println("js=="+js);
            }
        }
    
    
    
    }
    
    1. service
      这里写图片描述
      7.1AccessTokenService全局调用凭证的操作
    package com.application.service;
    
    import java.util.Date;
    
    /**
     * 全局调用凭证的操作
     * Created by 13592 on 2017/6/27.
     */
    public interface AccessTokenService {
        /**
         * 新增全局调用凭证信息
         * @param token 全局调用凭证id
         */
        void insertToken(String token);
    
        /**
         *  删除表中信息
         *  @return
         */
        void deleteToken();
    
        /**
         * 查询全局调用凭证id
         * @return
         */
        String selectToken();
    
        /**
         * 查询新增时间
         * @return
         */
        Date selectTime();
    }
    
    7.1.1AccessTokenServiceImpl
    
    package com.application.service.impl;
    
    import com.application.mapper.AccessTokenMapper;
    import com.application.pojo.AccessToken;
    import com.application.service.AccessTokenService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.Date;
    
    /**
     * Created by 13592 on 2017/6/27.
     */
    @Service
    public class AccessTokenServiceImpl implements AccessTokenService {
        @Autowired
        private AccessTokenMapper accessTokenMapper;
        @Override
        public void insertToken(String token){
            AccessToken accessToken = new AccessToken();
            Date curretTime = new Date(System.currentTimeMillis());
            accessToken.setToken(token);
            accessToken.setTime(curretTime);
            accessTokenMapper.insertToken(accessToken);
        }
        @Override
        public void deleteToken(){
            accessTokenMapper.deleteToken();
        }
        @Override
        public String selectToken(){
            return accessTokenMapper.selectToken();
        }
        @Override
        public Date selectTime(){return accessTokenMapper.selectTime();}
    }
    
    7.2PreviewService预览人员操作
    
    package com.application.service;
    
    
    import com.application.pojo.PreviewUser;
    
    import java.util.List;
    
    /**
     * 预览人员操作
     * Created by 13592 on 2017/6/29.
     */
    public interface PreviewService {
    
        /**
         * 查询用户id列表
         * @return
         */
        List<String> getOpenid();
    
        /**
         * 新增预览用户信息
         * @param previewUser
         */
        void insertOpenid(PreviewUser previewUser);
    
        /**
         * 查询预览列表
         * @return
         */
        List<PreviewUser> previewList();
    
        /**
         * 通过微信名称查询预览用户id
         * @param opename 微信名称
         * @return
         */
        String selectOpenid(String opename);
    }
    
    7.2.2PreviewServiceImpl
    
    package com.application.service.impl;
    
    import com.application.mapper.PreviewMapper;
    import com.application.pojo.PreviewUser;
    import com.application.service.PreviewService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.List;
    
    /**
     * Created by 13592 on 2017/6/29.
     */
    @Service
    public class PreviewServiceImpl implements PreviewService{
        @Autowired
        private PreviewMapper previewMapper;
        @Override
        public List<String> getOpenid(){
            List<String> openidList = previewMapper.getOpenid();
            return openidList;
        }
        @Override
        public void insertOpenid(PreviewUser previewUser){
            try {
                previewMapper.insertOpenid(previewUser);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        @Override
        public List<PreviewUser> previewList(){
            return previewMapper.previewList();
        }
        @Override
        public String selectOpenid(String openname){
            String id = "";
            try {
                id = previewMapper.selectOpenid(openname);
            }catch (Exception e){
                e.printStackTrace();
            }
            return id;
        }
    }
    
    1. mapper层-完成持久化DAO层
      使用MyBatis在我们通过xml集中配置SQL,并通过创建接口Mapper文件来完成持久化DAO层(mybatis内部使用的是动态代理,所以我们不需要自己编写实现类)。
      这里写图片描述
      8.1AccessTokenMapper
    package com.application.mapper;
    
    
    
    import com.application.pojo.AccessToken;
    
    import java.util.Date;
    
    /**
     * Created by 13592 on 2017/6/27.
     */
    public interface AccessTokenMapper {
        void insertToken(AccessToken token);
        void deleteToken();
        String selectToken();
        Date selectTime();
    
    }
    
     8.2PreviewMapper
    
    package com.application.mapper;
    
    
    import com.application.pojo.PreviewUser;
    
    import java.util.List;
    
    /**
     * Created by 13592 on 2017/6/29.
     */
    public interface PreviewMapper {
        List<String> getOpenid();
        void insertOpenid(PreviewUser previewUser);
        List<PreviewUser> previewList();
        String selectOpenid(String openname);
    
    }
    
    1. form页面接受参数类
      用于接受页面传过来的数据
      这里写图片描述
      9.1MessageFrom
    package com.application.from;
    
    /**
     * Created by 13592 on 2017/6/28.
     */
    public class MessageFrom {
        //预览用户
        private String preview_user;
        //标题名称
        private String title_name;
        //漏洞等级
        private String Vulnerability_levels;
        //漏洞时间
        private String Vulnerability_time;
        //漏洞概要
        private String Vulnerabiliry_Overview;
    
        public String getPreview_user() {
            return preview_user;
        }
    
        public void setPreview_user(String preview_user) {
            this.preview_user = preview_user;
        }
    
        public String getTitle_name() {
            return title_name;
        }
    
        public void setTitle_name(String title_name) {
            this.title_name = title_name;
        }
    
        public String getVulnerability_levels() {
            return Vulnerability_levels;
        }
    
        public void setVulnerability_levels(String vulnerability_levels) {
            Vulnerability_levels = vulnerability_levels;
        }
    
        public String getVulnerability_time() {
            return Vulnerability_time;
        }
    
        public void setVulnerability_time(String vulnerability_time) {
            Vulnerability_time = vulnerability_time;
        }
    
        public String getVulnerabiliry_Overview() {
            return Vulnerabiliry_Overview;
        }
    
        public void setVulnerabiliry_Overview(String vulnerabiliry_Overview) {
            Vulnerabiliry_Overview = vulnerabiliry_Overview;
        }
    }
    
     9.2PreviewUserForm
    
    package com.application.from;
    
    /**
     * 页面预览人员
     * Created by 13592 on 2017/6/30.
     */
    public class PreviewUserForm {
        //预览用户id
        private String openid;
        //预览用户名
        private String name;
    
        public String getOpenid() {
            return openid;
        }
    
        public void setOpenid(String openid) {
            this.openid = openid;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    1. 使用swagger
      这里写图片描述
      10.1Swagger2
    package com.application;
    
    import com.google.common.base.Predicates;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.request.async.DeferredResult;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    
    /**
     * 说明:
     * 时间: 2017/3/28 11:32
     *
     * @author wimas
     */
    @Configuration
    @EnableSwagger2
    public class Swagger2 {
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2).groupName("access")
                    .genericModelSubstitutes(DeferredResult.class)
                    .useDefaultResponseMessages(false)
                    .forCodeGeneration(true)
                    .pathMapping("/")// base,最终调用接口后会和paths拼接在一起s
                    .select()
                    .paths(Predicates.or(PathSelectors.regex("/access/.*")))//过滤的接口
                    .build()
                    .apiInfo(apiInfo());
        }
    
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("微信模板推送")
                    .description("")
                    .version("1.0")
                    .build();
        }
    }
    
    1. controller控制类
      11.1WechatController
    package com.application.controller;
    
    import com.application.from.MessageFrom;
    import com.application.from.PreviewUserForm;
    import com.application.pojo.AccessToken;
    import com.application.pojo.PreviewUser;
    import com.application.pojo.UserData;
    import com.application.pojo.UserList;
    import com.application.scheduling.WechatApiUrlConstants;
    import com.application.service.AccessTokenService;
    import com.application.service.PreviewService;
    import com.application.util.CommonUtil;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.apache.shiro.authz.annotation.RequiresPermissions;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import javax.validation.Valid;
    import java.util.*;
    
    /**
     * 微信推送信息
     * Created by 13592 on 2017/6/26.
     */
    @RestController
    @RequestMapping("/access")
    @Api(description = "微信模板推送")
    public class WechatController {
        @Autowired
        private PreviewService previewService;
        @Autowired
        private AccessTokenService accessTokenService;
    
    
        @ApiOperation(value = "模板推送")
        @PostMapping(value = "/token")
        @ApiImplicitParam(name = "mf", value = "模板信息", required = true, dataType = "MessageFrom")
        @RequiresPermissions("access:token")
        public void wxTuisong(@Valid @RequestBody MessageFrom mf){
            AccessToken token = new AccessToken();
            Date date = accessTokenService.selectTime();
            Long time = getDiff(date);
            if(time>=2){//时间间隔大于等于2个小时则重新取接口调用凭证
                accessTokenService.deleteToken();
                token = headtoken (WechatApiUrlConstants.appid,WechatApiUrlConstants.secret);//获取token
                accessTokenService.insertToken(token.getToken());
            }else{
                token.setToken(accessTokenService.selectToken());
            }
            if(token.getToken()!=null){
                UserList userList = getUserList(token.getToken(),"");
                String url = WechatApiUrlConstants.weixing_url.replace("ACCESS_TOKEN", token.getToken());//转换为响应接口模式
                List<String> previewUserList = previewService.getOpenid();//预览用户id列表
                List<String> opeidList = new ArrayList<String>();//所有关注用户列表
                for (int i = 0 ; i < userList.getData().length() ; i ++){
                    opeidList.add(userList.getData().get(i).toString());
                }
                List<String> list = new ArrayList<String>();
                if (mf.getPreview_user()!= "" || !mf.getPreview_user().equals("string")){
                    for (int i = 0 ; i < previewUserList.size(); i ++){
                        if(mf.getPreview_user().equals(previewUserList.get(i))){
                            reviewUser(mf,previewUserList.get(i),url);
                        }
                    }
                }else{
                    push(mf,opeidList,url);
                }
            }
        }
    
        @ApiOperation(value = "新增预览人员")
        @PostMapping(value = "/insert")
        @ApiImplicitParam(name = "puf", value = "模板信息", required = true, dataType = "PreviewUserForm")
        @RequiresPermissions("access:insert")
        public void insert(@Valid @RequestBody PreviewUserForm puf){
            PreviewUser previewUser = new PreviewUser();
            previewUser.setOpenid(puf.getOpenid());
            previewUser.setOpenname(puf.getName());
            previewService.insertOpenid(previewUser);
        }
    
        @ApiOperation(value = "查询预览列表")
        @GetMapping(value = "/selectList")
        @RequiresPermissions("access:selectList")
        public List<PreviewUser> selectList(){
            return previewService.previewList();
        }
    
        @ApiOperation(value = "查询预览id")
        @GetMapping(value = "/selectOpenid/{openname}")
        @RequiresPermissions("access:selectOpenid")
        public String selectOpenid(@ApiParam(name = "openname",value = "用户名称",required = true) @PathVariable String openname){
            return previewService.selectOpenid(openname);
        }
        /**
         * 请求token
         * @Description :
         * @param
         * @return
         * ---------------
         * @Author  : My
         * @CreateData : 2016-1-18
         */
        public static AccessToken headtoken (String appId,String appSrecet){
            AccessToken token = CommonUtil.getToken(appId, appSrecet);
            return token;
        }
    
    
        /**
         * 获取关注用户
         * @param token 接口调用凭证
         * @param openid
         * @return
         */
       public static UserList getUserList(String token,String openid){
            UserList userList = CommonUtil.getUserList(token,openid);
            return userList;
       }
    
        /**
         * 时间获取
         * @param time 获取时间
         * @return
         */
       public static Long getDiff(Date time){
            CommonUtil commonUtil = new CommonUtil();
            return commonUtil.dateDiff(time);
    
       }
    
        /**
         * 获取用户信息
         * @param openid 用户id
         * @param token 接口调用凭证
         * @return
         */
       public static UserData userDate(List<String> openid,String token){
           CommonUtil commonUtil = new CommonUtil();
           return commonUtil.getUser(openid,token);
       }
    
    
        /**
         * 预览推送模板
         * @param mf 模板信息
         * @param openid 用户id
         * @param url 接口
         */
       public static void reviewUser(MessageFrom mf, String openid,String url){
          /* CommonUtil commonUtil = new CommonUtil();
           commonUtil.reviewUser(mf, openid, url);*/
    
       }
    
        /**
         * 全推送模板
         * @param mf 模板信息
         * @param openidList 全关注用户
         * @param url 推送接口
         */
        public static void push(MessageFrom mf,List<String> openidList,String url){
           /* CommonUtil commonUtil = new CommonUtil();
            commonUtil.push(mf, openidList, url);*/
        }
    
        public static void main(String[] args){
            AccessToken token = new AccessToken();
    //        token = CommonUtil.getToken(APP_ID,SECRET);
            WechatController wechatController = new WechatController();
            CommonUtil commonUtil = new CommonUtil();
            MessageFrom massages = new MessageFrom();
            wechatController.wxTuisong(massages);
            System.out.println("==================================================");
    
        }
    }
    
    1. mybatis.xml
      这里写图片描述
      12.1AccessTokenMapper.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.application.mapper.AccessTokenMapper" >
        <resultMap id="BaseResultMap"  type="com.application.pojo.AccessToken">
            <result column="access_token_name" property="token" jdbcType="VARCHAR"/>
            <result column="access_token_time" property="time" jdbcType="TIMESTAMP"/>
        </resultMap>
    
        <insert id="insertToken" parameterType="com.application.pojo.AccessToken">
            INSERT INTO weixing_access_token (
              access_token_name,
              access_token_time
            )
            VALUES
            (
              #{token,jdbcType=VARCHAR},#{time,jdbcType=TIMESTAMP})
    
        </insert>
    
        <delete id="deleteToken">
            DELETE
            FROM
              weixing_access_token
            WHERE
              1 = 1
        </delete>
    
        <select id="selectToken" resultType="String">
            SELECT
              access_token_name
            FROM
              weixing_access_token
        </select>
    
        <select id="selectTime" resultType="Date">
            SELECT
              access_token_time
            FROM
              weixing_access_token
        </select>
    </mapper>
    12.2PreviewMapper.xml
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.application.mapper.PreviewMapper" >
        <resultMap id="BaseResultMap"  type="com.application.pojo.PreviewUser">
            <result column="openid" property="openid" jdbcType="VARCHAR"/>
            <result column="openname" property="openname" jdbcType="VARCHAR"/>
        </resultMap>
        <select id="getOpenid" resultType="java.lang.String">
          SELECT openid FROM  review_user
        </select>
        <insert id="insertOpenid" parameterType="com.application.pojo.PreviewUser">
          INSERT INTO review_user (openid, openname)
          VALUES
            (
              #{openid,jdbcType=VARCHAR},#{openname,jdbcType=VARCHAR})
        </insert>
        <select id="previewList" resultMap="BaseResultMap" parameterType="com.application.pojo.PreviewUser">
            SELECT * FROM  review_user
        </select>
        <select id="selectOpenid" resultType="string" parameterType="string">
          SELECT
            openid
          FROM
            review_user
          WHERE
            openname = #{openname,jdbcType=VARCHAR}
        </select>
    
    
    </mapper>
    1. spring boot启动类
      这里写图片描述
      13.1Application
      package com.application;

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cache.annotation.EnableCaching;

    /**
    * Created by 13592 on 2017/6/27.
    */

    @SpringBootApplication
    @EnableCaching
    @MapperScan(basePackages = “com.application.mapper”)
    public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    
    }
    

    }

    1. 数据库
      这里写图片描述
      14.1review_user预览表
      这里写图片描述
      14.2weixing_access_token全局调用接口凭证信息表
      这里写图片描述
    2. 启动Application
      正常启动的情况
      这里写图片描述
      启动完毕后,在浏览器输入http://localhost:8081/swagger-ui.html
      这里写图片描述
    3. 测试-输入模板消息
      这里写图片描述
      发送之后,因为填写了预览人,所以我会接受到这条推送信息
      微信收到的模板
      这里写图片描述
      后端显示返回结果
      在调用模板消息接口后,会返回JSON数据包。正常时的返回JSON数据包示例:
      {
      “errcode”:0,
      “errmsg”:”ok”,
      “msgid”:200228332
      }
    展开全文
  • 微信消息推送详解

    2017-06-29 09:25:05
    微信模版消息推送详解 应用企业注册微信公众号并认证https://mp.weixin.qq.com 登录微信公众号获取appID和appsecret本次使用的测试帐号进行说明   扫码关注和新建消息模版拿到openid作为我们推送的对象  ...
    

    微信模版消息推送详解

    1. 应用企业注册微信公众号并认证https://mp.weixin.qq.com

    2. 登录微信公众号获取appIDappsecret本次使用的测试帐号进行说明

       

    3. 扫码关注和新建消息模版拿到openid作为我们推送的对象

       

      4、查看官方开发文档https://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl

       

    1. 开始撸代码

      创建工具类

      TestWechatMsg.java

       

       

      package testWeb;

       

      import java.io.BufferedReader;

      import java.io.IOException;

      import java.io.InputStreamReader;

      import java.io.OutputStreamWriter;

      import java.net.HttpURLConnection;

      import java.net.MalformedURLException;

      import java.net.URL;

      import java.net.URLConnection;

      import java.util.HashMap;

      import java.util.Map;

       

      import com.alibaba.fastjson.JSON;

       

      import testWeb.bean.WechatSendMsg;

      import testWeb.bean.WechatSendMsgBody;

      import testWeb.bean.WechatSendMsgData;

       

      public class TestWechatMsg {

       

      private static String TEST_APPID = "wxa1d64a418271a654";

      private static String TEST_APPSECRET = "bd814671571d837e287ee10887a77fc1";

      private static String WECHART_URL_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";

      private static String WECHART_URL_SEND_TEMPLATE_MSG = "https://api.weixin.qq.com/cgi-bin/message/template/send";

      private static String OPEN_ID_1 = "oDfGiwEHCvsUQDl-nytTEzLVf9rA";

      private static String TEMPLATE_ID = "N6rCGkAPQa6W_0l32lVBtLwhhXEDfmA5oDX6kPHU_bg";

      private static String TOP_COLOR = "#FF0000";

      private static String GO_URL = "http://weixin.qq.com/download";

       

      public static void main(String[] args) {

      String tokenStr = getAccessToken();

      Map<String, String> dataMap = new HashMap<String, String>();

      dataMap.put("barcode", "000000211");

      dataMap.put("status", "已发车");

      dataMap.put("payment", "500");

      dataMap.put("goodspaystatus", "已支付");

      dataMap.put("postage", "50");

      dataMap.put("paystatus", "已支付");

      String result = sendWechatTempMsg(tokenStr, OPEN_ID_1, GO_URL, dataMap);

      System.out.println(result);

      }

       

      public static String doPostRequest(String urlStr, String data) {

      StringBuffer answer = new StringBuffer();

      // Build parameter string

      try {

      // Send the request

      URL url = new URL(urlStr);

      URLConnection conn = url.openConnection();

      conn.setDoOutput(true);

      OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

      // write parameters

      writer.write(data);

      writer.flush();

      // Get the response

      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

      String line;

      while ((line = reader.readLine()) != null) {

      answer.append(line);

      }

      writer.close();

      reader.close();

      } catch (MalformedURLException ex) {

      ex.printStackTrace();

      } catch (IOException ex) {

      ex.printStackTrace();

      }

      return answer.toString();

      }

       

      public static String doGetRequest(String urlStr, String data) {

      String res = "";

      // Build parameter string

      try {

      BufferedReader rd = null;

      // Send the request

      URL url = new URL(urlStr + "?" + data);

      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

      urlConnection.connect();

      rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));

      /*

       * while (rd.read() != -1) { res = rd.readLine();

       * //res=URLDecoder.decode(res,"UTF-8"); }

       */

      String line;

      while ((line = rd.readLine()) != null) {

      res += line;

      }

      rd.close();

      System.out.println(res);

      return res;

      } catch (MalformedURLException ex) {

      ex.printStackTrace();

      } catch (IOException ex) {

      ex.printStackTrace();

      }

       

      return res;

      }

       

      public static String getAccessToken() {

      String data = "grant_type=client_credential" + "&appid=" + TEST_APPID + "&secret=" + TEST_APPSECRET;

      String res = doGetRequest(WECHART_URL_TOKEN, data);

      Map<String, Object> dataMap = new HashMap<String, Object>();

      dataMap = JSON.parseObject(res);

      return (String) dataMap.get("access_token");

      }

       

      public static String sendWechatTempMsg(String accessToken, String openid, String url, Map<String, String> dataMap) {

      String result = null;

      String data = "access_token=" + accessToken;

      String sendMsg = getSendMsg(openid, url, dataMap);

      result = doPostRequest(WECHART_URL_SEND_TEMPLATE_MSG + "?" + data, sendMsg);

      return result;

      }

       

      public static String getSendMsg(String openid, String url, Map<String, String> dataMap) {

      WechatSendMsgBody msgBody = new WechatSendMsgBody();

      msgBody.setTouser(openid);

      msgBody.setTemplate_id(TEMPLATE_ID);

      msgBody.setTopcolor(TOP_COLOR);

      msgBody.setUrl(url);

      WechatSendMsgData data = new WechatSendMsgData();

      WechatSendMsg barcode = new WechatSendMsg();

      barcode.setValue(dataMap.get("barcode"));

      WechatSendMsg status = new WechatSendMsg();

      status.setValue(dataMap.get("status"));

      WechatSendMsg payment = new WechatSendMsg();

      payment.setValue(dataMap.get("payment"));

      WechatSendMsg goodspaystatus = new WechatSendMsg();

      goodspaystatus.setValue(dataMap.get("goodspaystatus"));

      WechatSendMsg postage = new WechatSendMsg();

      postage.setValue(dataMap.get("postage"));

      WechatSendMsg paystatus = new WechatSendMsg();

      paystatus.setValue(dataMap.get("paystatus"));

      data.setBarcode(barcode);

      data.setGoodspaystatus(goodspaystatus);

      data.setPayment(payment);

      data.setPaystatus(paystatus);

      data.setPostage(postage);

      data.setStatus(status);

      msgBody.setData(data);

      return JSON.toJSONString(msgBody);

      }

       

      }

       

    2. 封装对象后转换成json传递给微信平台发信息

        WechatSendMsgBody.java  消息体

    package testWeb.bean;

    import java.io.Serializable;
    import java.util.List;


    public class WechatSendMsgBody implements Serializable {


     /**
      *
      */
     private static final long serialVersionUID = 1L;
     //发送对象的
     private String touser;
     //消息模版id
     private String template_id;
     //消息链接url
     private String url;
     //标题颜色
     private String topcolor;
     //内容数据
     private WechatSendMsgData data;
     public String getTouser() {
      return touser;
     }
     public void setTouser(String touser) {
      this.touser = touser;
     }
     public String getTemplate_id() {
      return template_id;
     }
     public void setTemplate_id(String template_id) {
      this.template_id = template_id;
     }
     public String getUrl() {
      return url;
     }
     public void setUrl(String url) {
      this.url = url;
     }
     public String getTopcolor() {
      return topcolor;
     }
     public void setTopcolor(String topcolor) {
      this.topcolor = topcolor;
     }
     public WechatSendMsgData getData() {
      return data;
     }
     public void setData(WechatSendMsgData data) {
      this.data = data;
     }
    }

    WechatSendMsgData.java   消息数据

    package testWeb.bean;

    import java.io.Serializable;

    public class WechatSendMsgData implements Serializable {

     /**
      *
      */
     private static final long serialVersionUID = 1L;
     
     private WechatSendMsg barcode;
     private WechatSendMsg status;
     private WechatSendMsg payment;
     private WechatSendMsg goodspaystatus;
     private WechatSendMsg postage;
     private WechatSendMsg paystatus;
     
     
     public WechatSendMsg getBarcode() {
      return barcode;
     }
     public void setBarcode(WechatSendMsg barcode) {
      this.barcode = barcode;
     }
     public WechatSendMsg getStatus() {
      return status;
     }
     public void setStatus(WechatSendMsg status) {
      this.status = status;
     }
     public WechatSendMsg getPayment() {
      return payment;
     }
     public void setPayment(WechatSendMsg payment) {
      this.payment = payment;
     }
     public WechatSendMsg getGoodspaystatus() {
      return goodspaystatus;
     }
     public void setGoodspaystatus(WechatSendMsg goodspaystatus) {
      this.goodspaystatus = goodspaystatus;
     }
     public WechatSendMsg getPostage() {
      return postage;
     }
     public void setPostage(WechatSendMsg postage) {
      this.postage = postage;
     }
     public WechatSendMsg getPaystatus() {
      return paystatus;
     }
     public void setPaystatus(WechatSendMsg paystatus) {
      this.paystatus = paystatus;
     }
    }

    WechatSendMsg.java 数据内容颜色

    package testWeb.bean;

    import java.io.Serializable;

    public class WechatSendMsg implements Serializable {

     /**
      *
      */
     private static final long serialVersionUID = 1L;
     private String value;
     private String color="#173177";
     public String getValue() {
      return value;
     }
     public void setValue(String value) {
      this.value = value;
     }
     public String getColor() {
      return color;
     }
     public void setColor(String color) {
      this.color = color;
     } 
    }


    总结微信消息推送主要还是通过微信api进行get或者Post请求进行消息好发送类似于短信发送

    正式公众号openid获得方式请查看微信官方开发文档 

    1. https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

       

       

    
    展开全文
  • 微信推送例子

    2020-07-30 23:31:13
    C#版的微信推送例子,稍微改一下就可以直接用
  • 微信模板消息推送

    2019-03-14 10:32:01
    一、微信公众号或测试号内配置,我这里使用的...二、SpringBoot后台实现微信消息模板推送代码 注意:对于消息测试这类不是特别重要的功能,最好不要抛出异常 @Service @Slf4j public class PushMessageServiceIm...

    一、微信公众号或测试号内配置,我这里使用的是微信测试号

    点击【新增测试模板进行如下配置】

    配置模板标题和模板内容,配置完成后如下所示,会生成一个模板id,这个很重要:

    二、SpringBoot后台实现微信消息模板推送代码

    注意:对于消息测试这类不是特别重要的功能,最好不要抛出异常

    @Service
    @Slf4j
    public class PushMessageServiceImpl implements PushMessageService {
    
        @Autowired
        private WxMpService wxMpService;
    
        @Override
        public void orderStatus(OrderDTO orderDTO) {
            //【注意】:是在订单完结的时候执行这个操作
            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
            //设置模板id
            templateMessage.setTemplateId("Iyqg2KLTaMJKKveaciV6BvrXXXXXXXXXXXXXXXXX");
            //这个openid是指微信测试号相对于我的微信号的openid,指的是消息推送给那个用户
            templateMessage.setToUser("o7TyS59g2pb-XXXXXXXXXXXX");
            List<WxMpTemplateData> data = Arrays.asList(
                    new WxMpTemplateData("first", "亲,请记得收货。"),
                    new WxMpTemplateData("keyword1", "微信点餐"),
                    new WxMpTemplateData("keyword2", "18868812345"),
                    new WxMpTemplateData("keyword3", orderDTO.getOrderId()),
                    new WxMpTemplateData("keyword4", orderDTO.getOrderStatusEnum().getMessage()),
                    new WxMpTemplateData("keyword5", "¥" + orderDTO.getOrderAmount()),
                    new WxMpTemplateData("remark", "欢迎再次光临!")
            );
            templateMessage.setData(data);
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            }catch (WxErrorException e) {
                log.error("【微信模版消息】发送失败, {}", e);
            }
        }
    }

    三、测试代码

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class PushMessageServiceImplTest {
    
        @Autowired
        private PushMessageServiceImpl pushMessageService;
    
        @Autowired
        private OrderService orderService;
    
        @Test
        public void orderStatus() throws Exception{
            OrderDTO orderDTO = orderService.findOne("1551684966828109467");
            pushMessageService.orderStatus(orderDTO);
        }
    }

    四、测试结果

    在接口测试账号内会受到如下消息内容:

    展开全文
  • 微信推送功能实现

    2019-08-15 15:55:52
    微信推送 公众号:认证的公众号(个人的认证公众号每天只能发一篇文章),粉丝可以跟公众号聊天, 未认证公众号 服务号:企业认证(营业执照),沙箱环境测试 主动给用户发消息(推送),用户要接收到推送...
  • 微号帮平台48小时推送消息便是这样的功能,微信日常图文推送,不限制次数对活跃粉丝群发。一般用于近期活动优惠、会员卡会员制度营销等公众号场景。 1、48小时推送消息 48小时推送消息功能,支持定时未来一个月...
  • 今天来带大家学习下微信模版消息推送。 先看效果图: 核心代码只有下面几行,即可轻松实现微信模版消息推送 //1,配置 WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage(); wxStorage....
  • 微信推送简单流程图

    2020-07-28 23:31:40
    根据微信官方文档,个人整理出来的微信推送的一个简单的流程图(仅是个人理解)
  • php编写 微信信息推送接口、钉钉推送接口、企业微信推送接口
  • 西门子组态软件wincc短信报警,微信推送 wincc是西门子公司开发的上位机视窗监控编程软件,用户可以利用软件提供的控件、脚本、方法等进行开发,将控制系统动做成动画画形式的人机界面,从而实现对控制系统的监控。 ...
  • 首先我们要知道企业微信推送消息的步骤,企业微信官方提供了多个API供我们调用,这里我们只讲我们需要的API; 企业微信的官方开放的API地址:https://work.weixin.qq.com/api/doc 推送企业微信消息分为这么几步: ...
  • 使用微信推送报警消息及api 三种常见报警介质: 短信:它的好处是不用联网手机有信号就行,但是需要有短信网关,需要花钱 邮件:它也可以做到手机短信通知,基本现在邮箱都有这个功能,当然如果你使用qq邮箱微信也...
  • 微信推送的流程

    2019-07-22 20:14:32
    微信推送的流程 1.用户登入到服务系统,扫码关注我的服务号(二维码是微信提供的),现在用户并没有跟我系统绑定 2.用户跟我系统绑定: 生成一个链接地址(微信的),通过链接地址生成了二维码,让用户去扫 用户扫描之后...
  • 实现 SQL SERVER 数据库中的微信推送功能,可以将一些报警消息发送到微信讨论组或者微信订阅号、公众号、服务号,需要有认证的微信订阅号或者微信服务号
  • python使用微信推送消息 from wxpy import * #该库主要是用来模拟与对接微信操作的 import requests from datetime import datetime import time import schedule from apscheduler....
  • 之前已经讨论过如何过滤微信推送的事件消息和普通消息,今天查看线上微信接口推送事件日志发现有重复消息,在系统没有高负载的情况下,这是基本不可能出现的情况。   再看一遍微信公众号开发文档的排重建议 微信...
  • 企业微信推送 - news 推送方式
  • # -*-coding:utf-8 -*- __author__ = 'yangxin_ryan' import requests, json import urllib3 ...Python实现企业微信推送文件 备注:支持中文名字等 """ class WechatFile(object): def get_token(se...
  • 微信公众平台开发时,微信推送消息的机制是推送过来后如果5秒内收不到响应则认为没有推送成功,会再次推送,如果5秒内仍没有收到响应继续推送,总共推送三次。 微信文档原文: 普通消息 1、关于重试的消息排重,...
1 2 3 4 5 ... 20
收藏数 63,099
精华内容 25,239
关键字:

微信推送