Pārlūkot izejas kodu

feat(category): 添加分类图标上传与展示功能

- 在分类列表中新增图标列,支持图片预览
- 新增图标上传组件,支持选择、预览和删除图标
- 实现图标上传逻辑,调用 tournament 上传接口
- 表单重置时清空图标相关状态
- 编辑分类时回显已有图标
- 添加图标预览放大功能
- 扩展分类类型定义,增加 iconUrl 字段
fugui001 1 nedēļu atpakaļ
vecāks
revīzija
828c3796a7

+ 4 - 0
src/api/system/business/category/types.ts

@@ -33,6 +33,8 @@ export interface CategoryVO {
    *
    */
   updatedAt: string;
+
+  iconUrl?: string;
 }
 
 export interface CategoryForm extends BaseEntity {
@@ -70,6 +72,8 @@ export interface CategoryForm extends BaseEntity {
    *
    */
   updatedAt?: string;
+
+  iconUrl?: string;
 }
 
 export interface CategoryQuery extends PageQuery {

+ 116 - 1
src/views/system/business/category/index.vue

@@ -48,6 +48,12 @@
         <el-table-column label="分类名称" align="center" prop="name" />
         <el-table-column label="唯一编码" align="center" prop="code" />
         <el-table-column label="排序" align="center" prop="sort" />
+        <el-table-column label="图标" align="center" prop="iconUrl" width="120">
+          <template #default="scope">
+            <img v-if="scope.row.iconUrl" :src="scope.row.iconUrl" alt="" style="width: 40px; height: 40px; object-fit: cover; border-radius: 4px" />
+            <span v-else>无</span>
+          </template>
+        </el-table-column>
         <el-table-column label="是否显示" align="center" prop="isShow" />
         <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
           <template #default="scope">
@@ -80,6 +86,43 @@
         <el-form-item label="排序" prop="sort">
           <el-input v-model="form.sort" placeholder="请输入排序,越大越靠前" />
         </el-form-item>
+        <el-form-item label="图标" prop="iconUrl">
+          <div class="upload-container">
+            <el-upload
+              class="upload-icon"
+              action="#"
+              :on-change="handleIconChange"
+              :on-remove="handleIconRemove"
+              :file-list="fileList"
+              :auto-upload="false"
+              :limit="1"
+              accept="image/*"
+            >
+              <template #trigger>
+                <el-button type="primary">点击选择图标</el-button>
+              </template>
+
+              <!-- 预览图区域 -->
+              <template #default>
+                <div class="preview-area" @click="handlePreviewClick">
+                  <img
+                    v-if="iconPreviewUrl || form.iconUrl"
+                    :src="iconPreviewUrl || form.iconUrl"
+                    alt="预览图"
+                    style="max-width: 100px; max-height: 100px; margin-top: 10px; cursor: pointer"
+                  />
+                  <div v-else style="margin-top: 10px; color: #999">无</div>
+                </div>
+              </template>
+
+              <template #tip>
+                <div class="el-upload__tip">
+                  <span v-if="fileList.length > 0">当前已选文件:{{ fileList[0].name }}</span>
+                </div>
+              </template>
+            </el-upload>
+          </div>
+        </el-form-item>
         <el-form-item label="是否显示" prop="isShow">
           <el-radio-group v-model="form.isShow">
             <el-radio :label="1">是</el-radio>
@@ -100,7 +143,7 @@
 <script setup name="Category" lang="ts">
 import { listCategory, getCategory, delCategory, addCategory, updateCategory } from '@/api/system/business/category';
 import { CategoryVO, CategoryQuery, CategoryForm } from '@/api/system/business/category/types';
-
+import { uploadTournament } from '@/api/system/business/tournaments';
 const { proxy } = getCurrentInstance() as ComponentInternalInstance;
 
 const categoryList = ref<CategoryVO[]>([]);
@@ -174,6 +217,10 @@ const cancel = () => {
 const reset = () => {
   form.value = { ...initFormData };
   categoryFormRef.value?.resetFields();
+  // 清空上传相关状态
+  fileList.value = [];
+  iconPreviewUrl.value = '';
+  form.value.iconUrl = '';
 };
 
 /** 搜索按钮操作 */
@@ -200,6 +247,10 @@ const handleAdd = () => {
   reset();
   dialog.visible = true;
   dialog.title = '添加新闻分类';
+  // 清空上传相关状态
+  fileList.value = [];
+  iconPreviewUrl.value = '';
+  form.value.iconUrl = '';
 };
 
 /** 修改按钮操作 */
@@ -208,6 +259,10 @@ const handleUpdate = async (row?: CategoryVO) => {
   const _id = row?.id || ids.value[0];
   const res = await getCategory(_id);
   Object.assign(form.value, res.data);
+  // 设置预览图
+  if (form.value.iconUrl) {
+    iconPreviewUrl.value = form.value.iconUrl;
+  }
   dialog.visible = true;
   dialog.title = '修改新闻分类';
 };
@@ -252,4 +307,64 @@ const handleExport = () => {
 onMounted(() => {
   getList();
 });
+
+// 添加响应式变量(在其他ref声明附近添加)
+const iconPreviewUrl = ref('');
+const fileList = ref([]);
+const dialogVisible = ref(false);
+const previewSrc = ref('');
+
+/** 处理图标上传 */
+const handleIconChange = async (file) => {
+  const index = fileList.value.findIndex((f) => f.uid === file.uid);
+  fileList.value = [];
+
+  if (file.raw) {
+    iconPreviewUrl.value = URL.createObjectURL(file.raw);
+  }
+
+  if (index === -1) {
+    fileList.value.push(file);
+  }
+
+  try {
+    const rawFile = file.raw;
+    const res = await uploadTournament(rawFile);
+    if (res.code === 200) {
+      fileList.value[index] = {
+        ...file,
+        status: 'success',
+        response: res.data.url
+      };
+      form.value.iconUrl = fileList.value[index].response;
+      iconPreviewUrl.value = fileList.value[index].response;
+      proxy?.$modal.msgSuccess('上传成功');
+    } else {
+      throw new Error(res.msg);
+    }
+  } catch (error) {
+    fileList.value[index] = {
+      ...file,
+      status: 'fail',
+      error: '上传失败'
+    };
+    proxy?.$modal.msgError('上传失败,请重试');
+  }
+};
+
+/** 处理图标删除 */
+const handleIconRemove = (file, updatedFileList) => {
+  fileList.value = updatedFileList;
+  iconPreviewUrl.value = '';
+  form.value.iconUrl = '';
+};
+
+/** 点击预览图触发放大 */
+const handlePreviewClick = () => {
+  const currentSrc = iconPreviewUrl.value || form.value.iconUrl;
+  if (currentSrc) {
+    previewSrc.value = currentSrc;
+    dialogVisible.value = true;
+  }
+};
 </script>