40 lines
6.9 KiB
Vue
40 lines
6.9 KiB
Vue
|
|
<script setup>
|
|||
|
|
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 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
|
|||
|
|
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)} 米`:'尚无有效位置'
|
|||
|
|
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 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}
|
|||
|
|
}
|
|||
|
|
watch(()=>[active.value,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})
|
|||
|
|
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>
|
|||
|
|
<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>
|