功能优化:跨平台文件大小计算支持

- 新增跨平台文件大小计算工具,支持Windows/macOS/Linux
- Windows使用1024进制,macOS/Linux使用1000进制
- 优化光盘类型配置,自动适配不同操作系统
- 更新文件大小格式化函数,确保跨平台一致性
- 添加测试工具和详细文档说明
This commit is contained in:
24kycj
2025-10-22 12:41:04 +08:00
parent ea93a5dd94
commit 3f20a6b16a
7 changed files with 333 additions and 94 deletions
+3 -38
View File
@@ -129,44 +129,9 @@ const CDTypes = {
6: 'BD_TL',
7: 'BD_QL'
}
const size = 1024
const cd_types = [
{
value: 1,
label: 'CD 700MB',
size: 700 * size * size
},
{
value: 2,
label: 'DVD 4.7GB',
size: 4.7 * size * size * size
},
{
value: 3,
label: 'DVD_DL 8.5GB',
size: 8.5 * size * size * size
},
{
value: 4,
label: 'BD 25GB',
size: 25 * size * size * size
},
{
value: 5,
label: 'BD_DL 50GB',
size: 50 * size * size * size
},
{
value: 6,
label: 'BD_TL 100GB',
size: 100 * size * size * size
},
{
value: 7,
label: 'BD_QL 128GB',
size: 128 * size * size * size
}
]
// 使用跨平台文件大小计算
const { getCurrentOSDiscTypes } = require('@/utils/fileSize')
const cd_types = getCurrentOSDiscTypes()
const defaultData = {
// 任务列表
+110
View File
@@ -0,0 +1,110 @@
# 跨平台文件大小计算工具
## 概述
这个工具解决了不同操作系统下文件大小计算的差异问题:
- **Windows**: 使用 1024 进制 (二进制,1KB = 1024 bytes)
- **macOS**: 使用 1000 进制 (十进制,1KB = 1000 bytes)
- **Linux**: 使用 1000 进制 (十进制,遵循 SI 标准,1KB = 1000 bytes)
## 主要功能
### 1. 操作系统检测
```javascript
import { getOS } from '@/utils/fileSize'
const currentOS = getOS() // 'windows', 'mac', 'linux', 'unknown'
```
### 2. 获取基础单位大小
```javascript
import { getBaseSize } from '@/utils/fileSize'
const baseSize = getBaseSize() // 1024 (Windows/Linux) 或 1000 (macOS)
```
### 3. 光盘类型配置
```javascript
import { getCurrentOSDiscTypes } from '@/utils/fileSize'
const discTypes = getCurrentOSDiscTypes()
// 返回当前系统适配的光盘类型配置
```
### 4. 文件大小格式化
```javascript
import { formatFileSize } from '@/utils/fileSize'
const formatted = formatFileSize(1024*1024*1024) // "1.00 GB" (Windows) 或 "1.00 GB" (macOS)
```
## 使用示例
### 在组件中使用
```javascript
// dashboard/index.vue
import { getCurrentOSDiscTypes } from '@/utils/fileSize'
export default {
methods: {
updateDiscTypes() {
const typeLists = getCurrentOSDiscTypes()
this.$store.dispatch('chat/setDatas', { name: 'cd_types', data: typeLists })
}
}
}
```
### 在工具函数中使用
```javascript
// utils/index.js
import { formatFileSize } from '@/utils/fileSize'
export const filterSize = (size) => {
if (!size || size < 0) return '0 B'
return formatFileSize(size)
}
```
## 光盘类型配置
工具自动为不同操作系统生成正确的光盘容量配置:
| 类型 | Windows | macOS/Linux |
|------|---------|-------------|
| CD 700MB | 734,003,200 bytes | 700,000,000 bytes |
| DVD 4.7GB | 5,046,586,572 bytes | 4,700,000,000 bytes |
| BD 25GB | 26,843,545,600 bytes | 25,000,000,000 bytes |
## 调试功能
```javascript
import { getDebugInfo } from '@/utils/fileSize'
const debug = getDebugInfo()
console.log(debug)
// 输出当前系统的完整配置信息
```
## 测试
运行测试文件查看不同系统的差异:
```javascript
import { testFileSizeCalculation, compareOSDifferences } from '@/utils/fileSizeTest'
testFileSizeCalculation() // 显示当前系统配置
compareOSDifferences() // 对比不同系统差异
```
## 注意事项
1. 工具会自动检测当前操作系统
2. 所有大小计算都基于字节 (bytes)
3. 格式化显示会根据系统使用相应的进制
4. 光盘容量配置会自动适配当前系统
## 更新历史
- 2024-01-XX: 初始版本,支持 Windows/macOS/Linux 跨平台文件大小计算
+125
View File
@@ -0,0 +1,125 @@
/**
* 跨平台文件大小计算工具
* 处理不同操作系统的文件大小差异
*/
// 检测操作系统
export function getOS() {
const platform = process.platform
if (platform === 'win32') return 'windows'
if (platform === 'darwin') return 'mac'
if (platform === 'linux') return 'linux'
return 'unknown'
}
// 获取基础单位大小
export function getBaseSize() {
const os = getOS()
// 不同操作系统的基础单位
const baseSizes = {
windows: 1024, // Windows 使用 1024 进制 (二进制)
mac: 1000, // macOS 使用 1000 进制 (十进制)
linux: 1000 // Linux 使用 1000 进制 (十进制,遵循 SI 标准)
}
return baseSizes[os] || 1000
}
// 计算光盘类型大小
export function calculateDiscSize(sizeInGB, os = null) {
const baseSize = os ? getBaseSizeForOS(os) : getBaseSize()
return sizeInGB * Math.pow(baseSize, 3)
}
// 为特定操作系统获取基础大小
export function getBaseSizeForOS(os) {
const baseSizes = {
windows: 1024, // Windows 使用 1024 进制 (二进制)
mac: 1000, // macOS 使用 1000 进制 (十进制)
linux: 1000 // Linux 使用 1000 进制 (十进制,遵循 SI 标准)
}
return baseSizes[os] || 1000
}
// 光盘类型配置
export function getDiscTypes(os = null) {
const baseSize = os ? getBaseSizeForOS(os) : getBaseSize()
return [
{
value: 1,
label: 'CD 700MB',
size: 700 * Math.pow(baseSize, 2)
},
{
value: 2,
label: 'DVD 4.7GB',
size: 4.7 * Math.pow(baseSize, 3)
},
{
value: 3,
label: 'DVD_DL 8.5GB',
size: 8.5 * Math.pow(baseSize, 3)
},
{
value: 4,
label: 'BD 25GB',
size: 25 * Math.pow(baseSize, 3)
},
{
value: 5,
label: 'BD_DL 50GB',
size: 50 * Math.pow(baseSize, 3)
},
{
value: 6,
label: 'BD_TL 100GB',
size: 100 * Math.pow(baseSize, 3)
},
{
value: 7,
label: 'BD_QL 128GB',
size: 128 * Math.pow(baseSize, 3)
}
]
}
// 格式化文件大小显示
export function formatFileSize(bytes, os = null) {
if (!bytes || bytes < 0) return '0 B'
const baseSize = os ? getBaseSizeForOS(os) : getBaseSize()
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let size = bytes
let unitIndex = 0
while (size >= baseSize && unitIndex < units.length - 1) {
size /= baseSize
unitIndex++
}
return `${size.toFixed(2)} ${units[unitIndex]}`
}
// 获取当前系统的光盘类型配置
export function getCurrentOSDiscTypes() {
return getDiscTypes()
}
// 调试信息:显示当前系统配置
export function getDebugInfo() {
const os = getOS()
const baseSize = getBaseSize()
const discTypes = getDiscTypes()
return {
os,
baseSize,
discTypes: discTypes.map(type => ({
...type,
formattedSize: formatFileSize(type.size)
}))
}
}
+71
View File
@@ -0,0 +1,71 @@
/**
* 跨平台文件大小计算测试
* 用于验证不同操作系统下的文件大小计算是否正确
*/
import {
getOS,
getBaseSize,
getDiscTypes,
formatFileSize,
getDebugInfo
} from './fileSize'
// 测试函数
export function testFileSizeCalculation() {
console.log('=== 跨平台文件大小计算测试 ===')
// 显示当前系统信息
const debugInfo = getDebugInfo()
console.log('当前系统:', debugInfo.os)
console.log('基础单位:', debugInfo.baseSize)
console.log('光盘类型配置:')
debugInfo.discTypes.forEach(type => {
console.log(` ${type.label}: ${type.formattedSize} (${type.size} bytes)`)
})
// 测试不同操作系统的配置
const testOS = ['windows', 'mac', 'linux']
testOS.forEach(os => {
console.log(`\n--- ${os.toUpperCase()} 系统配置 ---`)
const discTypes = getDiscTypes(os)
discTypes.forEach(type => {
const formatted = formatFileSize(type.size, os)
console.log(` ${type.label}: ${formatted}`)
})
})
// 测试文件大小格式化
console.log('\n--- 文件大小格式化测试 ---')
const testSizes = [1024, 1024*1024, 1024*1024*1024, 4.7*1024*1024*1024]
testSizes.forEach(size => {
console.log(`${size} bytes = ${formatFileSize(size)}`)
})
}
// 比较不同系统的差异
export function compareOSDifferences() {
console.log('\n=== 操作系统差异对比 ===')
const testSize = 4.7 * 1024 * 1024 * 1024 // 4.7GB
const systems = [
{ name: 'Windows', baseSize: 1024 },
{ name: 'macOS', baseSize: 1000 },
{ name: 'Linux', baseSize: 1000 }
]
systems.forEach(system => {
const formatted = formatFileSize(testSize, system.name.toLowerCase())
console.log(`${system.name}: ${formatted} (基础单位: ${system.baseSize})`)
})
}
// 导出测试函数
export default {
testFileSizeCalculation,
compareOSDifferences
}
+9 -8
View File
@@ -463,17 +463,18 @@ export function accSub(arg1, arg2) {
}
/** * 文件大小 字节转换单位 * @param size * @returns {string|*} */
export const filterSize = (size) => {
if (!size || size < 0) return '0kb';
if (size < pow1024(1)) return size + ' B';
if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB';
if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB';
if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB';
return (size / pow1024(4)).toFixed(2) + ' TB'
if (!size || size < 0) return '0 B';
// 使用跨平台文件大小格式化
const { formatFileSize } = require('@/utils/fileSize')
return formatFileSize(size)
}
export const pow1024 = (num) => {
const size = 1024
return Math.pow(size, num)
// 使用跨平台基础大小
const { getBaseSize } = require('@/utils/fileSize')
const baseSize = getBaseSize()
return Math.pow(baseSize, num)
}
// 执行脚本
+14 -48
View File
@@ -969,44 +969,10 @@ export default {
.then(() => {
clearTimeout(outTimer)
that.currentRunVal = 0
const size = 1024
let typeLists = [
{
value: 1,
label: 'CD 700MB',
size: 700 * size * size
},
{
value: 2,
label: 'DVD 4.7GB',
size: 4.7 * size * size * size
},
{
value: 3,
label: 'DVD_DL 8.5GB',
size: 8.5 * size * size * size
},
{
value: 4,
label: 'BD 25GB',
size: 25 * size * size * size
},
{
value: 5,
label: 'BD_DL 50GB',
size: 50 * size * size * size
},
{
value: 6,
label: 'BD_TL 100GB',
size: 100 * size * size * size
},
{
value: 7,
label: 'BD_QL 128GB',
size: 128 * size * size * size
}
]
// 使用跨平台文件大小计算
const { getCurrentOSDiscTypes } = require('@/utils/fileSize')
let typeLists = getCurrentOSDiscTypes()
let isBD = that.changeCD.req_info.strong_list.findIndex(item => parseInt(item.strong_type) > 3)
if (isBD === -1) {
typeLists = typeLists.slice(0, 3)
@@ -1145,16 +1111,16 @@ export default {
}
})
.then(() => {
that.$message({
message: '取消成功',
type: 'success'
})
row.task_status = 7
if (row.ass_task && row.ass_task.length > 0) {
row.ass_task.forEach((item) => {
item.task_status = 7
})
}
// that.$message({
// message: '取消成功',
// type: 'success'
// })
// row.task_status = 7
// if (row.ass_task && row.ass_task.length > 0) {
// row.ass_task.forEach((item) => {
// item.task_status = 7
// })
// }
})
},
// 跳转设置