环境说明:
1.项目功能:年会抽奖活动开发,子功能:获取客户的相对公众号的唯一oppenid,然后确认是否关注本公众号,关注则获取客户信息后发抽奖券保存,否则先关注微信公众号;
2.java环境:SSM、jdk1.8、tomcat8、jar包直接导入项目、eclipe
小坑说明:判断是否关注微信公众号,使用如下的url
String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openId+"&lang=zh_CN";
关注则返回客户头像昵称等信息,不关注返回subscribe=0;
其中参数access_token为秘钥,分为全局和局部的秘钥,这里只能使用全局秘钥,但是在调试时候验证局部秘钥是否对oppenid有效的时候测试有效,一直使用局部秘钥访问报错,一度怀疑oppenid获取的和本公众号不匹配,最后百度有人说用全局秘钥更换后OK;
注意:全局access_token使用线程每2小时生成一次;
全部代码:、
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="net.sf.json.JSONArray"%>
<%@ page import="net.sf.json.JSONObject"%>
<%@ page import="com.hy.system.thread.TokenThread_hy"%>
<% String code = request.getParameter("code");
/*根据客户code获取客户openID */
String openId = "";
String access_token= "";
String nickname = ""; //客户昵称
String headimgurl= ""; //客户头像地址
String subscribe =""; //客户是否关注公众号
int cont = 1;
if (code != null) {
//获取openid连接
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx538164e175f2dfaf&secret=2c067baa70a74fce98c22ca7fee55247&code="+code+"&grant_type=authorization_code";
try {
URL getUrl=new URL(requestUrl);
HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] b = new byte[size];
is.read(b);
String message = new String(b, "UTF-8");
JSONObject json = JSONObject.fromObject(message);
System.out.println("1.返回值集合json="+json);
openId = json.getString("openid");
access_token = json.getString("access_token");
cont = 10;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//获取客户信息:昵称、头像
if (openId != "" && openId != null ) {
access_token = TokenThread_hy.access_token.getAccess_token();
System.out.println("全局秘钥="+access_token);
System.out.println("openId="+openId);
String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openId+"&lang=zh_CN";
try {
URL getUrl=new URL(requestUrl);
HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] b = new byte[size];
is.read(b);
String message = new String(b, "UTF-8");
JSONObject json = JSONObject.fromObject(message);
System.out.println("2.返回值集合json="+json);
subscribe = json.getString("subscribe");
/*为零则未关注本公众号 */
if(!("0".equals(subscribe))){
nickname = json.getString("nickname");
headimgurl = json.getString("headimgurl");
}
cont = 11;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
%>
//客户未关注微信公众号的让其关注,关注过的发送调用微信API的验证请求信息
$(function(){
var openId = '<%=openId%>';
var subscribe = '<%=subscribe%>';
console.info("subscribe="+subscribe);
/* openId ='46546465dfffddfdff'; */
if("0" == subscribe ){
console.info("请您先关注'沪佳家装工厂店'公众号后再来领取");
/* $("#all").empty();
alert("请您先关注'沪佳家装工厂店'公众号后再来领取!") */
window.location.href="guanzhugongzonghao.html"; //在当前窗口中打开新页面
}else{
<%-- var code = '<%=code%>'; --%>
/*微信公众号平台 调用其js SDK 接口 的验证请求 */
<%-- $.ajax({
url:"<%=request.getContextPath()%>/weixin/getverifyJSSDKCode_sendTicketAndsignInJSP.do",
type:"post",
dataType:"json",
data:{"code":code},
success:function(date){
//console.info("date="+date);
console.info(date);
/*步骤三:通过config接口注入权限验证配置 */
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: date.appId, // 必填,公众号的唯一标识
timestamp: date.timestamp, // 必填,生成签名的时间戳
nonceStr: date.nonceStr, // 必填,生成签名的随机串
signature: date.signature,// 必填,签名
jsApiList: ['addCard'] // 必填,需要使用的JS接口列表
});
/*
wx.error(function (res) {
alert("出错了:" + res.errMsg);//这个地方的好处就是wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。
});
*/
}
}); --%>
/*保存客户信息到数据库 并显示抽奖券的信息,已经领取过的显示抽奖券的信息 */
insertUserInfoToData();
}
});
/*保存客户信息到数据库 并显示抽奖券的信息,已经领取过的显示抽奖券的信息 */
function insertUserInfoToData(){
var nickename = '<%=nickname%>';
var headimgurl = '<%=headimgurl%>';
var openId = '<%=openId%>';
/*
openId ='46546465dfffddfdff';
nickename = '张飞2'; */
var getContextPath = '<%=request.getContextPath()%>';
console.info("获取的项目路径getContextPath = " + getContextPath);
$.ajax({
url:"<%=request.getContextPath()%>/weixin/insertUserInfoOrGetLottoleInfo.do",
type:"post",
data:{
"nickename":nickename,
"headimgurl":headimgurl,
"openId":openId
},
dataType:"json",
success:function(datas){
var msg = datas.msg;//结果信息
var code = datas.code;//结果代码
console.info("结果信息="+msg);
console.info("结果代码="+code);
if(code != 0 ){
var data = datas.info;//抽奖券信息
console.info("抽奖券信息="+data);
console.info(data);
var jsp ='';
jsp +=' ';
jsp +='
//发放年会抽奖券
function sendLotteryTicket(){
var code = '<%=code%>';
console.info(" code="+code);
$.ajax({
url:"<%=request.getContextPath()%>/weixin/sendTicketLottery.do",
type:"post",
data:{"code":code},
dataType:"json",
success:function(data){
console.info("发放年会抽奖券=");
console.info(data);
/*发放年会抽奖券 到微信卡包*/
wx.addCard({
cardList: [{
cardId: data.cardId,
cardExt: '{"code":"", "openid": "'+data.openid+'", "nonce_str":"'+data.nonceStr+'","timestamp": "'+data.timestamp+'", "signature":"'+data.signature+'"}'
}], // 需要添加的卡券列表
success: function (res) {
var cardList = res.cardList; // 添加的卡券列表信息
}
});
}
});
}