Skip to content

Commit cd3ec26

Browse files
author
linyuan.yang
committed
helper
1 parent 1db60c0 commit cd3ec26

5 files changed

Lines changed: 178 additions & 60 deletions

File tree

‎.vscode/launch.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@
5555
],
5656
"cwd": "${workspaceFolder}/packages/cli",
5757
"skipFiles": ["<node_internals>/**"]
58+
},
59+
{
60+
"type": "node-terminal",
61+
"request": "launch",
62+
"name": "Launch helper",
63+
"command": "pnpm dev",
64+
"cwd": "${workspaceFolder}/packages/helper"
65+
},
66+
{
67+
"type": "node-terminal",
68+
"request": "launch",
69+
"name": "Launch app",
70+
"command": "pnpm dev",
71+
"cwd": "${workspaceFolder}/packages/app"
5872
}
5973
]
6074
}

‎packages/helper/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub fn run() {
66
tauri::Builder::default()
77
.invoke_handler(tauri::generate_handler![
88
tools::xiaoai_login::xiaoai_open_login,
9+
tools::xiaoai_login::xiaoai_logout,
910
tools::xiaoai_login::xiaoai_list_devices,
1011
])
1112
.run(tauri::generate_context!())

‎packages/helper/src-tauri/src/tools/xiaoai_login.rs‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,38 @@ pub async fn xiaoai_open_login(app: AppHandle) -> Result<XiaoaiCreds, String> {
101101
}
102102

103103
// ─────────────────────────────────────────────────────────────────────────────
104-
// Command 2: list mina devices using the captured passToken.
104+
// Command 2: clear webview cookies / storage so the next login can use a
105+
// different Xiaomi account. Without this, the embedded WebView would silently
106+
// reuse the previous session.
107+
// ─────────────────────────────────────────────────────────────────────────────
108+
109+
#[tauri::command]
110+
pub async fn xiaoai_logout(app: AppHandle) -> Result<(), String> {
111+
if let Some(existing) = app.get_webview_window(LOGIN_WINDOW_LABEL) {
112+
existing.close().ok();
113+
sleep(Duration::from_millis(200)).await;
114+
}
115+
116+
let window = WebviewWindowBuilder::new(
117+
&app,
118+
LOGIN_WINDOW_LABEL,
119+
WebviewUrl::External("about:blank".parse().unwrap()),
120+
)
121+
.visible(false)
122+
.inner_size(100.0, 100.0)
123+
.build()
124+
.map_err(|e| format!("create logout webview failed: {e}"))?;
125+
126+
let result = window.clear_all_browsing_data();
127+
sleep(Duration::from_millis(100)).await;
128+
window.close().ok();
129+
130+
result.map_err(|e| format!("clear browsing data failed: {e}"))?;
131+
Ok(())
132+
}
133+
134+
// ─────────────────────────────────────────────────────────────────────────────
135+
// Command 3: list mina devices using the captured passToken.
105136
// 1. /pass/serviceLogin with passToken cookie → get { location, nonce, ssecurity }
106137
// 2. Walk redirect chain → harvest serviceToken cookie
107138
// 3. GET https://api2.mina.mi.com/admin/v2/device_list with serviceToken
Lines changed: 127 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
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
77
const phase = ref<Phase>('idle')
88
const error = ref('')
99
const creds = ref<XiaoaiCreds | null>(null)
1010
const devices = ref<XiaoaiDevice[]>([])
11-
const selectedDeviceName = ref('')
12-
const copied = ref(false)
11+
const copiedKey = ref('')
1312
1413
async 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: 16px; }
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>

‎packages/helper/src/tools/xiaoai-login/tauri.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export async function openLogin(): Promise<XiaoaiCreds> {
1717
return await invoke('xiaoai_open_login')
1818
}
1919

20+
export async function logout(): Promise<void> {
21+
await invoke('xiaoai_logout')
22+
}
23+
2024
export async function listDevices(creds: XiaoaiCreds): Promise<XiaoaiDevice[]> {
2125
return await invoke('xiaoai_list_devices', { ...creds })
2226
}

0 commit comments

Comments
 (0)