Files
ucvl-home-vision/frontend/src/components/FamilyLocations.vue
T

52 lines
9.1 KiB
Vue
Raw Normal View History

<script setup>
import {ref,computed,watch,onBeforeUnmount} from 'vue'
import {snapshot,bridge} from '../composables/bridge.js'
import FamilyMap from './FamilyMap.vue'
import LocationHistory from './LocationHistory.vue'
const result=ref(null),error=ref(''),consent=ref(false),days=ref(7),code=ref(''),expires=ref(0),busy=ref(false),selected=ref('')
const memberId=ref(''),viewerIds=ref([]),displayIds=ref([]),phoneIds=ref([]),viewOnly=ref(true)
const binding=computed(()=>result.value?.bindings?.find(x=>x.memberId===memberId.value))
const active=computed(()=>snapshot.value.loggedIn&&snapshot.value.page==='overview')
const home=computed(()=>snapshot.value.state.households?.find(x=>x.id===snapshot.value.state.familyId))
let timer,epoch=0,requestId=0
const items=computed(()=>result.value?.items||[])
const time=n=>n?new Date(n*1000).toLocaleString('zh-CN'):'尚未收到'
const status=d=>d.status==='stale'?'位置更新中断':d.status==='live'?'位置已更新':'等待手机定位'
const location=d=>d.position?`${d.position.latitude.toFixed(4)}°, ${d.position.longitude.toFixed(4)}° · 精度约 ${Math.round(d.position.accuracy)} 米`:'尚无有效位置'
async function load(){const e=epoch,r=++requestId;if(!active.value)return
try{const value=await bridge().api('/api/locations');if(e===epoch&&r===requestId){result.value=value;error.value=''}}
catch(err){if(e===epoch&&r===requestId){result.value=null;error.value=err.message}}
finally{if(e===epoch&&r===requestId&&active.value)timer=setTimeout(load,30000)}
}
async function action(name){if(busy.value)return;busy.value=true;error.value='';const e=epoch;requestId++;clearTimeout(timer)
try{const value=await bridge().api('/api/locations',{action:name,memberId:memberId.value,viewerIds:viewerIds.value,displayIds:displayIds.value,phoneIds:phoneIds.value,viewOnly:viewOnly.value,days:days.value});if(e!==epoch)return;code.value=value.code||'';expires.value=value.expires||0;if(name==='stop')consent.value=false;clearTimeout(timer);await load()}
catch(err){if(e===epoch)error.value=err.message}finally{if(e===epoch){busy.value=false;clearTimeout(timer);if(active.value)timer=setTimeout(load,30000)}}
}
watch([active,()=>snapshot.value.state.familyId,()=>snapshot.value.session],()=>{epoch++;clearTimeout(timer);result.value=null;code.value='';memberId.value='';viewerIds.value=[];displayIds.value=[];phoneIds.value=[];viewOnly.value=true;selected.value='';consent.value=false;busy.value=false;if(active.value)load()},{immediate:true})
watch([memberId,viewerIds,displayIds,phoneIds,viewOnly,days],()=>{code.value=''}, {deep:true})
onBeforeUnmount(()=>{epoch++;clearTimeout(timer)})
</script>
<template><section v-if="active" class="panel family-locations mb-4" aria-label="家庭位置共享">
<div class="d-flex flex-wrap justify-content-between gap-2"><div><h2>家人位置</h2><p class="text-secondary small">{{home?.name}} · {{items.length}} 位家人正在共享 · 只显示本人同意分享的最新位置</p></div><button class="btn btn-sm btn-outline-secondary align-self-start" @click="selected=''">查看全部位置</button></div>
<div v-if="items.some(d=>d.status==='stale')" class="alert alert-warning" role="status">{{items.filter(d=>d.status==='stale').map(d=>d.name).join('、')}}的位置超过 15 分钟未更新。请检查手机网络、定位权限或省电设置;这不代表人员失联。</div>
<div class="row g-3"><div class="col-12 col-lg-8"><FamilyMap :items="items" :selected="selected" :height="420" @select="selected=$event"/><p class="small text-secondary mb-0">点击家人卡片定位,圆圈表示定位精度范围。底图由智家提供,位置仅对获授权的人显示。</p></div>
<div class="col-12 col-lg-4"><div v-if="!items.length" class="location-empty"><span>⌖</span><h3 class="fs-5">等待家人主动分享</h3><p>家人无需注册账号。手机配对、本人同意并开启共享后,获准的查看人才能看到位置。</p></div><button v-for="d in items" :key="d.id" class="location-person w-100 text-start" :class="{'location-selected':selected===d.id}" @click="selected=d.id"><strong>{{d.name}}</strong><span :class="d.status==='stale'?'text-warning-emphasis':'text-success'">{{status(d)}}</span><small>{{location(d)}}</small><small>定位时间:{{time(d.position?.at)}}</small><small v-if="d.signal==='permission_denied'||d.signal==='location_disabled'">手机定位或权限未开启</small></button></div></div>
<LocationHistory :members="[...items,...(result?.members||[]).filter(m=>!items.some(i=>i.id===m.id))]"/>
<details class="mt-3 border-top pt-3"><summary>为家庭成员配对手机</summary>
<p class="small text-secondary mt-2">老人和孩子无需登录。先选择真实成员及具体查看人,再将配对码输入本人的手机。本人核对授权内容、允许定位并打开共享后才上传;管理员生成配对码不能代替本人同意。家庭屏需单独勾选。</p>
<p v-if="!result?.members?.length" class="alert alert-info">请先在“家庭成员”登记本人或家人;普通账号需关联本人档案。</p>
<form v-else @submit.prevent="action('pair')"><div class="row g-3"><label class="col-sm-8">绑定的家庭成员<select v-model="memberId" required class="form-select" :disabled="busy"><option value="">请选择成员</option><option v-for="m in result.members" :key="m.id" :value="m.id" :disabled="m.status==='historical'||m.lifeStatus==='deceased'">{{m.name}} {{m.label?`· ${m.label}`:''}}</option></select></label><label class="col-sm-4">手机配对有效期<select v-model.number="days" class="form-select" :disabled="busy"><option :value="1">1 天</option><option :value="7">7 天</option><option :value="30">30 天</option></select></label></div>
<label class="d-flex gap-2 mt-3"><input type="checkbox" v-model="viewOnly" :disabled="busy">先仅配对手机查看家人,本人可稍后在手机设置自己的共享</label><fieldset v-if="!viewOnly" class="mt-3"><legend class="fs-6">允许哪些账号查看</legend><label v-for="u in result.viewers" :key="u.id" class="d-inline-flex gap-2 me-4 mb-2"><input type="checkbox" v-model="viewerIds" :value="u.id" :disabled="busy">{{u.name}}({{u.username}})</label></fieldset>
<fieldset v-if="!viewOnly" class="mt-3"><legend class="fs-6">允许哪些家人手机查看</legend><label v-for="p in result.phones||[]" :key="p.id" class="d-inline-flex gap-2 me-4 mb-2"><input type="checkbox" v-model="phoneIds" :value="p.id" :disabled="busy">{{p.name}}的手机</label></fieldset><fieldset v-if="!viewOnly" class="mt-3"><legend class="fs-6">允许哪些公共显示终端展示(默认不选)</legend><p v-if="!result.displays.length" class="small text-secondary">尚无已配对显示终端。</p><label v-for="d in result.displays" :key="d.id" class="d-inline-flex gap-2 me-4 mb-2"><input type="checkbox" v-model="displayIds" :value="d.id" :disabled="busy">{{d.name}}</label></fieldset>
<p v-if="binding">{{binding.enabled?'手机已开启共享':'手机已绑定,共享关闭'}} · {{time(binding.expires)}} 到期</p>
<div class="d-flex flex-wrap gap-2 mt-3"><button class="btn btn-primary" :disabled="busy||!memberId||(!viewOnly&&!viewerIds.length&&!displayIds.length&&!phoneIds.length)">生成配对码</button><button v-if="binding||code" type="button" class="btn btn-outline-danger" :disabled="busy" @click="action('stop')">解除该手机授权</button><a class="btn btn-outline-secondary" href="/downloads" target="_blank" rel="noreferrer">下载配套 App</a></div>
<div v-if="code" class="alert alert-success mt-3" role="status">配对码 <strong class="font-monospace fs-3">{{code}}</strong> · 10 分钟内使用一次<br>请在成员本人的手机核对姓名与查看名单,手机授权有效至 {{time(expires)}}。重新配对会替换旧手机授权。</div>
</form><p class="small text-secondary mt-3">手机可在“我的位置设置”选择实时(约 10 秒)、1 分钟或 5 分钟上传,默认 5 分钟。实时位置仅在内存中保存,关闭共享后清除;本人可另行开启历史保存,长期归档到百度网盘并手动删除。定位或网络中断超过 15 分钟隐藏旧坐标,提示更新中断,不表示人员失联。</p></details>
<p v-if="error" class="text-danger mt-2" role="alert">{{error}}</p>
</section></template>
<style scoped>.family-locations h2{font-size:1.3rem}.location-empty{display:grid;align-content:center;min-height:270px;text-align:center;color:var(--ui-muted,#64776f);padding:1rem}.location-empty>span{font-size:3rem;color:#6c9685}.location-person{display:flex;flex-direction:column;gap:.35rem;background:var(--panel,#fff);border:1px solid var(--ui-line,#d9e3dd);border-radius:12px;padding:14px;margin-bottom:10px;color:var(--ink,#243c32)}.location-person>span,.location-person small{font-size:.85rem}.location-selected{border-color:#257a68;box-shadow:inset 3px 0 #257a68}.family-locations summary{cursor:pointer}.family-locations input[type=checkbox]{min-width:18px;width:18px;min-height:18px}</style>
<style scoped>
button.location-person{color:var(--ink,#243c32)!important;background:var(--panel,#fff)!important;align-items:stretch;text-align:left!important}
.location-person strong,.location-person small{color:var(--ink,#243c32)}
</style>