Separate system accounts and household people, add generation directory and consented member location sharing

This commit is contained in:
Codex
2026-10-18 20:00:43 +08:00
parent 67bc5a1ba9
commit 3b2c046060
52 changed files with 1497 additions and 311 deletions
+23 -13
View File
@@ -3,9 +3,11 @@ import {ref,computed,watch,onBeforeUnmount} from 'vue'
import {snapshot,bridge} from '../composables/bridge.js'
import LocalChart from './LocalChart.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([])
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
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'?'位置已更新':'等待手机定位'
@@ -14,26 +16,34 @@ const option=computed(()=>{
const focus=items.value.find(x=>x.id===selected.value)?.position
return {geo:{map:'world-land',roam:true,center:focus?[focus.longitude,focus.latitude]:[0,10],zoom:focus?5:1,boundingCoords:[[-180,90],[180,-90]],itemStyle:{areaColor:'#d9e4df',borderColor:'#94b2a6'},emphasis:{disabled:true},silent:false},tooltip:{show:false},series:[{type:'scatter',coordinateSystem:'geo',symbolSize:14,label:{show:true,position:'top',formatter:'{b}'},data:items.value.filter(d=>d.position).map(d=>({name:d.name,targetId:d.id,value:[d.position.longitude,d.position.latitude],itemStyle:{color:d.status==='stale'?'#c18d3e':'#257a68'}}))}]}
})
async function load(){const e=epoch;if(!active.value)return
try{const value=await bridge().api('/api/locations');if(e===epoch){result.value=value;error.value=''}}
catch(err){if(e===epoch){result.value=null;error.value=err.message}}
finally{if(e===epoch&&active.value)timer=setTimeout(load,30000)}
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
try{const value=await bridge().api('/api/locations',{action:name,consent:consent.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}
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,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='';selected.value='';consent.value=false;busy.value=false;if(active.value)load()},{immediate:true})
watch([active,()=>snapshot.value.state.familyId,()=>snapshot.value.session],()=>{epoch++;clearTimeout(timer);result.value=null;code.value='';memberId.value='';viewerIds.value=[];displayIds.value=[];selected.value='';consent.value=false;busy.value=false;if(active.value)load()},{immediate:true})
watch([memberId,viewerIds,displayIds,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"><LocalChart :option="option" :height="360" label="家庭位置世界平面图,详细位置见右侧列表" @select="selected=$event"/><p class="small text-secondary mb-0">可拖动缩放,点击家人定位。底图:Natural Earth 陆地轮廓(本地资源);定位采用 WGS84 经纬度。</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>
<details class="mt-3 border-top pt-3"><summary>我的位置共享</summary><p class="small text-secondary mt-2">手机安装配套 App,打开“赵府位置共享”。平台生成配对码后,还需本人在手机上同意并授予定位权限。约每 5 分钟更新,当前家庭成员和已授权家庭电视可见。仅在服务器内存保留最新点,停止后清除,不保存轨迹或写入数据库备份。</p>
<p v-if="!result?.canShare" class="alert alert-info">切换到你账号所属的家庭后可开启;管理员不能代替家人开启位置。</p>
<template v-else><p v-if="result?.own">{{result.own.enabled?'已开启共享':'手机已绑定,等待本人开启'}} · 有效期至 {{time(result.own.expires)}}</p><div class="d-flex flex-wrap align-items-center gap-2"><select v-model.number="days" class="form-select w-auto" aria-label="位置共享有效期"><option :value="1">1 天</option><option :value="7">7 天</option><option :value="30">30 天</option></select><a class="btn btn-outline-secondary" href="https://pro.ucvl.cn/admin/ucvl-home-vision/releases/tag/v0.1.44" target="_blank" rel="noreferrer">下载配套 App</a><button v-if="result?.own||code" class="btn btn-outline-danger" :disabled="busy" @click="action('stop')">停止我的共享</button></div><label class="d-flex gap-2 my-3"><input type="checkbox" v-model="consent">我同意向“{{home?.name}}”的成员和家庭电视共享本人位置,并可随时停止。</label><button class="btn btn-primary" :disabled="busy||!consent" @click="action('pair')">生成手机配对码</button><div v-if="code" class="alert alert-success mt-3">配对码 <strong class="font-monospace fs-3">{{code}}</strong> · 10 分钟内使用一次<br>共享有效期至 {{time(expires)}},未在手机开启前不采集位置。</div></template></details>
<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>
<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>
<fieldset 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 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||(!viewerIds.length&&!displayIds.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">约每 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>