String appid ="微信appid ";
String redirect_uri="回调地址";
String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=jetcms#wechat_redirect";
try {
PrintWriter out = response.getWriter();
out.println("");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@RequestMapping(value = "/weixinOpenid")
public String weixinLogin(String code, String redirectUrl, HttpServletRequest request, HttpServletResponse response,
ModelMap model) throws IOException {
String tokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?grant_type=authorization_code";
tokenUrl = tokenUrl + "&appid=" + appid + "&secret=" + secret + "&code=" + code;
JSONObject json = null;
try {
json = new JSONObject(NetUtils.getNetData(tokenUrl, "GET", ""));
// ------------在这里把使用过的code存入到缓存中--------
} catch (JSONException e2) {
e2.printStackTrace();
}
String openId = "";
String accessToken = "";
String[] s = new String[2];
if (json != null) {
try {
openId = json.getString("openid");
accessToken = json.getString("access_token");
s[0] = openId;
s[1] = accessToken;
System.out.println("手机登录 获取用户授权:" + json);
} catch (JSONException e) {
String errcode = null;
try {
errcode = json.getString("errcode");
System.out.println("手机登录 获取用户授权失败" + errcode);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
//实际上可以在这里保存openid并绑定用户号码、设备IMEI等等
return redirectUrl;
}
/**
* 获取用户信息 拉取用户信息(需scope为 snsapi_userinfo) 只有在用户将公众号绑定到微信开放平台帐号后,可以获取unionid
*
* @param
* @param
* @return
*/
public String[] getUserInfo(String openid, String accessToken) {
String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?lang=zh_CN";
userInfoUrl = userInfoUrl + "&access_token=" + accessToken + "&openid=" + openid;
JSONObject json = null;
try {
json = new JSONObject(NetUtils.getNetData(userInfoUrl, "GET", ""));
} catch (JSONException e2) {
e2.printStackTrace();
}
String nickname = ""; // 用户昵称
String sex = ""; // 用户的性别
String province = ""; // 用户个人资料填写的省份
String city = ""; // 普通用户个人资料填写的城市
String country = ""; // 国家,如中国为CN
String headimgurl = ""; // 用户头像,
String[] s = new String[6];
if (json != null) {
try {
nickname = json.getString("nickname");
sex = json.getString("sex");
province = json.getString("province");
city = json.getString("city");
country = json.getString("country");
headimgurl = json.getString("headimgurl");
s[0] = nickname;
s[1] = sex;
s[2] = province;
s[3] = city;
s[4] = country;
s[5] = headimgurl;
System.out.println("获取用户信息成功" + json);
} catch (JSONException e) {
String errcode = null;
try {
errcode = json.getString("errcode");
System.out.println("获取用户信息失败" + errcode);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
return s;
}
public String weixinToken() throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret="
+ secret;
System.out.println("weixinToken_url:" + url);
String res = NetUtils.getNetData(url, "GET", "");
System.out.println("weixinToken_res:" + res);
JSONObject json = null;
try {
json = new JSONObject(res);
System.out.println("weixinToken_access_token:" + json.getString("access_token"));
return json.getString("access_token");
} catch (JSONException e2) {
e2.printStackTrace();
}
return null;
}
/**
* 发送公众号模板消息推送
*
* @param access_token
* @param appid
*/
private void sendTemplate(String access_token, String appid) {
// TODO Auto-generated method stub
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
System.out.println("url:" + url);
JSONObject json = new JSONObject();
json.put("touser", appid);
json.put("template_id", "模版id");
json.put("url", "http://weixin.qq.com/download");
json.put("topcolor", "#FF0000");
// 具体的模版数据,根据模版来传
JSONObject dataJson = new JSONObject();
dataJson.put("first", getValue("奥捷迅科技"));
dataJson.put("keyword1", getValue(new Data().toString()));
dataJson.put("keyword2", getValue("深圳市宝安区奥捷迅科技"));
dataJson.put("keyword3", getValue("NB烟感"));
dataJson.put("keyword4", getValue("NB烟感烟雾报警"));
dataJson.put("keyword5", getValue("玉念聿辉"));
dataJson.put("remark", getValue("贵司存在严重电气火灾安全隐患,为避免造成不可挽救后果,请及时处理!谢谢!"));
json.put("data", dataJson);
System.out.println("json:" + json);
String res = NetUtils.getNetData(url, "POST", json.toString());
System.out.println("res:" + res);
}
private JSONObject getValue(String value) {
// TODO Auto-generated method stub
JSONObject json = new JSONObject();
json.put("value", value);
json.put("color", "#173177");
return json;
}
没什么技术含量,就是看文档花了我很多时间,特意抽时间把代码和思路贴出来,希望对有相关需求的朋友有所帮助。
就只是以上这么简单的几步代码,想看我到底是踩了多大的坑,才狠下决心要过来贴代码的朋友请点击这里。