Files
czzj-app/src/pages/device/components/device-header/index.vue
2026-10-28 10:36:39 +08:00

394 lines
8.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="device-header" :style="{ paddingTop: headerPaddingTop + 'px' }">
<view class="device-header__bar">
<view class="device-header__home-wrap">
<view class="device-header__home" @tap.stop="togglePicker">
<image
class="device-header__home-icon"
:src="staticAssets.iconHomeSelector"
mode="aspectFit"
/>
<text class="device-header__home-text">{{ homeName }}</text>
<image
class="device-header__arrow"
:class="{ 'device-header__arrow--open': pickerOpen }"
:src="staticAssets.iconHomeArrow"
mode="aspectFit"
/>
</view>
<view
v-if="pickerOpen"
class="device-header__mask"
@tap="closePicker"
@touchmove.stop.prevent="preventTouchMove"
/>
<view v-if="pickerOpen" class="device-header__dropdown">
<scroll-view
class="device-header__dropdown-scroll"
scroll-y
:show-scrollbar="false"
:style="dropdownScrollStyle"
>
<view
v-for="home in homes"
:key="String(home.id)"
class="device-header__option"
:class="{ 'device-header__option--active': isActiveHome(home) }"
@tap.stop="onSelectHome(home)"
>
<text
class="device-header__option-text"
:class="{ 'device-header__option-text--active': isActiveHome(home) }"
>
{{ home.homeName || '我的家' }}
</text>
<image
v-if="isActiveHome(home)"
class="device-header__check-icon"
:src="staticAssets.iconHomeSelected"
mode="aspectFit"
/>
</view>
</scroll-view>
<view class="device-header__option device-header__option--manage" @tap.stop="selectManage">
<text class="device-header__option-text">家庭管理</text>
</view>
</view>
</view>
<view class="device-header__actions">
<view class="device-header__notify-wrap" @tap="$emit('notify')">
<image
class="device-header__notify-icon"
:src="staticAssets.iconNotify"
mode="aspectFit"
/>
<view v-if="hasUnreadNotify" class="device-header__notify-badge" />
</view>
<view class="device-header__add-wrap">
<view class="device-header__add-btn" @tap.stop="toggleAddMenu">
<text class="device-header__add-icon">+</text>
</view>
<view v-if="addMenuOpen" class="device-header__add-mask" @tap="closeAddMenu" />
<view v-if="addMenuOpen" class="device-header__add-menu" @tap.stop>
<view class="device-header__add-menu-arrow" />
<view class="device-header__add-menu-item" @tap="onAddDevice">
<text class="device-header__add-menu-text">添加设备</text>
</view>
<view class="device-header__add-menu-item" @tap="onScan">
<text class="device-header__add-menu-text">扫一扫</text>
</view>
</view>
</view>
</view>
</view>
</view>
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { hasUnreadMessages } from '@/hooks/use-message-inbox'
import type { AppHomeVo } from '@/types/location'
import { parseId, sameId } from '@/utils/device-id'
import { staticAssets } from '@/utils/static-assets'
const props = defineProps<{
homeName: string
homeId: string
homes: AppHomeVo[]
}>()
const emit = defineEmits<{
notify: []
'add-device': []
scan: []
manage: []
'select-home': [home: AppHomeVo]
'picker-open-change': [open: boolean]
}>()
/** 单行选项约 88rpx,列表最多展示约 8 行高度 */
const DROPDOWN_ITEM_RPX = 88
const DROPDOWN_LIST_MAX_RPX = 704
const dropdownScrollStyle = computed(() => {
const count = Math.max(props.homes.length, 1)
const heightRpx = Math.min(count * DROPDOWN_ITEM_RPX, DROPDOWN_LIST_MAX_RPX)
return { height: `${uni.upx2px(heightRpx)}px` }
})
const pickerOpen = ref(false)
const addMenuOpen = ref(false)
const hasUnreadNotify = hasUnreadMessages
watch(pickerOpen, (open) => {
emit('picker-open-change', open)
})
function preventTouchMove() {}
const sys = uni.getSystemInfoSync()
const statusBarHeight = sys.statusBarHeight ?? 0
const headerPaddingTop =
statusBarHeight > 0
? statusBarHeight + uni.upx2px(24)
: uni.upx2px(48)
function togglePicker() {
closeAddMenu()
pickerOpen.value = !pickerOpen.value
}
function closePicker() {
pickerOpen.value = false
}
function isActiveHome(home: AppHomeVo) {
const id = parseId(home.id)
return !!id && sameId(id, props.homeId)
}
function onSelectHome(home: AppHomeVo) {
closePicker()
emit('select-home', home)
}
function selectManage() {
closePicker()
emit('manage')
}
function toggleAddMenu() {
closePicker()
addMenuOpen.value = !addMenuOpen.value
}
function closeAddMenu() {
addMenuOpen.value = false
}
function onAddDevice() {
closeAddMenu()
emit('add-device')
}
function onScan() {
closeAddMenu()
emit('scan')
}
</script>
<style lang="scss" scoped>
.device-header {
padding-left: 32rpx;
padding-right: 32rpx;
padding-bottom: 16rpx;
}
.device-header__bar {
height: 88rpx;
display: flex;
align-items: center;
justify-content: space-between;
}
.device-header__home-wrap {
position: relative;
z-index: 300;
}
.device-header__home {
display: flex;
align-items: center;
gap: 12rpx;
padding: 12rpx 28rpx 12rpx 20rpx;
background: #effbf4;
border-radius: 40rpx;
border: 1px solid #c9efd9;
}
.device-header__home-icon {
width: 32rpx;
height: 32rpx;
flex-shrink: 0;
}
.device-header__home-text {
font-size: 30rpx;
font-weight: 600;
color: #1fa85c;
white-space: nowrap;
}
.device-header__arrow {
width: 18rpx;
height: 18rpx;
flex-shrink: 0;
transition: transform 0.2s ease;
}
.device-header__arrow--open {
transform: rotate(180deg);
}
.device-header__mask {
position: fixed;
inset: 0;
z-index: 250;
background: transparent;
}
.device-header__dropdown {
position: absolute;
top: calc(100% + 12rpx);
left: 0;
min-width: 100%;
width: 280rpx;
border-radius: 24rpx;
overflow: hidden;
background: #fff;
box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
z-index: 300;
display: flex;
flex-direction: column;
}
.device-header__dropdown-scroll {
flex-shrink: 0;
width: 100%;
}
.device-header__option {
display: flex;
align-items: center;
justify-content: space-between;
padding: 28rpx 32rpx;
background: #fff;
}
.device-header__option--active {
background: linear-gradient(98deg, #45d685 0%, #21c25e 100%);
}
.device-header__option--manage {
border-top: 1rpx solid #f0f0f0;
}
.device-header__option-text {
font-size: 30rpx;
font-weight: 500;
color: $color-text;
line-height: 1.2;
}
.device-header__option-text--active {
color: #fff;
font-weight: 600;
}
.device-header__check-icon {
width: 36rpx;
height: 36rpx;
flex-shrink: 0;
}
.device-header__actions {
display: flex;
align-items: center;
gap: 16rpx;
}
.device-header__notify-wrap {
position: relative;
width: 72rpx;
height: 72rpx;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
}
.device-header__notify-icon {
width: 33rpx;
height: 33rpx;
flex-shrink: 0;
}
.device-header__notify-badge {
position: absolute;
top: 12rpx;
right: 12rpx;
width: 16rpx;
height: 16rpx;
border-radius: 50%;
background: #ff4d4f;
z-index: 2;
}
.device-header__add-wrap {
position: relative;
z-index: 350;
}
.device-header__add-btn {
width: 72rpx;
height: 72rpx;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.device-header__add-icon {
font-size: 52rpx;
font-weight: 300;
color: #667085;
line-height: 1;
}
.device-header__add-mask {
position: fixed;
inset: 0;
z-index: 340;
background: transparent;
}
.device-header__add-menu {
position: absolute;
top: calc(100% + 12rpx);
right: 0;
min-width: 240rpx;
padding: 16rpx 0;
background: #fff;
border-radius: 20rpx;
box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.12);
z-index: 360;
}
.device-header__add-menu-arrow {
position: absolute;
top: -10rpx;
right: 24rpx;
width: 20rpx;
height: 20rpx;
background: #fff;
transform: rotate(45deg);
box-shadow: -2rpx -2rpx 4rpx rgba(0, 0, 0, 0.04);
}
.device-header__add-menu-item {
padding: 28rpx 48rpx;
display: flex;
align-items: center;
justify-content: center;
}
.device-header__add-menu-text {
font-size: 30rpx;
color: $color-text;
line-height: 1.2;
white-space: nowrap;
}
</style>