上一章大家详细介绍了怎样根据推荐朋友在微信上自动跳转。此章大家将详细介绍怎样根据扫二维码在微信上自动跳转。

前提条件:从微信官方网账户获得appid,secret,grantType三个主要参数预留。

1.获得微信跳转连接页面。

这一页面主要是获得能够跳转到扫描仪网页页面的页面连接。

@GET@Path(value = "getData")@Produces(MediaType.APPLICATION_JSON)public Response getData() { Map result = new HashMap(); try { //......业务流程编码...... String recUrl = "https://XXXX.com/新项目名/oauth";//完成跳转的联接,该插口完成看第3节讲 result.put("url", recUrl); return Response.ok(result).build(); } catch (Exception e) { result.put("code", 0); result.put("msg", "出现异常"); return Response.ok(result).build(); } }

2.二维码网页页面。

这一网页页面能够根据扫二维码自动跳转,还可以一键复制在微信上开启完成自动跳转。

扫二维码自动跳转或是图片识别自动跳转 一键复制(微信中点一下网页链接可立即自动跳转) function convertCanvasToImage() { var image = new Image(); var canvas = document.getElementsByTagName('canvas')[0]; image.src = canvas.toDataURL("image/png"); return image;}$(function() { //能够立即一键复制在微信中,随后点一下网页链接可自动跳转到与扫二维码的同一个页面 var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" appid "&redirect_uri=" linkUrl; //linkUrl是后台管理getData方式的url "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; $("#url").val( url); $("#pic").qrcode({ render: "canvas", //table方法 width: 170, //总宽 height: 170, //相对高度 text: url //随意內容 }); var img = convertCanvasToImage(); $("canvas").remove(); $('#Code').append(img); $("#copyLink").click(function() { var copyText = $("#url"); copyText.select();//挑选 document.execCommand("Copy");//实行拷贝 alert("复制成功!"); })});

手机微信全自动启用oauth2/authorize插口,运作插口后获得一次性编码,会全自动跳转到redirect_uri?code = XXX & response _ type = code & scope = snsapi _ base & STATE = STATE & connect _redirect = 1 #手机微信_ redirect

3.自动跳转oauth插口。

页面能够根据一次性编码获得客户的openId,随后跳转到扫描仪的网页页面。(手机微信会回呼这一页面2次,第一次编码有值,第二次编码是空!)

@GET@Path(value = "oauth")public void oauth(@Context HttpServletResponse httpResponse, @QueryParam("code") String code) { String indexUrl = "https://XXXX.com/新项目名/ProduceDeatil.html"; //微信扫一扫自动跳转的网页页面 String wxUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=%s"; wxUrl = String.format(wxUrl, appId, secret, code, grantType); String response = HttpUtil.sendGet(wxUrl); if (response == null) { logger.error("手机微信access_token接受不成功"); return; } JSONObject jsonObject = JSONObject.parseObject(response); String openid = (String) jsonObject.get("openid"); try { httpResponse.sendRedirect(indexUrl "?openid=" openid); } catch (IOException e) { e.printStackTrace(); }}

评论(0条)

刀客源码 游客评论