收藏 '分享 到百度、微信'功能代码

<div class="bdsharebuttonbox">
	<a href="#" class="bds_more" data-cmd="more"></a>
	<a href="#" class="bds_qzone" data-cmd="qzone"></a>
	<a href="#" class="bds_tsina" data-cmd="tsina"></a>
	<a href="#" class="bds_tqq" data-cmd="tqq"></a>
	<a href="#" class="bds_renren" data-cmd="renren"></a>
	<a href="#" class="bds_weixin" data-cmd="weixin"></a>
</div>
<script>
window._bd_share_config={
	"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},
	"share":{},
	"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},
	"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}
};
with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
</script>

公司项目里要做一个分享功能,以上是同事找到的通用代码,特收藏下。

备注:获取百度分享代码地址  http://share.baidu.com/code

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端实现Web分享微信的核心是调用微信JS-SDK提供的接口,获取分享参数并生成分享链接。以下是一个使用Vue.js框架实现Web分享微信的示例代码: ```html <template> <div> <button @click="shareToWechat">分享微信</button> </div> </template> <script> import wx from 'weixin-js-sdk' export default { methods: { async shareToWechat() { const url = window.location.href.split('#')[0] // 分享的网址 const title = '分享标题' // 分享的标题 const description = '分享描述' // 分享的描述 const imageUrl = 'https://example.com/share-image.jpg' // 分享的图片链接 const response = await fetch('/api/wechat-signature?url=' + encodeURIComponent(url)) const { appId, timestamp, nonceStr, signature } = await response.json() wx.config({ debug: false, appId, timestamp, nonceStr, signature, jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'] }) wx.ready(() => { wx.updateAppMessageShareData({ title, desc: description, link: this.generateShareLink(url, title, description, imageUrl), imgUrl: imageUrl, success: () => { console.log('分享微信好友成功') }, cancel: () => { console.log('取消分享微信好友') } }) wx.updateTimelineShareData({ title, link: this.generateShareLink(url, title, description, imageUrl), imgUrl: imageUrl, success: () => { console.log('分享微信朋友圈成功') }, cancel: () => { console.log('取消分享微信朋友圈') } }) }) }, generateShareLink(url, title, description, imageUrl) { const encodedUrl = encodeURIComponent(url) const encodedTitle = encodeURIComponent(title) const encodedDescription = encodeURIComponent(description) const encodedImageUrl = encodeURIComponent(imageUrl) return `http://example.com/wechat-share.html?url=${encodedUrl}&title=${encodedTitle}&desc=${encodedDescription}&imgUrl=${encodedImageUrl}` } } } </script> ``` 这段代码中,我们使用了Vue.js框架和weixin-js-sdk库。在点击“分享微信”按钮时,我们首先向后端API请求获取微信签名,然后在前端JS代码中调用wx.config函数配置微信JS-SDK的参数。在配置完成后,我们使用wx.updateAppMessageShareData和wx.updateTimelineShareData函数来设置分享微信好友和微信朋友圈的参数,并在分享成功或取消分享时触发回调函数。最后,我们使用generateShareLink函数来生成分享链接,该链接包含了分享的网址、标题、描述和图片链接等参数,供微信客户端识别并显示分享内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值