Files
c/miniprogram/pages/home/index.js
2026-05-14 21:20:57 +08:00

149 lines
3.8 KiB
JavaScript

const { request } = require('../../utils/request')
const QUICK_TEXTS = [
'大家好,今晚 8 点社区线上读书会,欢迎参加。',
'恭喜中奖领取大额现金,点击链接立即到账。',
'本周活动报名已开放,请在群里接龙。',
'高薪兼职日结,扫码进群立刻赚钱。'
]
const VISIBILITY_OPTIONS = [
{ value: 'public', label: '公开信息发布' },
{ value: 'private', label: '私有信息发布' },
{ value: 'direct', label: '用户私信发布' }
]
Page({
data: {
loading: false,
user: null,
text: '',
result: null,
quickTexts: QUICK_TEXTS,
visibilityOptions: VISIBILITY_OPTIONS,
visibilityIndex: 0,
visibility: 'public',
recipientUsername: ''
},
onShow() {
this.bootstrap()
},
async bootstrap() {
const app = getApp()
if (!app.globalData.token) {
wx.reLaunch({ url: '/pages/login/index' })
return
}
try {
const user = await request({ url: '/auth/me' })
app.globalData.user = user
wx.setStorageSync('user', user)
this.setData({ user })
} catch (e) {
// ignore
}
},
formatPercent(value, digits = 2) {
const num = Number(value || 0)
return `${(num * 100).toFixed(digits)}%`
},
onInput(e) {
const field = e.currentTarget.dataset.field || 'text'
this.setData({ [field]: e.detail.value || '' })
},
fillQuick(e) {
this.setData({ text: e.currentTarget.dataset.text || '' })
},
onVisibilityChange(e) {
const idx = Number(e.detail.value)
const row = this.data.visibilityOptions[idx] || this.data.visibilityOptions[0]
this.setData({ visibilityIndex: idx, visibility: row.value })
},
async publish() {
if (this.data.loading) return
const text = (this.data.text || '').trim()
if (text.length < 2) {
wx.showToast({ title: '请输入至少 2 个字符', icon: 'none' })
return
}
const payload = {
text,
visibility: this.data.visibility
}
if (this.data.visibility === 'direct') {
const receiver = (this.data.recipientUsername || '').trim()
if (!receiver) {
wx.showToast({ title: '私信请填写接收人用户名', icon: 'none' })
return
}
payload.recipient_username = receiver
}
this.setData({ loading: true })
try {
const result = await request({
url: '/content/publish',
method: 'POST',
data: payload
})
this.setData({
result: {
...result,
detect: {
...(result.detect || {}),
confidence_text: this.formatPercent((result.detect || {}).confidence, 2)
},
post_threshold_text: this.formatPercent((result.post || {}).threshold, 1),
detect_spam_probability_text: this.formatPercent((result.detect || {}).spam_probability, 2)
}
})
wx.showToast({
title: result.publish_allowed ? '发布成功' : '已拦截,可申诉',
icon: result.publish_allowed ? 'success' : 'none'
})
} finally {
this.setData({ loading: false })
}
},
goBatch() {
wx.navigateTo({ url: '/pages/batch/index' })
},
goto(e) {
const path = e.currentTarget.dataset.path
if (!path) return
wx.navigateTo({ url: path })
},
showTokenWeight(e) {
const token = e.currentTarget.dataset.token
const weight = e.currentTarget.dataset.weight
const weightNum = Number(weight || 0)
const direction = weightNum >= 0 ? '倾向垃圾判定' : '倾向正常判定'
wx.showModal({
title: '关键词权重',
content: `关键词"${token}"\n权重贡献:${weightNum >= 0 ? '+' : ''}${weightNum.toFixed(4)}\n(${direction})`,
showCancel: false,
confirmText: '关闭'
})
},
logout() {
getApp().clearAuth()
wx.reLaunch({ url: '/pages/login/index' })
}
})