在微信小程序内调用该方法 并传入code
// AppID appsecret
public static final String appid = "xxxxxxxxxxxxxxx";
public static final String appsecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
/**
* 获取微信OpenId
*/
@RequestMapping("/getOpenId")
public void getOpenId(HttpServletResponse response, String wxCode) {
writeJsonData(response, new CommonModel(Constants.StatusCode.Success.VALUE,
Constants.StatusCode.Success.MSG, WxCodeUtils.getSessionKeyOropenid(wxCode)));
}
/**
* 获取微信小程序 session_key 和 openid
* 用户端
*
* @param code 调用微信登陆返回的Code
* @return
* @author zhuping
*/
public static JSONObject getSessionKeyOropenid(String code) {
//微信端登录code值
String wxCode = code;
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"; //请求地址
Map<String, String> requestUrlParam = new HashMap<String, String>();
requestUrlParam.put("appid", appid); //开发者设置中的appId
requestUrlParam.put("secret", appsecret); //开发者设置中的appSecret
requestUrlParam.put("js_code", wxCode); //小程序调用wx.login返回的code
requestUrlParam.put("grant_type", "authorization_code"); //默认参数
//发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
JSONObject jsonObject = JSON.parseObject(sendPost(requestUrl, requestUrlParam));
System.out.println(jsonObject);
return jsonObject;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, Map<String, ?> paramMap) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
String param = "";
Iterator<String> it = paramMap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
param += key + "=" + paramMap.get(key) + "&";
}
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
//log.error(e.getMessage(), e);
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
在小程序内拿到想要的信息传入后台登录或者注册
/**
* 登录 or 注册
*
* @param request
* @throws Exception
*/
@RequestMapping(value = "/deciphering")
@ResponseBody
public void deciphering(UserinfoDO userinfoDO, HttpServletRequest request, HttpServletResponse response) {
//根据openid查询是否有该用户,如果存在则更新,不存在则重新插入
String openId = userinfoDO.getOpenId();
UserinfoDO userinfoDO1 = userinfoService.selectDetilByopenId(openId);
if (userinfoDO1 != null) {
userinfoService.update(userinfoDO);
} else {
userinfoDO.setUserBalance(new BigDecimal(0));
userinfoDO.setCreateTime(new Date());
userinfoDO.setUserIdentify(1);
userinfoService.save(userinfoDO);
}
writeJsonData(response, new CommonModel(Constants.StatusCode.Success.VALUE,
Constants.StatusCode.Success.MSG));
}
注意小程序内要调起微信授权