织梦Dedecms内容页分享到微信显示缩略图与简介的方法

时间:2021-11-24
类别:教程 - CMS教程

织梦程序的内容文章页,分享到微信好友、朋友圈时需默认只显示链接图标,使用此插件,可以实现分享到朋友圈、好友时显示文章页缩略图片,也可以通过修改代码来显示固定的品牌图标。

织梦的微信分享显示缩略图的插件,可以实现自定义微信分享的效果(自定义缩略图,标题,摘要)样式良好,给用户的体验很好,对品牌展示也起到一定的效果。

没使用之前,如下图:

织梦Dedecms内容页分享到微信显示缩略图与简介的方法

使用插件之后,如下图:

织梦Dedecms内容页分享到微信显示缩略图与简介的方法

一、在使用此插件之前,需要准备如下工作:

1、PHP 5.3+ 并且 curl扩展已经开启

2、微信服务号一个,并且已经通过了实名认证;

3、一个ICP备案的域名。

二、操作步骤:

1、微信公众号有分享接口权限,如下图:

微信公众号有分享接口权限

2、查看AppId,AppSecret、IP白名单(填你服务器外网IP)、绑定你的域名。

查看AppId,AppSecret、IP白名单

3、文章内容页模板微信分享代码,以下代码添加到你的织梦内容文章页模板里面即可。

  1. <!--如果你的页面上已经有jquery库,请删除下面那一行代码--> 
  2. <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> 
  3. <script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script> 
  4. <script type="text/javascript"> 
  5. var url = encodeURIComponent(location.href.split("#")[0]); 
  6. $.ajax({ 
  7.     type: "get", 
  8.     url: "{dede:global.cfg_basehost/}/wechat.php?link=" + url, 
  9.     dataType: "json", 
  10.     contentType: "application/json; charset=utf-8", 
  11.     success: function(e) { 
  12.         var d = e.appid, 
  13.             i = e.timestamp, 
  14.             t = e.noncestr, 
  15.             n = e.signature; 
  16.         wx.config({ 
  17.             debug: 0,//如果分享失败,把0改成1开启错误提示看看 
  18.             appId: d, 
  19.             timestamp: i, 
  20.             nonceStr: t, 
  21.             signature: n, 
  22.             jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage"] 
  23.         }),  
  24.         wx.ready(function() { 
  25.             wx.onMenuShareTimeline({ 
  26.                 title: "{dede:field.title function=html2text(@me)/}", 
  27.                 desc: "{dede:field.description function=html2text(@me)/}", 
  28.                 link: "{dede:global.cfg_basehost/}{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}", 
  29.                 imgUrl: "{dede:global.cfg_basehost/}{dede:field.litpic/}" 
  30.             }),  
  31.             wx.onMenuShareAppMessage({ 
  32.                 title: "{dede:field.title function=html2text(@me)/}", 
  33.                 desc: "{dede:field.description function=html2text(@me)/}", 
  34.                 link: "{dede:global.cfg_basehost/}{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}", 
  35.                 imgUrl: "{dede:global.cfg_basehost/}{dede:field.litpic/}" 
  36.             }) 
  37.         }) 
  38.     } 
  39. }); 
  40. </script> 

4、织梦的分享文件下载,放在网站根目录,即可完成操作。

GBK编码与UTF8编码下载

三、重要提醒与注意点:

1、文章页的js代码里,有几处的代码,其中 {dede:global.cfg_basehost/},是调用后台-系统-基本参数-站点根网址,在后台请填写正确的域名。

2、如果你是手机站内容页,这样的 https://www.uu2018.com/m/view.php?aid=123

那你应该改成

link: "{dede:global.cfg_basehost/}/m/view.php?aid={dede:field.id/}",

3、如果你是手机站内容页,这样的 http://m.uu2018.com/view.php?aid=123

那你应该改成

link: "http://m.uu2018.com/view.php?aid={dede:field.id/}",

4、如果你是手机站内容页,这样的 https://www.uu2018.com/m/news/123.html

那你应该改成

link: "{dede:global.cfg_basehost/}/m/{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}",

4、如果你是手机站内容页,这样的 http://m.uu2018.com/news/123.html

那你应该改成

link: "http://m.uu2018.com{dede:field.id runphp='yes'}$result=GetOneArchive(@me);@me=$result['arcurl'];{/dede:field.id}",

    收藏