Files
banban/banban-mini/src/utils/page-share.ts
T

41 lines
1.5 KiB
TypeScript

import { useRouter, useShareAppMessage } from '@tarojs/taro'
const DEFAULT_SHARE_TITLES: Record<string, string> = {
'/pages/device/index': '伴伴设备管理',
'/pages/login/index': '登录伴伴小程序',
'/pages/bind/index': '绑定伴伴设备',
'/pages/traffic-binding/index': '伴伴流量服务',
'/pages/notification/index': '查看伴伴通知',
'/pages/chat/index': '伴伴聊天记录',
'/pages/chat/detail/index': '查看伴伴聊天',
'/pages/location/index': '查看设备位置',
'/pages/sleep/index': '伴伴设备管理',
'/pages/sleep-schedule/index': '设置设备休眠时间',
'/pages/family-invite/index': '家庭共享邀请',
'/pages/webview/index': '伴伴服务通知',
}
function normalizePath(path: string): string {
if (!path) return '/pages/device/index'
return path.startsWith('/') ? path : `/${path}`
}
function buildPagePath(path: string, params: Record<string, string | undefined>): string {
const query = Object.entries(params)
.filter(([, value]) => value !== undefined && value !== '')
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
.join('&')
return query ? `${path}?${query}` : path
}
export function usePageShare(title?: string): void {
const router = useRouter()
const pagePath = normalizePath(router.path)
useShareAppMessage(() => ({
title: title || DEFAULT_SHARE_TITLES[pagePath] || '伴伴小程序',
path: buildPagePath(pagePath, router.params || {}),
}))
}