vue spa 微信jssdk二次分享不显示自定义分享内容问题解决

本问题仅限于vue单页应用开发时的讨论。

1、不带参数的网页,如:http://xxx.com/#/CS350001/pray/buddha,分享后打开的网址,

会变成这样:http://xxx.com/?from=singlemessage&isappinstalled=0#/CS350001/pray/buddha,多了一串字符

?from=singlemessage&isappinstalled=0,这不影响,再打开这个链接并分享,还是正常的。

2、带参数的网页,如: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 +
            "&timestamp=" + 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)

如此一来,无论分享多少次,都会正常显示自定义的分享内容。

 

转载于:https://my.oschina.net/junko2013/blog/3019534

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值