Files
czzj-app/src/pages/mine/use-mine.ts
2026-10-13 16:23:43 +08:00

133 lines
3.9 KiB
TypeScript

import { onShow } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import { getErrorMessage } from '@/utils/api-error'
import { staticAssets } from '@/utils/static-assets'
import { fetchMineOverview } from './mine.api'
import type { MineMenuItem, MineQuickAction } from './mine.types'
const APP_VERSION = 'v3.2.0'
const QUICK_ACTIONS: MineQuickAction[] = [
{ id: 'notify', label: '消息通知', icon: staticAssets.iconMineNotify },
{ id: 'order', label: '我的订单', icon: staticAssets.iconMineOrder },
{ id: 'coupon', label: '优惠券', icon: staticAssets.iconMineCoupon },
{ id: 'address', label: '收货地址', icon: staticAssets.iconMineAddress },
]
const MENU_ITEMS: MineMenuItem[] = [
{ id: 'devices', title: '我的设备' },
{ id: 'members', title: '家庭成员' },
{ id: 'scene', title: '场景与自动化' },
{ id: 'service', title: '售后服务' },
{ id: 'support', title: '在线客服' },
{ id: 'feedback', title: '帮助与反馈' },
{ id: 'settings', title: '通用设置' },
{ id: 'privacy', title: '隐私与安全' },
{ id: 'about', title: '关于我们', value: APP_VERSION },
]
export function useMine() {
const loading = ref(false)
const nickName = ref('这里是昵称')
const phone = ref('')
const avatarUrl = ref('')
const deviceCount = ref(0)
const memberCount = ref(0)
const warrantyCount = ref(0)
const sceneCount = ref(0)
const statusBarHeight = ref(0)
const stats = computed(() => [
{ label: '我的设备', value: deviceCount.value },
{ label: '家庭成员', value: memberCount.value },
{ label: '保修中', value: warrantyCount.value },
])
const menuList = computed(() =>
MENU_ITEMS.map((item) => {
if (item.id === 'devices') {
return { ...item, value: `已绑定 ${deviceCount.value} 台` }
}
if (item.id === 'members') {
return { ...item, value: `${memberCount.value} 位成员` }
}
if (item.id === 'scene') {
return { ...item, value: `${sceneCount.value} 个场景` }
}
if (item.id === 'service') {
return { ...item, value: '报修 / 退换' }
}
return item
}),
)
async function loadOverview() {
if (loading.value) return
loading.value = true
try {
const { profile, deviceCount: count } = await fetchMineOverview()
deviceCount.value = count
nickName.value = profile?.nickName || profile?.userName || '这里是昵称'
phone.value = profile?.phonenumber || profile?.userName || ''
avatarUrl.value = profile?.avatarUrl || ''
} catch (err) {
uni.showToast({ title: getErrorMessage(err, '加载资料失败'), icon: 'none' })
} finally {
loading.value = false
}
}
function onEditNickname() {
uni.navigateTo({ url: '/pages/mine/edit/index' })
}
function onQuickAction(action: MineQuickAction) {
if (action.id === 'notify') {
uni.navigateTo({ url: '/pages/mine/notify/index' })
return
}
uni.showToast({ title: `${action.label}功能开发中`, icon: 'none' })
}
function onMenuTap(item: MineMenuItem) {
if (item.id === 'devices') {
uni.reLaunch({ url: '/pages/device/index' })
return
}
if (item.id === 'support') {
uni.navigateTo({ url: '/pages/mine/support/index' })
return
}
if (item.id === 'settings') {
uni.navigateTo({ url: '/pages/mine/storage/index' })
return
}
if (item.id === 'privacy') {
uni.navigateTo({ url: '/pages/mine/privacy/index' })
return
}
if (item.id === 'about') return
uni.showToast({ title: `${item.title}功能开发中`, icon: 'none' })
}
onShow(() => {
const sys = uni.getSystemInfoSync()
statusBarHeight.value = sys.statusBarHeight ?? 0
void loadOverview()
})
return {
statusBarHeight,
nickName,
phone,
avatarUrl,
stats,
quickActions: QUICK_ACTIONS,
menuList,
onEditNickname,
onQuickAction,
onMenuTap,
}
}