或者
问答详情页顶部banner图
您的位置:首页 >开发 > 移动应用 > 微信开发 > 网页被自动转接到 很快 微信开发

网页被自动转接到 很快 微信开发

提问者:果果  |   分类:微信开发  |   浏览215次  |   悬赏分:3积分 2017-04-02 06:00:25

我要回答

提 交

匿名

  • think

    网页授权分为四步: 1. 引导用户进入授权页面同意授权,获取code 2. 通过code换取网页授权access_token(与基础支持中的access_token不同) 3. 如果需要,开发者可以刷新网页授权access_token,避免过期 4. 通过网页授权access_token和openid获取用户基本信息(支持UnionID机制) 配置授权回调域名 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。所以第一步是配置域名,在微信公众号的公众号设置中可以配置,域名是需要备案的。 获取code 接口请求为: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为请求后重定向地址,也就是你要跳转至的网页地址,state为重定向后的参数。 scope的区别说明,有2种授权方式,根据自己的需要进行处理: scope为snsapi_base,静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面) scope为snsapi_userinfo,这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息 获取网页授权的access_token 获取code后,请求以下链接获取access_token,code为上一步得到的code: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code 代码说明 新用户进来的时候是没有cookie的,而且type=2,首先是要授权,授权的代码在下面。这个时候可以给其设置一个cookie,设置存活时间为10小时。授权完成后,还是会重定向进入这个方法来处理,只是type变化,这个时候进入测试或者正式环境,根据参数menuType进行判断是哪个目录被点击,然后进入相对应的页面。若cookie不为空,则直接跳转测试或者正式环境相对应的页面。 /** * * @param type 0-测试, 1-正式, 2-跳转获取CODE,3:认证过的测试号 * @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已过期....."); if(type == 0) { CookieUtil.addCookie(response, "wechatUserId", Randoms.getInt(1, 53)+"", 60 * 10); /* 测试环境 */ 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)); /* 生产环境 */ 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); /** * 授权的链接 * 注意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未过期....."); if(type == 0) { switch (menuType) { case 0: url = AESCryptoSecurity.decrypt(redirect, wechatConfig.getAPPID()); break; case 1: //社区 url = wechatConfig.getHOST_FRONT() + "page/topicList.html"; break; case 2: //活动 url = wechatConfig.getHOST_FRONT() + "page/activityList.html"; break; } } else { switch (menuType) { case 0: url = AESCryptoSecurity.decrypt(redirect, wechatConfig.getAPPID()); break; case 1: //社区 url = wechatConfig.getHOST_FRONT() + "page/topicList.html"; break; case 2: //活动 url = wechatConfig.getHOST_FRONT() + "page/activityList.html"; break; } } return new ModelAndView(new RedirectView(url)); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 下面的代码为获取code,获取access_token,获取用户信息等,认证完跳转至对应的页面 @RequestMapping("/auth") public ModelAndView auth(String code, Integer type, Integer menuType, String redirect) throws Exception { log.info("code:" + code + ",type:" + type + ",menuType:" + menuType); /* 向微信发请求获取access_token */ Map map = wechatUserService.getPageAccessToken(code); /* 向微信发请求,用access_token获取用户信息并保存 */ WechatUser pageWechatUser = wechatUserService.getPageWechatUser(map.get("access_token").toString(), map.get("openid").toString()); String url = null; if(type == 1) { /* 权限认证完成后,将type改为1或者0,重定向进入上面的方法进行页面跳转 */ 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)); }

    2017-06-07 11:19:27
    评论0  |   0
问答详情中间banner