Files
ks-app-employment-service/utils/wechatShare.js
2025-07-09 15:15:37 +08:00

63 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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'
// })
// })