最近做了一个类似于日程提醒的小程序,需要把用户语音识别出的日程或提醒按照设置的提醒日期通过微信公众号推送给对应的用户进行提醒这样子.这是需求背景.
这个项目我已经做完了,但是中间遇到很多坑,所以想出篇帖子,让大家不浪费爬坑的时间.后面我会尽量把所有需要的代码什么的,都贴上来,尽量做到拿了就能用的程度.
接下来,就是一个又一个的坑需要爬了.首先我说一下小程序与公众号开发的一个完整流程.与一些细节.
1.小程序开发首先需要appid 与secret 也就是账号密码,用来获取用户的unionid与openid.(需要根据js_code来获取)这两个名叫id的东西,都是用户的唯一标识,(以前不同的appid获取到的用户openid都是一样的,现在不一样了) unionid是干什么用的,我一会会说.
2.公众号开发也需要一个appid跟secret,这个跟小程序的不一样,具体怎么拿,跟小程序一样.有了appid与secret我们就可以获取到公众号用户的openid与unionid了(需要根据code来获取),(获取之前还需要先获取assess_token,这是一个微信的接口,用来验证身份,微信设置一个开发者账号每天可以请求200次的限制,每个token有7200s的有效时间...所以这里获取到的话,需要放到redis或者java缓存中去,然后设置一个7200s的过期时间,用的时候从缓存取,缓存里没有再请求.后面我会说怎么获取)这里的openid与小程序的openid是不一样的,但是unionid可以弄成一样的.怎么弄呢,看第三步.
3.这时候应该上微信公众平台把小程序绑定到其下.然后再注册一个微信开放平台,并且获取开发者资质,(一年好像是300).然后把你的小程序与公众号都绑定到开放平台下,这样的话,你的小程序获取到的unionid跟公众号获取到的unionid就是一样的了,这样就可以用它来进行小程序与公众号用户的关联了.
------------------------------------------------------然后进行代码------------------------------------------------------
1.根据js_code获取小程序用户的openid与unionid (js_code 是小程序在调用wx.login的时候获取到的.) 代码如下:
@RequestMapping(value = "/checkUserByOpenid", method = RequestMethod.GET)public @ResponseBody Map checkUserByOpenid(HttpServletRequest request,HttpServletResponse response,@RequestParam("js_code") String js_code) throws Exception {response.setHeader("Access-Control-Allow-Origin", "*");/*星号表示所有的域都可以接受,*/response.setHeader("Access-Control-Allow-Methods", "GET,POST");HashMap
// if(js_code!=null && !"".equals(js_code)){String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + js_code + "&grant_type=" + grant_type;String httpsRtn = HttpsPostUtil.doPost(url, "UTF-8");Map
// }return jsonMap;}
2.httpsclient工具类 发送请求给微信接口
package com.qs.util;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;/*** 利用HttpClient进行post请求的工具类** @author Kevin* @ClassName: HttpsClientUtil* @Description: TODO* @date 2017年2月7日 下午1:43:38*/
public class HttpsPostUtil {public static String doPost(String url, String charset) {HttpClient httpClient = null;HttpPost httpPost = null;String result = null;try {httpClient = new SSLClient();httpPost = new HttpPost(url);HttpResponse response = httpClient.execute(httpPost);if (response != null) {HttpEntity resEntity = response.getEntity();if (resEntity != null) {result = EntityUtils.toString(resEntity, charset);}}} catch (Exception ex) {ex.printStackTrace();}return result;}
}
package com.qs.util;import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;/*** 用于进行Https请求的HttpClient** @author Kevin * @ClassName: SSLClient* @Description: TODO* @date 2017年2月7日 下午1:42:07*/
public class SSLClient extends DefaultHttpClient {public SSLClient() throws Exception {super();SSLContext ctx = SSLContext.getInstance("TLS");X509TrustManager tm = new X509TrustManager() {public void checkClientTrusted(X509Certificate[] chain,String authType) throws CertificateException {}public void checkServerTrusted(X509Certificate[] chain,String authType) throws CertificateException {}public X509Certificate[] getAcceptedIssuers() {return null;}};ctx.init(null, new TrustManager[]{tm}, null);SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);ClientConnectionManager ccm = this.getConnectionManager();SchemeRegistry sr = ccm.getSchemeRegistry();sr.register(new Scheme("https", 443, ssf));}
}
3.然后再根据code获取公众号下用户的openid与uninonid等个人信息了,这里要多说一下,这个code在小程序里面是无法直接获取的,我是在小程序wx.login后调用了web view网页弹出 来引导用户进行授权的,这里大家可以参考一下微信的官方说明文档,需要配置一个服务器域名 或 测试用机的IP地址白名单,还要配置一个授权成功后回调的接口地址,也就是你自己写的接口,这个接口中就可以获取到code,code是从你的授权网页传递过来的,在网页上写一个onload默认加载,把页面地址栏里接收到的微信传来的code参数拿出来,传到自己的接口中去..下面我把接口中根据code获取微信公众平台用户的个人信息代码贴上
@RequestMapping(value = "/callBack", method = RequestMethod.GET)public @ResponseBody void callBack(HttpServletRequest request,HttpServletResponse response) throws Exception {response.setHeader("Access-Control-Allow-Origin", "*");/*星号表示所有的域都可以接受,*/response.setHeader("Access-Control-Allow-Methods", "GET,POST");//使用code获取公众号用户的openid------------String urls = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code";JSONObject result = WX_HttpsUtil.httpsRequest(urls, "GET");JSONObject resultJson = new JSONObject(result);String GZHopenid = (String) resultJson.get("openid");String errmsg = (String) resultJson.get("errmsg");//如果code失效 则不添加用户if(errmsg==null){//使用openid获取unionidString access_token = WX_TokenUtil.getWXToken().getAccessToken();String url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+GZHopenid+"&lang=zh_CN";JSONObject result2 = WX_HttpsUtil.httpsRequest(url2, "GET");JSONObject resultJson2 = new JSONObject(result2);String GZHunionid = (String) resultJson.get("unionid");}}
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
到这里为止,你的公众号与小程序的用户的个人信息就全部获取到了,可以通过unionid关联起来了...
下面开始通过微信公众平台来给用户推送消息.
ps:这里需要登录微信公众平台先设置一个自己需要推送的消息模板,可以从微信提供的模板库中选取,如果没有合适的,就自己申请一个模板,然后把模板ID复制粘贴到代码中.
如下几个都是需要的工具类
package weixin.util.wxservlet;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/*** 请求校验工具类* @author m**/
public class SignUtil {// 与接口配置信息中的Token要一致 private static String token = "weixin"; /** * 验证签名 * * @param signature * @param timestamp * @param nonce * @return */ public static boolean checkSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 将token、timestamp、nonce三个参数进行字典序排序 Arrays.sort(arr); StringBuilder content = new StringBuilder(); for (int i = 0; i
}
package weixin.util.wxservlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class WxServlet extends HttpServlet{public WxServlet() {// TODO Auto-generated constructor stub}/* (non-Javadoc)* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)*/@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 微信加密签名 String signature = request.getParameter("signature"); // 时间戳 String timestamp = request.getParameter("timestamp"); // 随机数 String nonce = request.getParameter("nonce"); // 随机字符串 String echostr = request.getParameter("echostr"); PrintWriter out = response.getWriter(); // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败 if (SignUtil.checkSignature(signature, timestamp, nonce)) { out.print(echostr); } out.close(); out = null; }/* (non-Javadoc)* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)*/@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubsuper.doPost(req, resp);}}
package weixin.util;import java.io.Serializable;
/*
*AccessToken 对象
*/
public class AccessToken implements Serializable {//获取到的凭证private String accessToken;//凭证有效时间,单位:秒private int expiresin; /*** @return the accessToken*/public String getAccessToken() {return accessToken;}/*** @param accessToken the accessToken to set*/public void setAccessToken(String accessToken) {this.accessToken = accessToken;}/*** @return the expiresin*/public int getExpiresin() {return expiresin;}/*** @param expiresin the expiresin to set*/public void setExpiresin(int expiresin) {this.expiresin = expiresin;}public AccessToken() {// TODO Auto-generated constructor stub}}
package weixin.util;import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;import javax.net.ssl.X509TrustManager;
/*** 微信请求 - 信任管理器*/public class MyX509TrustManager implements X509TrustManager{public MyX509TrustManager() {// TODO Auto-generated constructor stub}public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}public X509Certificate[] getAcceptedIssuers() {// return new X509Certificate[0];return null;}}
package weixin.util;
/*** 模板详细信息 * 根据需求自己更改*/
public class TemplateData {private String value;private String color;public TemplateData(String value,String color){this.value = value;this.color = color;}/*** @return the value*/public String getValue() {return value;}/*** @param value the value to set*/public void setValue(String value) {this.value = value;}/*** @return the color*/public String getColor() {return color;}/*** @param color the color to set*/public void setColor(String color) {this.color = color;}}
package weixin.util;import java.util.Map;/*** 注册成功,通知模板消息实体类*/
public class TemplateMessage {private String touser; //用户OpenIDprivate String template_id; //模板消息IDprivate String url; //URL置空,在发送后,点模板消息进入一个空白页面(ios),或无法点击(android)。private String topcolor; //标题颜色private Map
package weixin.util;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 javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;public class WX_HttpsUtil {private static Logger log = LoggerFactory.getLogger(WX_HttpsUtil.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.parseObject(buffer.toString());} catch (ConnectException ce) {log.error("连接超时:{}", ce);} catch (Exception e) {log.error("https请求异常:{}", e);}return jsonObject;}}
package weixin.util;import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;public class WX_TemplateMsgUtil {private static Logger log = LoggerFactory.getLogger(WX_TemplateMsgUtil.class);/*** 封装模板详细信息* @return*/public static JSONObject packJsonmsg(Map
// finally {
// if(templatId!=null) {
// //删除新增的 微信模板
// deleteWXTemplateMsgById(templatId);
// }
// }return "success";}}
package weixin.util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;public class WX_TokenUtil {private static Logger log = LoggerFactory.getLogger(WX_TokenUtil.class);/*** 获得微信 AccessToken* access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。* 开发者需要access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取* 的access_token失效。 * (此处我是把token存在java缓存里面了)此代码token没有加入缓存,后面附了缓存的工具类,根据需求自己添加*/public static AccessToken getWXToken() {String appId="****************";String appSecret="****************";String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId+"&secret="+ appSecret;JSONObject jsonObject = WX_HttpsUtil.httpsRequest(tokenUrl, "GET", null);System.out.println("jsonObject:"+jsonObject);AccessToken access_token = new AccessToken();if (null != jsonObject) {try {access_token.setAccessToken(jsonObject.getString("access_token"));access_token.setExpiresin(jsonObject.getInteger("expires_in"));} catch (JSONException e) {access_token = null;// 获取token失败log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInteger("errcode"), jsonObject.getString("errmsg"));}}return access_token;}}
package weixin.util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.alibaba.fastjson.JSONObject;public class WX_UserUtil {private static Logger log = LoggerFactory.getLogger(WX_UserUtil.class);/*** 根据微信openId 获取用户是否订阅* @param openId 微信openId* @return 是否订阅该公众号标识*/public static Integer subscribeState(String openId){String tmpurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+WX_TokenUtil.getWXToken().getAccessToken() +"&openid="+openId;JSONObject result = WX_HttpsUtil.httpsRequest(tmpurl, "GET",null);JSONObject resultJson = new JSONObject(result);log.error("获取用户是否订阅 errcode:{} errmsg:{}", resultJson.getInteger("errcode"), resultJson.getString("errmsg"));String errmsg = (String) resultJson.get("errmsg");System.out.println(errmsg);if(errmsg==null){//用户是否订阅该公众号标识(0代表此用户没有关注该公众号 1表示关注了该公众号)。Integer subscribe = (Integer) resultJson.get("subscribe");return subscribe;}return 0;}public static JSONObject getUserInfo(String openId){String tmpurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+WX_TokenUtil.getWXToken().getAccessToken() +"&openid="+openId;JSONObject result = WX_HttpsUtil.httpsRequest(tmpurl, "GET",null);JSONObject resultJson = new JSONObject(result);log.error("获取用户是否订阅 errcode:{} errmsg:{}", resultJson.getInteger("errcode"), resultJson.getString("errmsg"));String errmsg = (String) resultJson.get("errmsg");return resultJson;}
}
package cache.util;import java.util.Calendar;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
/** * 简单内存缓存管理器 * 可设置过期时间,单位毫秒 * 使用Timer定时清理过期数据,每分钟清理一次,可修改清理周期 * @author Kevin* @datetime 2018年4月16日 下午5:18:50 */
public class CacheManager { @SuppressWarnings("rawtypes") private static Map
}
最后进行测试发送推送消息
package weixin.util;import java.util.HashMap;
import java.util.Map;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;public class TestWX {public static void main(String[] args) {//新增用户成功 - 推送微信消息senMsg("openid 以及几个需要的模板消息参数,这个根据自己需求来修改");}public static void senMsg(String openId,String name,String event,String times,String remark){//用户是否订阅该公众号标识 (0代表此用户没有关注该公众号 1表示关注了该公众号)Integer state= WX_UserUtil.subscribeState(openId);System.out.println("state:"+state);// 绑定了微信并且关注了服务号的用户 , 注册成功-推送注册短信if(state==1){Map
// String regTempId = WX_TemplateMsgUtil.getWXTemplateMsgId("TM00464");JSON.toJSONString(param);JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(param));//调用发送微信消息给用户的接口 ********这里写自己在微信公众平台拿到的模板IDWX_TemplateMsgUtil.sendWechatMsgToUser(openId, ***********************************, "", "#000000", jsonObject);//获取公众号的自动回复规则String urlinfo="https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token="+WX_TokenUtil.getWXToken().getAccessToken();JSONObject joinfo = WX_HttpsUtil.httpsRequest(urlinfo, "GET", null);Object o=joinfo.get("is_add_friend_reply_open");// System.out.println("o:"+joinfo);String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";JSONObject Token = WX_HttpsUtil.httpsRequest(getTokenUrl, "GET", null);System.out.println("Token:"+Token);}}}
此时你应该可以成功推送消息了.如上代码我确保都是经过测试的,完全可以使用没有问题
帖子写的匆忙,如果任何疑问,请搜索公众号"老秦的快乐生活"获取我的联系方式,需要https工具的请直接公众号发送"https"获取代码