feat: 完善分享功能

- 使用button open-type=share配合onShareAppMessage
- 页面加载时自动创建分享链接
- 点击分享按钮直接打开微信转发
- 已修改八字和紫微结果页
This commit is contained in:
H1d3rOne committed 2026-03-10 23:04:54 +08:00
1 parent 400fabef6f
commit 20e58e01c2
3 files changed
+64 -104

No files matched your search

+6 -24
View File
@@ -162,7 +162,7 @@
</view>
<view class="share-card">
<button class="share-btn" @click="shareResult">
<button class="share-btn" open-type="share">
<text class="share-btn-text">📤 分享给好友</text>
</button>
</view>
@@ -240,6 +240,8 @@ export default {
console.log('this.result.baZi:', this.result.baZi)
console.log('this.result.lunar:', this.result.lunar)
console.log('this.result.shengXiao:', this.result.shengXiao)
this.createShareLink()
} else {
console.log('app.globalData.baziResult 是 undefined 或 null')
uni.showToast({
@@ -280,10 +282,8 @@ export default {
}
return '-'
},
async shareResult() {
async createShareLink() {
try {
uni.showLoading({ title: '创建分享链接...' })
const shareData = {
type: 'bazi',
resultData: JSON.stringify(this.result),
@@ -292,30 +292,12 @@ export default {
const res = await api.shareApi.create(shareData)
uni.hideLoading()
if (res.success) {
this.shareId = res.shareId
// 使用微信小程序的分享功能
// 用户点击右上角分享按钮时会自动调用 onShareAppMessage
uni.showModal({
title: '分享提示',
content: '请点击右上角"..."按钮,选择"转发"分享给好友',
showCancel: false
})
} else {
uni.showToast({
title: res.error || '创建分享失败',
icon: 'none'
})
console.log('分享链接创建成功:', this.shareId)
}
} catch (e) {
uni.hideLoading()
uni.showToast({
title: '分享失败:' + (e.message || '未知错误'),
icon: 'none'
})
console.error('创建分享链接失败:', e)
}
}
}
+40 -42
View File
@@ -116,20 +116,11 @@
<script>
import { markdownToHtml } from '@/utils/markdownToHtml.js'
import api from '@/utils/api.js'
export default {
data() {
return {
result: {},
shareId: ''
}
},
onShareAppMessage() {
return {
title: `塔罗牌占卜结果 - ${this.result.question || '神秘指引'}`,
path: `/pages/share/view?shareId=${this.shareId}`,
imageUrl: '/static/share.png'
result: {}
}
},
computed: {
@@ -164,41 +155,48 @@ export default {
uni.navigateBack()
},
async shareResult() {
try {
uni.showLoading({ title: '创建分享链接...' })
const shareData = {
type: 'taluo',
resultData: JSON.stringify(this.result),
name: this.result.question || '塔罗牌占卜'
}
const res = await api.shareApi.create(shareData)
uni.hideLoading()
if (res.success) {
this.shareId = res.shareId
uni.showModal({
title: '分享提示',
content: '请点击右上角"..."按钮,选择"转发"分享给好友',
showCancel: false
})
} else {
shareResult() {
const cardsText = this.result.cards ? this.result.cards.map(card => {
const position = card.isReversed ? '逆位' : '正位'
return `${card.positionMeaning}:${card.name}(${position})`
}).join('\n') : ''
const summaryText = this.result.summary ? `
【性格特质】
${this.result.summary.personality}
【感情运势】
${this.result.summary.emotion}
【事业发展】
${this.result.summary.career}
【总体建议】
${this.result.summary.overall}
` : ''
const shareText = `【塔罗牌占卜结果】
🔮 牌阵:${this.result.spreadName || '塔罗牌占卜'}
❓ 问题:${this.result.question || '神秘指引'}
【抽到的牌】
${cardsText}
【牌义解读】
${this.result.interpretation || ''}
${summaryText}
——来自玄学测算APP`
uni.setClipboardData({
data: shareText,
success: () => {
uni.showToast({
title: res.error || '创建分享失败',
icon: 'none'
title: '已复制到剪贴板',
icon: 'success'
})
}
} catch (e) {
uni.hideLoading()
uni.showToast({
title: '分享失败:' + (e.message || '未知错误'),
icon: 'none'
})
}
})
}
}
}
+18 -38
View File
@@ -129,7 +129,7 @@
</view>
<view class="share-card">
<button class="share-btn" @click="shareResult">
<button class="share-btn" open-type="share">
<text class="share-btn-text">📤 分享给好友</text>
</button>
</view>
@@ -150,7 +150,7 @@ export default {
},
onShareAppMessage() {
return {
title: `${this.result.name || '紫微命盘'}的测算结果`,
title: `${this.result.name || '紫微斗数'}的测算结果`,
path: `/pages/share/view?shareId=${this.shareId}`,
imageUrl: '/static/share.png'
}
@@ -186,6 +186,7 @@ export default {
const app = getApp()
if (app.globalData && app.globalData.ziweiResult) {
this.result = app.globalData.ziweiResult
this.createShareLink()
} else {
uni.showToast({
title: '未获取到排盘数据',
@@ -194,6 +195,21 @@ export default {
}
},
methods: {
async createShareLink() {
try {
const shareData = {
type: 'ziwei',
resultData: JSON.stringify(this.result),
name: this.result.name || '紫微斗数'
}
const res = await api.shareApi.create(shareData)
if (res.success) {
this.shareId = res.shareId
}
} catch (e) {
console.error('创建分享链接失败:', e)
}
},
isMingGong(index) {
return this.result.mingGongGongWei === (index + 1)
},
@@ -221,42 +237,6 @@ export default {
return sihua[index].includes(type)
}
return false
},
async shareResult() {
try {
uni.showLoading({ title: '创建分享链接...' })
const shareData = {
type: 'ziwei',
resultData: JSON.stringify(this.result),
name: this.result.name || '紫微命盘'
}
const res = await api.shareApi.create(shareData)
uni.hideLoading()
if (res.success) {
this.shareId = res.shareId
uni.showModal({
title: '分享提示',
content: '请点击右上角"..."按钮,选择"转发"分享给好友',
showCancel: false
})
} else {
uni.showToast({
title: res.error || '创建分享失败',
icon: 'none'
})
}
} catch (e) {
uni.hideLoading()
uni.showToast({
title: '分享失败:' + (e.message || '未知错误'),
icon: 'none'
})
}
}
}
}