feat: 删除不再使用的格式化数字工具函数,优化 SKU 列表组件的逻辑和结构

pull/171/head
吃货 2025-07-07 07:38:43 +08:00
parent 4fafb39efc
commit 3326c25a0d
3 changed files with 83 additions and 133 deletions

View File

@ -1,38 +0,0 @@
/**
*
* @param num
*/
export const formatToFraction = (num: number | string | undefined): string => {
if (typeof num === 'undefined') return '0.00';
const parsedNumber = typeof num === 'string' ? parseFloat(num) : num;
return (parsedNumber / 100.0).toFixed(2);
};
/**
* 1.00
* 使
*
* @param num
*/
// TODO @芋艿:看看怎么融合掉
export const floatToFixed2 = (num: number | string | undefined): string => {
let str = '0.00';
if (typeof num === 'undefined') {
return str;
}
const f = formatToFraction(num);
const decimalPart = f.toString().split('.')[1];
const len = decimalPart ? decimalPart.length : 0;
switch (len) {
case 0:
str = f.toString() + '.00';
break;
case 1:
str = f.toString() + '0';
break;
case 2:
str = f.toString();
break;
}
return str;
};

View File

@ -1,7 +1,7 @@
<template>
<!-- 情况一添加/修改 -->
<el-table
v-if="!isDetail && !isActivityComponent"
v-if="!isActivityComponent"
:data="isBatch ? skuList : formData!.skus!"
border
class="tabNumWidth"
@ -164,89 +164,7 @@
</el-table-column>
</el-table>
<!-- 情况二详情 -->
<el-table
v-if="isDetail"
ref="activitySkuListRef"
:data="formData!.skus!"
border
max-height="500"
size="small"
style="width: 99%"
@selection-change="handleSelectionChange"
>
<el-table-column v-if="isComponent" type="selection" width="45" />
<el-table-column align="center" label="图片" min-width="80">
<template #default="{ row }">
<el-image v-if="row.picUrl" :src="row.picUrl" class="h-50px w-50px" />
</template>
</el-table-column>
<template v-if="formData!.specType && !isBatch">
<!-- 根据商品属性动态添加 -->
<el-table-column
v-for="(item, index) in tableHeaders"
:key="index"
:label="item.label"
align="center"
min-width="80"
>
<template #default="{ row }">
<span style="font-weight: bold; color: #40aaff">
{{ row.properties?.[index]?.valueName }}
</span>
</template>
</el-table-column>
</template>
<el-table-column align="center" label="商品条码" min-width="100">
<template #default="{ row }">
{{ row.barCode }}
</template>
</el-table-column>
<el-table-column align="center" label="销售价(元)" min-width="80">
<template #default="{ row }">
{{ row.price }}
</template>
</el-table-column>
<el-table-column align="center" label="市场价(元)" min-width="80">
<template #default="{ row }">
{{ row.marketPrice }}
</template>
</el-table-column>
<el-table-column align="center" label="成本价(元)" min-width="80">
<template #default="{ row }">
{{ row.costPrice }}
</template>
</el-table-column>
<el-table-column align="center" label="库存" min-width="80">
<template #default="{ row }">
{{ row.stock }}
</template>
</el-table-column>
<el-table-column align="center" label="重量(kg)" min-width="80">
<template #default="{ row }">
{{ row.weight }}
</template>
</el-table-column>
<el-table-column align="center" label="体积(m^3)" min-width="80">
<template #default="{ row }">
{{ row.volume }}
</template>
</el-table-column>
<template v-if="formData!.subCommissionType">
<el-table-column align="center" label="一级返佣(元)" min-width="80">
<template #default="{ row }">
{{ row.firstBrokeragePrice }}
</template>
</el-table-column>
<el-table-column align="center" label="二级返佣(元)" min-width="80">
<template #default="{ row }">
{{ row.secondBrokeragePrice }}
</template>
</el-table-column>
</template>
</el-table>
<!-- 情况三作为活动组件 -->
<!-- 情况二作为活动组件 -->
<el-table
v-if="isActivityComponent"
:data="formData!.skus!"
@ -307,7 +225,8 @@
</el-table>
</template>
<script lang="ts" setup>
import { copyValueToTarget, formatToFraction } from '#/utils';
import { copyValueToTarget } from '#/utils';
import { formatToFraction } from '@vben/utils';
import type { PropertyAndValues, RuleConfig } from './model';
import UploadImg from '#/components/upload/image-upload.vue';
import { ElTable, ElInput, ElMessage } from 'element-plus';
@ -335,10 +254,6 @@ const props = defineProps({
type: Boolean,
default: false,
}, //
isDetail: {
type: Boolean,
default: false,
}, // sku
isComponent: {
type: Boolean,
default: false,

View File

@ -1,10 +1,12 @@
<script lang="ts" setup>
import { Page } from '@vben/common-ui';
import { onMounted, ref } from 'vue';
import { onMounted, ref, unref } from 'vue';
import { cloneDeep } from '@vben/utils';
import type { MallSpuApi } from '#/api/mall/product/spu';
import { useRouter, useRoute } from 'vue-router';
import { floatToFixed2, formatToFraction } from '#/utils';
import { floatToFixed2, formatToFraction, convertToInteger } from '@vben/utils';
import * as ProductSpuApi from '#/api/mall/product/spu';
import { ElMessage } from 'element-plus';
import InfoForm from '../components/info-form.vue';
import DeliveryForm from '../components/delivery-form.vue';
@ -50,7 +52,7 @@ const formData = ref<MallSpuApi.Spu>({
const formLoading = ref(false); // 12
const isDetail = ref(false); //
const { push, currentRoute } = useRouter(); //
const { push } = useRouter(); //
const { params, name } = useRoute(); //
/** 获得详情 */
const getDetail = async () => {
@ -87,6 +89,59 @@ const getDetail = async () => {
}
};
/** 提交按钮 */
const infoRef = ref<InstanceType<typeof InfoForm>>();
const skuRef = ref<InstanceType<typeof SkuForm>>();
const deliveryRef = ref<InstanceType<typeof DeliveryForm>>();
const descriptionRef = ref<InstanceType<typeof DescriptionForm>>();
const otherRef = ref<InstanceType<typeof OtherForm>>();
const submitForm = async () => {
//
formLoading.value = true;
try {
//
await unref(infoRef)?.validate();
await unref(skuRef)?.validate();
await unref(deliveryRef)?.validate();
await unref(descriptionRef)?.validate();
await unref(otherRef)?.validate();
// , server
const deepCopyFormData = cloneDeep(unref(formData.value)) as MallSpuApi.Spu;
deepCopyFormData.skus!.forEach((item) => {
// sku name
item.name = deepCopyFormData.name;
// sku
item.price = convertToInteger(item.price);
item.marketPrice = convertToInteger(item.marketPrice);
item.costPrice = convertToInteger(item.costPrice);
item.firstBrokeragePrice = convertToInteger(item.firstBrokeragePrice);
item.secondBrokeragePrice = convertToInteger(item.secondBrokeragePrice);
});
//
const newSliderPicUrls: any[] = [];
deepCopyFormData.sliderPicUrls!.forEach((item: any) => {
//
typeof item === 'object'
? newSliderPicUrls.push(item.url)
: newSliderPicUrls.push(item);
});
deepCopyFormData.sliderPicUrls = newSliderPicUrls;
//
const data = deepCopyFormData as MallSpuApi.Spu;
const id = params.id as unknown as number;
if (!id) {
await ProductSpuApi.createSpu(data);
ElMessage.success('创建成功');
} else {
await ProductSpuApi.updateSpu(data);
ElMessage.success('更新成功');
}
close();
} finally {
formLoading.value = false;
}
};
/** 关闭按钮 */
const close = () => {
push({ name: 'ProductSpu' });
@ -102,26 +157,44 @@ onMounted(async () => {
<Page :auto-content-height="true">
<ElTabs v-model="activeTab">
<ElTabPane label="基础设置" name="info">
<InfoForm :propFormData="formData" v-model:activeName="activeName" />
<InfoForm
:propFormData="formData"
v-model:activeName="activeName"
ref="infoRef"
/>
</ElTabPane>
<ElTabPane label="价格库存" name="sku">
<SkuForm :propFormData="formData" v-model:activeName="activeName" />
<SkuForm
:propFormData="formData"
v-model:activeName="activeName"
ref="skuRef"
/>
</ElTabPane>
<ElTabPane label="物流设置" name="delivery">
<DeliveryForm
:propFormData="formData"
v-model:activeName="activeName"
ref="deliveryRef"
/>
</ElTabPane>
<ElTabPane label="商品详情" name="description">
<DescriptionForm
:propFormData="formData"
v-model:activeName="activeName"
ref="descriptionRef"
/>
</ElTabPane>
<ElTabPane label="其它设置" name="other">
<OtherForm :propFormData="formData" v-model:activeName="activeName" />
<OtherForm
:propFormData="formData"
v-model:activeName="activeName"
ref="otherRef"
/>
</ElTabPane>
</ElTabs>
<ElButton type="primary" :loading="formLoading" @click="submitForm"
>保存</ElButton
>
<ElButton @click="close"></ElButton>
</Page>
</template>