在PBOOT CMS上加微信分享前端JS

分类:脚本代码 时间:2025-02-28 10:16

想在在PBOOT上加微信分享前端JS需添加以下JAVASCRIPT代码

 如需要PBOOT后端处理过程看后续分享

<script src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

   <script type="text/javascript">

// 假设这是当前页面的URL(需要编码并去除#及其后面的部分)  

var url = window.location.href.split('#')[0];  

 

// 生成时间戳和随机串(这里只是示例,实际中应该由后端生成)  

var timestamp = Math.round(new Date().getTime() / 1000);  

var nonceStr = Math.random().toString(36).substr(2, 15); // 生成随机串  

 

// 将这些参数发送到后端  

$.ajax({  

   url: '/api.php/do/signe', // 你的PHP脚本URL  

   type: 'POST',  

   data: {  

       url: url,  

       timestamp: timestamp,  

       nonceStr: nonceStr,

       ticket:'{label:ticket}'//微信公众号获取的 ticket

   },  

   success: function(response) {  

       // 假设后端返回了签名和其他配置信息  

       var config = JSON.parse(response);  

         

       // 使用微信JS-SDK进行配置  

       wx.config({  

           debug: false,  

           appId: config.appId, // 从后端获取  

           timestamp: config.timestamp, // 从后端获取  

           nonceStr: config.nonceStr, // 从后端获取  

           signature: config.signature, // 从后端获取  

           jsApiList: ['onMenuShareTimeline',

  'onMenuShareAppMessage',

  'startRecord',

  'stopRecord',

  'onVoiceRecordEnd',

  'playVoice',

  'pauseVoice',

  'stopVoice',

  'onVoicePlayEnd',

  'uploadVoice',

  'downloadVoice',

  'chooseImage',

  'previewImage',

  'uploadImage',

  'downloadImage']  

       });  

         

   wx.ready(function(){

var shareData = {

title: "{content:title}",//微信分享标题

       desc: "{content:description}",//微信分享介绍

       link: url,//分享地址

       imgUrl: "{content:ico}",//分享LOGO

trigger: function (res) {},

complete: function (res) {},

success: function (res) {

},

cancel: function (res) {},

fail: function (res) {}

};


wx.onMenuShareTimeline(shareData);

wx.onMenuShareAppMessage(shareData);

wx.onMenuShareQQ(shareData);

wx.onMenuShareWeibo(shareData);

wx.onMenuShareQZone(shareData);

});


       wx.error(function(res) {  

           // 配置失败的处理  

           console.log(res.errMsg);  

       });  

   },  

   error: function(xhr, status, error) {  

       // 请求失败的处理  

       console.error("请求后端签名失败: ", error);  

   }  

});





</script>

推荐内容
热点内容
123开发资源网