mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-07-03 22:42:45 +08:00
数学计算添加进制转换
This commit is contained in:
parent
5e70c4c9a0
commit
dc7a2c9049
@ -1,198 +1,40 @@
|
|||||||
/**
|
/**
|
||||||
* 长度单位转换
|
* 进制转换
|
||||||
* @param {number} value 数值
|
* @param {string|number} value 要转换的值
|
||||||
* @param {string} fromUnit 源单位
|
* @param {string} fromBase 源进制
|
||||||
* @param {string} toUnit 目标单位
|
* @param {string} toBase 目标进制
|
||||||
* @returns {number} 转换结果
|
* @returns {string} 转换结果
|
||||||
*/
|
*/
|
||||||
function length(value, fromUnit, toUnit) {
|
function base(value, fromBase, toBase) {
|
||||||
const units = {
|
const bases = {
|
||||||
mm: 0.001, // 毫米
|
binary: 2,
|
||||||
cm: 0.01, // 厘米
|
octal: 8,
|
||||||
dm: 0.1, // 分米
|
decimal: 10,
|
||||||
m: 1, // 米(基准单位)
|
hex: 16,
|
||||||
km: 1000, // 千米
|
|
||||||
in: 0.0254, // 英寸
|
|
||||||
ft: 0.3048, // 英尺
|
|
||||||
yd: 0.9144, // 码
|
|
||||||
mi: 1609.344, // 英里
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!(fromUnit in units)) {
|
if (!(fromBase in bases)) {
|
||||||
throw new Error(`不支持的源单位: ${fromUnit}`);
|
throw new Error(`不支持的源进制: ${fromBase}`);
|
||||||
}
|
}
|
||||||
if (!(toUnit in units)) {
|
if (!(toBase in bases)) {
|
||||||
throw new Error(`不支持的目标单位: ${toUnit}`);
|
throw new Error(`不支持的目标进制: ${toBase}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 先转换为米,再转换为目标单位
|
// 先转为十进制
|
||||||
return (value * units[fromUnit]) / units[toUnit];
|
let decimal;
|
||||||
}
|
if (fromBase === "decimal") {
|
||||||
|
decimal = Number(value);
|
||||||
/**
|
} else {
|
||||||
* 重量单位转换
|
decimal = parseInt(String(value), bases[fromBase]);
|
||||||
* @param {number} value 数值
|
|
||||||
* @param {string} fromUnit 源单位
|
|
||||||
* @param {string} toUnit 目标单位
|
|
||||||
* @returns {number} 转换结果
|
|
||||||
*/
|
|
||||||
function weight(value, fromUnit, toUnit) {
|
|
||||||
const units = {
|
|
||||||
mg: 0.001, // 毫克
|
|
||||||
g: 1, // 克(基准单位)
|
|
||||||
kg: 1000, // 千克
|
|
||||||
t: 1000000, // 吨
|
|
||||||
oz: 28.3495, // 盎司
|
|
||||||
lb: 453.592, // 磅
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!(fromUnit in units)) {
|
|
||||||
throw new Error(`不支持的源单位: ${fromUnit}`);
|
|
||||||
}
|
|
||||||
if (!(toUnit in units)) {
|
|
||||||
throw new Error(`不支持的目标单位: ${toUnit}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 先转换为克,再转换为目标单位
|
// 再转为目标进制
|
||||||
return (value * units[fromUnit]) / units[toUnit];
|
if (toBase === "decimal") {
|
||||||
}
|
return String(decimal);
|
||||||
|
|
||||||
/**
|
|
||||||
* 面积单位转换
|
|
||||||
* @param {number} value 数值
|
|
||||||
* @param {string} fromUnit 源单位
|
|
||||||
* @param {string} toUnit 目标单位
|
|
||||||
* @returns {number} 转换结果
|
|
||||||
*/
|
|
||||||
function area(value, fromUnit, toUnit) {
|
|
||||||
const units = {
|
|
||||||
mm2: 0.000001, // 平方毫米
|
|
||||||
cm2: 0.0001, // 平方厘米
|
|
||||||
dm2: 0.01, // 平方分米
|
|
||||||
m2: 1, // 平方米(基准单位)
|
|
||||||
km2: 1000000, // 平方千米
|
|
||||||
ha: 10000, // 公顷
|
|
||||||
in2: 0.00064516, // 平方英寸
|
|
||||||
ft2: 0.092903, // 平方英尺
|
|
||||||
yd2: 0.836127, // 平方码
|
|
||||||
ac: 4046.86, // 英亩
|
|
||||||
mi2: 2589988.11, // 平方英里
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!(fromUnit in units)) {
|
|
||||||
throw new Error(`不支持的源单位: ${fromUnit}`);
|
|
||||||
}
|
}
|
||||||
if (!(toUnit in units)) {
|
return decimal.toString(bases[toBase]);
|
||||||
throw new Error(`不支持的目标单位: ${toUnit}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 先转换为平方米,再转换为目标单位
|
|
||||||
return (value * units[fromUnit]) / units[toUnit];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 体积单位转换
|
|
||||||
* @param {number} value 数值
|
|
||||||
* @param {string} fromUnit 源单位
|
|
||||||
* @param {string} toUnit 目标单位
|
|
||||||
* @returns {number} 转换结果
|
|
||||||
*/
|
|
||||||
function volume(value, fromUnit, toUnit) {
|
|
||||||
const units = {
|
|
||||||
ml: 0.001, // 毫升
|
|
||||||
l: 1, // 升(基准单位)
|
|
||||||
m3: 1000, // 立方米
|
|
||||||
cm3: 0.001, // 立方厘米
|
|
||||||
mm3: 0.000001, // 立方毫米
|
|
||||||
gal: 3.78541, // 加仑(美制)
|
|
||||||
qt: 0.946353, // 夸脱(美制)
|
|
||||||
pt: 0.473176, // 品脱(美制)
|
|
||||||
cup: 0.236588, // 杯(美制)
|
|
||||||
floz: 0.0295735, // 液量盎司(美制)
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!(fromUnit in units)) {
|
|
||||||
throw new Error(`不支持的源单位: ${fromUnit}`);
|
|
||||||
}
|
|
||||||
if (!(toUnit in units)) {
|
|
||||||
throw new Error(`不支持的目标单位: ${toUnit}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 先转换为升,再转换为目标单位
|
|
||||||
return (value * units[fromUnit]) / units[toUnit];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 温度单位转换
|
|
||||||
* @param {number} value 数值
|
|
||||||
* @param {string} fromUnit 源单位
|
|
||||||
* @param {string} toUnit 目标单位
|
|
||||||
* @returns {number} 转换结果
|
|
||||||
*/
|
|
||||||
function temperature(value, fromUnit, toUnit) {
|
|
||||||
const conversions = {
|
|
||||||
C: {
|
|
||||||
F: (v) => (v * 9) / 5 + 32,
|
|
||||||
K: (v) => v + 273.15,
|
|
||||||
C: (v) => v,
|
|
||||||
},
|
|
||||||
F: {
|
|
||||||
C: (v) => ((v - 32) * 5) / 9,
|
|
||||||
K: (v) => ((v - 32) * 5) / 9 + 273.15,
|
|
||||||
F: (v) => v,
|
|
||||||
},
|
|
||||||
K: {
|
|
||||||
C: (v) => v - 273.15,
|
|
||||||
F: (v) => ((v - 273.15) * 9) / 5 + 32,
|
|
||||||
K: (v) => v,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!(fromUnit in conversions)) {
|
|
||||||
throw new Error(`不支持的源单位: ${fromUnit}`);
|
|
||||||
}
|
|
||||||
if (!(toUnit in conversions[fromUnit])) {
|
|
||||||
throw new Error(`不支持的目标单位: ${toUnit}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return conversions[fromUnit][toUnit](value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 时间单位转换
|
|
||||||
* @param {number} value 数值
|
|
||||||
* @param {string} fromUnit 源单位
|
|
||||||
* @param {string} toUnit 目标单位
|
|
||||||
* @returns {number} 转换结果
|
|
||||||
*/
|
|
||||||
function time(value, fromUnit, toUnit) {
|
|
||||||
const units = {
|
|
||||||
ms: 0.001, // 毫秒
|
|
||||||
s: 1, // 秒(基准单位)
|
|
||||||
min: 60, // 分钟
|
|
||||||
h: 3600, // 小时
|
|
||||||
d: 86400, // 天
|
|
||||||
w: 604800, // 周
|
|
||||||
mo: 2592000, // 月(按30天计算)
|
|
||||||
y: 31536000, // 年(按365天计算)
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!(fromUnit in units)) {
|
|
||||||
throw new Error(`不支持的源单位: ${fromUnit}`);
|
|
||||||
}
|
|
||||||
if (!(toUnit in units)) {
|
|
||||||
throw new Error(`不支持的目标单位: ${toUnit}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 先转换为秒,再转换为目标单位
|
|
||||||
return (value * units[fromUnit]) / units[toUnit];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
length,
|
base,
|
||||||
weight,
|
|
||||||
area,
|
|
||||||
volume,
|
|
||||||
temperature,
|
|
||||||
time,
|
|
||||||
};
|
};
|
||||||
|
@ -10,6 +10,6 @@ module.exports = {
|
|||||||
statistics,
|
statistics,
|
||||||
geometry,
|
geometry,
|
||||||
trigonometry,
|
trigonometry,
|
||||||
conversion,
|
|
||||||
random,
|
random,
|
||||||
|
conversion,
|
||||||
};
|
};
|
||||||
|
@ -148,6 +148,8 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
runCommand() {
|
runCommand() {
|
||||||
|
if (!this.localCommand.code)
|
||||||
|
return quickcommand.showMessageBox("请检查参数是否正确", "info");
|
||||||
// 创建一个带临时变量的命令副本
|
// 创建一个带临时变量的命令副本
|
||||||
const tempCommand = {
|
const tempCommand = {
|
||||||
...this.localCommand,
|
...this.localCommand,
|
||||||
|
@ -1,21 +1,32 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="button-group">
|
<component
|
||||||
<div
|
:is="!!label ? 'BorderLabel' : 'div'"
|
||||||
v-for="opt in options"
|
:label="label"
|
||||||
:key="opt.value"
|
:icon="icon"
|
||||||
:class="['button-item', { active: modelValue === opt.value }]"
|
:model-value="isCollapse"
|
||||||
@click="$emit('update:modelValue', opt.value)"
|
>
|
||||||
>
|
<div class="button-group">
|
||||||
{{ opt.label }}
|
<div
|
||||||
|
v-for="opt in options"
|
||||||
|
:key="opt.value"
|
||||||
|
:class="['button-item', { active: modelValue === opt.value }]"
|
||||||
|
@click="$emit('update:modelValue', opt.value)"
|
||||||
|
>
|
||||||
|
{{ opt.label }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</component>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { defineComponent } from "vue";
|
import { defineComponent } from "vue";
|
||||||
|
import BorderLabel from "./BorderLabel.vue";
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: "ButtonGroup",
|
name: "ButtonGroup",
|
||||||
|
components: {
|
||||||
|
BorderLabel,
|
||||||
|
},
|
||||||
props: {
|
props: {
|
||||||
modelValue: {
|
modelValue: {
|
||||||
required: true,
|
required: true,
|
||||||
@ -24,6 +35,18 @@ export default defineComponent({
|
|||||||
type: Array,
|
type: Array,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
label: {
|
||||||
|
type: String,
|
||||||
|
default: "",
|
||||||
|
},
|
||||||
|
icon: {
|
||||||
|
type: String,
|
||||||
|
default: "",
|
||||||
|
},
|
||||||
|
isCollapse: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
emits: ["update:modelValue"],
|
emits: ["update:modelValue"],
|
||||||
});
|
});
|
||||||
|
@ -314,22 +314,23 @@ export const mathCommands = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: "quickcomposer.math.conversion.decimal",
|
value: "quickcomposer.math.conversion.base",
|
||||||
label: "单位换算",
|
label: "进制转换",
|
||||||
desc: "数学单位换算",
|
desc: "数字进制转换",
|
||||||
icon: "swap_horiz",
|
icon: "swap_horiz",
|
||||||
config: [
|
config: [
|
||||||
{
|
{
|
||||||
key: "value",
|
key: "value",
|
||||||
label: "数值",
|
label: "数值",
|
||||||
component: "NumberInput",
|
component: "VariableInput",
|
||||||
icon: "pin",
|
icon: "pin",
|
||||||
width: 12,
|
width: 12,
|
||||||
|
defaultValue: newVarInputVal("var", ""),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "from",
|
key: "from",
|
||||||
label: "从",
|
label: "从",
|
||||||
component: "q-select",
|
component: "ButtonGroup",
|
||||||
icon: "input",
|
icon: "input",
|
||||||
width: 6,
|
width: 6,
|
||||||
options: [
|
options: [
|
||||||
@ -337,14 +338,13 @@ export const mathCommands = {
|
|||||||
{ label: "二进制", value: "binary" },
|
{ label: "二进制", value: "binary" },
|
||||||
{ label: "八进制", value: "octal" },
|
{ label: "八进制", value: "octal" },
|
||||||
{ label: "十六进制", value: "hex" },
|
{ label: "十六进制", value: "hex" },
|
||||||
{ label: "度", value: "degrees" },
|
|
||||||
{ label: "弧度", value: "radians" },
|
|
||||||
],
|
],
|
||||||
|
defaultValue: "decimal",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "to",
|
key: "to",
|
||||||
label: "到",
|
label: "到",
|
||||||
component: "q-select",
|
component: "ButtonGroup",
|
||||||
icon: "output",
|
icon: "output",
|
||||||
width: 6,
|
width: 6,
|
||||||
options: [
|
options: [
|
||||||
@ -352,9 +352,8 @@ export const mathCommands = {
|
|||||||
{ label: "二进制", value: "binary" },
|
{ label: "二进制", value: "binary" },
|
||||||
{ label: "八进制", value: "octal" },
|
{ label: "八进制", value: "octal" },
|
||||||
{ label: "十六进制", value: "hex" },
|
{ label: "十六进制", value: "hex" },
|
||||||
{ label: "度", value: "degrees" },
|
|
||||||
{ label: "弧度", value: "radians" },
|
|
||||||
],
|
],
|
||||||
|
defaultValue: "hex",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user