织梦文章微信分享带缩略图与简介【站长亲测】
没整之前
整了之后
测试效果可以试试我博客文章
准备工作
- 微信服务号一个,并且已经通过了实名认证;
- 一个ICP备案的域名;
操作步骤
1、查看AppId,AppSecret以及绑定域名
2、文章内容页模板微信分享代码
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script type="text/javascript">
var url = encodeURIComponent(location.href.split("#")[0]);
$.ajax({
type: "get",
url: "https://www.dedehtml.com/wechat.php?link=" + url,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(e) {
var d = e.appid,
i = e.timestamp,
t = e.noncestr,
n = e.signature;
wx.config({
debug: 0,
appId: d,
timestamp: i,
nonceStr: t,
signature: n,
jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage"]
}),
wx.ready(function() {
wx.onMenuShareTimeline({
title: "{dede:field.title/}",
desc: "{dede:field.description/}",
link: "{dede:global.cfg_basehost/}{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}",
imgUrl: "{dede:global.cfg_basehost/}{dede:field.litpic/}"
}),
wx.onMenuShareAppMessage({
title: "{dede:field.title/}",
desc: "{dede:field.description/}",
link: "{dede:global.cfg_basehost/}{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}",
imgUrl: "{dede:global.cfg_basehost/}{dede:field.litpic/}"
})
})
}
});
</script>
注意:以上代码也可以加一个判断,需要单独设置微信分享链接标题、图片、简介的话可以用使用;首先在文章模型里面增加sharetitle、sharepic、sharems,对应的内容是标题、图片、描述;
判断代码:
wx.ready(function() {
wx.onMenuShareTimeline({
title: "{dede:field name='array' runphp='yes'} if (@me['sharetitle']=='') @me=@me['title'];else @me=@me['sharetitle'];{/dede:field}",
desc: "{dede:field name='array' runphp='yes'} if (@me['sharems']=='') @me=@me['description'];else @me=@me['sharems'];{/dede:field}",
link: "{dede:global.cfg_basehost/}{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}",
imgUrl: "{dede:global.cfg_basehost/}{dede:field name='array' runphp='yes'} if (@me['sharepic']=='') @me=@me['litpic'];else @me=@me['sharepic'];{/dede:field}"
}),
wx.onMenuShareAppMessage({
title: "{dede:field name='array' runphp='yes'} if (@me['sharetitle']=='') @me=@me['title'];else @me=@me['sharetitle'];{/dede:field}",
desc: "{dede:field name='array' runphp='yes'} if (@me['sharems']=='') @me=@me['description'];else @me=@me['sharems'];{/dede:field}",
link: "{dede:global.cfg_basehost/}{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}",
imgUrl: "{dede:global.cfg_basehost/}{dede:field name='array' runphp='yes'} if (@me['sharepic']=='') @me=@me['litpic'];else @me=@me['sharepic'];{/dede:field}"
})
以上判断的意思是,有微信分享单独的内容,就显示微信分享内容,若没有,就按照正常的分享内容进行展示;亲测没问题。
1) 和 2) 网站不是https的要把它改成http
3) 域名换成自己的域名
3、wechat.php文件下载,放在网站根目录
链接:https://pan.baidu.com/s/14ZJa2XBPNtnTaW4azP8C-g
提取码:i45o