feat: 新增小程序分享功能 — 全局默认+页面级定制

This commit is contained in:
2026-06-08 10:09:53 +08:00
parent 1abc55323d
commit 8cf240f76b
3 changed files with 72 additions and 2 deletions

View File

@@ -75,6 +75,11 @@
<view class="invite-action" @tap="copyInviteCode(group.invite_code)">
<text class="invite-action-text">复制</text>
</view>
<!-- #ifdef MP-WEIXIN -->
<button class="invite-action share-btn" open-type="share" @tap="shareInviteCode = group.invite_code">
<text class="invite-action-text">分享</text>
</button>
<!-- #endif -->
<view v-if="group.role === 'owner'" class="invite-action" @tap="handleRefreshCode(group)">
<Icon name="refresh" :size="24" color="#8B7E7E" />
<text class="invite-action-text">刷新</text>
@@ -135,7 +140,7 @@
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { onShow, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { useGroupStore } from '@/stores/group'
import { waitForReady } from '@/utils/app-ready'
import { statusBarHeight, capsuleRight } from '@/utils/system'
@@ -150,6 +155,7 @@ const newGroupName = ref('')
const inviteCode = ref('')
const saving = ref(false)
const initialLoaded = ref(false)
const shareInviteCode = ref('') // 当前要分享的邀请码
// 成员管理弹窗
const showMemberModal = ref(false)
@@ -168,6 +174,24 @@ onShow(() => {
if (initialLoaded.value) groupStore.fetchGroups()
})
// #ifdef MP-WEIXIN
onShareAppMessage(() => {
// 空值保护:通过右上角菜单触发时 invite_code 可能为空
const code = shareInviteCode.value
const path = code
? `/pages/group-manage/index?invite_code=${code}`
: '/pages/group-manage/index'
return {
title: '一起来记账吧!',
path
}
})
onShareTimeline(() => ({
title: '小菜记账 — 一起来记账吧!'
}))
// #endif
function goBack() {
uni.navigateBack()
}
@@ -555,6 +579,20 @@ function handleDissolve(group: Group) {
color: $text-sec;
}
/* 分享按钮重置默认样式 */
.share-btn {
margin: 0;
padding: 4rpx 12rpx;
line-height: normal;
background: $bg;
border: none;
border-radius: $radius-sm;
font-size: inherit;
color: inherit;
&::after { border: none; }
}
/* 操作按钮 */
.group-actions {
display: flex;

View File

@@ -120,7 +120,7 @@
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { onShow, onPullDownRefresh } from '@dcloudio/uni-app'
import { onShow, onPullDownRefresh, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { useStatsStore } from '@/stores/stats'
import { useGroupStore } from '@/stores/group'
import { waitForReady } from '@/utils/app-ready'
@@ -312,6 +312,20 @@ onPullDownRefresh(() => {
loadData().finally(() => uni.stopPullDownRefresh())
})
// #ifdef MP-WEIXIN
onShareAppMessage(() => {
const expense = formatAmount(overview.value.expense)
return {
title: `我的${formatMonth(currentMonth.value)}账单:支出 ${expense}`,
path: '/pages/stats/index'
}
})
onShareTimeline(() => ({
title: `小菜记账 — 我的${formatMonth(currentMonth.value)}账单`
}))
// #endif
function prevMonth() {
const [y, m] = currentMonth.value.split('-').map(Number)
currentMonth.value = m === 1 ? `${y - 1}-12` : `${y}-${String(m - 1).padStart(2, '0')}`