63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
![]() |
import wx from 'weixin-js-sdk'
|
|||
|
import config from "@/config.js"
|
|||
|
|
|||
|
export function setupWechatShare({
|
|||
|
title,
|
|||
|
desc,
|
|||
|
link,
|
|||
|
imgUrl
|
|||
|
}) {
|
|||
|
// 通过后端接口获取签名(必须)
|
|||
|
fetch(`${config.baseUrl}/wechat-signature?url=${encodeURIComponent(location.href.split('#')[0])}`)
|
|||
|
.then(res => res.json())
|
|||
|
.then(({
|
|||
|
appId,
|
|||
|
timestamp,
|
|||
|
nonceStr,
|
|||
|
signature
|
|||
|
}) => {
|
|||
|
wx.config({
|
|||
|
debug: false,
|
|||
|
appId,
|
|||
|
timestamp,
|
|||
|
nonceStr,
|
|||
|
signature,
|
|||
|
jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData']
|
|||
|
})
|
|||
|
|
|||
|
wx.ready(() => {
|
|||
|
// 分享给好友
|
|||
|
wx.updateAppMessageShareData({
|
|||
|
title,
|
|||
|
desc,
|
|||
|
link,
|
|||
|
imgUrl,
|
|||
|
success: () => {
|
|||
|
console.log('分享配置成功')
|
|||
|
}
|
|||
|
})
|
|||
|
|
|||
|
// 分享到朋友圈
|
|||
|
wx.updateTimelineShareData({
|
|||
|
title,
|
|||
|
link,
|
|||
|
imgUrl,
|
|||
|
success: () => {
|
|||
|
console.log('朋友圈分享配置成功')
|
|||
|
}
|
|||
|
})
|
|||
|
})
|
|||
|
})
|
|||
|
}
|
|||
|
|
|||
|
// 使用
|
|||
|
// import { setupWechatShare } from '@/utils/wechatShare.js'
|
|||
|
|
|||
|
// onMounted(() => {
|
|||
|
// setupWechatShare({
|
|||
|
// title: '职位推荐:高级前端工程师',
|
|||
|
// desc: '某知名互联网公司,年薪40W,点击查看详情',
|
|||
|
// link: location.href,
|
|||
|
// imgUrl: 'https://yourcdn.com/job-thumbnail.png'
|
|||
|
// })
|
|||
|
// })
|