后台:
@RequestMapping({"auth"})
public String auth(HttpServletRequest request, HttpServletResponse response) throws Exception {
String htmlPath = props.getStr("htmlPath");
String code = request.getParameter("code");
String state = request.getParameter("state");
Map<String, String> result = weixinutils.getUserInfoAccessToken(code);
String openId = (String)result.get("openid");
System.out.println("openid=" + openId);
String token;
if (StringUtils.isEmpty(openId)) {
token = htmlPath + "?openId=" + openId;
request.setAttribute("url", token);
return "sys/wei_xin_render";
} else {
token = this.redisUtils.get("token");
System.out.println("=======================token=" + token);
String access_token = "";
String name64;
if (StringUtils.isNotBlank(token)) {
access_token = token;
} else {
String appid = props.getStr("appid");
name64 = props.getStr("AppSecret");
Map<String, String> accessToken = weixinutils.getAccessToken(appid, name64);
access_token = (String)accessToken.get("access_token");
this.redisUtils.set("token", access_token);
}
Map<String, String> userInfo = weixinutils.getAutoUserInfo(access_token, openId);
name64 = "";
String headPic = StringUtil.addPath("default.jpg");
Integer sex = 0;
if (Integer.parseInt((String)userInfo.get("subscribe")) == 1) {
headPic = (String)userInfo.get("headimgurl");
String nickName = (String)userInfo.get("nickname");
sex = Integer.parseInt((String)userInfo.get("sex"));
name64 = StringUtil.base64NikeName(nickName);
}
YwEmployee employee = (YwEmployee)this.employeeService.getOne((Wrapper)((QueryWrapper)(new QueryWrapper()).eq("deleted", 0)).eq("wx_openid", openId));
System.out.println("员工=" + employee);
boolean bindFlag = false;
String url;
if (null == employee) {
employee = new YwEmployee();
employee.setDeleted(0).setWxHeader(headPic).setWxNickname(name64).setGmtCreate(new Date()).setWxOpenid(openId).setSex(sex);
this.employeeService.add(employee);
url = htmlPath + "?openId=" + openId;
request.setAttribute("url", url);
return "sys/wei_xin_render";
} else {
if (!employee.getWxNickname().equals(name64) || !employee.getWxHeader().equals(headPic)) {
employee.setWxNickname(name64).setWxHeader(headPic).setSex(sex);
this.employeeService.updateById(employee);
}
url = htmlPath + "?openId=" + openId;
request.setAttribute("url", url);
return "sys/wei_xin_render";
}
}
}
工具类:
import cn.hutool.http.HttpUtil;
import cn.hutool.setting.dialect.Props;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.HashMap;
import java.util.Map;
public class weixinutils {
static Props props = new Props("config.properties", "utf-8");
/**
* @auther: Zww
* 功能描述:
* @param: 获取全局的accessToken
* @return:
* @date: 2019/5/28 15:58
*/
public static Map<String, String> getAccessToken(String wx_appid, String wx_appsecret) {
Map<String, String> data = new HashMap();
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ wx_appid + "&secret=" + wx_appsecret;
JsonObject accessTokenInfo = null;
try {
Gson token_gson = new Gson();
String result = HttpUtil.get(url);
System.out.println("全局的accessToken="+result);
accessTokenInfo = token_gson.fromJson(result, JsonObject.class);
data.put("access_token", accessTokenInfo.get("access_token").toString().replaceAll("\"", ""));
data.put("expires_in", accessTokenInfo.get("expires_in").toString().replaceAll("\"", ""));
} catch (Exception ex) {
ex.printStackTrace();
}
return data;
}
/**
* @auther: Zww
* 功能描述:
* @param: 获取授权AccessToken
* @return:
* @date: 2019/5/28 15:39
*/
public static Map<String, String> getUserInfoAccessToken(String code) {
Props props = new Props("config.properties", "utf-8");
String appid = props.getStr("appid");
String AppSecret = props.getStr("AppSecret");
JsonObject object = null;
Map<String, String> data = new HashMap<>();
String url = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appid, AppSecret, code);
String result = HttpUtil.get(url);
Gson token_gson = new Gson();
object = token_gson.fromJson(result, JsonObject.class);
data.put("openid", object.get("openid").toString().replaceAll("\"", ""));
data.put("access_token", object.get("access_token").toString().replaceAll("\"", ""));
return data;
}
/**网页授权获取用户信息
* 获取用户信息 subscribe = 0 没关注公众号获取不了用户信息 1 关注公众号能获取用户信息
*
* @param accessToken 全局的
* @param openId
* @return
*/
public static Map<String, String> getAutoUserInfo(String accessToken, String openId) {
Map<String, String> data = new HashMap();
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";
JsonObject userInfo = null;
try {
String result = HttpUtil.get(url);
System.out.println("用户信息"+result);
Gson token_gson = new Gson();
userInfo = token_gson.fromJson(result, JsonObject.class);
data.put("subscribe", userInfo.get("subscribe").toString().replaceAll("\"", ""));
data.put("openid", userInfo.get("openid").toString().replaceAll("\"", ""));
if(Integer.parseInt(userInfo.get("subscribe").toString().replaceAll("\"", ""))==1){
data.put("sex", userInfo.get("sex").toString().replaceAll("\"", ""));
data.put("nickname", userInfo.get("nickname").toString().replaceAll("\"", ""));
data.put("city", userInfo.get("city").toString().replaceAll("\"", ""));
data.put("province", userInfo.get("province").toString().replaceAll("\"", ""));
data.put("country", userInfo.get("country").toString().replaceAll("\"", ""));
data.put("headimgurl", userInfo.get("headimgurl").toString().replaceAll("\"", ""));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return data;
}
}