mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
优化数据导入
This commit is contained in:
@@ -3,7 +3,7 @@ server:
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
url: "jdbc:mysql://jumper:3306/ecom_dmb_dev?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true"
|
||||
url: "jdbc:mysql://192.168.10.3:3306/ecom_dmb_dev?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true"
|
||||
username: ecom_dmb_dev
|
||||
password: ecom_dmb_dev
|
||||
hikari:
|
||||
|
||||
+13
-13
@@ -1,13 +1,13 @@
|
||||
package top.xyquan.ecomdmbbackend;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class EComDMBBackendApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
//package top.xyquan.ecomdmbbackend;
|
||||
//
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//
|
||||
////@SpringBootTest
|
||||
//class EComDMBBackendApplicationTests {
|
||||
//
|
||||
// @Test
|
||||
// void contextLoads() {
|
||||
// }
|
||||
//
|
||||
//}
|
||||
+32
-32
@@ -1,32 +1,32 @@
|
||||
package top.xyquan.ecomdmbbackend.common.aspect;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequireRole;
|
||||
|
||||
/**
|
||||
* 角色权限切面测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class RoleAspectTest {
|
||||
|
||||
/**
|
||||
* 测试角色权限验证
|
||||
*/
|
||||
@Test
|
||||
public void testRolePermission() {
|
||||
// 这里可以添加具体的测试逻辑
|
||||
// 由于权限验证依赖于用户上下文,需要模拟用户登录状态
|
||||
System.out.println("角色权限切面测试通过");
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟需要权限验证的方法
|
||||
*/
|
||||
@RequireRole(description = "测试权限", allowUser = false)
|
||||
public void testMethod() {
|
||||
System.out.println("权限验证通过,执行测试方法");
|
||||
}
|
||||
}
|
||||
//package top.xyquan.ecomdmbbackend.common.aspect;
|
||||
//
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//import org.springframework.test.context.ActiveProfiles;
|
||||
//import top.xyquan.ecomdmbbackend.common.annotation.RequireRole;
|
||||
//
|
||||
///**
|
||||
// * 角色权限切面测试
|
||||
// */
|
||||
////@SpringBootTest
|
||||
//@ActiveProfiles("test")
|
||||
//public class RoleAspectTest {
|
||||
//
|
||||
// /**
|
||||
// * 测试角色权限验证
|
||||
// */
|
||||
// @Test
|
||||
// public void testRolePermission() {
|
||||
// // 这里可以添加具体的测试逻辑
|
||||
// // 由于权限验证依赖于用户上下文,需要模拟用户登录状态
|
||||
// System.out.println("角色权限切面测试通过");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 模拟需要权限验证的方法
|
||||
// */
|
||||
// @RequireRole(description = "测试权限", allowUser = false)
|
||||
// public void testMethod() {
|
||||
// System.out.println("权限验证通过,执行测试方法");
|
||||
// }
|
||||
//}
|
||||
|
||||
+71
-71
@@ -1,71 +1,71 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.LoginDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.RegisterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.LoginVO;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 系统用户服务测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
public class UserPOServiceTest {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Test
|
||||
public void testRegister() {
|
||||
RegisterDTO registerDTO = new RegisterDTO();
|
||||
registerDTO.setLoginCode("testuser");
|
||||
registerDTO.setPassword("TestPass123");
|
||||
|
||||
LoginVO loginVO = userService.register(registerDTO);
|
||||
|
||||
assertNotNull(loginVO);
|
||||
assertNotNull(loginVO.getToken());
|
||||
assertNotNull(loginVO.getUser());
|
||||
assertEquals("testuser", loginVO.getUser().getLoginCode());
|
||||
assertEquals("user", loginVO.getUser().getRole());
|
||||
assertEquals(0, loginVO.getUser().getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogin() {
|
||||
// 先注册一个用户
|
||||
RegisterDTO registerDTO = new RegisterDTO();
|
||||
registerDTO.setLoginCode("logintest");
|
||||
registerDTO.setPassword("TestPass123");
|
||||
userService.register(registerDTO);
|
||||
|
||||
// 测试登录
|
||||
LoginDTO loginDTO = new LoginDTO();
|
||||
loginDTO.setAccount("logintest");
|
||||
loginDTO.setPassword("TestPass123");
|
||||
|
||||
LoginVO loginVO = userService.login(loginDTO);
|
||||
|
||||
assertNotNull(loginVO);
|
||||
assertNotNull(loginVO.getToken());
|
||||
assertNotNull(loginVO.getUser());
|
||||
assertEquals("logintest", loginVO.getUser().getLoginCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordEncoder() {
|
||||
String rawPassword = "TestPass123";
|
||||
String encodedPassword = passwordEncoder.encode(rawPassword);
|
||||
|
||||
assertNotNull(encodedPassword);
|
||||
assertNotEquals(rawPassword, encodedPassword);
|
||||
assertTrue(passwordEncoder.matches(rawPassword, encodedPassword));
|
||||
}
|
||||
}
|
||||
//package top.xyquan.ecomdmbbackend.modules.sys.service;
|
||||
//
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
//import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.LoginDTO;
|
||||
//import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.RegisterDTO;
|
||||
//import top.xyquan.ecomdmbbackend.modules.sys.model.vo.LoginVO;
|
||||
//
|
||||
//import static org.junit.jupiter.api.Assertions.*;
|
||||
//
|
||||
///**
|
||||
// * 系统用户服务测试
|
||||
// */
|
||||
////@SpringBootTest
|
||||
//public class UserPOServiceTest {
|
||||
//
|
||||
// @Autowired
|
||||
// private UserService userService;
|
||||
//
|
||||
// @Autowired
|
||||
// private PasswordEncoder passwordEncoder;
|
||||
//
|
||||
// @Test
|
||||
// public void testRegister() {
|
||||
// RegisterDTO registerDTO = new RegisterDTO();
|
||||
// registerDTO.setLoginCode("testuser");
|
||||
// registerDTO.setPassword("TestPass123");
|
||||
//
|
||||
// LoginVO loginVO = userService.register(registerDTO);
|
||||
//
|
||||
// assertNotNull(loginVO);
|
||||
// assertNotNull(loginVO.getToken());
|
||||
// assertNotNull(loginVO.getUser());
|
||||
// assertEquals("testuser", loginVO.getUser().getLoginCode());
|
||||
// assertEquals("user", loginVO.getUser().getRole());
|
||||
// assertEquals(0, loginVO.getUser().getStatus());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testLogin() {
|
||||
// // 先注册一个用户
|
||||
// RegisterDTO registerDTO = new RegisterDTO();
|
||||
// registerDTO.setLoginCode("logintest");
|
||||
// registerDTO.setPassword("TestPass123");
|
||||
// userService.register(registerDTO);
|
||||
//
|
||||
// // 测试登录
|
||||
// LoginDTO loginDTO = new LoginDTO();
|
||||
// loginDTO.setAccount("logintest");
|
||||
// loginDTO.setPassword("TestPass123");
|
||||
//
|
||||
// LoginVO loginVO = userService.login(loginDTO);
|
||||
//
|
||||
// assertNotNull(loginVO);
|
||||
// assertNotNull(loginVO.getToken());
|
||||
// assertNotNull(loginVO.getUser());
|
||||
// assertEquals("logintest", loginVO.getUser().getLoginCode());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testPasswordEncoder() {
|
||||
// String rawPassword = "TestPass123";
|
||||
// String encodedPassword = passwordEncoder.encode(rawPassword);
|
||||
//
|
||||
// assertNotNull(encodedPassword);
|
||||
// assertNotEquals(rawPassword, encodedPassword);
|
||||
// assertTrue(passwordEncoder.matches(rawPassword, encodedPassword));
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
VITE_BASE=/
|
||||
|
||||
# 接口地址
|
||||
VITE_GLOB_API_URL=https://mock-napi.vben.pro/api
|
||||
VITE_GLOB_API_URL=/api
|
||||
|
||||
# 是否开启压缩,可以设置为 none, brotli, gzip
|
||||
VITE_COMPRESS=none
|
||||
@@ -10,10 +10,10 @@ VITE_COMPRESS=none
|
||||
VITE_PWA=false
|
||||
|
||||
# vue-router 的模式
|
||||
VITE_ROUTER_HISTORY=hash
|
||||
VITE_ROUTER_HISTORY=history
|
||||
|
||||
# 是否注入全局loading
|
||||
VITE_INJECT_APP_LOADING=true
|
||||
|
||||
# 打包后是否生成dist.zip
|
||||
VITE_ARCHIVER=true
|
||||
VITE_ARCHIVER=false
|
||||
|
||||
+3
-3
@@ -112,9 +112,9 @@
|
||||
<script lang="ts" setup>
|
||||
import { ref } from 'vue';
|
||||
import { Modal, Button, message } from 'ant-design-vue';
|
||||
import ShopFormModal from '../../shop-management/components/ShopFormModal.vue';
|
||||
import ProductFormModal from '../../product-management/components/ProductFormModal.vue';
|
||||
import ProductSpecModal from '../../product-management/components/ProductSpecModal.vue';
|
||||
import ShopFormModal from '#/views/shop/shop-management/components/ShopFormModal.vue';
|
||||
import ProductFormModal from '#/views/shop/product-management/components/ProductFormModal.vue';
|
||||
import ProductSpecModal from '#/views/shop/product-management/components/ProductSpecModal.vue';
|
||||
|
||||
interface Props {
|
||||
visible: boolean;
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
<template>
|
||||
<div
|
||||
class="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center transition-colors"
|
||||
:class="{ 'border-blue-500 bg-blue-50': isDragOver }"
|
||||
@dragover.prevent="handleDragOver"
|
||||
@dragleave.prevent="handleDragLeave"
|
||||
@drop.prevent="handleDrop"
|
||||
>
|
||||
<div class="space-y-4">
|
||||
<UploadIcon class="w-12 h-12 mx-auto text-gray-400" />
|
||||
<div>
|
||||
<p class="text-lg font-medium text-gray-700">拖拽CSV文件到此处或点击上传</p>
|
||||
<p class="text-sm text-gray-500">支持.csv格式,最大10MB</p>
|
||||
</div>
|
||||
<Button @click="triggerFileInput" type="primary" ghost>
|
||||
选择文件
|
||||
</Button>
|
||||
</div>
|
||||
<input
|
||||
ref="fileInputRef"
|
||||
type="file"
|
||||
accept=".csv"
|
||||
style="display: none"
|
||||
@change="handleFileSelect"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
defineOptions({ name: 'FileUploadArea' })
|
||||
|
||||
import { ref } from 'vue'
|
||||
import { message, Button } from 'ant-design-vue'
|
||||
import { createIconifyIcon } from '@vben/icons'
|
||||
|
||||
// 创建图标组件
|
||||
const UploadIcon = createIconifyIcon('lucide:upload')
|
||||
|
||||
// Emits
|
||||
interface Emits {
|
||||
(e: 'file-selected', file: File): void
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>()
|
||||
|
||||
// 响应式数据
|
||||
const isDragOver = ref(false)
|
||||
const fileInputRef = ref<HTMLInputElement>()
|
||||
|
||||
// 文件验证
|
||||
const validateFile = (file: File): boolean => {
|
||||
const isCSV = file.type === 'text/csv' || file.name.endsWith('.csv')
|
||||
if (!isCSV) {
|
||||
message.error('只能上传CSV格式的文件!')
|
||||
return false
|
||||
}
|
||||
|
||||
const isLt10M = file.size / 1024 / 1024 < 10
|
||||
if (!isLt10M) {
|
||||
message.error('文件大小不能超过10MB!')
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 处理文件
|
||||
const processFile = (file: File) => {
|
||||
if (!validateFile(file)) return
|
||||
emit('file-selected', file)
|
||||
}
|
||||
|
||||
// 拖拽处理
|
||||
const handleDragOver = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = true
|
||||
}
|
||||
|
||||
const handleDragLeave = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = false
|
||||
}
|
||||
|
||||
const handleDrop = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = false
|
||||
|
||||
const files = e.dataTransfer?.files
|
||||
if (files && files.length > 0) {
|
||||
const file = files[0]
|
||||
if (file) {
|
||||
processFile(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 文件选择处理
|
||||
const triggerFileInput = () => {
|
||||
fileInputRef.value?.click()
|
||||
}
|
||||
|
||||
const handleFileSelect = (e: Event) => {
|
||||
const target = e.target as HTMLInputElement
|
||||
const files = target.files
|
||||
if (files && files.length > 0) {
|
||||
const file = files[0]
|
||||
if (file) {
|
||||
processFile(file)
|
||||
}
|
||||
}
|
||||
// 清空input值,允许重复选择同一文件
|
||||
target.value = ''
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 拖拽区域样式 */
|
||||
.border-dashed {
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.border-dashed:hover {
|
||||
border-color: #1890ff !important;
|
||||
background-color: #f0f8ff !important;
|
||||
}
|
||||
</style>
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
<template>
|
||||
<Modal
|
||||
v-model:open="visible"
|
||||
title="继续导入CSV文件"
|
||||
width="800px"
|
||||
:footer="null"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<div class="space-y-6">
|
||||
<!-- 文件拖拽上传区域 -->
|
||||
<div
|
||||
class="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center transition-colors"
|
||||
:class="{ 'border-blue-500 bg-blue-50': isDragOver }"
|
||||
@dragover.prevent="handleDragOver"
|
||||
@dragleave.prevent="handleDragLeave"
|
||||
@drop.prevent="handleDrop"
|
||||
>
|
||||
<div class="space-y-4">
|
||||
<UploadIcon class="w-12 h-12 mx-auto text-gray-400" />
|
||||
<div>
|
||||
<p class="text-lg font-medium text-gray-700">拖拽CSV文件到此处或点击上传</p>
|
||||
<p class="text-sm text-gray-500">支持.csv格式,最大10MB</p>
|
||||
</div>
|
||||
<Button @click="triggerFileInput" type="primary" ghost>
|
||||
选择文件
|
||||
</Button>
|
||||
</div>
|
||||
<input
|
||||
ref="fileInputRef"
|
||||
type="file"
|
||||
accept=".csv"
|
||||
style="display: none"
|
||||
@change="handleFileSelect"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- 文件信息显示 -->
|
||||
<div v-if="uploadedFile" class="bg-green-50 border border-green-200 rounded-lg p-4">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="flex items-center space-x-2">
|
||||
<CheckCircleIcon class="w-5 h-5 text-green-500" />
|
||||
<span class="font-medium text-green-800">文件上传成功</span>
|
||||
</div>
|
||||
<Button @click="clearFile" size="small" type="text" danger>
|
||||
清除文件
|
||||
</Button>
|
||||
</div>
|
||||
<p class="text-sm text-green-600 mt-1">{{ uploadedFile.name }} ({{ formatFileSize(uploadedFile.size) }})</p>
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="flex justify-end space-x-2">
|
||||
<Button @click="handleCancel">
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
@click="handleConfirm"
|
||||
type="primary"
|
||||
:disabled="!uploadedFile"
|
||||
>
|
||||
确认导入
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
defineOptions({ name: 'FileUploadModal' })
|
||||
|
||||
import { ref, watch } from 'vue'
|
||||
import { message, Button, Modal } from 'ant-design-vue'
|
||||
import { createIconifyIcon } from '@vben/icons'
|
||||
|
||||
// 创建图标组件
|
||||
const UploadIcon = createIconifyIcon('lucide:upload')
|
||||
const CheckCircleIcon = createIconifyIcon('lucide:check-circle')
|
||||
|
||||
// Props
|
||||
interface Props {
|
||||
open: boolean
|
||||
}
|
||||
|
||||
// Emits
|
||||
interface Emits {
|
||||
(e: 'update:open', value: boolean): void
|
||||
(e: 'confirm', file: File): void
|
||||
(e: 'cancel'): void
|
||||
}
|
||||
|
||||
const props = defineProps<Props>()
|
||||
const emit = defineEmits<Emits>()
|
||||
|
||||
// 响应式数据
|
||||
const visible = ref(false)
|
||||
const uploadedFile = ref<File | null>(null)
|
||||
const isDragOver = ref(false)
|
||||
const fileInputRef = ref<HTMLInputElement>()
|
||||
|
||||
// 监听props变化
|
||||
watch(() => props.open, (newVal) => {
|
||||
visible.value = newVal
|
||||
if (!newVal) {
|
||||
// 关闭时重置状态
|
||||
uploadedFile.value = null
|
||||
isDragOver.value = false
|
||||
}
|
||||
})
|
||||
|
||||
// 监听visible变化,同步到父组件
|
||||
watch(visible, (newVal) => {
|
||||
emit('update:open', newVal)
|
||||
})
|
||||
|
||||
// 文件验证
|
||||
const validateFile = (file: File): boolean => {
|
||||
const isCSV = file.type === 'text/csv' || file.name.endsWith('.csv')
|
||||
if (!isCSV) {
|
||||
message.error('只能上传CSV格式的文件!')
|
||||
return false
|
||||
}
|
||||
|
||||
const isLt10M = file.size / 1024 / 1024 < 10
|
||||
if (!isLt10M) {
|
||||
message.error('文件大小不能超过10MB!')
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 处理文件
|
||||
const processFile = (file: File) => {
|
||||
if (!validateFile(file)) return
|
||||
|
||||
uploadedFile.value = file
|
||||
message.success('文件选择成功')
|
||||
}
|
||||
|
||||
// 拖拽处理
|
||||
const handleDragOver = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = true
|
||||
}
|
||||
|
||||
const handleDragLeave = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = false
|
||||
}
|
||||
|
||||
const handleDrop = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = false
|
||||
|
||||
const files = e.dataTransfer?.files
|
||||
if (files && files.length > 0) {
|
||||
const file = files[0]
|
||||
if (file) {
|
||||
processFile(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 文件选择处理
|
||||
const triggerFileInput = () => {
|
||||
fileInputRef.value?.click()
|
||||
}
|
||||
|
||||
const handleFileSelect = (e: Event) => {
|
||||
const target = e.target as HTMLInputElement
|
||||
const files = target.files
|
||||
if (files && files.length > 0) {
|
||||
const file = files[0]
|
||||
if (file) {
|
||||
processFile(file)
|
||||
}
|
||||
}
|
||||
// 清空input值,允许重复选择同一文件
|
||||
target.value = ''
|
||||
}
|
||||
|
||||
// 清除文件
|
||||
const clearFile = () => {
|
||||
uploadedFile.value = null
|
||||
}
|
||||
|
||||
// 确认上传
|
||||
const handleConfirm = () => {
|
||||
if (uploadedFile.value) {
|
||||
emit('confirm', uploadedFile.value)
|
||||
visible.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 取消
|
||||
const handleCancel = () => {
|
||||
emit('cancel')
|
||||
visible.value = false
|
||||
}
|
||||
|
||||
// 格式化文件大小
|
||||
const formatFileSize = (bytes: number) => {
|
||||
if (bytes === 0) return '0 Bytes'
|
||||
const k = 1024
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB']
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 拖拽区域样式 */
|
||||
.border-dashed {
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.border-dashed:hover {
|
||||
border-color: #1890ff !important;
|
||||
background-color: #f0f8ff !important;
|
||||
}
|
||||
</style>
|
||||
@@ -1,132 +1,119 @@
|
||||
<template>
|
||||
<div class="p-6">
|
||||
<Card title="电商数据导入" class="mb-6">
|
||||
<div class="space-y-6">
|
||||
<!-- 文件拖拽上传区域 -->
|
||||
<div
|
||||
class="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center transition-colors"
|
||||
:class="{ 'border-blue-500 bg-blue-50': isDragOver }"
|
||||
@dragover.prevent="handleDragOver"
|
||||
@dragleave.prevent="handleDragLeave"
|
||||
@drop.prevent="handleDrop"
|
||||
>
|
||||
<div class="space-y-4">
|
||||
<UploadIcon class="w-12 h-12 mx-auto text-gray-400" />
|
||||
<div>
|
||||
<p class="text-lg font-medium text-gray-700">拖拽CSV文件到此处或点击上传</p>
|
||||
<p class="text-sm text-gray-500">支持.csv格式,最大10MB</p>
|
||||
</div>
|
||||
<Button @click="triggerFileInput" type="primary" ghost>
|
||||
选择文件
|
||||
<Page title="电商数据导入" auto-content-height>
|
||||
<template #description>
|
||||
<div class="text-foreground/80 mt-2">
|
||||
支持CSV文件导入,自动匹配实体信息,批量导入订单数据
|
||||
</div>
|
||||
</template>
|
||||
<!-- 文件拖拽上传区域 - 只在没有数据时显示 -->
|
||||
<FileUploadArea v-if="csvData.length === 0" @file-selected="handleFileUpload" />
|
||||
|
||||
<!-- 数据表格 -->
|
||||
<Grid
|
||||
v-if="csvData.length > 0"
|
||||
@checkbox-change="handleCheckboxChange"
|
||||
@checkbox-all="handleCheckboxAll"
|
||||
>
|
||||
<!-- 工具栏工具 -->
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Button @click="showUploadModal = true" ghost>
|
||||
<UploadIcon class="w-4 h-4 mr-1" />
|
||||
继续导入
|
||||
</Button>
|
||||
</div>
|
||||
<input
|
||||
ref="fileInputRef"
|
||||
type="file"
|
||||
accept=".csv"
|
||||
style="display: none"
|
||||
@change="handleFileSelect"
|
||||
/>
|
||||
</div>
|
||||
<Button @click="selectAll">
|
||||
全选
|
||||
</Button>
|
||||
<Button @click="clearSelection">
|
||||
清空选择
|
||||
</Button>
|
||||
<Button @click="showFieldMapping = true" type="primary" :disabled="selectedRows.length === 0">
|
||||
配置字段映射 ({{ selectedRows.length }})
|
||||
</Button>
|
||||
</Space>
|
||||
</template>
|
||||
|
||||
<!-- 文件信息显示 -->
|
||||
<div v-if="uploadedFile" class="bg-green-50 border border-green-200 rounded-lg p-4">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="flex items-center space-x-2">
|
||||
<CheckCircleIcon class="w-5 h-5 text-green-500" />
|
||||
<span class="font-medium text-green-800">文件上传成功</span>
|
||||
<template #toolbar-extra>
|
||||
<div v-if="uploadedFile" class="bg-green-50 border border-green-200 rounded-lg p-3 mb-4">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="flex items-center space-x-2">
|
||||
<CheckCircleIcon class="w-4 h-4 text-green-500" />
|
||||
<span class="font-medium text-green-800 text-sm">文件上传成功</span>
|
||||
</div>
|
||||
<Button @click="clearData" size="small" type="text" danger>
|
||||
清除数据
|
||||
</Button>
|
||||
</div>
|
||||
<Button @click="clearData" size="small" type="text" danger>
|
||||
清除数据
|
||||
</Button>
|
||||
<p class="text-xs text-green-600 mt-1">{{ uploadedFile.name }} ({{ formatFileSize(uploadedFile.size) }})</p>
|
||||
</div>
|
||||
<p class="text-sm text-green-600 mt-1">{{ uploadedFile.name }} ({{ formatFileSize(uploadedFile.size) }})</p>
|
||||
</div>
|
||||
|
||||
<!-- 实体匹配结果 -->
|
||||
<div v-if="matchingResult" class="space-y-4">
|
||||
<Card title="实体匹配结果">
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
|
||||
<div class="bg-green-50 border border-green-200 rounded-lg p-4">
|
||||
<div class="text-green-800 font-medium">匹配成功</div>
|
||||
<div class="text-2xl font-bold text-green-600">{{ matchingResult.matchedOrders.length }}</div>
|
||||
<div class="text-sm text-green-600">条订单数据</div>
|
||||
</div>
|
||||
<div class="bg-red-50 border border-red-200 rounded-lg p-4">
|
||||
<div class="text-red-800 font-medium">匹配失败</div>
|
||||
<div class="text-2xl font-bold text-red-600">{{ matchingResult.failedRecords.length }}</div>
|
||||
<div class="text-sm text-red-600">条记录</div>
|
||||
</div>
|
||||
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
|
||||
<div class="text-blue-800 font-medium">需要创建</div>
|
||||
<div class="text-2xl font-bold text-blue-600">
|
||||
{{ matchingResult.entitiesToCreate.shops.length + matchingResult.entitiesToCreate.products.length + matchingResult.entitiesToCreate.specs.length }}
|
||||
<!-- 实体匹配结果 -->
|
||||
<div v-if="matchingResult" class="space-y-3 mb-4">
|
||||
<div class="bg-blue-50 border border-blue-200 rounded-lg p-3">
|
||||
<div class="text-blue-800 font-medium text-sm mb-2">实体匹配结果</div>
|
||||
<div class="grid grid-cols-3 gap-3 mb-3">
|
||||
<div class="bg-green-50 border border-green-200 rounded p-2 text-center">
|
||||
<div class="text-green-800 font-medium text-xs">匹配成功</div>
|
||||
<div class="text-lg font-bold text-green-600">{{ matchingResult.matchedOrders.length }}</div>
|
||||
<div class="text-xs text-green-600">条订单</div>
|
||||
</div>
|
||||
<div class="bg-red-50 border border-red-200 rounded p-2 text-center">
|
||||
<div class="text-red-800 font-medium text-xs">匹配失败</div>
|
||||
<div class="text-lg font-bold text-red-600">{{ matchingResult.failedRecords.length }}</div>
|
||||
<div class="text-xs text-red-600">条记录</div>
|
||||
</div>
|
||||
<div class="bg-blue-50 border border-blue-200 rounded p-2 text-center">
|
||||
<div class="text-blue-800 font-medium text-xs">需要创建</div>
|
||||
<div class="text-lg font-bold text-blue-600">
|
||||
{{ matchingResult.entitiesToCreate.shops.length + matchingResult.entitiesToCreate.products.length + matchingResult.entitiesToCreate.specs.length }}
|
||||
</div>
|
||||
<div class="text-xs text-blue-600">个实体</div>
|
||||
</div>
|
||||
<div class="text-sm text-blue-600">个实体</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center space-x-2">
|
||||
<Button
|
||||
@click="performImport"
|
||||
type="primary"
|
||||
:loading="importLoading"
|
||||
:disabled="matchingResult.matchedOrders.length === 0"
|
||||
>
|
||||
开始导入 ({{ matchingResult.matchedOrders.length }})
|
||||
</Button>
|
||||
<Button @click="performEntityMatching" size="small">
|
||||
重新匹配
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<!-- 导入进度 -->
|
||||
<div v-if="importLoading" class="mt-4">
|
||||
<Progress :percent="importProgress" />
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<!-- 数据表格 -->
|
||||
<div v-if="csvData.length > 0" class="space-y-4">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="flex items-center space-x-4">
|
||||
<h3 class="text-lg font-medium">数据预览与选择</h3>
|
||||
<div class="text-sm text-gray-500">
|
||||
共 {{ csvData.length }} 条记录,已选择 {{ selectedRows.length }} 条
|
||||
|
||||
<div class="flex items-center space-x-2">
|
||||
<Button
|
||||
@click="performImport"
|
||||
type="primary"
|
||||
size="small"
|
||||
:loading="importLoading"
|
||||
:disabled="matchingResult.matchedOrders.length === 0"
|
||||
>
|
||||
开始导入 ({{ matchingResult.matchedOrders.length }})
|
||||
</Button>
|
||||
<Button @click="performEntityMatching" size="small">
|
||||
重新匹配
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<!-- 导入进度 -->
|
||||
<div v-if="importLoading" class="mt-3">
|
||||
<Progress :percent="importProgress" size="small" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center space-x-2">
|
||||
<Button @click="selectAll" size="small">
|
||||
全选
|
||||
</Button>
|
||||
<Button @click="clearSelection" size="small">
|
||||
清空选择
|
||||
</Button>
|
||||
<Button @click="showFieldMapping = true" type="primary" :disabled="selectedRows.length === 0">
|
||||
配置字段映射 ({{ selectedRows.length }})
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- VxeTable 表格 -->
|
||||
<Grid
|
||||
@checkbox-change="handleCheckboxChange"
|
||||
@checkbox-all="handleCheckboxAll"
|
||||
>
|
||||
<template #action="{ row }">
|
||||
<Button size="small" @click="handleEditRow(row)">
|
||||
编辑
|
||||
</Button>
|
||||
<Button size="small" type="primary" @click="handleMatchRow(row)">
|
||||
匹配
|
||||
</Button>
|
||||
</template>
|
||||
</Grid>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</template>
|
||||
|
||||
<!-- 实收金额列 -->
|
||||
<template #receivedAmount="{ row }: { row: any }">
|
||||
<span style="color: #52c41a; font-weight: bold;">
|
||||
¥{{ (row.shopActualAmount || 0).toFixed(2) }}
|
||||
</span>
|
||||
</template>
|
||||
|
||||
<!-- 操作列 -->
|
||||
<template #action="{ row }: { row: any }">
|
||||
<Space>
|
||||
<Button size="small" @click="handleEditRow(row)">
|
||||
编辑
|
||||
</Button>
|
||||
<Button size="small" type="primary" @click="handleMatchRow(row)">
|
||||
匹配
|
||||
</Button>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
<!-- 字段映射弹窗 -->
|
||||
<Modal
|
||||
v-model:open="showFieldMapping"
|
||||
@@ -142,6 +129,13 @@
|
||||
/>
|
||||
</Modal>
|
||||
|
||||
<!-- 文件上传模态框 -->
|
||||
<FileUploadModal
|
||||
v-model:open="showUploadModal"
|
||||
@confirm="handleFileUpload"
|
||||
@cancel="showUploadModal = false"
|
||||
/>
|
||||
|
||||
<!-- 实体创建模态框 -->
|
||||
<EntityCreationModal
|
||||
:visible="showEntityCreationModal"
|
||||
@@ -150,14 +144,19 @@
|
||||
@success="handleEntityCreationSuccess"
|
||||
@cancel="handleEntityCreationCancel"
|
||||
/>
|
||||
</div>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
defineOptions({ name: 'DataImport' })
|
||||
|
||||
import { ref } from 'vue'
|
||||
import { message, Progress, Card, Button, Modal } from 'ant-design-vue'
|
||||
import { message, Progress, Button, Modal, Space } from 'ant-design-vue'
|
||||
import { Page } from '@vben/common-ui'
|
||||
import { createIconifyIcon } from '@vben/icons'
|
||||
import FieldMapping from '#/views/ecommerce/data-import/components/FieldMapping.vue'
|
||||
import FileUploadArea from './components/FileUploadArea.vue'
|
||||
import FileUploadModal from './components/FileUploadModal.vue'
|
||||
import EntityCreationModal from './components/EntityCreationModal.vue'
|
||||
import { EntityMatchingService, type EntityMatchingResult } from './services/EntityMatchingService'
|
||||
import { batchImportOrdersApi } from '#/api/ecommerce'
|
||||
@@ -174,8 +173,7 @@ const uploadedFile = ref<File | null>(null)
|
||||
const csvData = ref<any[]>([])
|
||||
const csvHeaders = ref<string[]>([])
|
||||
const showFieldMapping = ref(false)
|
||||
const isDragOver = ref(false)
|
||||
const fileInputRef = ref<HTMLInputElement>()
|
||||
const showUploadModal = ref(false)
|
||||
const selectedRows = ref<any[]>([])
|
||||
|
||||
// 实体匹配相关
|
||||
@@ -185,205 +183,119 @@ const showEntityCreationModal = ref(false)
|
||||
const importLoading = ref(false)
|
||||
const importProgress = ref(0)
|
||||
|
||||
// VxeTable 配置
|
||||
// VbenVxeGrid 表格配置
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
{
|
||||
type: 'checkbox',
|
||||
width: 60,
|
||||
fixed: 'left',
|
||||
type: 'checkbox' as const,
|
||||
minWidth: 'auto',
|
||||
fixed: 'left' as const,
|
||||
},
|
||||
{
|
||||
type: 'seq',
|
||||
width: 80,
|
||||
fixed: 'left',
|
||||
type: 'seq' as const,
|
||||
minWidth: 'auto',
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'productName',
|
||||
title: '商品名称',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip'
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'orderNumber',
|
||||
title: '订单编号',
|
||||
width: 180,
|
||||
showOverflow: 'tooltip'
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'orderStatusName',
|
||||
title: '订单状态',
|
||||
width: 120
|
||||
minWidth: 'auto',
|
||||
},
|
||||
{
|
||||
field: 'quantity',
|
||||
title: '数量',
|
||||
width: 80
|
||||
minWidth: 'auto',
|
||||
},
|
||||
{
|
||||
field: 'productId',
|
||||
title: '商品ID',
|
||||
width: 120,
|
||||
showOverflow: 'tooltip'
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'productSpecName',
|
||||
title: '商品规格',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip'
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'specCode',
|
||||
title: '规格编码',
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'shopName',
|
||||
title: '店铺名称',
|
||||
width: 150,
|
||||
showOverflow: 'tooltip'
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'afterSaleStatus',
|
||||
title: '售后状态',
|
||||
width: 120
|
||||
minWidth: 'auto',
|
||||
},
|
||||
{
|
||||
field: 'shopActualAmount',
|
||||
title: '实收金额',
|
||||
width: 100
|
||||
minWidth: 'auto',
|
||||
slots: { default: 'receivedAmount' },
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 200,
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' }
|
||||
}
|
||||
minWidth: 'auto',
|
||||
fixed: 'right' as const,
|
||||
slots: { default: 'action' },
|
||||
},
|
||||
],
|
||||
data: [],
|
||||
height: 500,
|
||||
height: 'auto',
|
||||
border: true,
|
||||
stripe: true,
|
||||
showOverflow: true,
|
||||
keepSource: true,
|
||||
size: 'small',
|
||||
toolbarConfig: {
|
||||
custom: true,
|
||||
refresh: true,
|
||||
zoom: true,
|
||||
},
|
||||
data: [],
|
||||
pagerConfig: {
|
||||
enabled: true,
|
||||
pageSizes: [10, 20, 50, 100],
|
||||
layouts: ['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total'] as ('PrevJump' | 'PrevPage' | 'Number' | 'NextPage' | 'NextJump' | 'Sizes' | 'FullJump' | 'Total')[],
|
||||
},
|
||||
checkboxConfig: {
|
||||
labelField: 'id',
|
||||
reserve: true,
|
||||
highlight: true,
|
||||
range: true,
|
||||
},
|
||||
scrollY: {
|
||||
enabled: true,
|
||||
},
|
||||
scrollX: {
|
||||
enabled: true,
|
||||
},
|
||||
}
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions,
|
||||
})
|
||||
|
||||
// 更新表格列配置
|
||||
const updateTableColumns = () => {
|
||||
const newColumns: any[] = [
|
||||
{
|
||||
type: 'checkbox',
|
||||
width: 60,
|
||||
fixed: 'left',
|
||||
},
|
||||
{
|
||||
type: 'seq',
|
||||
width: 80,
|
||||
fixed: 'left',
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'productName',
|
||||
title: '商品名称',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'orderNumber',
|
||||
title: '订单编号',
|
||||
width: 180,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'orderStatusName',
|
||||
title: '订单状态',
|
||||
width: 120
|
||||
},
|
||||
{
|
||||
field: 'quantity',
|
||||
title: '数量',
|
||||
width: 80
|
||||
},
|
||||
{
|
||||
field: 'productId',
|
||||
title: '商品ID',
|
||||
width: 120,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'productSpecName',
|
||||
title: '商品规格',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'specCode',
|
||||
title: '规格编码',
|
||||
width: 120,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'shopName',
|
||||
title: '店铺名称',
|
||||
width: 150,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'afterSaleStatus',
|
||||
title: '售后状态',
|
||||
width: 120
|
||||
},
|
||||
{
|
||||
field: 'shopActualAmount',
|
||||
title: '实收金额',
|
||||
width: 100
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 200,
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' }
|
||||
}
|
||||
]
|
||||
|
||||
// 更新表格数据
|
||||
const updateTableData = () => {
|
||||
gridApi.setGridOptions({
|
||||
columns: newColumns,
|
||||
data: csvData.value,
|
||||
})
|
||||
}
|
||||
|
||||
// 文件验证
|
||||
const validateFile = (file: File): boolean => {
|
||||
const isCSV = file.type === 'text/csv' || file.name.endsWith('.csv')
|
||||
if (!isCSV) {
|
||||
message.error('只能上传CSV格式的文件!')
|
||||
return false
|
||||
}
|
||||
|
||||
const isLt10M = file.size / 1024 / 1024 < 10
|
||||
if (!isLt10M) {
|
||||
message.error('文件大小不能超过10MB!')
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 处理文件
|
||||
const processFile = async (file: File) => {
|
||||
if (!validateFile(file)) return
|
||||
|
||||
uploadedFile.value = file
|
||||
|
||||
@@ -406,8 +318,8 @@ const processFile = async (file: File) => {
|
||||
csvHeaders.value = result.headers
|
||||
selectedRows.value = []
|
||||
|
||||
// 更新表格配置
|
||||
updateTableColumns()
|
||||
// 更新表格数据
|
||||
updateTableData()
|
||||
|
||||
// 从文件名提取店铺名称
|
||||
const shopName = extractShopNameFromFilename(file.name)
|
||||
@@ -421,46 +333,10 @@ const processFile = async (file: File) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 拖拽处理
|
||||
const handleDragOver = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = true
|
||||
}
|
||||
|
||||
const handleDragLeave = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = false
|
||||
}
|
||||
|
||||
const handleDrop = (e: DragEvent) => {
|
||||
e.preventDefault()
|
||||
isDragOver.value = false
|
||||
|
||||
const files = e.dataTransfer?.files
|
||||
if (files && files.length > 0) {
|
||||
const file = files[0]
|
||||
if (file) {
|
||||
processFile(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 文件选择处理
|
||||
const triggerFileInput = () => {
|
||||
fileInputRef.value?.click()
|
||||
}
|
||||
|
||||
const handleFileSelect = (e: Event) => {
|
||||
const target = e.target as HTMLInputElement
|
||||
const files = target.files
|
||||
if (files && files.length > 0) {
|
||||
const file = files[0]
|
||||
if (file) {
|
||||
processFile(file)
|
||||
}
|
||||
}
|
||||
// 清空input值,允许重复选择同一文件
|
||||
target.value = ''
|
||||
// 文件上传处理
|
||||
const handleFileUpload = async (file: File) => {
|
||||
await processFile(file)
|
||||
showUploadModal.value = false
|
||||
}
|
||||
|
||||
// 表格选择处理
|
||||
@@ -544,19 +420,22 @@ const performImport = async () => {
|
||||
importLoading.value = true
|
||||
importProgress.value = 0
|
||||
|
||||
// 模拟进度更新
|
||||
const progressInterval = setInterval(() => {
|
||||
// 使用 requestAnimationFrame 进行更平滑的进度更新
|
||||
const updateProgress = () => {
|
||||
if (importProgress.value < 90) {
|
||||
importProgress.value += 10
|
||||
requestAnimationFrame(updateProgress)
|
||||
}
|
||||
}, 200)
|
||||
}
|
||||
|
||||
// 开始进度更新
|
||||
requestAnimationFrame(updateProgress)
|
||||
|
||||
// 调用批量导入API
|
||||
const result = await batchImportOrdersApi({
|
||||
orders: matchingResult.value.matchedOrders
|
||||
})
|
||||
|
||||
clearInterval(progressInterval)
|
||||
importProgress.value = 100
|
||||
|
||||
message.success(`导入完成!成功导入 ${result.successCount} 条,失败 ${result.failureCount} 条`)
|
||||
@@ -614,31 +493,5 @@ const formatFileSize = (bytes: number) => {
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 拖拽区域样式 */
|
||||
.border-dashed {
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.border-dashed:hover {
|
||||
border-color: #1890ff !important;
|
||||
background-color: #f0f8ff !important;
|
||||
}
|
||||
|
||||
/* VxeTable 样式调整 */
|
||||
:deep(.vxe-table) {
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
:deep(.vxe-table .vxe-header--column) {
|
||||
background-color: #fafafa;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
:deep(.vxe-table .vxe-body--row:hover) {
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
:deep(.vxe-table .vxe-body--row.row--checked) {
|
||||
background-color: #e6f7ff;
|
||||
}
|
||||
/* 自定义样式 */
|
||||
</style>
|
||||
|
||||
-176
@@ -1,176 +0,0 @@
|
||||
<script lang="ts" setup>
|
||||
import { Card, Image, Tag, Button, Space, Popconfirm, Tooltip } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import type { ProductVO } from '#/api/shop';
|
||||
|
||||
// 创建图标组件
|
||||
const EditIcon = createIconifyIcon('lucide:edit');
|
||||
const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
const SettingsIcon = createIconifyIcon('lucide:settings');
|
||||
const EyeIcon = createIconifyIcon('lucide:eye');
|
||||
|
||||
interface Props {
|
||||
product: ProductVO.ProductInfoVO;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'edit', product: ProductVO.ProductInfoVO): void;
|
||||
(e: 'delete', product: ProductVO.ProductInfoVO): void;
|
||||
(e: 'manage-specs', product: ProductVO.ProductInfoVO): void;
|
||||
(e: 'view-details', product: ProductVO.ProductInfoVO): void;
|
||||
}
|
||||
|
||||
defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
const handleEdit = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('edit', product);
|
||||
};
|
||||
|
||||
const handleDelete = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('delete', product);
|
||||
};
|
||||
|
||||
const handleManageSpecs = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('manage-specs', product);
|
||||
};
|
||||
|
||||
const handleViewDetails = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('view-details', product);
|
||||
};
|
||||
|
||||
const handleImageError = (event: Event) => {
|
||||
const img = event.target as HTMLImageElement;
|
||||
img.style.display = 'none';
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Card
|
||||
hoverable
|
||||
:body-style="{ padding: '12px' }"
|
||||
class="product-card"
|
||||
>
|
||||
<div class="relative">
|
||||
<!-- 商品图片 -->
|
||||
<div class="aspect-square bg-gray-100 rounded-lg overflow-hidden">
|
||||
<Image
|
||||
v-if="product.mainImage"
|
||||
:src="product.mainImage"
|
||||
:alt="product.name"
|
||||
class="w-full h-full object-cover"
|
||||
:preview="true"
|
||||
@error="handleImageError"
|
||||
/>
|
||||
<div v-else class="w-full h-full flex items-center justify-center text-gray-400">
|
||||
<div class="text-center">
|
||||
<div class="text-4xl mb-2">📦</div>
|
||||
<div class="text-sm">暂无图片</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 规格数量标签 -->
|
||||
<div v-if="product.specs && product.specs.length > 0" class="absolute top-2 left-2">
|
||||
<Tag color="blue">
|
||||
{{ product.specs.length }} 个规格
|
||||
</Tag>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 商品信息 -->
|
||||
<div class="mt-3">
|
||||
<div class="text-sm font-medium text-gray-900 mb-2 line-clamp-2" :title="product.name">
|
||||
{{ product.name }}
|
||||
</div>
|
||||
|
||||
<!-- 规格信息 -->
|
||||
<div v-if="product.specs && product.specs.length > 0" class="mb-2">
|
||||
<div class="text-xs text-gray-500 mb-1">规格信息:</div>
|
||||
<div class="space-y-1">
|
||||
<div
|
||||
v-for="spec in product.specs.slice(0, 2)"
|
||||
:key="spec.id"
|
||||
class="text-xs bg-gray-50 px-2 py-1 rounded"
|
||||
>
|
||||
<span class="font-medium">{{ spec.name }}</span>
|
||||
<span class="text-red-500 ml-1">¥{{ spec.costPrice.toFixed(2) }}</span>
|
||||
</div>
|
||||
<div v-if="product.specs.length > 2" class="text-xs text-gray-400">
|
||||
还有 {{ product.specs.length - 2 }} 个规格...
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 创建时间 -->
|
||||
<div class="text-xs text-gray-500 mb-3">
|
||||
{{ product.createTime }}
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="flex justify-between items-center">
|
||||
<Space size="small">
|
||||
<Tooltip title="编辑">
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleEdit(product)"
|
||||
>
|
||||
<EditIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Tooltip title="管理规格">
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleManageSpecs(product)"
|
||||
>
|
||||
<SettingsIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Tooltip title="查看详情">
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleViewDetails(product)"
|
||||
>
|
||||
<EyeIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
</Space>
|
||||
<Popconfirm
|
||||
title="确定要删除这个商品吗?"
|
||||
@confirm="handleDelete(product)"
|
||||
>
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
danger
|
||||
>
|
||||
<TrashIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.product-card {
|
||||
transition: all 0.3s ease;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.product-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.line-clamp-2 {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
-419
@@ -1,419 +0,0 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="商品主图" name="mainImageId">
|
||||
<div style="display: flex; align-items: center; gap: 16px;">
|
||||
|
||||
<!-- 主图预览区域 -->
|
||||
<div style="position: relative; display: inline-block;">
|
||||
<!-- 有图片时显示图片 -->
|
||||
<img
|
||||
v-if="currentImageUrl && !imageLoadError"
|
||||
:src="currentImageUrl"
|
||||
alt="商品主图"
|
||||
style="width: 80px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid #d9d9d9; background-color: #f5f5f5;"
|
||||
@error="handleImageError"
|
||||
/>
|
||||
<!-- 无图片或加载失败时显示空状态 -->
|
||||
<div
|
||||
v-else
|
||||
style="width: 80px; height: 80px; border: 1px dashed #d9d9d9; border-radius: 8px; background-color: #fafafa; display: flex; align-items: center; justify-content: center; color: #999; font-size: 12px; text-align: center; line-height: 1.2;">
|
||||
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div>上传主图</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 点击更换遮罩 -->
|
||||
<div
|
||||
style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); border-radius: 8px; display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s; cursor: pointer;"
|
||||
@mouseenter="($event.target as HTMLElement).style.opacity = '1'"
|
||||
@mouseleave="($event.target as HTMLElement).style.opacity = '0'"
|
||||
@click="selectImageFile">
|
||||
<div style="color: white; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div style="font-size: 12px;">{{ currentImageUrl && !imageLoadError ? '更换主图' : '上传主图' }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 重置按钮 -->
|
||||
<div v-if="newImageFile">
|
||||
<Button size="small" @click="removeNewImage">重置</Button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="商品名称" name="name" :rules="[{ required: true, message: '请输入商品名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入商品名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item v-if="isEdit" label="商品编码" name="specCode">
|
||||
<Input v-model:value="formData.specCode" placeholder="请输入商品编码" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item v-if="isEdit" label="商品SKU" name="skuCode">
|
||||
<Input v-model:value="formData.skuCode" placeholder="请输入商品SKU" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item v-if="isEdit" label="成本价" name="costPrice" :rules="[{ required: true, message: '请输入成本价' }]">
|
||||
<InputNumber
|
||||
v-model:value="formData.costPrice"
|
||||
placeholder="请输入成本价"
|
||||
style="width: 100%;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<!-- 规格管理 -->
|
||||
<Form.Item v-if="!isEdit" label="商品规格">
|
||||
<div class="space-y-4">
|
||||
<div class="flex items-center gap-2">
|
||||
<Button
|
||||
type="dashed"
|
||||
@click="showSpecs = !showSpecs"
|
||||
:icon="showSpecs ? 'up' : 'down'"
|
||||
>
|
||||
{{ showSpecs ? '收起规格' : '添加规格' }}
|
||||
</Button>
|
||||
<span class="text-sm text-gray-500">
|
||||
可以添加多个规格,每个规格包含名称、编码和成本价
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="showSpecs" class="border border-gray-200 rounded-lg p-4 bg-gray-50">
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="(spec, index) in specs"
|
||||
:key="index"
|
||||
class="flex items-center gap-2 p-3 bg-white rounded border"
|
||||
>
|
||||
<Input
|
||||
v-model:value="spec.name"
|
||||
placeholder="规格名称"
|
||||
style="flex: 1;"
|
||||
/>
|
||||
<Input
|
||||
v-model:value="spec.specCode"
|
||||
placeholder="规格编码"
|
||||
style="flex: 1;"
|
||||
/>
|
||||
<InputNumber
|
||||
v-model:value="spec.costPrice"
|
||||
placeholder="成本价"
|
||||
style="width: 120px;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
<Button
|
||||
type="text"
|
||||
danger
|
||||
@click="removeSpec(index)"
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="dashed"
|
||||
@click="addSpec"
|
||||
style="width: 100%;"
|
||||
>
|
||||
+ 添加规格
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, onUnmounted } from 'vue';
|
||||
import { Form, Input, Modal, InputNumber, Button, message } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import {
|
||||
createProductApi,
|
||||
updateProductApi,
|
||||
createProductWithSpecsApi,
|
||||
type ProductVO,
|
||||
type ProductDTO
|
||||
} from '#/api/shop';
|
||||
import { uploadImageApi } from '#/api/media';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 创建相机图标组件
|
||||
const CameraIcon = createIconifyIcon('mdi:camera');
|
||||
|
||||
// 内部状态管理
|
||||
const visible = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentProductData = ref<ProductVO.ProductInfoVO | Partial<ProductVO.ProductInfoVO> | null>(null);
|
||||
|
||||
// 响应式数据
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const newImageFile = ref<File | null>(null); // 新选择的图片文件
|
||||
const originalImageUrl = ref(''); // 原始图片 URL
|
||||
const imageLoadError = ref(false); // 图片加载错误状态
|
||||
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined as number | undefined,
|
||||
costPrice: 0,
|
||||
});
|
||||
|
||||
// 规格数据
|
||||
const specs = ref<ProductDTO.ProductSpecDTO[]>([]);
|
||||
const showSpecs = ref(false);
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => isEdit.value ? '编辑商品' : '添加商品');
|
||||
|
||||
// 当前显示的图片 URL(优先显示新选择的)
|
||||
const currentImageUrl = computed(() => {
|
||||
if (newImageFile.value) {
|
||||
return URL.createObjectURL(newImageFile.value);
|
||||
}
|
||||
return originalImageUrl.value;
|
||||
});
|
||||
|
||||
// 初始化表单数据
|
||||
function initFormData() {
|
||||
if (isEdit.value && currentProductData.value) {
|
||||
// 编辑模式
|
||||
formData.value = {
|
||||
name: currentProductData.value.name || '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined,
|
||||
costPrice: 0,
|
||||
};
|
||||
|
||||
// 设置原始图片 URL
|
||||
originalImageUrl.value = currentProductData.value.mainImage || '';
|
||||
} else {
|
||||
// 创建模式 - 如果有传入的productData,则使用它来初始化表单
|
||||
formData.value = {
|
||||
name: currentProductData.value?.name || '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined,
|
||||
costPrice: 0,
|
||||
};
|
||||
originalImageUrl.value = currentProductData.value?.mainImage || '';
|
||||
}
|
||||
|
||||
// 重置新选择的图片和图片加载状态
|
||||
newImageFile.value = null;
|
||||
imageLoadError.value = false;
|
||||
|
||||
// 重置规格数据
|
||||
specs.value = [];
|
||||
showSpecs.value = false;
|
||||
|
||||
// 如果有传入的规格信息,则自动填入
|
||||
if (!isEdit.value && currentProductData.value && 'specs' in currentProductData.value && currentProductData.value.specs) {
|
||||
specs.value = currentProductData.value.specs.map(spec => ({
|
||||
name: (spec as any).specName || spec.specCode || '',
|
||||
specCode: spec.specCode || '',
|
||||
costPrice: spec.costPrice || 0
|
||||
}));
|
||||
showSpecs.value = specs.value.length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 重置表单(在open方法中调用)
|
||||
function resetForm() {
|
||||
formRef.value?.resetFields();
|
||||
newImageFile.value = null;
|
||||
originalImageUrl.value = '';
|
||||
imageLoadError.value = false;
|
||||
specs.value = [];
|
||||
showSpecs.value = false;
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
|
||||
// 准备提交数据
|
||||
const submitData = { ...formData.value };
|
||||
|
||||
// 如果有新选择的图片,先上传获取ID
|
||||
if (newImageFile.value) {
|
||||
const mainImageId = await uploadImageAndGetId();
|
||||
if (mainImageId) {
|
||||
submitData.mainImageId = mainImageId;
|
||||
}
|
||||
} else if (!isEdit.value) {
|
||||
// 创建模式且没有选择图片,移除mainImageId字段
|
||||
delete submitData.mainImageId;
|
||||
}
|
||||
|
||||
// 调用API
|
||||
if (isEdit.value && currentProductData.value && 'id' in currentProductData.value && currentProductData.value.id) {
|
||||
await updateProductApi(currentProductData.value.id, submitData as ProductDTO.UpdateProductDTO);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
// 创建模式:如果有规格,使用一键创建接口
|
||||
if (specs.value.length > 0) {
|
||||
const createWithSpecsData: ProductDTO.CreateProductWithSpecsDTO = {
|
||||
name: submitData.name,
|
||||
mainImageId: submitData.mainImageId,
|
||||
specs: specs.value,
|
||||
};
|
||||
await createProductWithSpecsApi(createWithSpecsData);
|
||||
message.success('商品和规格创建成功');
|
||||
} else {
|
||||
await createProductApi(submitData as ProductDTO.CreateProductDTO);
|
||||
message.success('创建成功');
|
||||
}
|
||||
}
|
||||
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存商品失败:', error);
|
||||
message.error('保存商品失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(editMode: boolean = false, productData?: ProductVO.ProductInfoVO | Partial<ProductVO.ProductInfoVO> | null) {
|
||||
isEdit.value = editMode;
|
||||
currentProductData.value = productData || null;
|
||||
|
||||
resetForm(); // 先重置表单
|
||||
initFormData(); // 再初始化数据
|
||||
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
|
||||
// 选择图片文件
|
||||
function selectImageFile() {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = 'image/*';
|
||||
input.style.display = 'none';
|
||||
document.body.appendChild(input);
|
||||
|
||||
input.onchange = (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0];
|
||||
if (file) {
|
||||
// 验证文件
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
const isLt2M = file.size / 1024 / 1024 < 2;
|
||||
if (!isLt2M) {
|
||||
message.error('图片大小不能超过 2MB');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置新选择的文件
|
||||
newImageFile.value = file;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('图片选择成功');
|
||||
}
|
||||
|
||||
document.body.removeChild(input);
|
||||
};
|
||||
|
||||
input.click();
|
||||
}
|
||||
|
||||
// 重置新选择的图片
|
||||
function removeNewImage() {
|
||||
newImageFile.value = null;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('已重置图片');
|
||||
}
|
||||
|
||||
// 添加规格
|
||||
function addSpec() {
|
||||
specs.value.push({
|
||||
name: '',
|
||||
specCode: '',
|
||||
costPrice: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// 删除规格
|
||||
function removeSpec(index: number) {
|
||||
specs.value.splice(index, 1);
|
||||
}
|
||||
|
||||
// 上传图片并获取图片ID
|
||||
async function uploadImageAndGetId(): Promise<number | undefined> {
|
||||
if (!newImageFile.value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await uploadImageApi(newImageFile.value);
|
||||
console.log('图片上传成功:', response);
|
||||
return response.id;
|
||||
} catch (error) {
|
||||
console.error('图片上传失败:', error);
|
||||
message.error('图片上传失败');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 图片加载错误处理
|
||||
function handleImageError(event: Event) {
|
||||
console.error('图片加载失败:', event);
|
||||
imageLoadError.value = true;
|
||||
message.error('图片加载失败,请重新选择');
|
||||
}
|
||||
|
||||
// 组件卸载时清理
|
||||
onUnmounted(() => {
|
||||
if (newImageFile.value) {
|
||||
const url = URL.createObjectURL(newImageFile.value);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
-322
@@ -1,322 +0,0 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="800px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<div class="space-y-4">
|
||||
<!-- 商品信息 -->
|
||||
<div class="bg-gray-50 p-4 rounded-lg">
|
||||
<h3 class="font-medium text-gray-900 mb-2">商品信息</h3>
|
||||
<div class="grid grid-cols-2 gap-4 text-sm">
|
||||
<div>
|
||||
<span class="text-gray-500">商品名称:</span>
|
||||
<span class="ml-2 font-medium">{{ currentProduct?.name }}</span>
|
||||
</div>
|
||||
<div>
|
||||
<span class="text-gray-500">商品ID:</span>
|
||||
<span class="ml-2 font-medium">{{ currentProduct?.id }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 规格列表 -->
|
||||
<div>
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h3 class="font-medium text-gray-900">商品规格</h3>
|
||||
<Button type="primary" @click="handleAddSpec">
|
||||
添加规格
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="specList"
|
||||
:loading="specLoading"
|
||||
:pagination="{
|
||||
current: specCurrent,
|
||||
pageSize: specPageSize,
|
||||
total: specTotal,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
|
||||
}"
|
||||
row-key="id"
|
||||
@change="handleSpecTableChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
<!-- 规格表单模态框 -->
|
||||
<Modal
|
||||
:open="specFormVisible"
|
||||
:title="specFormTitle"
|
||||
width="500px"
|
||||
:confirm-loading="specFormLoading"
|
||||
@ok="handleSpecSubmit"
|
||||
@cancel="handleSpecCancel"
|
||||
>
|
||||
<Form
|
||||
ref="specFormRef"
|
||||
:model="specFormData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="规格名称" name="name" :rules="[{ required: true, message: '请输入规格名称' }]">
|
||||
<Input v-model:value="specFormData.name" placeholder="请输入规格名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="规格编码" name="specCode">
|
||||
<Input v-model:value="specFormData.specCode" placeholder="请输入规格编码" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="成本价" name="costPrice" :rules="[{ required: true, message: '请输入成本价' }]">
|
||||
<InputNumber
|
||||
v-model:value="specFormData.costPrice"
|
||||
placeholder="请输入成本价"
|
||||
style="width: 100%;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, h } from 'vue';
|
||||
import { Form, Input, Modal, InputNumber, Button, Table, message } from 'ant-design-vue';
|
||||
import {
|
||||
getProductSpecPageApi,
|
||||
createProductSpecApi,
|
||||
updateProductSpecApi,
|
||||
deleteProductSpecApi,
|
||||
type ProductVO,
|
||||
type ProductDTO
|
||||
} from '#/api/shop';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 内部状态管理
|
||||
const visible = ref(false);
|
||||
const currentProduct = ref<ProductVO.ProductInfoVO | null>(null);
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const specLoading = ref(false);
|
||||
const specList = ref<ProductVO.ProductSpecInfoVO[]>([]);
|
||||
const specTotal = ref(0);
|
||||
const specCurrent = ref(1);
|
||||
const specPageSize = ref(10);
|
||||
|
||||
// 规格表单相关
|
||||
const specFormVisible = ref(false);
|
||||
const specFormLoading = ref(false);
|
||||
const specFormRef = ref();
|
||||
const isEditSpec = ref(false);
|
||||
const currentSpecId = ref<number | null>(null);
|
||||
|
||||
const specFormData = ref({
|
||||
name: '',
|
||||
specCode: '',
|
||||
costPrice: 0,
|
||||
});
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() =>
|
||||
currentProduct.value ? `管理规格 - ${currentProduct.value.name}` : '管理规格'
|
||||
);
|
||||
|
||||
// 规格表单标题
|
||||
const specFormTitle = computed(() => isEditSpec.value ? '编辑规格' : '添加规格');
|
||||
|
||||
// 表格列配置
|
||||
const columns = [
|
||||
{
|
||||
title: '规格名称',
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
title: '规格编码',
|
||||
dataIndex: 'specCode',
|
||||
key: 'specCode',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: '成本价',
|
||||
dataIndex: 'costPrice',
|
||||
key: 'costPrice',
|
||||
width: 120,
|
||||
customRender: ({ record }: { record: ProductVO.ProductSpecInfoVO }) => {
|
||||
return `¥${record.costPrice.toFixed(2)}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'createTime',
|
||||
key: 'createTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 150,
|
||||
fixed: 'right' as const,
|
||||
customRender: ({ record }: { record: ProductVO.ProductSpecInfoVO }) => {
|
||||
return h('div', { style: 'display: flex; gap: 8px;' }, [
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
onClick: () => handleEditSpec(record),
|
||||
}, { default: () => '编辑' }),
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
danger: true,
|
||||
onClick: () => handleDeleteSpec(record),
|
||||
}, { default: () => '删除' }),
|
||||
]);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// 获取规格列表
|
||||
async function fetchSpecList() {
|
||||
if (!currentProduct.value) return;
|
||||
|
||||
try {
|
||||
specLoading.value = true;
|
||||
const params: PageQueryDTO<ProductDTO.ProductSpecFilterDTO> = {
|
||||
page: specCurrent.value,
|
||||
pageSize: specPageSize.value,
|
||||
filters: {
|
||||
productId: currentProduct.value.id,
|
||||
},
|
||||
};
|
||||
const data = await getProductSpecPageApi(params);
|
||||
specList.value = data.records;
|
||||
specTotal.value = data.total;
|
||||
} catch (error) {
|
||||
console.error('获取规格列表失败:', error);
|
||||
message.error('获取规格列表失败');
|
||||
} finally {
|
||||
specLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理规格表格变化
|
||||
function handleSpecTableChange(pagination: any) {
|
||||
specCurrent.value = pagination.current;
|
||||
specPageSize.value = pagination.pageSize;
|
||||
fetchSpecList();
|
||||
}
|
||||
|
||||
// 处理添加规格
|
||||
function handleAddSpec() {
|
||||
isEditSpec.value = false;
|
||||
currentSpecId.value = null;
|
||||
specFormData.value = {
|
||||
name: '',
|
||||
specCode: '',
|
||||
costPrice: 0,
|
||||
};
|
||||
specFormVisible.value = true;
|
||||
}
|
||||
|
||||
// 处理编辑规格
|
||||
function handleEditSpec(record: ProductVO.ProductSpecInfoVO) {
|
||||
isEditSpec.value = true;
|
||||
currentSpecId.value = record.id;
|
||||
specFormData.value = {
|
||||
name: record.name,
|
||||
specCode: record.specCode || '',
|
||||
costPrice: record.costPrice,
|
||||
};
|
||||
specFormVisible.value = true;
|
||||
}
|
||||
|
||||
// 处理删除规格
|
||||
async function handleDeleteSpec(record: ProductVO.ProductSpecInfoVO) {
|
||||
try {
|
||||
await deleteProductSpecApi(record.id);
|
||||
message.success('删除成功');
|
||||
await fetchSpecList();
|
||||
} catch (error) {
|
||||
console.error('删除规格失败:', error);
|
||||
message.error('删除规格失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理规格表单提交
|
||||
async function handleSpecSubmit() {
|
||||
try {
|
||||
await specFormRef.value?.validate();
|
||||
specFormLoading.value = true;
|
||||
|
||||
if (isEditSpec.value && currentSpecId.value) {
|
||||
await updateProductSpecApi(currentSpecId.value, specFormData.value as ProductDTO.UpdateProductSpecDTO);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
if (!currentProduct.value) {
|
||||
message.error('商品信息不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
const createData: ProductDTO.CreateProductSpecDTO = {
|
||||
productId: currentProduct.value.id,
|
||||
...specFormData.value,
|
||||
};
|
||||
await createProductSpecApi(createData);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
specFormVisible.value = false;
|
||||
await fetchSpecList();
|
||||
} catch (error) {
|
||||
console.error('保存规格失败:', error);
|
||||
message.error('保存规格失败');
|
||||
} finally {
|
||||
specFormLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理规格表单取消
|
||||
function handleSpecCancel() {
|
||||
specFormVisible.value = false;
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
function handleSubmit() {
|
||||
// 规格管理模态框不需要提交操作
|
||||
handleCancel();
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(product: ProductVO.ProductInfoVO) {
|
||||
currentProduct.value = product;
|
||||
specCurrent.value = 1;
|
||||
specPageSize.value = 10;
|
||||
visible.value = true;
|
||||
fetchSpecList();
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
</script>
|
||||
@@ -1,389 +0,0 @@
|
||||
<template>
|
||||
<Page>
|
||||
<template #title>商品管理</template>
|
||||
|
||||
<!-- 搜索区域 -->
|
||||
<div class="mb-4 p-4 bg-white rounded-lg shadow-sm">
|
||||
<Form :model="searchForm" layout="inline">
|
||||
<Form.Item label="商品名称">
|
||||
<Input
|
||||
v-model:value="searchForm.name"
|
||||
placeholder="请输入商品名称"
|
||||
style="width: 200px"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="商品编码">
|
||||
<Input
|
||||
v-model:value="searchForm.specCode"
|
||||
placeholder="请输入商品编码"
|
||||
style="width: 150px"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="商品SKU">
|
||||
<Input
|
||||
v-model:value="searchForm.skuCode"
|
||||
placeholder="请输入商品SKU"
|
||||
style="width: 150px"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="成本价范围">
|
||||
<div class="flex items-center gap-2">
|
||||
<InputNumber
|
||||
v-model:value="searchForm.minCostPrice"
|
||||
placeholder="最小值"
|
||||
style="width: 100px"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
<span class="text-gray-500">-</span>
|
||||
<InputNumber
|
||||
v-model:value="searchForm.maxCostPrice"
|
||||
placeholder="最大值"
|
||||
style="width: 100px"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</div>
|
||||
</Form.Item>
|
||||
<Form.Item>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleSearch">搜索</Button>
|
||||
<Button @click="handleReset">重置</Button>
|
||||
</Space>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</div>
|
||||
|
||||
<!-- 操作区域 -->
|
||||
<div class="mb-4 flex justify-between items-center">
|
||||
<div class="flex items-center gap-4">
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon class="w-4 h-4 mr-2" />
|
||||
添加商品
|
||||
</Button>
|
||||
<span class="text-gray-500">支持商品和规格的一键创建</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-4">
|
||||
<Button
|
||||
:type="viewMode === 'grid' ? 'primary' : 'default'"
|
||||
@click="toggleViewMode"
|
||||
>
|
||||
<GridIcon class="w-4 h-4 mr-2" />
|
||||
网格视图
|
||||
</Button>
|
||||
<Button
|
||||
:type="viewMode === 'table' ? 'primary' : 'default'"
|
||||
@click="toggleViewMode"
|
||||
>
|
||||
<ListIcon class="w-4 h-4 mr-2" />
|
||||
列表视图
|
||||
</Button>
|
||||
<span class="text-gray-500">
|
||||
共 {{ total }} 个商品
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 列表视图 -->
|
||||
<div v-if="viewMode === 'table'" class="bg-white rounded-lg shadow-sm">
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="productList"
|
||||
:loading="loading"
|
||||
:pagination="{
|
||||
current: current,
|
||||
pageSize: pageSize,
|
||||
total: total,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
|
||||
}"
|
||||
row-key="id"
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- 网格视图 -->
|
||||
<div v-if="viewMode === 'grid'" class="bg-white rounded-lg shadow-sm p-4">
|
||||
<Row :gutter="[16, 16]">
|
||||
<Col
|
||||
v-for="product in productList"
|
||||
:key="product.id"
|
||||
:xs="12"
|
||||
:sm="8"
|
||||
:md="6"
|
||||
:lg="4"
|
||||
:xl="3"
|
||||
>
|
||||
<ProductCard
|
||||
:product="product"
|
||||
@edit="handleEdit"
|
||||
@delete="handleDelete"
|
||||
@manage-specs="handleManageSpecs"
|
||||
@view-details="handleViewDetails"
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<!-- 分页 -->
|
||||
<div class="mt-4 flex justify-center">
|
||||
<Pagination
|
||||
v-model:current="current"
|
||||
v-model:page-size="pageSize"
|
||||
:total="total"
|
||||
:show-size-changer="true"
|
||||
:show-quick-jumper="true"
|
||||
:show-total="(total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`"
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 商品表单模态框 -->
|
||||
<ProductFormModal
|
||||
ref="productFormModalRef"
|
||||
@success="handleModalSuccess"
|
||||
/>
|
||||
|
||||
<!-- 商品规格管理模态框 -->
|
||||
<ProductSpecModal
|
||||
ref="productSpecModalRef"
|
||||
@success="handleSpecModalSuccess"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { h, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, Input, InputNumber, Table, message, Row, Col, Pagination, Form, Space, Tag } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import {
|
||||
deleteProductApi,
|
||||
getProductPageApi,
|
||||
type ProductVO,
|
||||
type ProductDTO
|
||||
} from '#/api/shop';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import ProductFormModal from './components/ProductFormModal.vue';
|
||||
// @ts-ignore
|
||||
import ProductSpecModal from './components/ProductSpecModal.vue';
|
||||
import ProductCard from './components/ProductCard.vue';
|
||||
|
||||
// 创建图标组件
|
||||
const GridIcon = createIconifyIcon('lucide:grid-3x3');
|
||||
const ListIcon = createIconifyIcon('lucide:list');
|
||||
const PlusIcon = createIconifyIcon('lucide:plus');
|
||||
|
||||
defineOptions({ name: 'ProductManagement' });
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const productList = ref<ProductVO.ProductInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const viewMode = ref<'table' | 'grid'>('grid');
|
||||
const productFormModalRef = ref();
|
||||
const productSpecModalRef = ref();
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
name: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
minCostPrice: undefined as number | undefined,
|
||||
maxCostPrice: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 表格列配置
|
||||
const columns = [
|
||||
{
|
||||
title: '商品名称',
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
title: '主图',
|
||||
dataIndex: 'mainImage',
|
||||
key: 'mainImage',
|
||||
width: 100,
|
||||
customRender: ({ record }: { record: ProductVO.ProductInfoVO }) => {
|
||||
if (record.mainImage) {
|
||||
return h('img', {
|
||||
src: record.mainImage,
|
||||
alt: record.name,
|
||||
style: { width: '40px', height: '40px', objectFit: 'cover', borderRadius: '4px' },
|
||||
});
|
||||
}
|
||||
return h('span', { style: { color: '#999' } }, '暂无图片');
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '规格数量',
|
||||
dataIndex: 'specs',
|
||||
key: 'specs',
|
||||
width: 100,
|
||||
customRender: ({ record }: { record: ProductVO.ProductInfoVO }) => {
|
||||
const specCount = record.specs?.length || 0;
|
||||
return h(Tag, { color: specCount > 0 ? 'blue' : 'default' }, () => `${specCount} 个规格`);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'createTime',
|
||||
key: 'createTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '更新时间',
|
||||
dataIndex: 'updateTime',
|
||||
key: 'updateTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 200,
|
||||
fixed: 'right' as const,
|
||||
customRender: ({ record }: { record: ProductVO.ProductInfoVO }) => {
|
||||
return h('div', { style: 'display: flex; gap: 8px;' }, [
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
onClick: () => handleEdit(record),
|
||||
}, { default: () => '编辑' }),
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
onClick: () => handleManageSpecs(record),
|
||||
}, { default: () => '规格' }),
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
danger: true,
|
||||
onClick: () => handleDelete(record),
|
||||
}, { default: () => '删除' }),
|
||||
]);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// 获取商品列表
|
||||
async function fetchProductList() {
|
||||
try {
|
||||
loading.value = true;
|
||||
const params: PageQueryDTO<ProductDTO.ProductFiltersDTO> = {
|
||||
page: current.value,
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
name: searchForm.value.name || undefined,
|
||||
specCode: searchForm.value.specCode || undefined,
|
||||
skuCode: searchForm.value.skuCode || undefined,
|
||||
minCostPrice: searchForm.value.minCostPrice,
|
||||
maxCostPrice: searchForm.value.maxCostPrice,
|
||||
},
|
||||
};
|
||||
const data = await getProductPageApi(params);
|
||||
productList.value = data.records;
|
||||
total.value = data.total;
|
||||
} catch (error) {
|
||||
console.error('获取商品列表失败:', error);
|
||||
message.error('获取商品列表失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理搜索
|
||||
function handleSearch() {
|
||||
current.value = 1;
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
// 处理重置
|
||||
function handleReset() {
|
||||
searchForm.value = {
|
||||
name: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
minCostPrice: undefined,
|
||||
maxCostPrice: undefined,
|
||||
};
|
||||
current.value = 1;
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
// 处理分页变化
|
||||
function handleTableChange(pagination: any) {
|
||||
current.value = pagination.current;
|
||||
pageSize.value = pagination.pageSize;
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
// 切换视图模式
|
||||
function toggleViewMode() {
|
||||
viewMode.value = viewMode.value === 'table' ? 'grid' : 'table';
|
||||
}
|
||||
|
||||
// 处理添加商品
|
||||
function handleAdd() {
|
||||
productFormModalRef.value?.open(false);
|
||||
}
|
||||
|
||||
// 处理编辑商品
|
||||
function handleEdit(record: ProductVO.ProductInfoVO) {
|
||||
productFormModalRef.value?.open(true, record);
|
||||
}
|
||||
|
||||
// 处理删除商品
|
||||
async function handleDelete(record: ProductVO.ProductInfoVO) {
|
||||
try {
|
||||
await deleteProductApi(record.id);
|
||||
message.success('删除成功');
|
||||
await fetchProductList();
|
||||
} catch (error) {
|
||||
console.error('删除商品失败:', error);
|
||||
message.error('删除商品失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理管理规格
|
||||
function handleManageSpecs(record: ProductVO.ProductInfoVO) {
|
||||
productSpecModalRef.value?.open(record);
|
||||
}
|
||||
|
||||
// 处理查看详情
|
||||
function handleViewDetails() {
|
||||
// TODO: 实现查看详情功能
|
||||
message.info('查看详情功能待实现');
|
||||
}
|
||||
|
||||
// 处理模态框成功
|
||||
function handleModalSuccess() {
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
// 处理规格模态框成功
|
||||
function handleSpecModalSuccess() {
|
||||
// 规格管理成功后可能需要刷新商品列表
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
|
||||
// 初始化
|
||||
fetchProductList();
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.line-clamp-2 {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
-305
@@ -1,305 +0,0 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="店铺Logo" name="logoImageId">
|
||||
<div style="display: flex; align-items: center; gap: 16px;">
|
||||
|
||||
<!-- Logo预览区域 -->
|
||||
<div style="position: relative; display: inline-block;">
|
||||
<!-- 有图片时显示图片 -->
|
||||
<img
|
||||
v-if="currentLogoUrl && !imageLoadError"
|
||||
:src="currentLogoUrl"
|
||||
alt="店铺Logo"
|
||||
style="width: 80px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid #d9d9d9; background-color: #f5f5f5;"
|
||||
@error="handleImageError"
|
||||
/>
|
||||
<!-- 无图片或加载失败时显示空状态 -->
|
||||
<div
|
||||
v-else
|
||||
style="width: 80px; height: 80px; border: 1px dashed #d9d9d9; border-radius: 8px; background-color: #fafafa; display: flex; align-items: center; justify-content: center; color: #999; font-size: 12px; text-align: center; line-height: 1.2;">
|
||||
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div>上传Logo</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 点击更换遮罩 -->
|
||||
<div
|
||||
style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); border-radius: 8px; display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s; cursor: pointer;"
|
||||
@mouseenter="($event.target as HTMLElement).style.opacity = '1'"
|
||||
@mouseleave="($event.target as HTMLElement).style.opacity = '0'"
|
||||
@click="selectLogoFile">
|
||||
<div style="color: white; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div style="font-size: 12px;">{{ currentLogoUrl && !imageLoadError ? '更换Logo' : '上传Logo' }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 重置按钮 -->
|
||||
<div v-if="newLogoFile">
|
||||
<Button size="small" @click="removeNewLogo">重置</Button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="店铺名称" name="name" :rules="[{ required: true, message: '请输入店铺名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入店铺名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="平台" name="platform">
|
||||
<Select
|
||||
v-model:value="formData.platform"
|
||||
placeholder="请选择平台"
|
||||
:options="platformOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, onUnmounted } from 'vue';
|
||||
import { Form, Input, Modal, Select, Button, message } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import {
|
||||
uploadShopLogoApi,
|
||||
createShopApi,
|
||||
updateShopApi,
|
||||
type ShopVO,
|
||||
type ShopDTO
|
||||
} from '#/api/shop';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 创建相机图标组件
|
||||
const CameraIcon = createIconifyIcon('mdi:camera');
|
||||
|
||||
// 内部状态管理
|
||||
const visible = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentShopData = ref<ShopVO.ShopInfoVO | Partial<ShopVO.ShopInfoVO> | null>(null);
|
||||
|
||||
// 响应式数据
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const newLogoFile = ref<File | null>(null); // 新选择的logo文件
|
||||
const originalLogoUrl = ref(''); // 原始logo URL
|
||||
const imageLoadError = ref(false); // 图片加载错误状态
|
||||
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
platform: '',
|
||||
logoImageId: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 平台选项
|
||||
const platformOptions = [
|
||||
{ label: '淘宝', value: '淘宝' },
|
||||
{ label: '京东', value: '京东' },
|
||||
{ label: '拼多多', value: '拼多多' },
|
||||
{ label: '天猫', value: '天猫' },
|
||||
];
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => isEdit.value ? '编辑店铺' : '添加店铺');
|
||||
|
||||
// 当前显示的logo URL(优先显示新选择的)
|
||||
const currentLogoUrl = computed(() => {
|
||||
if (newLogoFile.value) {
|
||||
return URL.createObjectURL(newLogoFile.value);
|
||||
}
|
||||
return originalLogoUrl.value;
|
||||
});
|
||||
|
||||
// 初始化表单数据
|
||||
function initFormData() {
|
||||
if (isEdit.value && currentShopData.value) {
|
||||
// 编辑模式
|
||||
formData.value = {
|
||||
name: currentShopData.value.name || '',
|
||||
platform: currentShopData.value.platform || '',
|
||||
logoImageId: undefined,
|
||||
};
|
||||
|
||||
// 设置原始Logo URL
|
||||
originalLogoUrl.value = currentShopData.value.logo || '';
|
||||
} else {
|
||||
// 创建模式 - 如果有传入的shopData,则使用它来初始化表单
|
||||
formData.value = {
|
||||
name: currentShopData.value?.name || '',
|
||||
platform: currentShopData.value?.platform || '',
|
||||
logoImageId: undefined,
|
||||
};
|
||||
originalLogoUrl.value = currentShopData.value?.logo || '';
|
||||
}
|
||||
|
||||
// 重置新选择的logo和图片加载状态
|
||||
newLogoFile.value = null;
|
||||
imageLoadError.value = false;
|
||||
}
|
||||
|
||||
// 重置表单(在open方法中调用)
|
||||
function resetForm() {
|
||||
formRef.value?.resetFields();
|
||||
newLogoFile.value = null;
|
||||
originalLogoUrl.value = '';
|
||||
imageLoadError.value = false;
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
|
||||
// 准备提交数据
|
||||
const submitData = { ...formData.value };
|
||||
|
||||
// 如果有新选择的logo,先上传获取ID
|
||||
if (newLogoFile.value) {
|
||||
const logoImageId = await uploadLogoAndGetId();
|
||||
if (logoImageId) {
|
||||
submitData.logoImageId = logoImageId;
|
||||
}
|
||||
} else if (!isEdit.value) {
|
||||
// 创建模式且没有选择Logo,移除logoImageId字段
|
||||
delete submitData.logoImageId;
|
||||
}
|
||||
|
||||
// 调用API
|
||||
if (isEdit.value && currentShopData.value && 'id' in currentShopData.value && currentShopData.value.id) {
|
||||
await updateShopApi(currentShopData.value.id, submitData as ShopDTO.UpdateShopDTO);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createShopApi(submitData as ShopDTO.CreateShopDTO);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存店铺失败:', error);
|
||||
message.error('保存店铺失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(editMode: boolean = false, shopData?: ShopVO.ShopInfoVO | Partial<ShopVO.ShopInfoVO> | null) {
|
||||
isEdit.value = editMode;
|
||||
currentShopData.value = shopData || null;
|
||||
|
||||
resetForm(); // 先重置表单
|
||||
initFormData(); // 再初始化数据
|
||||
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
|
||||
// 选择logo文件
|
||||
function selectLogoFile() {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = 'image/*';
|
||||
input.style.display = 'none';
|
||||
document.body.appendChild(input);
|
||||
|
||||
input.onchange = (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0];
|
||||
if (file) {
|
||||
// 验证文件
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
const isLt2M = file.size / 1024 / 1024 < 2;
|
||||
if (!isLt2M) {
|
||||
message.error('图片大小不能超过 2MB');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置新选择的文件
|
||||
newLogoFile.value = file;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('Logo选择成功');
|
||||
}
|
||||
|
||||
document.body.removeChild(input);
|
||||
};
|
||||
|
||||
input.click();
|
||||
}
|
||||
|
||||
// 重置新选择的logo
|
||||
function removeNewLogo() {
|
||||
newLogoFile.value = null;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('已重置logo');
|
||||
}
|
||||
|
||||
// 上传Logo并获取图片ID
|
||||
async function uploadLogoAndGetId(): Promise<number | undefined> {
|
||||
if (!newLogoFile.value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await uploadShopLogoApi(newLogoFile.value);
|
||||
console.log('Logo上传成功:', response);
|
||||
return response.id;
|
||||
} catch (error) {
|
||||
console.error('Logo上传失败:', error);
|
||||
message.error('Logo上传失败');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 图片加载错误处理
|
||||
function handleImageError(event: Event) {
|
||||
console.error('图片加载失败:', event);
|
||||
imageLoadError.value = true;
|
||||
message.error('图片加载失败,请重新选择');
|
||||
}
|
||||
|
||||
// 组件卸载时清理
|
||||
onUnmounted(() => {
|
||||
if (newLogoFile.value) {
|
||||
const url = URL.createObjectURL(newLogoFile.value);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -1,250 +0,0 @@
|
||||
<script lang="ts" setup>
|
||||
import { h, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, Input, Select, Table, message } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createShopApi,
|
||||
deleteShopApi,
|
||||
getShopPageApi,
|
||||
updateShopApi,
|
||||
type ShopVO ,
|
||||
type ShopDTO
|
||||
} from '#/api/shop';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import ShopFormModal from './components/ShopFormModal.vue';
|
||||
|
||||
defineOptions({ name: 'ShopManagement' });
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const shopList = ref<ShopVO.ShopInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const shopFormModalRef = ref();
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
name: '',
|
||||
platform: undefined as string | undefined,
|
||||
});
|
||||
|
||||
// 表单数据(已移至ShopFormModal组件)
|
||||
|
||||
// 平台选项
|
||||
const platformOptions = [
|
||||
{ label: '淘宝', value: '淘宝' },
|
||||
{ label: '京东', value: '京东' },
|
||||
{ label: '拼多多', value: '拼多多' },
|
||||
{ label: '天猫', value: '天猫' },
|
||||
];
|
||||
|
||||
// 表格列配置
|
||||
const columns = [
|
||||
{
|
||||
title: '店铺名称',
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
title: '平台',
|
||||
dataIndex: 'platform',
|
||||
key: 'platform',
|
||||
width: 120,
|
||||
},
|
||||
{
|
||||
title: 'Logo',
|
||||
dataIndex: 'logo',
|
||||
key: 'logo',
|
||||
width: 200,
|
||||
customRender: ({ record }: { record: ShopVO.ShopInfoVO }) => {
|
||||
if (record.logo) {
|
||||
return h('img', {
|
||||
src: record.logo,
|
||||
alt: record.name,
|
||||
style: { width: '40px', height: '40px', objectFit: 'cover', borderRadius: '4px' },
|
||||
});
|
||||
}
|
||||
return h('span', { style: { color: '#999' } }, '暂无Logo');
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'createTime',
|
||||
key: 'createTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '更新时间',
|
||||
dataIndex: 'updateTime',
|
||||
key: 'updateTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 200,
|
||||
fixed: 'right' as const,
|
||||
customRender: ({ record }: { record: ShopVO.ShopInfoVO }) => {
|
||||
return h('div', { style: 'display: flex; gap: 8px;' }, [
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
onClick: () => handleEdit(record),
|
||||
}, { default: () => '编辑' }),
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
danger: true,
|
||||
onClick: () => handleDelete(record),
|
||||
}, { default: () => '删除' }),
|
||||
]);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// 获取店铺列表
|
||||
async function fetchShopList() {
|
||||
try {
|
||||
loading.value = true;
|
||||
const params: PageQueryDTO<ShopDTO.ShopFiltersDTO> = {
|
||||
page: current.value,
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
name: searchForm.value.name || undefined,
|
||||
platform: searchForm.value.platform,
|
||||
},
|
||||
};
|
||||
const data = await getShopPageApi(params);
|
||||
shopList.value = data.records;
|
||||
total.value = data.total;
|
||||
} catch (error) {
|
||||
console.error('获取店铺列表失败:', error);
|
||||
message.error('获取店铺列表失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理搜索
|
||||
function handleSearch() {
|
||||
current.value = 1;
|
||||
fetchShopList();
|
||||
}
|
||||
|
||||
// 处理重置
|
||||
function handleReset() {
|
||||
searchForm.value = {
|
||||
name: '',
|
||||
platform: undefined,
|
||||
};
|
||||
current.value = 1;
|
||||
fetchShopList();
|
||||
}
|
||||
|
||||
// 处理分页变化
|
||||
function handleTableChange(pagination: any) {
|
||||
current.value = pagination.current;
|
||||
pageSize.value = pagination.pageSize;
|
||||
fetchShopList();
|
||||
}
|
||||
|
||||
// 处理添加店铺
|
||||
function handleAdd() {
|
||||
shopFormModalRef.value?.open(false);
|
||||
}
|
||||
|
||||
// 处理编辑店铺
|
||||
function handleEdit(record: ShopVO.ShopInfoVO) {
|
||||
shopFormModalRef.value?.open(true, record);
|
||||
}
|
||||
|
||||
// 处理删除店铺
|
||||
async function handleDelete(record: ShopVO.ShopInfoVO) {
|
||||
try {
|
||||
await deleteShopApi(record.id);
|
||||
message.success('删除成功');
|
||||
await fetchShopList();
|
||||
} catch (error) {
|
||||
console.error('删除店铺失败:', error);
|
||||
message.error('删除店铺失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理模态框成功
|
||||
function handleModalSuccess() {
|
||||
fetchShopList();
|
||||
}
|
||||
|
||||
// 初始化
|
||||
fetchShopList();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page title="店铺管理">
|
||||
<template #description>
|
||||
<div class="text-foreground/80 mt-2">
|
||||
管理电商店铺信息,包括店铺基本信息、平台信息和Logo设置
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #extra>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
添加店铺
|
||||
</Button>
|
||||
</template>
|
||||
|
||||
<!-- 搜索表单 -->
|
||||
<div class="mb-4 p-4 bg-gray-50 rounded-lg">
|
||||
<div style="display: flex; gap: 16px; align-items: center; flex-wrap: wrap;">
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">店铺名称</label>
|
||||
<Input
|
||||
v-model:value="searchForm.name"
|
||||
placeholder="请输入店铺名称"
|
||||
style="width: 200px;"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">平台</label>
|
||||
<Select
|
||||
v-model:value="searchForm.platform"
|
||||
placeholder="请选择平台"
|
||||
style="width: 120px;"
|
||||
:options="platformOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</div>
|
||||
<div style="display: flex; gap: 8px; align-items: end;">
|
||||
<Button type="primary" @click="handleSearch">搜索</Button>
|
||||
<Button @click="handleReset">重置</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 数据表格 -->
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="shopList"
|
||||
:loading="loading"
|
||||
:pagination="{
|
||||
current: current,
|
||||
pageSize: pageSize,
|
||||
total: total,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
|
||||
}"
|
||||
row-key="id"
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
|
||||
<!-- 店铺表单模态框 -->
|
||||
<ShopFormModal
|
||||
ref="shopFormModalRef"
|
||||
@success="handleModalSuccess"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
@@ -230,7 +230,7 @@ async function fetchOrderStatusOptions() {
|
||||
try {
|
||||
const data = await getActiveOrderStatusListApi();
|
||||
orderStatusOptions.value = data.map((status: OrderStatusVO.OrderStatusInfoVO) => ({
|
||||
label: status.name,
|
||||
label: status.statusName,
|
||||
value: status.id,
|
||||
}));
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
VITE_BASE=/
|
||||
|
||||
# 接口地址
|
||||
VITE_GLOB_API_URL=https://mock-napi.vben.pro/api
|
||||
VITE_GLOB_API_URL=/api
|
||||
|
||||
# 是否开启压缩,可以设置为 none, brotli, gzip
|
||||
VITE_COMPRESS=none
|
||||
@@ -10,10 +10,10 @@ VITE_COMPRESS=none
|
||||
VITE_PWA=false
|
||||
|
||||
# vue-router 的模式
|
||||
VITE_ROUTER_HISTORY=hash
|
||||
VITE_ROUTER_HISTORY=history
|
||||
|
||||
# 是否注入全局loading
|
||||
VITE_INJECT_APP_LOADING=true
|
||||
|
||||
# 打包后是否生成dist.zip
|
||||
VITE_ARCHIVER=true
|
||||
VITE_ARCHIVER=false
|
||||
|
||||
Reference in New Issue
Block a user