1
0
mirror of https://github.com/ZeroCatDev/Classworks.git synced 2025-12-07 13:03:59 +00:00

feat: 添加设备认证对话框的预配置支持,优化自动认证逻辑

This commit is contained in:
SunWuyuan 2025-11-02 15:15:46 +08:00
parent 9aae9601f6
commit 7f166ffddc
No known key found for this signature in database
GPG Key ID: A6A54CF66F56BB64
3 changed files with 157 additions and 5 deletions

View File

@ -153,7 +153,9 @@
max-width="500"
>
<DeviceAuthDialog
ref="deviceAuthDialog"
:show-cancel="true"
:preconfig="deviceAuthPreconfig"
@success="handleAuthSuccess"
@cancel="showDeviceAuthDialog = false"
/>
@ -184,13 +186,25 @@
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import { ref, computed, onMounted, watch } from 'vue'
import { getSetting, setSetting } from '@/utils/settings'
import DeviceAuthDialog from './auth/DeviceAuthDialog.vue'
import TokenInputDialog from './auth/TokenInputDialog.vue'
import AlternativeCodeDialog from './auth/AlternativeCodeDialog.vue'
import FirstTimeGuide from './auth/FirstTimeGuide.vue'
const props = defineProps({
preconfig: {
type: Object,
default: () => ({
namespace: null,
authCode: null,
autoOpen: false,
autoExecute: false
})
}
})
const emit = defineEmits(['done'])
// kvToken provider kv-local
@ -202,10 +216,25 @@ const showDeviceAuthDialog = ref(false)
const showTokenDialog = ref(false)
const showAlternativeCodeDialog = ref(false)
//
const deviceAuthDialog = ref(null)
const provider = computed(() => getSetting('server.provider'))
const isKvProvider = computed(() => provider.value === 'kv-server' || provider.value === 'classworkscloud')
const kvToken = computed(() => getSetting('server.kvToken'))
//
const deviceAuthPreconfig = computed(() => {
if (props.preconfig?.namespace) {
return {
namespace: props.preconfig.namespace,
password: props.preconfig.authCode || '',
autoExecute: props.preconfig.autoExecute || false
}
}
return null
})
const evaluateVisibility = () => {
const path = window.location.pathname
const onHome = path === '/' || path === '/index' || path === '/index.html'
@ -213,6 +242,21 @@ const evaluateVisibility = () => {
visible.value = onHome && need
}
//
watch(
() => props.preconfig,
(newPreconfig) => {
if (newPreconfig?.autoOpen && newPreconfig?.namespace && visible.value) {
console.log('检测到预配数据,自动打开设备认证对话框')
//
setTimeout(() => {
showDeviceAuthDialog.value = true
}, 500)
}
},
{ immediate: true, deep: true }
)
onMounted(() => {
evaluateVisibility()
})
@ -231,8 +275,16 @@ const handleAutoAuthorize = () => {
window.location.href = url
}
const handleAuthSuccess = () => {
const handleAuthSuccess = (tokenData) => {
showDeviceAuthDialog.value = false
console.log('认证成功:', tokenData)
//
if (props.preconfig?.namespace) {
//
console.log(`预配数据认证成功: ${props.preconfig.namespace}`)
}
evaluateVisibility()
emit('done')
}

View File

@ -100,14 +100,18 @@
</template>
<script setup>
import { ref } from 'vue'
import { ref, watch } from 'vue'
import { getSetting, setSetting } from '@/utils/settings'
import axios from '@/axios/axios'
defineProps({
const props = defineProps({
showCancel: {
type: Boolean,
default: false
},
preconfig: {
type: Object,
default: null
}
})
@ -120,6 +124,30 @@ const form = ref({
const authenticating = ref(false)
const error = ref('')
//
watch(
() => props.preconfig,
(newPreconfig) => {
if (newPreconfig) {
console.log('应用预配置数据:', newPreconfig)
form.value.namespace = newPreconfig.namespace || ''
form.value.password = newPreconfig.password || ''
//
if (newPreconfig.autoExecute && newPreconfig.namespace) {
console.log('检测到自动执行标志且有命名空间,自动执行认证')
// UI
setTimeout(() => {
authenticate()
}, 300)
} else if (newPreconfig.namespace) {
console.log('预配置数据已填入,等待手动认证')
}
}
},
{ immediate: true, deep: true }
)
const authenticate = async () => {
if (!form.value.namespace || authenticating.value) return
error.value = ''

View File

@ -43,7 +43,11 @@
</template>
</v-app-bar>
<!-- 初始化选择卡片仅在首页且需要授权时显示不影响顶栏 -->
<init-service-chooser v-if="shouldShowInit" @done="settingsTick++" />
<init-service-chooser
v-if="shouldShowInit"
:preconfig="preconfigData"
@done="settingsTick++"
/>
<!-- 学生姓名管理组件 -->
<StudentNameManager
@ -797,6 +801,13 @@ export default {
$offKvChanged: null,
$offConnect: null,
debouncedRealtimeRefresh: null,
//
preconfigData: {
namespace: null,
authCode: null,
autoOpen: false,
autoExecute: false
},
};
},
@ -1203,6 +1214,9 @@ export default {
},
async initializeData() {
//
this.parsePreconfigData();
const configApplied = await this.parseUrlConfig();
const urlParams = new URLSearchParams(window.location.search);
@ -2232,6 +2246,64 @@ export default {
currentDate.setDate(currentDate.getDate() + offset);
this.handleDateSelect(currentDate);
},
//
parsePreconfigData() {
try {
const urlParams = new URLSearchParams(window.location.search);
const namespace = urlParams.get("namespace");
const authCode = urlParams.get("authCode") || urlParams.get("auth_code");
const autoExecute = urlParams.get("autoExecute") || urlParams.get("auto_execute");
if (namespace) {
this.preconfigData.namespace = namespace;
this.preconfigData.authCode = authCode;
this.preconfigData.autoOpen = true;
// true/false1/0yes/no
this.preconfigData.autoExecute = this.parseBoolean(autoExecute);
console.log("检测到预配数据:", {
namespace: this.preconfigData.namespace,
hasAuthCode: !!this.preconfigData.authCode,
autoExecute: this.preconfigData.autoExecute
});
// URL
this.cleanupUrlParams(['namespace', 'authCode', 'auth_code', 'autoExecute', 'auto_execute']);
}
} catch (error) {
console.error("解析预配数据失败:", error);
}
},
//
parseBoolean(value) {
if (!value) return false;
const lowerValue = value.toLowerCase();
return lowerValue === 'true' || lowerValue === '1' || lowerValue === 'yes';
},
// URL
cleanupUrlParams(params) {
try {
const url = new URL(window.location);
let hasChanged = false;
params.forEach(param => {
if (url.searchParams.has(param)) {
url.searchParams.delete(param);
hasChanged = true;
}
});
if (hasChanged) {
// 使 replaceState
window.history.replaceState({}, document.title, url.toString());
}
} catch (error) {
console.error("清理URL参数失败:", error);
}
},
},
};
</script>