微信分享ios 不显示图片和简介问题总结

问题

        我做了微信分享,各种配置都ok,安卓实现分享,但是ios分享不显示图片和简介 。(如下图)

 

解决过程

        经过一天的折腾,核对各种问题最终发现问题所在。是我的url中带有汉字使用escape()进行的加密苹果分享的时候会自动的对(汉字)解码decodeURIComponent()造成了url签名错误。

http://adminpc.z1mei.com/Admin/TLogin?ref=/htmls/TraceOne/TraceCameraLive.html?name=%E7%AE%80%E4%BB%8B&id=166&word=7B928D5BFFD7FE508CCCAFC73F2B52EB2

解决办法是,在打开要分享的页面的时候,使用encodeURIComponent()对汉字进行加密,就可以了。

补充说明

        我在获取urll参数的时候使用的了getUrlParam()这个js方法。这里方法网上有一堆,比较老,在获取参数的同时使用了unescope()对参数进行了解码。目前来说escape() http://www.w3school.com.cn/ 都不在推荐使用了。具体代替为encodeURIComponent();

getUrlParam()我的升级代码

function getUrlParam(name,fnDecode) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) {
        if (typeof fnDecode === "function") {
            return fnDecode(r[2]);
        }
        return unescape(r[2]);
    }
    return null; //返回参数值
}

url的三个js编码函数escape(),encodeURI(),encodeURIComponent()

escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值。比如"春节"的返回结果是%u6625%u8282,,escape()不对"+"编码 主要用于汉字编码,现在已经不提倡使用。

encodeURI()是Javascript中真正用来对URL编码的函数。 编码整个url地址,但对特殊含义的符号"; / ? : @ & = + $ , #",也不进行编码。对应的解码函数是:decodeURI()。

encodeURIComponent() 能编码"; / ? : @ & = + $ , #"这些特殊字符。对应的解码函数是decodeURIComponent()。

假如要传递带&符号的网址,所以用encodeURIComponent()

参考资料(加粗为重要的资料)

https://blog.csdn.net/angle_lzc/article/details/84864455

https://blog.csdn.net/weixin_34310127/article/details/87616016

https://www.cnblogs.com/huaxingtianxia/p/7125862.html

https://www.oschina.net/question/2600088_2189362?sort=time

http://www.w3school.com.cn/jsref/jsref_escape.asp

        

原文地址:https://www.cnblogs.com/Blogs-Wang/p/10976052.html