思路:此篇主要介紹如何在點(diǎn)擊微信的菜單后獲得用戶的信息并跳轉(zhuǎn)至該網(wǎng)頁(yè)。?
網(wǎng)頁(yè)授權(quán)分為四步:?
1. 引導(dǎo)用戶進(jìn)入授權(quán)頁(yè)面同意授權(quán),獲取code?
2. 通過(guò)code換取網(wǎng)頁(yè)授權(quán)access_token(與基礎(chǔ)支持中的access_token不同)?
3. 如果需要,開發(fā)者可以刷新網(wǎng)頁(yè)授權(quán)access_token,避免過(guò)期?
4. 通過(guò)網(wǎng)頁(yè)授權(quán)access_token和openid獲取用戶基本信息(支持UnionID機(jī)制) 配置授權(quán)回調(diào)域名
如果用戶在微信客戶端中訪問(wèn)第三方網(wǎng)頁(yè),公眾號(hào)可以通過(guò)微信網(wǎng)頁(yè)授權(quán)機(jī)制,來(lái)獲取用戶基本信息,進(jìn)而實(shí)現(xiàn)業(yè)務(wù)邏輯。所以第一步是配置域名,在微信公眾號(hào)的公眾號(hào)設(shè)置中可以配置,域名是需要備案的。
獲取code接口請(qǐng)求為:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect?
redirect_uri為請(qǐng)求后重定向地址,也就是你要跳轉(zhuǎn)至的網(wǎng)頁(yè)地址,state為重定向后的參數(shù)。?
scope的區(qū)別說(shuō)明,有2種授權(quán)方式,根據(jù)自己的需要進(jìn)行處理:
- scope為snsapi_base,靜默授權(quán)并自動(dòng)跳轉(zhuǎn)到回調(diào)頁(yè)的。用戶感知的就是直接進(jìn)入了回調(diào)頁(yè)(往往是業(yè)務(wù)頁(yè)面)
- scope為snsapi_userinfo,這種授權(quán)需要用戶手動(dòng)同意,并且由于用戶同意過(guò),所以無(wú)須關(guān)注,就可在授權(quán)后獲取該用戶的基本信息
獲取code后,請(qǐng)求以下鏈接獲取access_token,code為上一步得到的code:?
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
新用戶進(jìn)來(lái)的時(shí)候是沒有cookie的,而且type=2,首先是要授權(quán),授權(quán)的代碼在下面。這個(gè)時(shí)候可以給其設(shè)置一個(gè)cookie,設(shè)置存活時(shí)間為10小時(shí)。授權(quán)完成后,還是會(huì)重定向進(jìn)入這個(gè)方法來(lái)處理,只是type變化,這個(gè)時(shí)候進(jìn)入測(cè)試或者正式環(huán)境,根據(jù)參數(shù)menuType進(jìn)行判斷是哪個(gè)目錄被點(diǎn)擊,然后進(jìn)入相對(duì)應(yīng)的頁(yè)面。若cookie不為空,則直接跳轉(zhuǎn)測(cè)試或者正式環(huán)境相對(duì)應(yīng)的頁(yè)面。
?
?
/**
*
* @param type 0-測(cè)試, 1-正式, 2-跳轉(zhuǎn)獲取CODE,3:認(rèn)證過(guò)的測(cè)試號(hào)
* @param menuType
* @param request
* @param wechatUserId
* @param response
* @return
*/
@RequestMapping("/view")
public ModelAndView view(Integer type,Integer menuType, Integer wechatUserId, String redirect,HttpServletRequest request, HttpServletResponse response)
{
Cookie cookie = CookieUtil.getCookieByName(request, "wechatUserId");
log.info("type:" + type + ",menuType:" + menuType + ",wechatUserId:" + wechatUserId + ",redirect:" + redirect);
String url = null;
if(cookie == null)
{
log.info("Cookie已過(guò)期.....");
if(type == 0)
{
CookieUtil.addCookie(response, "wechatUserId", Randoms.getInt(1, 53)+"", 60 * 10); /* 測(cè)試環(huán)境 */
url = "view?format=json&type=0&menuType=" + menuType + "&redirect=" + redirect;
log.info("url:" + url);
return new ModelAndView(new RedirectView(url));
}
else if(type == 1)
{
CookieUtil.addCookie(response, "wechatUserId", wechatUserId+"", (60 * 60 * 10));
/* 生產(chǎn)環(huán)境 */
url = "view?format=json&type=1&menuType=" + menuType + "&redirect=" + redirect;
log.info("url:" + url);
return new ModelAndView(new RedirectView(url));
}
else if(type == 2)
{
String wechatRedirece = UrlUtil.encode(wechatConfig.getHOST() + "wechat/user/auth?format=json&type=1&menuType=" + menuType + "&redirect=" + redirect);
/**
* 授權(quán)的鏈接
* 注意redirect_uri為重定向地址,/auth在下面的代碼中
* public String getAUTHORIZE_URL() {
* return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+getAPPID() +"&redirect_uri=";
}
*/
url = wechatConfig.getAUTHORIZE_URL() + wechatRedirece + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
log.info("url:" + url);
return new ModelAndView(new RedirectView(url));
}
else
{
return new ModelAndView(new RedirectView(url));
}
}
else
{
log.info("Cookie未過(guò)期.....");
if(type == 0)
{
switch (menuType)
{
case 0:
url = AESCryptoSecurity.decrypt(redirect, wechatConfig.getAPPID());
break;
case 1:
//社區(qū)
url = wechatConfig.getHOST_FRONT() + "page/topicList.html";
break;
case 2:
//活動(dòng)
url = wechatConfig.getHOST_FRONT() + "page/activityList.html";
break;
}
}
else
{
switch (menuType)
{
case 0:
url = AESCryptoSecurity.decrypt(redirect, wechatConfig.getAPPID());
break;
case 1:
//社區(qū)
url = wechatConfig.getHOST_FRONT() + "page/topicList.html";
break;
case 2:
//活動(dòng)
url = wechatConfig.getHOST_FRONT() + "page/activityList.html";
break;
}
}
return new ModelAndView(new RedirectView(url));
}
}
下面的代碼為獲取code,獲取access_token,獲取用戶信息等,認(rèn)證完跳轉(zhuǎn)至對(duì)應(yīng)的頁(yè)面
?
?
@RequestMapping("/auth")
public ModelAndView auth(String code, Integer type, Integer menuType, String redirect) throws Exception
{
log.info("code:" + code + ",type:" + type + ",menuType:" + menuType);
/* 向微信發(fā)請(qǐng)求獲取access_token */
Map<String, Object> map = wechatUserService.getPageAccessToken(code);
/* 向微信發(fā)請(qǐng)求,用access_token獲取用戶信息并保存 */
WechatUser pageWechatUser = wechatUserService.getPageWechatUser(map.get("access_token").toString(), map.get("openid").toString());
String url = null;
if(type == 1)
{
/* 權(quán)限認(rèn)證完成后,將type改為1或者0,重定向進(jìn)入上面的方法進(jìn)行頁(yè)面跳轉(zhuǎn) */
url = wechatConfig.getHOST() + "wechat/menu/view?&type=1&menuType=" + menuType + "&wechatUserId=" + pageWechatUser.getWechatId() + "&redirect=" + redirect;
log.info("url:" + url);
}
return new ModelAndView(new RedirectView(url));
}
?
?
?
本文摘自 :https://blog.51cto.com/u