微信JS-SDK说明文档
- 后台获取拍照页面需先授权(原理同上一篇博客:微信网页授权)
@RequestMapping("/getTakePhotos")
public ModelAndView getTakePhotos(HttpServletRequest request) {
logger.info(">>>>>>>>>>>>>>>>获取拍照页面>>>>>>>>>>>>>>>>");
ModelAndView modelAndView = new ModelAndView();
String code = request.getParameter("code");
logger.info("code为:{}", code);
if (StringUtils.isBlank(code)) {
logger.info("进来这人没code,有问题");
modelAndView.addObject("openId", "");
modelAndView.addObject("errorInfo", "openId为空");
modelAndView.setViewName("/mobile/404");
return modelAndView;
}
String openid = "";
try {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + ParamesAPI.appId+ "&secret=" + ParamesAPI.secret + "&code=" + code + "&grant_type=authorization_code";
logger.info("url:{}", url);
JSONObject result = HttpsClient.httpRequest(url, "GET", null);
logger.info("获取openId的json:{}", result);
openid = (String) result.get("openid");
if (StringUtils.isEmpty(openid)) {
logger.info("openid为空:{}", openid);
/**
* 跳转错误页面
*/
modelAndView.setViewName("/mobile/404");
return modelAndView;
}
request.getSession().setAttribute("openId", openid);
} catch (Exception e) {
logger.info("未知错误:{}", e);
modelAndView.addObject("openId", "");
modelAndView.addObject("errorInfo", "openId为空");
modelAndView.setViewName("/mobile/404");
return modelAndView;
}
modelAndView.setViewName("mobile/takePhotos");
return modelAndView;
}
- 拍照页面takePhoto.jsp
步骤一:绑定域名
先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<html>
<head>
<meta charset="utf-8">
<title>微信拍照</title>
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="bl