4141 <el-tabs v-model =" loginTab" >
4242 <el-tab-pane label =" 扫码登录" name =" qrcode" >
4343 <div class =" login-qrcode" >
44- <div v-if =" !qrcodeUrl" class =" qrcode-placeholder" >
45- <el-button type =" primary" :loading =" qrcodeLoading" @click =" generateQRCode" >
46- 生成二维码
44+ <div v-if =" qrcodeLoading" class =" qrcode-placeholder" >
45+ <div class =" loading-spinner" ></div >
46+ <p >正在生成二维码...</p >
47+ </div >
48+ <div v-else-if =" !qrcodeUrl" class =" qrcode-placeholder" >
49+ <el-button type =" primary" @click =" generateQRCode" >
50+ 重新生成二维码
4751 </el-button >
4852 </div >
4953 <div v-else class =" qrcode-content" >
8589</template >
8690
8791<script setup>
88- import { ref , onUnmounted } from ' vue'
92+ import { ref , onUnmounted , nextTick , watch } from ' vue'
8993import { ElMessage , ElMessageBox } from ' element-plus'
9094import { userAPI } from ' @/api'
9195import QRCode from ' qrcode'
@@ -107,6 +111,31 @@ let pollingTimer = null
107111const cookieInput = ref (' ' )
108112const cookieLoginLoading = ref (false )
109113
114+ // 监听对话框打开,自动生成二维码
115+ watch (showLoginDialog, (newVal ) => {
116+ if (newVal && loginTab .value === ' qrcode' ) {
117+ // 对话框打开且在扫码登录标签,自动生成二维码
118+ nextTick (() => {
119+ if (! qrcodeUrl .value ) {
120+ generateQRCode ()
121+ }
122+ })
123+ } else if (! newVal) {
124+ // 对话框关闭,清理状态
125+ cancelLogin ()
126+ }
127+ })
128+
129+ // 监听标签切换
130+ watch (loginTab, (newVal ) => {
131+ if (newVal === ' qrcode' && showLoginDialog .value && ! qrcodeUrl .value ) {
132+ // 切换到扫码登录且二维码未生成,自动生成
133+ nextTick (() => {
134+ generateQRCode ()
135+ })
136+ }
137+ })
138+
110139const loadUsers = async () => {
111140 loading .value = true
112141 try {
@@ -142,11 +171,20 @@ const generateQRCode = async () => {
142171 authKey = data .key
143172 qrcodeUrl .value = data .image
144173
174+ // 等待 DOM 更新后再生成二维码
175+ await nextTick ()
176+
145177 // 生成二维码
146- await QRCode .toCanvas (qrcodeCanvas .value , data .image , {
147- width: 200 ,
148- margin: 2
149- })
178+ if (qrcodeCanvas .value ) {
179+ await QRCode .toCanvas (qrcodeCanvas .value , data .image , {
180+ width: 200 ,
181+ margin: 2
182+ })
183+ } else {
184+ ElMessage .error (' 二维码画布未准备好' )
185+ loginStatus .value = ' 二维码画布未准备好'
186+ return
187+ }
150188
151189 startPolling ()
152190 } catch (error) {
@@ -299,10 +337,26 @@ loadUsers()
299337
300338.qrcode-placeholder {
301339 display : flex ;
340+ flex-direction : column ;
302341 justify-content : center ;
303342 align-items : center ;
304343}
305344
345+ .loading-spinner {
346+ width : 40px ;
347+ height : 40px ;
348+ border : 4px solid #f3f3f3 ;
349+ border-top : 4px solid #409eff ;
350+ border-radius : 50% ;
351+ animation : spin 1s linear infinite ;
352+ margin-bottom : 10px ;
353+ }
354+
355+ @keyframes spin {
356+ 0% { transform : rotate (0deg ); }
357+ 100% { transform : rotate (360deg ); }
358+ }
359+
306360.qrcode-content {
307361 display : flex ;
308362 flex-direction : column ;
0 commit comments