添加生成安全随机标识符功能以改进文件上传逻辑
This commit is contained in:
@@ -566,15 +566,42 @@ export default {
|
|||||||
this.serviceForm.practicalSolutionTime = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
|
this.serviceForm.practicalSolutionTime = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 使用密码学安全随机源生成标识符
|
||||||
|
generateSecureId(length = 16) {
|
||||||
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||||
|
const charsLength = chars.length;
|
||||||
|
const randomBytes = new Uint8Array(length);
|
||||||
|
|
||||||
|
// H5/现代浏览器
|
||||||
|
if (typeof globalThis !== 'undefined' && globalThis.crypto && typeof globalThis.crypto.getRandomValues === 'function') {
|
||||||
|
globalThis.crypto.getRandomValues(randomBytes);
|
||||||
|
}
|
||||||
|
// 微信小程序环境
|
||||||
|
else if (typeof wx !== 'undefined' && typeof wx.getRandomValues === 'function') {
|
||||||
|
wx.getRandomValues(randomBytes);
|
||||||
|
}
|
||||||
|
// 兜底:若运行环境不支持 CSPRNG,显式报错,避免继续使用可预测随机数
|
||||||
|
else {
|
||||||
|
throw new Error('当前环境不支持密码学安全随机数,请在服务端生成标识符');
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = '';
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
id += chars[randomBytes[i] % charsLength];
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
},
|
||||||
|
|
||||||
// 触发文件上传
|
// 触发文件上传
|
||||||
triggerFileUpload() {
|
triggerFileUpload() {
|
||||||
if (this.serviceForm.fileUrl.length < 6) {
|
if (this.serviceForm.fileUrl.length < 6) {
|
||||||
// 模拟添加文件
|
// 模拟添加文件
|
||||||
const mockFiles = ['文档.pdf', '图片.jpg', '表格.xlsx', '报告.docx'];
|
const mockFiles = ['文档.pdf', '图片.jpg', '表格.xlsx', '报告.docx'];
|
||||||
const randomFile = mockFiles[Math.floor(Math.random() * mockFiles.length)];
|
const secureId = this.generateSecureId(20);
|
||||||
|
const randomFile = mockFiles[Date.now() % mockFiles.length];
|
||||||
this.serviceForm.fileUrl.push({
|
this.serviceForm.fileUrl.push({
|
||||||
name: randomFile,
|
name: randomFile,
|
||||||
url: `mock-url/${Date.now()}/${randomFile}`
|
url: `mock-url/${secureId}/${randomFile}`
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({ title: '最多只能上传6个文件', icon: 'none' });
|
uni.showToast({ title: '最多只能上传6个文件', icon: 'none' });
|
||||||
|
|||||||
Reference in New Issue
Block a user