需求背景:
? ? ? ? 由h5提供頁面,通過后臺請求微信api生成對應的schemal短鏈,該h5頁面嵌入到原生的ios以及安卓app上,當用戶點擊后通過短連接跳轉到其他小程序中
以下為生成微信scheme代碼示例,生成后短鏈為:weixin://dl/business/?t=l77aMn7aZF 這種
/*** 生成短連接* @param data* @return*/public static String getUrlScheme(JSONObject data) {try {log.info("==>獲取小程序跳轉地址,mchnt:{}", data);String appId = data.getString("appId");String secret = data.getString("secret");String accessToken = getAccessToken(appId, secret);return generateWechatScheme(accessToken, data);} catch (Exception e) {log.error("==>獲取小程序跳轉地址失敗", e);throw new FuiouException("5022","獲取小程序跳轉地址失敗");}}/*** 向微信服務端請求生成小程序 scheme*/public static String generateWechatScheme(String accessToken, JSONObject data) {CloseableHttpClient client = HttpClientUtil.createClient();String url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" + accessToken;HttpPost request = new HttpPost(url);request.setHeader("Content-Type", "application/json; charset=utf-8");String path = data.get("path").toString();String queryStr = data.get("queryStr").toString();String env_version = data.get("env_version").toString();// 構建請求體 JSONString requestBody = "{\"jump_wxa\":{\"path\":\"" + path + "\",\"query\":\"" + queryStr+ "\",\"env_version\":\"" + env_version+ "\"},\"is_expire\":false,\"expire_type\":1,\"expire_interval\":" + 1 + "}";request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));log.info("==>調用微信generatescheme接口,request body:" + requestBody);String response = HttpClientUtil.executeAndClose(client, request);log.info("==>調用微信generatescheme接口,response:" + response);JSONObject jsResp = JSONObject.parseObject(response);String openlink = jsResp.getString("openlink");log.info("==>獲取到的微信小程序scheme:" + openlink);return openlink;}/*** 獲取 access_token*/private static String getAccessToken(String appId, String secret) {String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;CloseableHttpClient client = HttpClientUtil.createClient();HttpGet request = new HttpGet(url);String response = HttpClientUtil.executeAndClose(client, request);log.info("==>獲取微信accessToken,response:" + response);JSONObject jsResp = JSONObject.parseObject(response);String accessToken = jsResp.getString("access_token");return accessToken;}
所遇到問題:
? ? ? ? 短鏈如:weixin://dl/business/?t=l77aMn7aZF
? ? ? ? 通過短鏈在前端利用js? window.location.href =?weixin://dl/business/?t=l77aMn7aZF 進行跳轉,在安卓上面能正常跳轉,但是ios無法跳轉,且沒有反應
解決問題思路
? ? ? ? 1、檢查schema 是否正確,在ios真機測試也是可以跳;排除
? ? ? ? 2、將window.location.href 替換為?window.location? 以及換為 windos.open(xx) 等多種方式均不可行;
? ? ? ?3、懷疑是h5頁面的meta 寫的有問題,隨即替換為meta 信息,進行測試,還是不行;
? ? ? ?4、本來都準備放棄,讓ios提供一個原生方法,自己這邊判斷如果是ios就調用原生方法進行跳轉,附一段js ios 判斷代碼
function isIos(){var u = window.navigator.userAgent;var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);return isiOS; }
? ? ? ?5、這時候經同事提醒,將鏈接放到其他頁面,看是否可跳轉,然后將鏈接放到首頁測試是否可以跳轉,這時候神奇的事情發生了,竟然ios 能正常跳了,同樣是 window.location.href?
? ? ?6、拿到h5頁面開始分析首頁和2級頁面的區別,發現首頁沒有無層級關系,而二級目錄有一個ifream頁面,以下為二級頁面
? ??
最終結局方案
? ? ? ? 經過以上分析后得知,window.location.href 不能跳出父框架,因為需要喚起小程序,所以需要跳出ifream ,但是安卓沒問題,可能是語法的限制,最終知道解決方案
window.top.location.href = xx 微信的短鏈 及可正常跳轉