Commit 5ac52de1 authored by panyf's avatar panyf
Browse files

[CP]完善JWT鉴权,实现方案从多页面改为单页面,使用dialog进行登录操作

parent c897835e
<template> <template>
<div id="app" class="flex flex-col items-center justify-center h-screen"> <div id="app" class="flex flex-col items-center justify-center h-screen">
<div class="upload-container"> <div class="upload-container">
<el-upload <!-- 登录对话框 -->
ref="uploadRef" <el-dialog :visible.sync="loginDialogVisible" title="登录">
:action="uploadUrl" <template v-slot:default>
:auto-upload="false" <el-form @submit.prevent="handleLogin">
:on-change="handleFileChange" <el-form-item label="用户名">
:file-list="fileList" <el-input v-model="loginForm.username"></el-input>
accept=".cc" </el-form-item>
:multiple="false" <el-form-item label="密码">
> <el-input type="password" v-model="loginForm.password"></el-input>
<template #trigger> </el-form-item>
<el-button size="small" type="primary">选取文件</el-button> <!-- 修改:移除 native-type 属性,使用 @click 触发登录方法 -->
<el-button type="primary" @click="handleLogin">登录</el-button>
<el-alert
v-if="loginMessage"
:title="loginMessage"
:type="loginMessageType"
show-icon
class="mt-4"
></el-alert>
</el-form>
</template> </template>
</el-dialog>
<div class="button-container">
<el-button <el-button
style="margin-left: 10px;"
size="small" size="small"
type="success" type="info"
:disabled="isUploading" @click="loginDialogVisible = true"
@click="uploadFile"
> >
上传并编译 登录
</el-button> </el-button>
</el-upload> <el-upload
ref="uploadRef"
:action="uploadUrl"
:auto-upload="false"
:on-change="handleFileChange"
:file-list="fileList"
accept=".cc"
:multiple="false"
:headers="{'Authorization': `Bearer ${token}`}"
>
<template #trigger>
<el-button size="small" type="primary">选取文件</el-button>
</template>
<el-button
style="margin-left: 10px;"
size="small"
type="success"
:disabled="isUploading ||!token"
@click="uploadFile"
>
上传并编译
</el-button>
</el-upload>
</div>
<!-- 动态进度条 --> <!-- 动态进度条 -->
<el-progress <el-progress
...@@ -31,7 +63,7 @@ ...@@ -31,7 +63,7 @@
:status="progressStatus" :status="progressStatus"
class="mt-4" class="mt-4"
:stroke-width="10" :stroke-width="10"
:show-info="false" :show-info="false"
></el-progress> ></el-progress>
<!-- 成功提示 --> <!-- 成功提示 -->
...@@ -78,13 +110,21 @@ export default { ...@@ -78,13 +110,21 @@ export default {
downloadUrl: null, downloadUrl: null,
downloadFileName: 'radarcal', downloadFileName: 'radarcal',
errorMessage: '', errorMessage: '',
showErrorMessage: false, // 控制错误信息栏的显示 showErrorMessage: false,
isUploading: false, // 是否正在上传和编译 isUploading: false,
progressPercentage: 0, // 进度条百分比 progressPercentage: 0,
progressStatus: '', // 进度条状态(success/error) progressStatus: '',
isSuccess: false, // 是否编译成功 isSuccess: false,
isDownloadAvailable: false, // 是否显示下载按钮 isDownloadAvailable: false,
progressInterval: null // 用于存储进度条更新的定时器 progressInterval: null,
loginDialogVisible: false,
loginForm: {
username: '',
password: ''
},
token: null,
loginMessage: '',
loginMessageType: ''
}; };
}, },
methods: { methods: {
...@@ -111,11 +151,11 @@ export default { ...@@ -111,11 +151,11 @@ export default {
// 设置进度条定时器,100 秒内均匀增长到 100% // 设置进度条定时器,100 秒内均匀增长到 100%
this.progressInterval = setInterval(() => { this.progressInterval = setInterval(() => {
if (this.progressPercentage < 100) { if (this.progressPercentage < 100) {
this.progressPercentage += 1; // 每秒增加 1% this.progressPercentage += 1;
} else { } else {
clearInterval(this.progressInterval); // 停止定时器 clearInterval(this.progressInterval);
} }
}, 1000); // 每秒更新一次进度条 }, 1000);
try { try {
const formData = new FormData(); const formData = new FormData();
...@@ -123,33 +163,36 @@ export default { ...@@ -123,33 +163,36 @@ export default {
const response = await fetch(this.uploadUrl, { const response = await fetch(this.uploadUrl, {
method: 'POST', method: 'POST',
headers: {
'Authorization': `Bearer ${this.token}`
},
body: formData body: formData
}); });
clearInterval(this.progressInterval); // 停止进度条定时器 clearInterval(this.progressInterval);
if (response.ok) { if (response.ok) {
const blob = await response.blob(); const blob = await response.blob();
this.downloadUrl = window.URL.createObjectURL(blob); this.downloadUrl = window.URL.createObjectURL(blob);
this.isDownloadAvailable = true; // 显示下载按钮 this.isDownloadAvailable = true;
this.isSuccess = true; // 标记编译成功 this.isSuccess = true;
this.progressPercentage = 100; // 进度条完成 this.progressPercentage = 100;
this.progressStatus = 'success'; // 进度条状态为成功 this.progressStatus ='success';
this.$message.success('编译成功!'); // 提示编译成功 this.$message.success('编译成功!');
} else { } else {
const errorText = await response.text(); const errorText = await response.text();
this.errorMessage = errorText; this.errorMessage = errorText;
this.showErrorMessage = true; // 显示错误信息栏 this.showErrorMessage = true;
this.progressStatus = 'error'; // 进度条状态为失败 this.progressStatus = 'error';
} }
} catch (error) { } catch (error) {
clearInterval(this.progressInterval); // 停止进度条定时器 clearInterval(this.progressInterval);
console.error('发生错误:', error); console.error('发生错误:', error);
this.errorMessage = '发生错误,请重试'; this.errorMessage = '发生错误,请重试';
this.showErrorMessage = true; // 显示错误信息栏 this.showErrorMessage = true;
this.progressStatus = 'error'; // 进度条状态为失败 this.progressStatus = 'error';
} finally { } finally {
this.isUploading = false; // 隐藏进度条 this.isUploading = false;
} }
}, },
downloadFile() { downloadFile() {
...@@ -161,6 +204,39 @@ export default { ...@@ -161,6 +204,39 @@ export default {
link.click(); link.click();
document.body.removeChild(link); document.body.removeChild(link);
} }
},
async handleLogin() {
try {
console.log('登录信息:', this.loginForm);
const formData = new URLSearchParams();
formData.append('username', this.loginForm.username);
formData.append('password', this.loginForm.password);
const response = await fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formData
});
if (response.ok) {
const token = await response.text();
this.token = token;
this.loginMessage = '获取 token 成功';
this.$message.success('获取 token 成功');
this.loginMessageType ='success';
this.loginDialogVisible = false;
} else {
const errorText = await response.text();
this.loginMessage = errorText;
this.loginMessageType = 'error';
}
} catch (error) {
console.error('登录请求出错:', error);
this.loginMessage = '登录请求出错,请重试';
this.loginMessageType = 'error';
}
} }
} }
}; };
...@@ -172,4 +248,10 @@ export default { ...@@ -172,4 +248,10 @@ export default {
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
} }
.button-container {
display: flex;
flex-direction: row;
gap: 20px;
margin-bottom: 20px;
}
</style> </style>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment