修复: 添加原密码验证UI并修复中文文件名乱码
## 问题1: 修改密码缺少原密码验证UI
后端已有current_password验证,但前端没有输入框:
- 用户无法输入当前密码
- 导致密码修改功能无法正常使用
修复内容(前端):
1. app.html: 添加当前密码输入框
2. app.js:
- 添加current_password字段到data
- 添加current_password必填验证
- 请求体中包含current_password
- 成功后清空current_password
## 问题2: 中文文件名上传后乱码
原因:
- multer默认将文件名从UTF-8转换为Latin1编码
- req.file.originalname获取到的是乱码
修复内容(后端):
1. 配置multer.diskStorage自定义文件名处理
2. 在filename回调中将Latin1转回UTF-8:
Buffer.from(file.originalname, 'latin1').toString('utf8')
3. 在上传路由中同样转换originalname
4. 临时文件名使用时间戳+随机数+原始文件名避免冲突
影响范围:
- 所有文件上传操作
- 中文、日文、韩文等非ASCII文件名
测试建议:
- 上传中文文件名文件(如测试文档.pdf)
- 上传emoji文件名
- 修改密码功能完整流程测试
This commit is contained in:
@@ -840,9 +840,11 @@ app.post('/api/upload', authMiddleware, upload.single('file'), async (req, res)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const remotePath = req.body.path || '/';
|
const remotePath = req.body.path || '/';
|
||||||
|
// 修复中文文件名:multer将UTF-8转为了Latin1,需要转回来
|
||||||
|
const originalFilename = Buffer.from(req.file.originalname, 'latin1').toString('utf8');
|
||||||
const remoteFilePath = remotePath === '/'
|
const remoteFilePath = remotePath === '/'
|
||||||
? `/${req.file.originalname}`
|
? `/${originalFilename}`
|
||||||
: `${remotePath}/${req.file.originalname}`;
|
: `${remotePath}/${originalFilename}`;
|
||||||
|
|
||||||
let storage;
|
let storage;
|
||||||
|
|
||||||
@@ -862,7 +864,7 @@ app.post('/api/upload', authMiddleware, upload.single('file'), async (req, res)
|
|||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: '文件上传成功',
|
message: '文件上传成功',
|
||||||
filename: req.file.originalname,
|
filename: originalFilename,
|
||||||
path: remoteFilePath
|
path: remoteFilePath
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1069,6 +1069,10 @@
|
|||||||
<!-- 所有用户都可以改密码 -->
|
<!-- 所有用户都可以改密码 -->
|
||||||
<form @submit.prevent="changePassword">
|
<form @submit.prevent="changePassword">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label">当前密码</label>
|
||||||
|
<input type="password" class="form-input" v-model="changePasswordForm.current_password" placeholder="输入当前密码" required>
|
||||||
|
</div>
|
||||||
<label class="form-label">新密码 (至少6字符)</label>
|
<label class="form-label">新密码 (至少6字符)</label>
|
||||||
<input type="password" class="form-input" v-model="changePasswordForm.new_password" placeholder="输入新密码" minlength="6" required>
|
<input type="password" class="form-input" v-model="changePasswordForm.new_password" placeholder="输入新密码" minlength="6" required>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ createApp({
|
|||||||
|
|
||||||
// 修改密码表单
|
// 修改密码表单
|
||||||
changePasswordForm: {
|
changePasswordForm: {
|
||||||
|
current_password: '',
|
||||||
new_password: ''
|
new_password: ''
|
||||||
},
|
},
|
||||||
// 用户名修改表单
|
// 用户名修改表单
|
||||||
@@ -407,6 +408,11 @@ handleDragLeave(e) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async changePassword() {
|
async changePassword() {
|
||||||
|
if (!this.changePasswordForm.current_password) {
|
||||||
|
alert('请输入当前密码');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.changePasswordForm.new_password.length < 6) {
|
if (this.changePasswordForm.new_password.length < 6) {
|
||||||
alert('新密码至少6个字符');
|
alert('新密码至少6个字符');
|
||||||
return;
|
return;
|
||||||
@@ -416,6 +422,7 @@ handleDragLeave(e) {
|
|||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${this.apiBase}/api/user/change-password`,
|
`${this.apiBase}/api/user/change-password`,
|
||||||
{
|
{
|
||||||
|
current_password: this.changePasswordForm.current_password,
|
||||||
new_password: this.changePasswordForm.new_password
|
new_password: this.changePasswordForm.new_password
|
||||||
},
|
},
|
||||||
{ headers: { Authorization: `Bearer ${this.token}` } }
|
{ headers: { Authorization: `Bearer ${this.token}` } }
|
||||||
@@ -424,6 +431,7 @@ handleDragLeave(e) {
|
|||||||
if (response.data.success) {
|
if (response.data.success) {
|
||||||
alert('密码修改成功!');
|
alert('密码修改成功!');
|
||||||
this.changePasswordForm.new_password = '';
|
this.changePasswordForm.new_password = '';
|
||||||
|
this.changePasswordForm.current_password = '';
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert('密码修改失败: ' + (error.response?.data?.message || error.message));
|
alert('密码修改失败: ' + (error.response?.data?.message || error.message));
|
||||||
|
|||||||
Reference in New Issue
Block a user