| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- // pages/thirdPartyContentEdit/thirdPartyContentEdit.js
- const thirdPartyApi = require('../../api/thirdPartyContent');
- const categoryApi = require('../../api/category');
- Page({
- data: {
- contentId: null,
- isEdit: false,
- categoryList: [],
- categoryIndex: 0,
- selectedCategoryName: '请选择分类',
- form: {
- title: '',
- author: '',
- categoryId: null,
- coverUrl: '',
- description: '',
- publisher: '',
- contentType: 1,
- totalChapters: 0,
- status: 1,
- isFree: 0
- }
- },
- async onLoad(options) {
- // 先加载分类列表
- await this.loadCategoryList();
-
- if (options.id) {
- this.setData({
- contentId: parseInt(options.id),
- isEdit: true
- });
- this.loadContentDetail();
- }
- },
- // 加载分类列表
- async loadCategoryList() {
- try {
- const res = await categoryApi.getCategoryList();
- if (res.code === 200) {
- const categoryList = res.data || [];
- let categoryIndex = 0;
- let selectedCategoryName = '请选择分类';
-
- // 如果是编辑模式,找到当前分类的索引
- if (this.data.form.categoryId) {
- const index = categoryList.findIndex(cat => cat.categoryId === this.data.form.categoryId);
- if (index >= 0) {
- categoryIndex = index;
- selectedCategoryName = categoryList[index].categoryName;
- }
- }
-
- this.setData({
- categoryList: categoryList,
- categoryIndex: categoryIndex,
- selectedCategoryName: selectedCategoryName
- });
- }
- } catch (error) {
- console.error('加载分类列表失败:', error);
- }
- },
- // 加载书籍详情
- async loadContentDetail() {
- wx.showLoading({ title: '加载中...' });
- try {
- const res = await thirdPartyApi.getContentDetail(this.data.contentId);
- if (res.code === 200) {
- const form = {
- title: res.data.title || '',
- author: res.data.author || '',
- categoryId: res.data.categoryId || null,
- coverUrl: res.data.coverUrl || '',
- description: res.data.description || '',
- publisher: res.data.publisher || '',
- contentType: res.data.contentType || 1,
- totalChapters: res.data.totalChapters || 0,
- status: res.data.status || 1,
- isFree: res.data.isFree || 0
- };
-
- // 更新分类选择
- let categoryIndex = 0;
- let selectedCategoryName = '请选择分类';
- if (form.categoryId) {
- const index = this.data.categoryList.findIndex(cat => cat.categoryId === form.categoryId);
- if (index >= 0) {
- categoryIndex = index;
- selectedCategoryName = this.data.categoryList[index].categoryName;
- }
- }
-
- this.setData({
- form: form,
- categoryIndex: categoryIndex,
- selectedCategoryName: selectedCategoryName
- });
- wx.setNavigationBarTitle({
- title: '编辑书籍'
- });
- } else {
- wx.showToast({
- title: res.message || '加载失败',
- icon: 'none'
- });
- }
- } catch (error) {
- wx.showToast({
- title: '加载失败',
- icon: 'none'
- });
- } finally {
- wx.hideLoading();
- }
- },
- // 表单输入
- onInput(e) {
- const field = e.currentTarget.dataset.field;
- let value = e.detail.value;
-
- // 如果是数字字段,转换为数字
- if (field === 'totalChapters') {
- value = value ? parseInt(value) || 0 : 0;
- }
-
- this.setData({
- [`form.${field}`]: value
- });
- },
- // 选择分类
- onCategoryChange(e) {
- const index = parseInt(e.detail.value);
- const category = this.data.categoryList[index];
- if (category) {
- this.setData({
- 'form.categoryId': category.categoryId,
- categoryIndex: index,
- selectedCategoryName: category.categoryName
- });
- }
- },
- // 选择内容类型
- onContentTypeChange(e) {
- this.setData({
- 'form.contentType': parseInt(e.detail.value) + 1
- });
- },
- // 选择状态
- onStatusChange(e) {
- this.setData({
- 'form.status': parseInt(e.detail.value) + 1
- });
- },
- // 选择是否免费
- onFreeChange(e) {
- this.setData({
- 'form.isFree': parseInt(e.detail.value)
- });
- },
- // 提交
- async onSubmit() {
- // 验证必填项
- if (!this.data.form.title || !this.data.form.title.trim()) {
- wx.showToast({
- title: '请输入书名',
- icon: 'none'
- });
- return;
- }
- if (!this.data.form.author || !this.data.form.author.trim()) {
- wx.showToast({
- title: '请输入作者',
- icon: 'none'
- });
- return;
- }
- if (!this.data.form.categoryId) {
- wx.showToast({
- title: '请选择分类',
- icon: 'none'
- });
- return;
- }
- wx.showLoading({ title: this.data.isEdit ? '保存中...' : '上传中...' });
- try {
- let res;
- if (this.data.isEdit) {
- res = await thirdPartyApi.updateContent(this.data.contentId, this.data.form);
- } else {
- res = await thirdPartyApi.uploadContent(this.data.form);
- }
- if (res.code === 200) {
- wx.showToast({
- title: res.message || (this.data.isEdit ? '保存成功' : '上传成功'),
- icon: 'success'
- });
- setTimeout(() => {
- wx.navigateBack();
- }, 1500);
- } else {
- wx.showToast({
- title: res.message || '操作失败',
- icon: 'none'
- });
- }
- } catch (error) {
- console.error('提交失败:', error);
- wx.showToast({
- title: '操作失败',
- icon: 'none'
- });
- } finally {
- wx.hideLoading();
- }
- }
- });
|