1
This commit is contained in:
107
miniprogram/pages/admin-samples/index.js
Normal file
107
miniprogram/pages/admin-samples/index.js
Normal file
@@ -0,0 +1,107 @@
|
||||
const { request } = require('../../utils/request')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
keyword: '',
|
||||
label: '',
|
||||
loading: false,
|
||||
samples: [],
|
||||
form: {
|
||||
text: '',
|
||||
label: 'spam'
|
||||
},
|
||||
importText: '[{"text":"点击领取限时现金券","label":"spam"},{"text":"今天下午发布会彩排","label":"ham"}]'
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.fetchSamples()
|
||||
},
|
||||
|
||||
onInput(e) {
|
||||
const field = e.currentTarget.dataset.field
|
||||
this.setData({ [field]: e.detail.value || '' })
|
||||
},
|
||||
|
||||
onFormInput(e) {
|
||||
const field = e.currentTarget.dataset.field
|
||||
this.setData({ [`form.${field}`]: e.detail.value || '' })
|
||||
},
|
||||
|
||||
onLabelChange(e) {
|
||||
this.setData({ label: e.detail.value || '' })
|
||||
},
|
||||
|
||||
onFormLabelChange(e) {
|
||||
this.setData({ 'form.label': e.detail.value || 'spam' })
|
||||
},
|
||||
|
||||
async fetchSamples() {
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const url = `/spam/samples?keyword=${encodeURIComponent(this.data.keyword)}&label=${encodeURIComponent(this.data.label)}&page=1&page_size=80`
|
||||
const data = await request({ url })
|
||||
this.setData({ samples: data.items || [] })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
async createSample() {
|
||||
const payload = {
|
||||
text: (this.data.form.text || '').trim(),
|
||||
label: this.data.form.label || 'spam'
|
||||
}
|
||||
if (payload.text.length < 2) {
|
||||
wx.showToast({ title: '样本文本至少 2 个字符', icon: 'none' })
|
||||
return
|
||||
}
|
||||
await request({ url: '/spam/samples', method: 'POST', data: payload })
|
||||
wx.showToast({ title: '新增成功', icon: 'success' })
|
||||
this.setData({ 'form.text': '' })
|
||||
this.fetchSamples()
|
||||
},
|
||||
|
||||
async deleteSample(e) {
|
||||
const id = Number(e.currentTarget.dataset.id)
|
||||
wx.showModal({
|
||||
title: '删除样本',
|
||||
content: `确认删除样本 ID ${id} 吗?`,
|
||||
success: async (res) => {
|
||||
if (!res.confirm) return
|
||||
await request({ url: `/spam/samples/${id}`, method: 'DELETE' })
|
||||
wx.showToast({ title: '删除成功', icon: 'success' })
|
||||
this.fetchSamples()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
async toggleActive(e) {
|
||||
const id = Number(e.currentTarget.dataset.id)
|
||||
const active = !!e.detail.value
|
||||
await request({
|
||||
url: `/spam/samples/${id}`,
|
||||
method: 'PUT',
|
||||
data: { is_active: active }
|
||||
})
|
||||
wx.showToast({ title: '状态已更新', icon: 'success' })
|
||||
},
|
||||
|
||||
async retrain() {
|
||||
await request({ url: '/spam/train', method: 'POST', data: {} })
|
||||
wx.showToast({ title: '模型重训完成', icon: 'success' })
|
||||
},
|
||||
|
||||
async importSamples() {
|
||||
let items = []
|
||||
try {
|
||||
items = JSON.parse(this.data.importText)
|
||||
} catch (err) {
|
||||
wx.showToast({ title: 'JSON 格式错误', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
await request({ url: '/spam/samples/import', method: 'POST', data: { items } })
|
||||
wx.showToast({ title: '导入完成', icon: 'success' })
|
||||
this.fetchSamples()
|
||||
}
|
||||
})
|
||||
3
miniprogram/pages/admin-samples/index.json
Normal file
3
miniprogram/pages/admin-samples/index.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "样本管理"
|
||||
}
|
||||
64
miniprogram/pages/admin-samples/index.wxml
Normal file
64
miniprogram/pages/admin-samples/index.wxml
Normal file
@@ -0,0 +1,64 @@
|
||||
<view class="container">
|
||||
<view class="hero fade-up">
|
||||
<view class="hero-badge">SAMPLE LAB</view>
|
||||
<view class="hero-title">训练样本管理</view>
|
||||
<view class="hero-sub">支持增删样本、批量导入、启停样本与一键重训,持续提升识别准确率。</view>
|
||||
</view>
|
||||
|
||||
<view class="card fade-up fade-up-delay-1">
|
||||
<view class="card-title">筛选样本</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="field-label">关键词</text>
|
||||
<input class="input" placeholder="输入关键词" value="{{keyword}}" data-field="keyword" bindinput="onInput" />
|
||||
</view>
|
||||
|
||||
<radio-group class="row" bindchange="onLabelChange">
|
||||
<label class="label"><radio value="" checked="{{label === ''}}" />全部</label>
|
||||
<label class="label"><radio value="spam" checked="{{label === 'spam'}}" />垃圾</label>
|
||||
<label class="label"><radio value="ham" checked="{{label === 'ham'}}" />正常</label>
|
||||
</radio-group>
|
||||
|
||||
<button class="btn btn-primary" loading="{{loading}}" bindtap="fetchSamples">查询</button>
|
||||
</view>
|
||||
|
||||
<view class="card fade-up fade-up-delay-1">
|
||||
<view class="card-title">新增样本</view>
|
||||
|
||||
<textarea class="textarea" placeholder="输入样本文本" value="{{form.text}}" data-field="text" bindinput="onFormInput" />
|
||||
|
||||
<radio-group class="row" bindchange="onFormLabelChange">
|
||||
<label class="label"><radio value="spam" checked="{{form.label === 'spam'}}" />垃圾</label>
|
||||
<label class="label"><radio value="ham" checked="{{form.label === 'ham'}}" />正常</label>
|
||||
</radio-group>
|
||||
|
||||
<button class="btn btn-accent" bindtap="createSample">提交样本</button>
|
||||
</view>
|
||||
|
||||
<view class="card fade-up fade-up-delay-2">
|
||||
<view class="card-title">批量导入</view>
|
||||
<view class="card-desc">支持 JSON 数组导入。导入后可直接重训模型。</view>
|
||||
<textarea class="textarea" value="{{importText}}" data-field="importText" bindinput="onInput" />
|
||||
|
||||
<view class="btn-row">
|
||||
<button class="btn btn-primary" bindtap="importSamples">执行导入</button>
|
||||
<button class="btn btn-ghost" bindtap="retrain">一键重训模型</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="card fade-up fade-up-delay-3" wx:if="{{samples.length}}">
|
||||
<view class="card-title">样本列表</view>
|
||||
|
||||
<view class="list-item" wx:for="{{samples}}" wx:key="id">
|
||||
<view class="item-title">{{item.text}}</view>
|
||||
<view class="item-sub">标签:{{item.label === 'spam' ? '垃圾' : '正常'}} · 来源:{{item.source}} · ID:{{item.id}}</view>
|
||||
|
||||
<view class="row">
|
||||
<text class="label">参与训练</text>
|
||||
<switch data-id="{{item.id}}" checked="{{item.is_active}}" bindchange="toggleActive" />
|
||||
</view>
|
||||
|
||||
<button class="btn btn-ghost" data-id="{{item.id}}" bindtap="deleteSample">删除样本</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
1
miniprogram/pages/admin-samples/index.wxss
Normal file
1
miniprogram/pages/admin-samples/index.wxss
Normal file
@@ -0,0 +1 @@
|
||||
/* admin-samples styles use global theme */
|
||||
Reference in New Issue
Block a user