You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
4.9 KiB
191 lines
4.9 KiB
import http from '../../../utils/api';
|
|
const baseUrl = require('../../../utils/baseUrl');
|
|
|
|
Page({
|
|
data: {
|
|
baseUrl: baseUrl,
|
|
// 表单数据
|
|
formData: {
|
|
title: '',
|
|
summary: '',
|
|
categoryName: '',
|
|
tags: '', // 改为字符串格式,如 "传染病,疫苗"
|
|
content: ''
|
|
},
|
|
// 分类列表 - 直接从接口返回的 rows 赋值
|
|
categoryList: [],
|
|
categoryIndex: -1,
|
|
// 标签列表 - 直接从接口返回的 rows 赋值
|
|
tagList: [],
|
|
// 已选中的标签对象数组
|
|
selectedTags: [],
|
|
// 提交状态
|
|
submitting: false
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.getCategoryList();
|
|
this.getTagList();
|
|
},
|
|
|
|
// 输入处理
|
|
onInput(e) {
|
|
const { field } = e.currentTarget.dataset;
|
|
const { value } = e.detail;
|
|
this.setData({
|
|
[`formData.${field}`]: value
|
|
});
|
|
},
|
|
|
|
// 获取文章分类 - 直接使用 res.rows
|
|
getCategoryList() {
|
|
http.articleZd({
|
|
data: {
|
|
dictType: 'vet_experience_category'
|
|
},
|
|
success: (res) => {
|
|
// 直接使用 res.rows 赋值
|
|
if (res.rows && Array.isArray(res.rows)) {
|
|
this.setData({ categoryList: res.rows });
|
|
} else {
|
|
console.error('分类数据格式错误', res);
|
|
wx.showToast({ title: '分类加载失败', icon: 'none' });
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('分类接口异常', err);
|
|
wx.showToast({ title: '网络错误', icon: 'none' });
|
|
}
|
|
});
|
|
},
|
|
|
|
// 获取文章标签 - 直接使用 res.rows
|
|
getTagList() {
|
|
http.videoZd({
|
|
data: {
|
|
dictType: 'vet_experience_tag'
|
|
},
|
|
success: (res) => {
|
|
// 直接使用 res.rows 赋值
|
|
if (res.rows && Array.isArray(res.rows)) {
|
|
this.setData({ tagList: res.rows });
|
|
} else {
|
|
console.error('标签数据格式错误', res);
|
|
wx.showToast({ title: '标签加载失败', icon: 'none' });
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('标签接口异常', err);
|
|
wx.showToast({ title: '网络错误', icon: 'none' });
|
|
}
|
|
});
|
|
},
|
|
|
|
// 分类选择
|
|
onCategoryChange(e) {
|
|
const index = parseInt(e.detail.value, 10);
|
|
const selectedCategory = this.data.categoryList[index];
|
|
this.setData({
|
|
categoryIndex: index,
|
|
[`formData.categoryName`]: selectedCategory.dictLabel
|
|
});
|
|
},
|
|
|
|
// 标签点击切换
|
|
toggleTag(e) {
|
|
const tagItem = e.currentTarget.dataset.item;
|
|
let selectedTags = [...this.data.selectedTags];
|
|
const index = selectedTags.findIndex(t => t.dictValue === tagItem.dictValue);
|
|
|
|
if (index > -1) {
|
|
selectedTags.splice(index, 1);
|
|
} else {
|
|
selectedTags.push(tagItem);
|
|
}
|
|
|
|
// 生成逗号分隔的标签字符串,如 "传染病,疫苗"
|
|
const tagString = selectedTags.map(item => item.dictLabel).join(',');
|
|
|
|
this.setData({
|
|
selectedTags: selectedTags,
|
|
[`formData.tags`]: tagString
|
|
});
|
|
},
|
|
|
|
// 表单提交
|
|
formSubmit(e) {
|
|
const { title, categoryName, content } = this.data.formData;
|
|
|
|
// 表单验证
|
|
if (!title || title.trim() === '') {
|
|
this.showError('请填写文章标题');
|
|
return;
|
|
}
|
|
if (!categoryName) {
|
|
this.showError('请选择文章分类');
|
|
return;
|
|
}
|
|
if (this.data.selectedTags.length === 0) {
|
|
this.showError('请至少选择一个标签');
|
|
return;
|
|
}
|
|
if (!content || content.trim() === '') {
|
|
this.showError('请填写文章内容');
|
|
return;
|
|
}
|
|
|
|
this.setData({ submitting: true });
|
|
|
|
// 构建提交数据 - tags 已经是字符串格式
|
|
const postData = {
|
|
title: this.data.formData.title,
|
|
summary: this.data.formData.summary || '',
|
|
categoryName: this.data.formData.categoryName,
|
|
tags: this.data.formData.tags, // 已经是 "传染病,疫苗" 格式
|
|
content: this.data.formData.content
|
|
};
|
|
|
|
console.log('提交数据:', postData); // 调试用
|
|
|
|
http.shareAdd({
|
|
data: postData,
|
|
success: (res) => {
|
|
if(res.code == 200){
|
|
wx.showToast({
|
|
title: '发布成功',
|
|
icon: 'success',
|
|
duration: 2000,
|
|
success: () => {
|
|
setTimeout(() => {
|
|
wx.navigateBack();
|
|
}, 1500);
|
|
}
|
|
})
|
|
}else{
|
|
console.error('发布失败', err);
|
|
this.showError('发布失败,请重试');
|
|
this.setData({ submitting: false });
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('发布失败', err);
|
|
this.showError('发布失败,请重试');
|
|
this.setData({ submitting: false });
|
|
},
|
|
complete: () => {
|
|
setTimeout(() => {
|
|
this.setData({ submitting: false });
|
|
}, 3000);
|
|
}
|
|
});
|
|
},
|
|
|
|
// 错误提示
|
|
showError(msg) {
|
|
wx.showToast({
|
|
title: msg,
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
});
|