mirror of
https://github.com/ctwj/urldb.git
synced 2025-11-25 03:15:04 +08:00
update: category
This commit is contained in:
@@ -52,41 +52,54 @@ func ToResourceResponseList(resources []entity.Resource) []dto.ResourceResponse
|
||||
}
|
||||
|
||||
// ToCategoryResponse 将Category实体转换为CategoryResponse
|
||||
func ToCategoryResponse(category *entity.Category, resourceCount int64) dto.CategoryResponse {
|
||||
func ToCategoryResponse(category *entity.Category, resourceCount int64, tagCount int64) dto.CategoryResponse {
|
||||
return dto.CategoryResponse{
|
||||
ID: category.ID,
|
||||
Name: category.Name,
|
||||
Description: category.Description,
|
||||
ResourceCount: resourceCount,
|
||||
TagCount: tagCount,
|
||||
}
|
||||
}
|
||||
|
||||
// ToCategoryResponseList 将Category实体列表转换为CategoryResponse列表
|
||||
func ToCategoryResponseList(categories []entity.Category, resourceCounts map[uint]int64) []dto.CategoryResponse {
|
||||
func ToCategoryResponseList(categories []entity.Category, resourceCounts map[uint]int64, tagCounts map[uint]int64) []dto.CategoryResponse {
|
||||
responses := make([]dto.CategoryResponse, len(categories))
|
||||
for i, category := range categories {
|
||||
count := resourceCounts[category.ID]
|
||||
responses[i] = ToCategoryResponse(&category, count)
|
||||
resourceCount := resourceCounts[category.ID]
|
||||
tagCount := tagCounts[category.ID]
|
||||
responses[i] = ToCategoryResponse(&category, resourceCount, tagCount)
|
||||
}
|
||||
return responses
|
||||
}
|
||||
|
||||
// ToTagResponse 将Tag实体转换为TagResponse
|
||||
func ToTagResponse(tag *entity.Tag, resourceCount int64) dto.TagResponse {
|
||||
return dto.TagResponse{
|
||||
response := dto.TagResponse{
|
||||
ID: tag.ID,
|
||||
Name: tag.Name,
|
||||
Description: tag.Description,
|
||||
CategoryID: tag.CategoryID,
|
||||
ResourceCount: resourceCount,
|
||||
}
|
||||
|
||||
// 设置分类名称
|
||||
if tag.CategoryID != nil && tag.Category.ID != 0 {
|
||||
response.CategoryName = tag.Category.Name
|
||||
} else if tag.CategoryID != nil {
|
||||
// 如果CategoryID存在但Category没有预加载,设置为"未知分类"
|
||||
response.CategoryName = "未知分类"
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
// ToTagResponseList 将Tag实体列表转换为TagResponse列表
|
||||
func ToTagResponseList(tags []entity.Tag, resourceCounts map[uint]int64) []dto.TagResponse {
|
||||
responses := make([]dto.TagResponse, len(tags))
|
||||
for i, tag := range tags {
|
||||
count := resourceCounts[tag.ID]
|
||||
responses[i] = ToTagResponse(&tag, count)
|
||||
resourceCount := resourceCounts[tag.ID]
|
||||
responses[i] = ToTagResponse(&tag, resourceCount)
|
||||
}
|
||||
return responses
|
||||
}
|
||||
|
||||
@@ -82,12 +82,14 @@ type UpdateCategoryRequest struct {
|
||||
type CreateTagRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
CategoryID *uint `json:"category_id"`
|
||||
}
|
||||
|
||||
// UpdateTagRequest 更新标签请求
|
||||
type UpdateTagRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CategoryID *uint `json:"category_id"`
|
||||
}
|
||||
|
||||
// CreateReadyResourceRequest 创建待处理资源请求
|
||||
|
||||
@@ -35,6 +35,7 @@ type CategoryResponse struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
ResourceCount int64 `json:"resource_count"`
|
||||
TagCount int64 `json:"tag_count"`
|
||||
}
|
||||
|
||||
// TagResponse 标签响应
|
||||
@@ -42,6 +43,8 @@ type TagResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CategoryID *uint `json:"category_id"`
|
||||
CategoryName string `json:"category_name"`
|
||||
ResourceCount int64 `json:"resource_count"`
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ type Category struct {
|
||||
|
||||
// 关联关系
|
||||
Resources []Resource `json:"resources" gorm:"foreignKey:CategoryID"`
|
||||
Tags []Tag `json:"tags" gorm:"foreignKey:CategoryID"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -11,11 +11,13 @@ type Tag struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
Name string `json:"name" gorm:"size:100;not null;unique;comment:标签名称"`
|
||||
Description string `json:"description" gorm:"type:text;comment:标签描述"`
|
||||
CategoryID *uint `json:"category_id" gorm:"comment:分类ID,可以为空"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
|
||||
// 关联关系
|
||||
Category Category `json:"category" gorm:"foreignKey:CategoryID"`
|
||||
Resources []Resource `json:"resources" gorm:"many2many:resource_tags;"`
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ func (r *BaseRepositoryImpl[T]) FindAll() ([]T, error) {
|
||||
|
||||
// Update 更新实体
|
||||
func (r *BaseRepositoryImpl[T]) Update(entity *T) error {
|
||||
return r.db.Save(entity).Error
|
||||
return r.db.Model(entity).Updates(entity).Error
|
||||
}
|
||||
|
||||
// Delete 删除实体
|
||||
|
||||
@@ -11,7 +11,9 @@ type CategoryRepository interface {
|
||||
BaseRepository[entity.Category]
|
||||
FindByName(name string) (*entity.Category, error)
|
||||
FindWithResources() ([]entity.Category, error)
|
||||
FindWithTags() ([]entity.Category, error)
|
||||
GetResourceCount(categoryID uint) (int64, error)
|
||||
GetTagCount(categoryID uint) (int64, error)
|
||||
FindWithPagination(page, pageSize int) ([]entity.Category, int64, error)
|
||||
Search(query string, page, pageSize int) ([]entity.Category, int64, error)
|
||||
}
|
||||
@@ -45,6 +47,13 @@ func (r *CategoryRepositoryImpl) FindWithResources() ([]entity.Category, error)
|
||||
return categories, err
|
||||
}
|
||||
|
||||
// FindWithTags 查找包含标签的分类
|
||||
func (r *CategoryRepositoryImpl) FindWithTags() ([]entity.Category, error) {
|
||||
var categories []entity.Category
|
||||
err := r.db.Preload("Tags").Find(&categories).Error
|
||||
return categories, err
|
||||
}
|
||||
|
||||
// GetResourceCount 获取分类下的资源数量
|
||||
func (r *CategoryRepositoryImpl) GetResourceCount(categoryID uint) (int64, error) {
|
||||
var count int64
|
||||
@@ -52,6 +61,13 @@ func (r *CategoryRepositoryImpl) GetResourceCount(categoryID uint) (int64, error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetTagCount 获取分类下的标签数量
|
||||
func (r *CategoryRepositoryImpl) GetTagCount(categoryID uint) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&entity.Tag{}).Where("category_id = ?", categoryID).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// FindWithPagination 分页查询分类
|
||||
func (r *CategoryRepositoryImpl) FindWithPagination(page, pageSize int) ([]entity.Category, int64, error) {
|
||||
var categories []entity.Category
|
||||
|
||||
@@ -11,10 +11,13 @@ type TagRepository interface {
|
||||
BaseRepository[entity.Tag]
|
||||
FindByName(name string) (*entity.Tag, error)
|
||||
FindWithResources() ([]entity.Tag, error)
|
||||
FindByCategoryID(categoryID uint) ([]entity.Tag, error)
|
||||
FindByCategoryIDPaginated(categoryID uint, page, pageSize int) ([]entity.Tag, int64, error)
|
||||
GetResourceCount(tagID uint) (int64, error)
|
||||
FindByResourceID(resourceID uint) ([]entity.Tag, error)
|
||||
FindWithPagination(page, pageSize int) ([]entity.Tag, int64, error)
|
||||
Search(query string, page, pageSize int) ([]entity.Tag, int64, error)
|
||||
UpdateWithNulls(tag *entity.Tag) error
|
||||
}
|
||||
|
||||
// TagRepositoryImpl Tag的Repository实现
|
||||
@@ -46,6 +49,35 @@ func (r *TagRepositoryImpl) FindWithResources() ([]entity.Tag, error) {
|
||||
return tags, err
|
||||
}
|
||||
|
||||
// FindByCategoryID 根据分类ID查找标签
|
||||
func (r *TagRepositoryImpl) FindByCategoryID(categoryID uint) ([]entity.Tag, error) {
|
||||
var tags []entity.Tag
|
||||
err := r.db.Where("category_id = ?", categoryID).Preload("Category").Find(&tags).Error
|
||||
return tags, err
|
||||
}
|
||||
|
||||
// FindByCategoryIDPaginated 分页根据分类ID查找标签
|
||||
func (r *TagRepositoryImpl) FindByCategoryIDPaginated(categoryID uint, page, pageSize int) ([]entity.Tag, int64, error) {
|
||||
var tags []entity.Tag
|
||||
var total int64
|
||||
|
||||
// 获取总数
|
||||
err := r.db.Model(&entity.Tag{}).Where("category_id = ?", categoryID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = r.db.Where("category_id = ?", categoryID).Preload("Category").
|
||||
Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&tags).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return tags, total, nil
|
||||
}
|
||||
|
||||
// GetResourceCount 获取标签下的资源数量
|
||||
func (r *TagRepositoryImpl) GetResourceCount(tagID uint) (int64, error) {
|
||||
var count int64
|
||||
@@ -74,7 +106,7 @@ func (r *TagRepositoryImpl) FindWithPagination(page, pageSize int) ([]entity.Tag
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = r.db.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&tags).Error
|
||||
err = r.db.Preload("Category").Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&tags).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -99,10 +131,16 @@ func (r *TagRepositoryImpl) Search(query string, page, pageSize int) ([]entity.T
|
||||
// 分页搜索
|
||||
offset := (page - 1) * pageSize
|
||||
err = r.db.Where("name ILIKE ? OR description ILIKE ?", searchQuery, searchQuery).
|
||||
Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&tags).Error
|
||||
Preload("Category").Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&tags).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return tags, total, nil
|
||||
}
|
||||
|
||||
// UpdateWithNulls 更新标签,包括null值
|
||||
func (r *TagRepositoryImpl) UpdateWithNulls(tag *entity.Tag) error {
|
||||
// 使用Select方法明确指定要更新的字段,包括null值
|
||||
return r.db.Model(tag).Select("name", "description", "category_id", "updated_at").Updates(tag).Error
|
||||
}
|
||||
|
||||
@@ -35,17 +35,26 @@ func GetCategories(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取每个分类的资源数量
|
||||
// 获取每个分类的资源数量和标签数量
|
||||
resourceCounts := make(map[uint]int64)
|
||||
tagCounts := make(map[uint]int64)
|
||||
for _, category := range categories {
|
||||
count, err := repoManager.CategoryRepository.GetResourceCount(category.ID)
|
||||
// 获取资源数量
|
||||
resourceCount, err := repoManager.CategoryRepository.GetResourceCount(category.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
resourceCounts[category.ID] = count
|
||||
resourceCounts[category.ID] = resourceCount
|
||||
|
||||
// 获取标签数量
|
||||
tagCount, err := repoManager.CategoryRepository.GetTagCount(category.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
tagCounts[category.ID] = tagCount
|
||||
}
|
||||
|
||||
responses := converter.ToCategoryResponseList(categories, resourceCounts)
|
||||
responses := converter.ToCategoryResponseList(categories, resourceCounts, tagCounts)
|
||||
|
||||
// 返回分页格式的响应
|
||||
SuccessResponse(c, gin.H{
|
||||
@@ -77,7 +86,7 @@ func CreateCategory(c *gin.Context) {
|
||||
|
||||
SuccessResponse(c, gin.H{
|
||||
"message": "分类创建成功",
|
||||
"category": converter.ToCategoryResponse(category, 0),
|
||||
"category": converter.ToCategoryResponse(category, 0, 0),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -67,6 +68,7 @@ func CreateTag(c *gin.Context) {
|
||||
tag := &entity.Tag{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
CategoryID: req.CategoryID,
|
||||
}
|
||||
|
||||
err := repoManager.TagRepository.Create(tag)
|
||||
@@ -123,14 +125,23 @@ func UpdateTag(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 添加调试信息
|
||||
fmt.Printf("更新标签 - ID: %d, 请求CategoryID: %v, 当前CategoryID: %v\n", id, req.CategoryID, tag.CategoryID)
|
||||
|
||||
if req.Name != "" {
|
||||
tag.Name = req.Name
|
||||
}
|
||||
if req.Description != "" {
|
||||
tag.Description = req.Description
|
||||
}
|
||||
// 处理CategoryID,包括设置为null的情况
|
||||
tag.CategoryID = req.CategoryID
|
||||
|
||||
err = repoManager.TagRepository.Update(tag)
|
||||
// 添加调试信息
|
||||
fmt.Printf("更新后CategoryID: %v\n", tag.CategoryID)
|
||||
|
||||
// 使用专门的更新方法,确保能更新null值
|
||||
err = repoManager.TagRepository.UpdateWithNulls(tag)
|
||||
if err != nil {
|
||||
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -199,3 +210,43 @@ func GetTagsGlobal(c *gin.Context) {
|
||||
responses := converter.ToTagResponseList(tags, resourceCounts)
|
||||
SuccessResponse(c, responses)
|
||||
}
|
||||
|
||||
// GetTagsByCategory 根据分类ID获取标签列表
|
||||
func GetTagsByCategory(c *gin.Context) {
|
||||
categoryIDStr := c.Param("categoryId")
|
||||
categoryID, err := strconv.ParseUint(categoryIDStr, 10, 32)
|
||||
if err != nil {
|
||||
ErrorResponse(c, "无效的分类ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取分页参数
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
tags, total, err := repoManager.TagRepository.FindByCategoryIDPaginated(uint(categoryID), page, pageSize)
|
||||
if err != nil {
|
||||
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取每个标签的资源数量
|
||||
resourceCounts := make(map[uint]int64)
|
||||
for _, tag := range tags {
|
||||
count, err := repoManager.TagRepository.GetResourceCount(tag.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
resourceCounts[tag.ID] = count
|
||||
}
|
||||
|
||||
responses := converter.ToTagResponseList(tags, resourceCounts)
|
||||
|
||||
// 返回分页格式的响应
|
||||
SuccessResponse(c, gin.H{
|
||||
"items": responses,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
1
main.go
1
main.go
@@ -123,6 +123,7 @@ func main() {
|
||||
api.PUT("/tags/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.UpdateTag)
|
||||
api.DELETE("/tags/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.DeleteTag)
|
||||
api.GET("/tags/:id", handlers.GetTagByID)
|
||||
api.GET("/categories/:categoryId/tags", handlers.GetTagsByCategory)
|
||||
|
||||
// 待处理资源管理
|
||||
api.GET("/ready-resources", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.GetReadyResources)
|
||||
|
||||
@@ -357,6 +357,15 @@ export const useTagApi = () => {
|
||||
return parseApiResponse(response)
|
||||
}
|
||||
|
||||
const getTagsByCategory = async (categoryId: number, params?: any) => {
|
||||
const response = await $fetch(`/categories/${categoryId}/tags`, {
|
||||
baseURL: config.public.apiBase,
|
||||
params,
|
||||
headers: getAuthHeaders() as Record<string, string>
|
||||
})
|
||||
return parseApiResponse(response)
|
||||
}
|
||||
|
||||
const getTag = async (id: number) => {
|
||||
const response = await $fetch(`/tags/${id}`, {
|
||||
baseURL: config.public.apiBase,
|
||||
@@ -404,6 +413,7 @@ export const useTagApi = () => {
|
||||
|
||||
return {
|
||||
getTags,
|
||||
getTagsByCategory,
|
||||
getTag,
|
||||
createTag,
|
||||
updateTag,
|
||||
|
||||
@@ -88,7 +88,7 @@
|
||||
</div>
|
||||
<div class="ml-4">
|
||||
<h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">平台管理</h3>
|
||||
<p class="text-sm text-gray-600 dark:text-gray-400">管理网盘平台</p>
|
||||
<p class="text-sm text-gray-600 dark:text-gray-400">暂不支持修改</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
|
||||
@@ -42,6 +42,17 @@
|
||||
</button>
|
||||
</div>
|
||||
<div class="flex gap-2">
|
||||
<!-- 分类筛选 -->
|
||||
<select
|
||||
v-model="selectedCategory"
|
||||
@change="onCategoryChange"
|
||||
class="px-3 py-2 rounded-md border border-gray-300 dark:border-gray-700 focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-200 dark:bg-gray-900 dark:text-gray-100 text-sm"
|
||||
>
|
||||
<option value="">全部分类</option>
|
||||
<option v-for="category in categories" :key="category.id" :value="category.id">
|
||||
{{ category.name }}
|
||||
</option>
|
||||
</select>
|
||||
<div class="relative">
|
||||
<input
|
||||
v-model="searchQuery"
|
||||
@@ -71,6 +82,7 @@
|
||||
<tr class="bg-slate-800 dark:bg-gray-700 text-white dark:text-gray-100">
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">ID</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">标签名称</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">分类</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">描述</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">资源数量</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">创建时间</th>
|
||||
@@ -79,12 +91,12 @@
|
||||
</thead>
|
||||
<tbody class="divide-y divide-gray-200 dark:divide-gray-700">
|
||||
<tr v-if="loading" class="text-center py-8">
|
||||
<td colspan="6" class="text-gray-500 dark:text-gray-400">
|
||||
<td colspan="7" class="text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-spinner fa-spin mr-2"></i>加载中...
|
||||
</td>
|
||||
</tr>
|
||||
<tr v-else-if="tags.length === 0" class="text-center py-8">
|
||||
<td colspan="6" class="text-gray-500 dark:text-gray-400">
|
||||
<td colspan="7" class="text-gray-500 dark:text-gray-400">
|
||||
<div class="flex flex-col items-center justify-center py-12">
|
||||
<svg class="w-16 h-16 text-gray-300 dark:text-gray-600 mb-4" fill="none" stroke="currentColor" viewBox="0 0 48 48">
|
||||
<circle cx="24" cy="24" r="20" stroke-width="3" stroke-dasharray="6 6" />
|
||||
@@ -110,6 +122,12 @@
|
||||
<td class="px-4 py-3 text-sm text-gray-900 dark:text-gray-100">
|
||||
<span :title="tag.name">{{ tag.name }}</span>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-600 dark:text-gray-400">
|
||||
<span v-if="tag.category_name" class="px-2 py-1 bg-blue-100 dark:bg-blue-900/20 text-blue-800 dark:text-blue-300 rounded-full text-xs">
|
||||
{{ tag.category_name }}
|
||||
</span>
|
||||
<span v-else class="text-gray-400 dark:text-gray-500 italic text-xs">未分类</span>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-600 dark:text-gray-400">
|
||||
<span v-if="tag.description" :title="tag.description">{{ tag.description }}</span>
|
||||
<span v-else class="text-gray-400 dark:text-gray-500 italic">无描述</span>
|
||||
@@ -120,7 +138,7 @@
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ formatTime(tag.create_time) }}
|
||||
{{ formatTime(tag.created_at) }}
|
||||
</td>
|
||||
<td class="px-4 py-3 text-sm">
|
||||
<div class="flex items-center gap-2">
|
||||
@@ -226,6 +244,19 @@
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">分类:</label>
|
||||
<select
|
||||
v-model="formData.category_id"
|
||||
class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-gray-100"
|
||||
>
|
||||
<option value="">选择分类(可选)</option>
|
||||
<option v-for="category in categories" :key="category.id" :value="category.id">
|
||||
{{ category.name }}
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">描述:</label>
|
||||
<textarea
|
||||
@@ -267,7 +298,8 @@ const config = useRuntimeConfig()
|
||||
// 页面状态
|
||||
const pageLoading = ref(true)
|
||||
const loading = ref(false)
|
||||
const tags = ref([])
|
||||
const tags = ref<any[]>([])
|
||||
const categories = ref<any[]>([])
|
||||
|
||||
// 分页状态
|
||||
const currentPage = ref(1)
|
||||
@@ -275,19 +307,21 @@ const pageSize = ref(20)
|
||||
const totalCount = ref(0)
|
||||
const totalPages = ref(0)
|
||||
|
||||
// 搜索状态
|
||||
// 搜索和筛选状态
|
||||
const searchQuery = ref('')
|
||||
const selectedCategory = ref('')
|
||||
let searchTimeout: NodeJS.Timeout | null = null
|
||||
|
||||
// 模态框状态
|
||||
const showAddModal = ref(false)
|
||||
const submitting = ref(false)
|
||||
const editingTag = ref(null)
|
||||
const editingTag = ref<any>(null)
|
||||
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
description: ''
|
||||
description: '',
|
||||
category_id: ''
|
||||
})
|
||||
|
||||
// 获取认证头
|
||||
@@ -313,6 +347,24 @@ const checkAuth = () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取分类列表
|
||||
const fetchCategories = async () => {
|
||||
try {
|
||||
const response = await $fetch('/categories', {
|
||||
baseURL: config.public.apiBase,
|
||||
headers: getAuthHeaders() as Record<string, string>
|
||||
})
|
||||
|
||||
if (response && typeof response === 'object' && 'code' in response && response.code === 200) {
|
||||
categories.value = (response as any).data?.items || []
|
||||
} else {
|
||||
categories.value = (response as any).items || []
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取分类列表失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取标签列表
|
||||
const fetchTags = async () => {
|
||||
try {
|
||||
@@ -323,15 +375,27 @@ const fetchTags = async () => {
|
||||
search: searchQuery.value
|
||||
}
|
||||
|
||||
const response = await $fetch('/tags', {
|
||||
baseURL: config.public.apiBase,
|
||||
params
|
||||
})
|
||||
let response: any
|
||||
if (selectedCategory.value) {
|
||||
// 如果选择了分类,使用按分类查询的接口
|
||||
response = await $fetch(`/categories/${selectedCategory.value}/tags`, {
|
||||
baseURL: config.public.apiBase,
|
||||
params,
|
||||
headers: getAuthHeaders() as Record<string, string>
|
||||
})
|
||||
} else {
|
||||
// 否则使用普通查询接口
|
||||
response = await $fetch('/tags', {
|
||||
baseURL: config.public.apiBase,
|
||||
params,
|
||||
headers: getAuthHeaders() as Record<string, string>
|
||||
})
|
||||
}
|
||||
|
||||
// 解析响应
|
||||
if (response && typeof response === 'object' && 'code' in response && response.code === 200) {
|
||||
tags.value = response.data.items || []
|
||||
totalCount.value = response.data.total || 0
|
||||
tags.value = response.data?.items || []
|
||||
totalCount.value = response.data?.total || 0
|
||||
totalPages.value = Math.ceil(totalCount.value / pageSize.value)
|
||||
} else {
|
||||
tags.value = response.items || []
|
||||
@@ -345,6 +409,12 @@ const fetchTags = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 分类变化处理
|
||||
const onCategoryChange = () => {
|
||||
currentPage.value = 1
|
||||
fetchTags()
|
||||
}
|
||||
|
||||
// 搜索防抖
|
||||
const debounceSearch = () => {
|
||||
if (searchTimeout) {
|
||||
@@ -372,7 +442,8 @@ const editTag = (tag: any) => {
|
||||
editingTag.value = tag
|
||||
formData.value = {
|
||||
name: tag.name,
|
||||
description: tag.description || ''
|
||||
description: tag.description || '',
|
||||
category_id: tag.category_id || ''
|
||||
}
|
||||
showAddModal.value = true
|
||||
}
|
||||
@@ -387,7 +458,7 @@ const deleteTag = async (tagId: number) => {
|
||||
await $fetch(`/tags/${tagId}`, {
|
||||
baseURL: config.public.apiBase,
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders()
|
||||
headers: getAuthHeaders() as Record<string, string>
|
||||
})
|
||||
await fetchTags()
|
||||
} catch (error) {
|
||||
@@ -400,19 +471,31 @@ const handleSubmit = async () => {
|
||||
try {
|
||||
submitting.value = true
|
||||
|
||||
// 正确处理category_id,空字符串应该转换为null
|
||||
let categoryId = null
|
||||
if (formData.value.category_id && formData.value.category_id !== '') {
|
||||
categoryId = parseInt(formData.value.category_id)
|
||||
}
|
||||
|
||||
const submitData = {
|
||||
name: formData.value.name,
|
||||
description: formData.value.description,
|
||||
category_id: categoryId
|
||||
}
|
||||
|
||||
if (editingTag.value) {
|
||||
await $fetch(`/tags/${editingTag.value.id}`, {
|
||||
baseURL: config.public.apiBase,
|
||||
method: 'PUT',
|
||||
body: formData.value,
|
||||
headers: getAuthHeaders()
|
||||
body: submitData,
|
||||
headers: getAuthHeaders() as Record<string, string>
|
||||
})
|
||||
} else {
|
||||
await $fetch('/tags', {
|
||||
baseURL: config.public.apiBase,
|
||||
method: 'POST',
|
||||
body: formData.value,
|
||||
headers: getAuthHeaders()
|
||||
body: submitData,
|
||||
headers: getAuthHeaders() as Record<string, string>
|
||||
})
|
||||
}
|
||||
|
||||
@@ -431,7 +514,8 @@ const closeModal = () => {
|
||||
editingTag.value = null
|
||||
formData.value = {
|
||||
name: '',
|
||||
description: ''
|
||||
description: '',
|
||||
category_id: ''
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,6 +541,7 @@ const handleLogout = () => {
|
||||
onMounted(async () => {
|
||||
try {
|
||||
checkAuth()
|
||||
await fetchCategories()
|
||||
await fetchTags()
|
||||
|
||||
// 检查URL参数,如果action=add则自动打开新增弹窗
|
||||
|
||||
Reference in New Issue
Block a user