JS解决微信头像失效

JS解决微信头像失效  JS解决微信头像失效

var images = document.getElementsByTagName("img");
for (var i = 0; i < images.length; i++) {
    var img = images[i];
    if (img !== null && img.src !== null && img.src) {
        if (img.src.indexOf("wx.qlogo.cn") >= 0 || img.src.indexOf("thirdwx.qlogo.cn") >= 0) {
            avatarStatusChecker(img, function (img, status) {
                //失效头像
                if (!status) {
                    img.src = "/Assets/img/defaultAvatar.png";
                }
            });
        }
    }
}

function avatarStatusChecker(img, fn) {
    var xhr = new XMLHttpRequest();
    xhr.open(‘get‘, img.src, true);
    xhr.send();

    xhr.onreadystatechange = function (e) {
        if (this.readyState === 4 && this.status === 200) {
            var hsCode = (this.responseText.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a }, 0));
            if (typeof fn === "function") {
                fn(img, hsCode !== -1574534679);
            };
        }
    };

}

 

JS解决微信头像失效

上一篇:微信公众平台如何配置业务域名 微信开发


下一篇:微信退款接收返回的参数解析错误,接收Map中整形,数据String转Integer报错:java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer