本問題僅限於vue單頁應用開發時的討論。vue
一、不帶參數的網頁,如:http://xxx.com/#/CS350001/pray/buddha,分享後打開的網址,ios
會變成這樣:http://xxx.com/?from=singlemessage&isappinstalled=0#/CS350001/pray/buddha,多了一串字符axios
?from=singlemessage&isappinstalled=0,這不影響,再打開這個連接並分享,仍是正常的。api
二、帶參數的網頁,如:http://xxx.com/#/pray/videoDetail?orderId=64,分享後沒法顯示自定義的分享內容,這是由於url在傳遞給後臺時帶有?等特殊字符,直接傳會致使驗籤失敗,因此須要url encode下,總結以下代碼:微信
let url = window.location.href; this.$axios({ method:"get", url:'/wx/jssdk/config?url='+(url.indexOf('/?') == '-1'?url:encodeURIComponent(url)), }).then(function (response) {})
其中/wx/jssdk/config是我後臺接口地址,代碼以下:微信開發
@GetMapping("config") public @ResponseBody JsonResult config() throws Exception { ApiConfigKit.putApiConfig(getApiConfig()); JsTicket jsApiTicket = JsTicketApi.getTicket(JsTicketApi.JsApiType.jsapi); String jsapi_ticket = jsApiTicket.getTicket(); String nonce_str = createNonceStr(); String url = getPara("url"); String timestamp = createTimestamp(); // 這裏參數的順序要按照 key 值 ASCII 碼升序排序 //注意這裏參數名必須所有小寫,且必須有序 String str = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url; String signature = HashKit.sha1(str); Map<String,Object> map = new HashMap(); map.put("appId", ApiConfigKit.getApiConfig().getAppId()); map.put("nonceStr", nonce_str); map.put("timestamp", timestamp); map.put("url", url); map.put("signature", signature); map.put("jsapi_ticket", jsapi_ticket); return JsonResult.success(map); }
(ps:微信開發引入了jfinal-weixin)app
如此一來,不管分享多少次,都會正常顯示自定義的分享內容。ide