
文章插圖
上一章介紹了通過分享好友實現微信跳轉,這一章將介紹通過掃碼實現微信跳轉 。
前提:從微信公眾號那邊獲取appid,secret,grantType三個參數備用 。
1、獲取微信跳轉鏈接接口該接口主要是獲取能重定向到掃碼后頁面的接口鏈接 。
@GET@Path(value = "https://www.520longzhigu.com/diannao/getData")@Produces(MediaType.APPLICATION_JSON)public Response getData() {Map<String, String> result = new HashMap<>();try {//......業(yè)務代碼......String recUrl = "https://XXXX.com/項目名/oauth";//實現重定向的連接,該接口實現看第3節(jié)講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、二維碼頁面該頁面可以通過掃碼進行跳轉,或者復制鏈接在微信中打開實現跳轉 。
<input style="width: 1px;height: 1px;" id="url" value="" type="text" /><div id="root"><div id="pic"><div id="Code"></div></div><div id="txt">掃碼跳轉或者識別圖片跳轉</div><div id="copyLink">復制鏈接(微信中點擊鏈接可直接跳轉)</div></div>function convertCanvasToImage() {var image = new Image();var canvas = document.getElementsByTagName('canvas')[0];image.src = https://www.520longzhigu.com/diannao/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");//執(zhí)行復制alert("復制成功!");})});微信自動調用oauth2/authorize接口,運行完接口后得到一次性的code,會自動重定向到redirect_uri?code=XXX&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect
3、跳轉的oauth接口該接口可通過一次性的code獲取用戶的openId,然后重定向到掃碼后的頁面 。(微信會兩次回調這個接口,第一次的code是有值的,第二次code為空!)
@GET@Path(value = "https://www.520longzhigu.com/diannao/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();}}
以上關于本文的內容,僅作參考!溫馨提示:如遇健康、疾病相關的問題,請您及時就醫(yī)或請專業(yè)人士給予相關指導!
「愛刨根生活網」www.malaban59.cn小編還為您精選了以下內容,希望對您有所幫助:- 舍我其誰全集免費觀看,舍我其誰在線觀看平臺鏈接分享
- 1-16集 《云南蟲谷》無刪減電視劇免費在線播放 高清
- 百度翻譯在線翻譯中文翻譯英文-百度翻譯在線翻譯拍照翻譯
- 百度翻譯app中在線拍照翻譯的具體操作步驟 百度翻譯在線翻譯
- 云南蟲谷第8集高清在線免費完整版 第八集劇情介紹都說了什么內容?
- 云南蟲谷第9集高清在線免費完整版 第九集劇情介紹都說了什么內容?
- 云南蟲谷第10集高清在線免費完整版 第十集劇情介紹都說了什么內容?
- 毛不易《愛若無聲》歌詞是什么 《愛若無聲》完整版歌詞在線試聽
- 直播:北京環(huán)球影城初體驗 北京環(huán)球影城在線玩云參觀
- 全1-24集全免費版 國語熟肉已完結
