增加批量导入违禁词
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"backend/internal/repo"
|
"backend/internal/repo"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -51,6 +52,31 @@ func (h *BannedWordsHandler) Create(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"id": item.ID, "word": item.Word, "hits": item.Hits, "created_at": item.CreatedAt}})
|
c.JSON(http.StatusOK, gin.H{"data": gin.H{"id": item.ID, "word": item.Word, "hits": item.Hits, "created_at": item.CreatedAt}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Import bulk-adds words from a free-form text blob — split on newlines,
|
||||||
|
// commas (英文/中文), 顿号 and semicolons — skipping blanks and existing entries.
|
||||||
|
func (h *BannedWordsHandler) Import(c *gin.Context) {
|
||||||
|
var body struct {
|
||||||
|
Text string `json:"text"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil || strings.TrimSpace(body.Text) == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"detail": "请提供要导入的违禁词"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
words := strings.FieldsFunc(body.Text, func(r rune) bool {
|
||||||
|
switch r {
|
||||||
|
case '\n', '\r', ',', ',', '、', ';', ';':
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
added, skipped, err := h.words.BulkCreate(c.Request.Context(), words)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "导入失败"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"added": added, "skipped": skipped})
|
||||||
|
}
|
||||||
|
|
||||||
func (h *BannedWordsHandler) Delete(c *gin.Context) {
|
func (h *BannedWordsHandler) Delete(c *gin.Context) {
|
||||||
n, err := h.words.Delete(c.Request.Context(), c.Param("id"))
|
n, err := h.words.Delete(c.Request.Context(), c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -142,6 +142,7 @@ func New(cfg *config.Config, auth *service.AuthService, handlers Handlers) *gin.
|
|||||||
authed.DELETE("/images", handlers.AdminRead.DeleteImage)
|
authed.DELETE("/images", handlers.AdminRead.DeleteImage)
|
||||||
authed.GET("/banned-words", handlers.BannedWords.List)
|
authed.GET("/banned-words", handlers.BannedWords.List)
|
||||||
authed.POST("/banned-words", handlers.BannedWords.Create)
|
authed.POST("/banned-words", handlers.BannedWords.Create)
|
||||||
|
authed.POST("/banned-words/import", handlers.BannedWords.Import)
|
||||||
authed.DELETE("/banned-words/:id", handlers.BannedWords.Delete)
|
authed.DELETE("/banned-words/:id", handlers.BannedWords.Delete)
|
||||||
authed.GET("/refresh/profiles", handlers.ProviderAdmin.RefreshProfiles)
|
authed.GET("/refresh/profiles", handlers.ProviderAdmin.RefreshProfiles)
|
||||||
authed.POST("/refresh/profiles/:profile_id/refresh-now", handlers.ProviderAdmin.RefreshNow)
|
authed.POST("/refresh/profiles/:profile_id/refresh-now", handlers.ProviderAdmin.RefreshNow)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func NewBannedWordRepository(db *gorm.DB) *BannedWordRepository {
|
|||||||
|
|
||||||
func (r *BannedWordRepository) List(ctx context.Context) ([]model.BannedWord, error) {
|
func (r *BannedWordRepository) List(ctx context.Context) ([]model.BannedWord, error) {
|
||||||
var items []model.BannedWord
|
var items []model.BannedWord
|
||||||
err := r.db.WithContext(ctx).Order("hits DESC, created_at DESC").Find(&items).Error
|
err := r.db.WithContext(ctx).Order("created_at DESC, hits DESC").Find(&items).Error
|
||||||
return items, err
|
return items, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +42,43 @@ func (r *BannedWordRepository) Create(ctx context.Context, word string) (*model.
|
|||||||
return item, nil
|
return item, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BulkCreate inserts the given words, skipping blanks and ones already in the
|
||||||
|
// table (case-insensitive). Returns how many were added vs skipped.
|
||||||
|
func (r *BannedWordRepository) BulkCreate(ctx context.Context, words []string) (added, skipped int, err error) {
|
||||||
|
existing, err := r.List(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
seen := make(map[string]bool, len(existing))
|
||||||
|
for _, w := range existing {
|
||||||
|
seen[strings.ToLower(w.Word)] = true
|
||||||
|
}
|
||||||
|
for _, w := range words {
|
||||||
|
w = strings.TrimSpace(w)
|
||||||
|
if w == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
key := strings.ToLower(w)
|
||||||
|
if seen[key] {
|
||||||
|
skipped++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
item := &model.BannedWord{
|
||||||
|
ID: strings.ReplaceAll(uuid.NewString(), "-", "")[:32],
|
||||||
|
Word: w,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
UpdatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
if e := r.db.WithContext(ctx).Create(item).Error; e != nil {
|
||||||
|
skipped++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[key] = true
|
||||||
|
added++
|
||||||
|
}
|
||||||
|
return added, skipped, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *BannedWordRepository) Delete(ctx context.Context, id string) (int64, error) {
|
func (r *BannedWordRepository) Delete(ctx context.Context, id string) (int64, error) {
|
||||||
res := r.db.WithContext(ctx).Delete(&model.BannedWord{}, "id = ?", id)
|
res := r.db.WithContext(ctx).Delete(&model.BannedWord{}, "id = ?", id)
|
||||||
return res.RowsAffected, res.Error
|
return res.RowsAffected, res.Error
|
||||||
|
|||||||
@@ -25,6 +25,24 @@ async function add() {
|
|||||||
else flash(r.data?.detail || '添加失败')
|
else flash(r.data?.detail || '添加失败')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bulk import — paste words separated by newlines / commas / 、 / ;
|
||||||
|
const importOpen = ref(false)
|
||||||
|
const importText = ref('')
|
||||||
|
const importing = ref(false)
|
||||||
|
async function doImport() {
|
||||||
|
const text = importText.value.trim()
|
||||||
|
if (!text) { flash('请先粘贴要导入的违禁词'); return }
|
||||||
|
importing.value = true
|
||||||
|
const r = await api('/banned-words/import', jsonBody('POST', { text }))
|
||||||
|
importing.value = false
|
||||||
|
if (r.ok) {
|
||||||
|
importOpen.value = false
|
||||||
|
importText.value = ''
|
||||||
|
flash(`导入完成:新增 ${r.data?.added ?? 0} 个,跳过 ${r.data?.skipped ?? 0} 个`)
|
||||||
|
load()
|
||||||
|
} else flash(r.data?.detail || '导入失败')
|
||||||
|
}
|
||||||
|
|
||||||
async function del(w) {
|
async function del(w) {
|
||||||
if (!confirm(`删除违禁词「${w.word}」?`)) return
|
if (!confirm(`删除违禁词「${w.word}」?`)) return
|
||||||
const r = await api(`/banned-words/${w.id}`, { method: 'DELETE' })
|
const r = await api(`/banned-words/${w.id}`, { method: 'DELETE' })
|
||||||
@@ -104,6 +122,7 @@ onMounted(load)
|
|||||||
</button>
|
</button>
|
||||||
<input v-model="newWord" @keyup.enter="add" class="field !py-1.5 text-xs w-52" placeholder="输入违禁词后回车" />
|
<input v-model="newWord" @keyup.enter="add" class="field !py-1.5 text-xs w-52" placeholder="输入违禁词后回车" />
|
||||||
<button @click="add" class="btn-primary shrink-0">+ 添加</button>
|
<button @click="add" class="btn-primary shrink-0">+ 添加</button>
|
||||||
|
<button @click="importOpen = true" class="btn-soft shrink-0">批量导入</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -147,6 +166,18 @@ onMounted(load)
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div v-if="importOpen" class="fixed inset-0 z-50 grid place-items-center bg-black/60 p-4" @click.self="importOpen = false">
|
||||||
|
<div class="card w-full max-w-lg p-5 space-y-3">
|
||||||
|
<h3 class="text-sm font-semibold">批量导入违禁词</h3>
|
||||||
|
<p class="text-xs text-white/45">每行一个,或用逗号、顿号、分号分隔;已存在的词会自动跳过。</p>
|
||||||
|
<textarea v-model="importText" rows="8" class="field w-full text-xs font-mono resize-y" placeholder="违禁词1 违禁词2 违禁词3"></textarea>
|
||||||
|
<div class="flex justify-end gap-2">
|
||||||
|
<button @click="importOpen = false" class="btn-soft">取消</button>
|
||||||
|
<button @click="doImport" :disabled="importing" class="btn-primary">{{ importing ? '导入中…' : '导入' }}</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<transition name="fade">
|
<transition name="fade">
|
||||||
<div v-if="toast" class="fixed bottom-6 left-1/2 -translate-x-1/2 z-[60] bg-slate-900 text-white text-xs px-4 py-2 rounded-lg shadow-lg">{{ toast }}</div>
|
<div v-if="toast" class="fixed bottom-6 left-1/2 -translate-x-1/2 z-[60] bg-slate-900 text-white text-xs px-4 py-2 rounded-lg shadow-lg">{{ toast }}</div>
|
||||||
</transition>
|
</transition>
|
||||||
|
|||||||
Reference in New Issue
Block a user