关于微信自定义分享的链接、标题、描述、图片都未生效问题详解

之前写了一篇关于微信js-sdk分享详解及demo实例(图文并茂很详细)的文章
很多人有这样的问题,为什么debug都返回ok但微信自定义分享的链接、标题、描述、图片却都未生效

先附上一段标准且通俗易懂的ajax(获取微信JSAPI的配置参数)

var urls = window.location.href;
$.ajax({
    type: "GET",
    url: Global.path+ Global.contextPath +'/getJsApiConfig?url='+urls,
	dataType: "json",
	contentType: "text/html;charset=UTF-8",
    beforeSend : function(){
         loading.show();
    },
    success: function(rs){
        if(rs.code == 0){
        	
			wx.config({
			    debug: false,
			    appId: rs.data.appId,
			    timestamp: rs.data.timestamp,
			    nonceStr: rs.data.nonceStr,
			    signature: rs.data.signature,
			    jsApiList: [
			        'checkJsApi',
			        'onMenuShareTimeline',
			        'onMenuShareAppMessage'
			    ]
			});
			var shareTitle = "使用充电服务,还有广告奖励金等你拿!";
			var shareDesc = "帮你店铺服务升级";
			var shareLink =location.origin + '/' +Global.path + "/page/index"+ pageExt;
			var shareImgUrl =location.origin + '/' +Global.path + "/static/images/logo.jpg?v=29dd16b52d";
			wx.ready(function () {
			   //分享给朋友
			    wx.onMenuShareAppMessage({
			        title: shareTitle,
			        desc: shareDesc,
			        link: shareLink,
			        imgUrl: shareImgUrl,
			        success: function (res) {
			            showTip('朋友分享成功');
			        },
			        fail: function (res) {
			            alert(JSON.stringify(res));
			        }
			    });
			    //分享到朋友圈
			    wx.onMenuShareTimeline({
			        title: shareTitle,
			        desc: shareDesc,
			        link: shareLink,
			        imgUrl: shareImgUrl,
			        success: function (res) {
			            showTip('朋友圈分享成功');
			        },
			        fail: function (res) {
			            alert(JSON.stringify(res));
			        }
			    });
			})				
					
        }
    },
    complete:function(){
    	loading.hide();
    },
	error: function(rs) {
		showTip(rs.msg);
	}        
});	

分享的链接、标题、描述、图片都未生效的主要原因是link和imgUrl都需要补全前半部分(location.origin)。

如上代码的:

var shareLink =**location.origin** + '/' +Global.path + "/page/index"+ pageExt;
var shareImgUrl =**location.origin** + '/' +Global.path + "/static/images/logo.jpg?v=29dd16b52d";
posted @ 2022-07-20 18:17  猫老板的豆  阅读(1157)  评论(0编辑  收藏  举报