flat: 添加微信分享卡片
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import wx from 'weixin-js-sdk'
|
||||
import config from "@/config.js"
|
||||
import {
|
||||
$api
|
||||
} from '../common/globalFunction';
|
||||
|
||||
export function setupWechatShare({
|
||||
title,
|
||||
@@ -8,56 +10,72 @@ export function setupWechatShare({
|
||||
imgUrl
|
||||
}) {
|
||||
// 通过后端接口获取签名(必须)
|
||||
fetch(`${config.baseUrl}/wechat-signature?url=${encodeURIComponent(location.href.split('#')[0])}`)
|
||||
.then(res => res.json())
|
||||
.then(({
|
||||
$api.createRequest('/app/job/getWechatUrl', {
|
||||
imgUrl: location.href.split('#')[0]
|
||||
}, 'POST').then((resData) => {
|
||||
const {
|
||||
appId,
|
||||
timestamp,
|
||||
nonceStr,
|
||||
signature
|
||||
}) => {
|
||||
wx.config({
|
||||
debug: false,
|
||||
appId,
|
||||
timestamp,
|
||||
nonceStr,
|
||||
signature,
|
||||
jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData']
|
||||
})
|
||||
} = resData.data
|
||||
|
||||
wx.ready(() => {
|
||||
// 分享给好友
|
||||
wx.updateAppMessageShareData({
|
||||
title,
|
||||
desc,
|
||||
link,
|
||||
imgUrl,
|
||||
success: () => {
|
||||
console.log('分享配置成功')
|
||||
}
|
||||
})
|
||||
|
||||
// 分享到朋友圈
|
||||
wx.updateTimelineShareData({
|
||||
title,
|
||||
link,
|
||||
imgUrl,
|
||||
success: () => {
|
||||
console.log('朋友圈分享配置成功')
|
||||
}
|
||||
})
|
||||
})
|
||||
wx.config({
|
||||
debug: false,
|
||||
appId,
|
||||
timestamp,
|
||||
nonceStr,
|
||||
signature,
|
||||
jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData']
|
||||
})
|
||||
wx.ready(() => {
|
||||
// 分享给好友
|
||||
wx.updateAppMessageShareData({
|
||||
title,
|
||||
desc,
|
||||
link,
|
||||
imgUrl,
|
||||
success: () => {
|
||||
$api.msg('分享配置成功')
|
||||
}
|
||||
})
|
||||
// 分享到朋友圈
|
||||
wx.updateTimelineShareData({
|
||||
title,
|
||||
link,
|
||||
imgUrl,
|
||||
success: () => {
|
||||
$api.msg('朋友圈分享配置成功')
|
||||
}
|
||||
})
|
||||
}).catch((err) => {
|
||||
$api.msg('获取微信签名失败')
|
||||
console.error('获取微信签名失败:', err);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
// 使用
|
||||
// import { setupWechatShare } from '@/utils/wechatShare.js'
|
||||
export function updateWechatShare({
|
||||
title,
|
||||
desc,
|
||||
link,
|
||||
imgUrl
|
||||
}) {
|
||||
if (!window.wx) return;
|
||||
wx.updateAppMessageShareData({
|
||||
title,
|
||||
desc,
|
||||
link,
|
||||
imgUrl,
|
||||
success: () => console.log('分享配置成功'),
|
||||
fail: (err) => console.warn('分享配置失败', err)
|
||||
});
|
||||
}
|
||||
|
||||
// onMounted(() => {
|
||||
// setupWechatShare({
|
||||
// title: '职位推荐:高级前端工程师',
|
||||
// desc: '某知名互联网公司,年薪40W,点击查看详情',
|
||||
// link: location.href,
|
||||
// imgUrl: 'https://yourcdn.com/job-thumbnail.png'
|
||||
// })
|
||||
// })
|
||||
|
||||
// tools
|
||||
export function generateShareLink(jobId) {
|
||||
const base = location.origin + '/app/static/share.html';
|
||||
const query = jobId ? `?jobId=${jobId}&_t=${Date.now()}` : `?_t=${Date.now()}`;
|
||||
return `${base}${query}`;
|
||||
}
|
Reference in New Issue
Block a user