11<script setup lang="ts">
2- import { ref , computed } from ' vue'
3- import { openLogin , listDevices , type XiaoaiCreds , type XiaoaiDevice } from ' ./tauri'
2+ import { ref } from ' vue'
3+ import { openLogin , logout , listDevices , type XiaoaiCreds , type XiaoaiDevice } from ' ./tauri'
44
5- type Phase = ' idle' | ' logging_in' | ' fetching_devices' | ' awaiting_devices ' | ' done '
5+ type Phase = ' idle' | ' logging_in' | ' fetching_devices' | ' done ' | ' logging_out '
66
77const phase = ref <Phase >(' idle' )
88const error = ref (' ' )
99const creds = ref <XiaoaiCreds | null >(null )
1010const devices = ref <XiaoaiDevice []>([])
11- const selectedDeviceName = ref (' ' )
12- const copied = ref (false )
11+ const copiedKey = ref (' ' )
1312
1413async function start() {
1514 error .value = ' '
16- copied .value = false
15+ copiedKey .value = ' '
1716 phase .value = ' logging_in'
1817 try {
1918 creds .value = await openLogin ()
@@ -37,29 +36,16 @@ async function start() {
3736 phase .value = ' idle'
3837 return
3938 }
40- selectedDeviceName .value = devices .value [0 ].name
41- phase .value = ' awaiting_devices'
42- }
43-
44- function confirmDevice() {
4539 phase .value = ' done'
4640}
4741
48- const finalJson = computed (() => {
49- if (! creds .value || ! selectedDeviceName .value ) return ' '
50- return JSON .stringify ({
51- userId: creds .value .userId ,
52- passToken: creds .value .passToken ,
53- deviceId: creds .value .deviceId ,
54- device: selectedDeviceName .value ,
55- }, null , 2 )
56- })
57-
58- async function copy() {
42+ async function copyValue(key : string , value : string ) {
5943 try {
60- await navigator .clipboard .writeText (finalJson .value )
61- copied .value = true
62- setTimeout (() => { copied .value = false }, 2000 )
44+ await navigator .clipboard .writeText (value )
45+ copiedKey .value = key
46+ setTimeout (() => {
47+ if (copiedKey .value === key ) copiedKey .value = ' '
48+ }, 2000 )
6349 } catch (e : any ) {
6450 error .value = ` 复制失败: ${String (e ?.message || e )} `
6551 }
@@ -69,20 +55,36 @@ function reset() {
6955 phase .value = ' idle'
7056 creds .value = null
7157 devices .value = []
72- selectedDeviceName .value = ' '
7358 error .value = ' '
74- copied .value = false
59+ copiedKey .value = ' '
60+ }
61+
62+ async function switchAccount() {
63+ const prev = phase .value
64+ phase .value = ' logging_out'
65+ error .value = ' '
66+ try {
67+ await logout ()
68+ } catch (e : any ) {
69+ error .value = ` 登出失败: ${String (e ?.message || e )} `
70+ phase .value = prev
71+ return
72+ }
73+ reset ()
7574}
7675 </script >
7776
7877<template >
7978 <div class =" xiaoai" >
8079 <h2 >小爱登录</h2 >
81- <p class =" lead" >点击下方按钮打开小米官方登录页。登录成功后会自动关闭、拉取音箱列表,最后输出可粘贴的 JSON 。</p >
80+ <p class =" lead" >点击下方按钮打开小米官方登录页。登录成功并拉取到音箱后,下面每一项都可以单独复制 。</p >
8281
8382 <section v-if =" phase === 'idle'" class =" card" >
84- <button class =" btn" @click =" start" >打开小米登录</button >
85- <p class =" hint" >支持账号密码 / 短信 / 扫码 / 图形验证(按小米页面引导)</p >
83+ <div class =" actions" >
84+ <button class =" btn" @click =" start" >打开小米登录</button >
85+ <button class =" btn btn-outline" @click =" switchAccount" >清除登录缓存</button >
86+ </div >
87+ <p class =" hint" >支持账号密码 / 短信 / 扫码 / 图形验证(按小米页面引导)。如果点击登录后自动登上的是上次的账号,先点「清除登录缓存」。</p >
8688 <p v-if =" error" class =" error" >{{ error }}</p >
8789 </section >
8890
@@ -95,28 +97,67 @@ function reset() {
9597 <div class =" status" >登录成功,正在拉取音箱列表…</div >
9698 </section >
9799
98- <section v-else-if =" phase === 'awaiting_devices'" class =" card" >
99- <div class =" status success" >登录成功,请选择音箱</div >
100- <ul class =" device-list" >
101- <li v-for =" d in devices" :key =" d.deviceID" >
102- <label >
103- <input type =" radio" v-model =" selectedDeviceName" :value =" d.name" />
104- <span class =" device-name" >{{ d.name }}</span >
105- <span v-if =" d.alias && d.alias !== d.name" class =" device-alias" >({{ d.alias }})</span >
106- </label >
107- </li >
108- </ul >
109- <button class =" btn" @click =" confirmDevice" >使用该音箱</button >
100+ <section v-else-if =" phase === 'logging_out'" class =" card" >
101+ <div class =" status" >正在清除登录态…</div >
102+ <p class =" hint" >清除完成后即可用另一个账号登录。</p >
110103 </section >
111104
112- <section v-else-if =" phase === 'done'" class =" card" >
105+ <section v-else-if =" phase === 'done' && creds " class =" card" >
113106 <div class =" status success" >凭据已就绪</div >
114- <pre class =" json" >{{ finalJson }}</pre >
107+
108+ <div class =" field-group" >
109+ <h3 class =" group-title" >账号凭据</h3 >
110+ <div class =" field" >
111+ <span class =" field-label" >userId</span >
112+ <code class =" field-value" >{{ creds.userId }}</code >
113+ <button class =" copy-btn" @click =" copyValue('userId', creds.userId)" >
114+ {{ copiedKey === 'userId' ? '已复制 ✓' : '复制' }}
115+ </button >
116+ </div >
117+ <div class =" field" >
118+ <span class =" field-label" >passToken</span >
119+ <code class =" field-value" >{{ creds.passToken }}</code >
120+ <button class =" copy-btn" @click =" copyValue('passToken', creds.passToken)" >
121+ {{ copiedKey === 'passToken' ? '已复制 ✓' : '复制' }}
122+ </button >
123+ </div >
124+ </div >
125+
126+ <div class =" field-group" >
127+ <h3 class =" group-title" >音箱列表({{ devices.length }})</h3 >
128+ <div v-for =" (d, i) in devices" :key =" d.deviceID" class =" device-block" >
129+ <div class =" device-header" >
130+ <span class =" device-name" >{{ d.name }}</span >
131+ <span v-if =" d.alias && d.alias !== d.name" class =" device-alias" >({{ d.alias }})</span >
132+ </div >
133+ <div class =" field" >
134+ <span class =" field-label" >name</span >
135+ <code class =" field-value" >{{ d.name }}</code >
136+ <button class =" copy-btn" @click =" copyValue(`name-${i}`, d.name)" >
137+ {{ copiedKey === `name-${i}` ? '已复制 ✓' : '复制' }}
138+ </button >
139+ </div >
140+ <div class =" field" >
141+ <span class =" field-label" >deviceID</span >
142+ <code class =" field-value" >{{ d.deviceID }}</code >
143+ <button class =" copy-btn" @click =" copyValue(`deviceID-${i}`, d.deviceID)" >
144+ {{ copiedKey === `deviceID-${i}` ? '已复制 ✓' : '复制' }}
145+ </button >
146+ </div >
147+ <div class =" field" >
148+ <span class =" field-label" >miotDID</span >
149+ <code class =" field-value" >{{ d.miotDID }}</code >
150+ <button class =" copy-btn" @click =" copyValue(`miotDID-${i}`, d.miotDID)" >
151+ {{ copiedKey === `miotDID-${i}` ? '已复制 ✓' : '复制' }}
152+ </button >
153+ </div >
154+ </div >
155+ </div >
156+
115157 <div class =" actions" >
116- <button class =" btn" @click =" copy" >{{ copied ? '已复制 ✓' : '复制 JSON' }}</button >
117- <button class =" btn btn-outline" @click =" reset" >重新登录</button >
158+ <button class =" btn btn-outline" @click =" reset" >返回</button >
118159 </div >
119- <p class = " hint " >回到 admin → 添加 / 编辑 xiaoai 频道 → 展开"粘贴凭据 JSON" → 粘贴 → 解析并填入 → 保存。 </p >
160+ <p v-if = " error " class = " error " >{{ error }} </p >
120161 </section >
121162 </div >
122163</template >
@@ -129,19 +170,46 @@ function reset() {
129170 padding : 20px ; margin-bottom : 16px ;
130171}
131172.status { font-size : 14px ; margin-bottom : 12px ; }
132- .status.success { color : var (--success ); font-weight : 500 ; }
173+ .status.success { color : var (--success ); font-weight : 500 ; margin-bottom : 16 px ; }
133174.hint { font-size : 12px ; color : var (--fg-muted ); margin : 12px 0 0 ; }
134175.error { color : var (--danger ); margin : 12px 0 0 ; font-size : 13px ; }
135- .device-list { list-style : none ; padding : 0 ; margin : 0 0 16px ; }
136- .device-list li { padding : 8px 0 ; border-bottom : 1px solid var (--border ); }
137- .device-list li :last-child { border-bottom : none ; }
138- .device-list label { display : flex ; align-items : center ; gap : 8px ; cursor : pointer ; }
139- .device-name { font-weight : 500 ; }
140- .device-alias { color : var (--fg-muted ); font-size : 12px ; }
141- .json {
142- background : var (--bg ); border : 1px solid var (--border ); border-radius : var (--radius );
143- padding : 12px ; font : 12px /1.5 ui-monospace , SFMono-Regular, Consolas, monospace ;
144- margin : 0 0 12px ; white-space : pre-wrap ; word-break : break-all ;
176+
177+ .field-group { margin-bottom : 20px ; }
178+ .field-group :last-of-type { margin-bottom : 12px ; }
179+ .group-title {
180+ font-size : 13px ; font-weight : 600 ; color : var (--fg-muted );
181+ margin : 0 0 10px ; text-transform : uppercase ; letter-spacing : 0.5px ;
145182}
146- .actions { display : flex ; gap : 12px ; }
183+
184+ .device-block {
185+ border : 1px solid var (--border ); border-radius : var (--radius );
186+ padding : 12px ; margin-bottom : 10px ; background : var (--bg );
187+ }
188+ .device-block :last-child { margin-bottom : 0 ; }
189+ .device-header { margin-bottom : 8px ; }
190+ .device-name { font-weight : 600 ; }
191+ .device-alias { color : var (--fg-muted ); font-size : 12px ; margin-left : 4px ; }
192+
193+ .field {
194+ display : flex ; align-items : center ; gap : 8px ;
195+ padding : 6px 0 ; border-bottom : 1px solid var (--border );
196+ }
197+ .field :last-child { border-bottom : none ; }
198+ .field-label {
199+ flex : 0 0 90px ; font-size : 12px ; color : var (--fg-muted );
200+ }
201+ .field-value {
202+ flex : 1 1 auto ; min-width : 0 ;
203+ font : 12px /1.5 ui-monospace , SFMono-Regular, Consolas, monospace ;
204+ background : transparent ; padding : 4px 6px ; border-radius : 4px ;
205+ word-break : break-all ; user-select : text ;
206+ }
207+ .copy-btn {
208+ flex : 0 0 auto ; padding : 4px 10px ; font-size : 12px ;
209+ border : 1px solid var (--border ); background : var (--card );
210+ border-radius : 4px ; cursor : pointer ;
211+ }
212+ .copy-btn :hover { background : var (--border ); }
213+
214+ .actions { display : flex ; gap : 12px ; margin-top : 8px ; }
147215 </style >
0 commit comments