kuaiying_wx/pages/video_list/video_list.js
2025-03-04 15:25:38 +08:00

329 lines
9.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const app = getApp();
Page({
data: {
inputValue: '', // 输入框的内容
detectResult: '', // 识别结果
detectType: 'text', // 默认识别类型为文本
maxLength: 20000, // 最大允许的字节数
inputLength: 0, // 当前输入的字节数
task_id: '', // 任务ID
detectButtonDisabled: false, // 文本检测按钮是否禁用
modifyButtonDisabled: false // 智能修改按钮是否禁用
},
onLoad: function(e) {
wx.showShareMenu({
withShareTicket: true,
menus: ["shareAppMessage", "shareTimeline"]
});
},
onShow: function() {
var t = this;
t.resetData()
app.getCurrentTabbar(2, this);
app.checkUpdateVersion();
app.getUserInfo().then(() => {
console.log('获取用户信息完成');
console.log(wx.getStorageSync('defaultDailyFreeParseNum'));
const rewrittenText = wx.getStorageSync('rewrittenText'); // 从缓存中获取存储的文案
if (rewrittenText) {
this.setData({
inputValue: rewrittenText // 设置到页面的数据中
});
wx.removeStorageSync('rewrittenText'); // 清除数据,避免下次重复加载
wx.showToast({
title: '点击开始检测按钮检测文本违规词。',
icon: 'none'
});
}
}).catch(error => {
console.error('获取用户信息失败:', error);
}).finally(() => {
console.log('getUserInfo调用完成');
});
},
resetData: function() {
// 重置页面数据为默认值
this.setData({
inputValue: '', // 输入框的内容
detectResult: '', // 识别结果
detectType: 'text', // 默认识别类型为文本
inputLength: 0, // 当前输入的字节数
task_id: '', // 任务ID
detectButtonDisabled: false, // 文本检测按钮是否禁用
modifyButtonDisabled: false // 智能修改按钮是否禁用
});
},
// 切换识别类型
switchDetectType(e) {
const detectType = e.currentTarget.dataset.type;
this.recordClick(detectType);
this.setData({
detectType,
detectResult: '' // 切换时清空结果
});
if (detectType === 'audio' || detectType === 'video') {
wx.showToast({
title: '该功能正在开发中',
icon: 'none'
});
} else {
wx.showToast({
title: '切换到文本识别',
icon: 'none'
});
}
},
// 文本识别函数
detectText() {
const { inputValue, maxLength } = this.data;
const byteSize = this.getByteLength(inputValue);
// 检查文本是否为空或长度过短
if (!inputValue.trim() || this.getByteLength(inputValue) < 10) {
wx.showToast({
title: '请输入有效的文本',
icon: 'none'
});
return;
}
// 超出字节长度检测
if (byteSize > maxLength) {
wx.showToast({
title: `内容不能超过 ${maxLength} 字节`,
icon: 'none'
});
return;
}
// 禁用检测按钮
this.setData({ detectButtonDisabled: true });
wx.showLoading({
title: '正在检测...',
});
// 调用后端审核接口
app.apiRequest({
url: '/myapp/text_review/',
method: 'POST',
data: {
text: inputValue,
openid: wx.getStorageSync('openid'),
nickname: wx.getStorageSync('uuid')
},
success: (res) => {
wx.hideLoading();
this.setData({ detectButtonDisabled: false }); // 启用检测按钮
if (res.data.code === 200) {
// 审核成功
this.setData({
detectResult: res.data.data.result,
task_id: res.data.task_id
});
console.log(res.data.data.result);
wx.showToast({
title: '检测成功',
icon: 'success'
});
if (res.data.data.result.conclusion === '不合规') {
wx.showModal({
title: "提示",
content: "检测结果不合规,是否使用智能合规功能一键修改文案?",
confirmText: "一键修改",
cancelText: "取消",
success: (modalRes) => {
if (modalRes.confirm) {
this.modifyContent(); // 调用一键合规函数
}
}
});
}
} else if (res.data.code === 400 && res.data.message === '余额不足') {
// 积分不足,提示充值
wx.showModal({
title: "检测文本",
content: '创意点不足!',
confirmColor: "#00B269",
cancelColor: "#858585",
success: function(e) {
if (e.confirm) {
console.log("确定");
wx.navigateTo({
url: "../vip_recharge/vip_recharge?show=true"
});
} else {
console.log("取消");
}
}
});
} else {
// 其他失败情况
wx.showToast({
title: res.data.message || '检测失败,请稍后重试',
icon: 'none'
});
}
},
fail: (err) => {
wx.hideLoading();
this.setData({ detectButtonDisabled: false }); // 启用检测按钮
console.error('请求失败:', err);
wx.showToast({
title: '网络异常,请稍后重试',
icon: 'none'
});
}
});
},
// 计算文本的字节长度(区分中英文)
getByteLength(text) {
return text.replace(/[^\x00-\xff]/g, 'aa').length;
},
// 处理输入框内容变化
bindInput(e) {
const inputValue = e.detail.value;
const inputLength = this.getByteLength(inputValue);
// 更新输入内容和字节长度
this.setData({
inputValue,
inputLength
});
// 如果字数超出最大限制,提示用户
if (inputLength > this.data.maxLength) {
wx.showToast({
title: `已超出 ${this.data.maxLength} 字节`,
icon: 'none'
});
}
},
// 复制内容
copyContent() {
wx.setClipboardData({
data: this.data.inputValue,
success() {
wx.showToast({
title: '内容已复制',
icon: 'none'
});
}
});
},
// 清空输入框内容
clearInput() {
wx.showModal({
title: "提示",
content: "确定要清空所有内容吗?",
success: (res) => {
if (res.confirm) {
this.setData({
inputValue: '',
inputLength: 0,
detectResult: ''
});
wx.showToast({
title: '已清空内容',
icon: 'none'
});
}
}
});
},
// 调用智能修改AI合规
modifyContent() {
// 禁用智能修改按钮
this.setData({ modifyButtonDisabled: true });
wx.showLoading({
title: '正在修改...',
});
app.apiRequest({
url: '/myapp/ai_modify_text/',
method: 'POST',
data: {
task_id: this.data.task_id,
openid: wx.getStorageSync('openid')
},
success: (res) => {
wx.hideLoading();
this.setData({ modifyButtonDisabled: false }); // 启用智能修改按钮
if (res.data.code === 200) {
// 修改成功
this.setData({
inputValue: res.data.data.modified_text,
detectResult: '' // 清空检测结果
});
wx.showToast({
title: '修改成功',
icon: 'success'
});
}else if (res.data.code === 400 && res.data.message === '积分不足,请充值') {
// 积分不足,提示用户充值并引导
wx.showModal({
title: "提示",
content: "您的积分不足,无法进行智能修改,请充值后再尝试。",
confirmText: "去充值",
cancelText: "取消",
success: (modalRes) => {
if (modalRes.confirm) {
wx.navigateTo({
url: "../vip_recharge/vip_recharge?show=true"
});
}
}
});
} else {
// 其他失败情况
wx.showToast({
title: res.data.message || '修改失败,请稍后重试',
icon: 'none'
});
}
},
fail: (err) => {
wx.hideLoading();
this.setData({ modifyButtonDisabled: false }); // 启用智能修改按钮
console.error('请求失败:', err);
wx.showToast({
title: '网络异常,请稍后重试',
icon: 'none'
});
}
});
},
// 记录点击的功能类型
recordClick(functionClicked) {
app.apiRequest({
url: `/myapp/record_click/`, // 点击统计接口
method: 'POST',
data: {
openid: wx.getStorageSync('openid'),
nickname: wx.getStorageSync('uuid'),
function_clicked: functionClicked
},
header: {
'content-type': 'application/json'
},
success: (res) => {
}
});
}
});