// pages/thirdPartyContent/thirdPartyContent.js const thirdPartyApi = require('../../api/thirdPartyContent'); const categoryApi = require('../../api/category'); Page({ data: { contentList: [], categoryList: [], loading: false, current: 1, size: 10, total: 0, totalPages: 0, keyword: '', statistics: { pending: 0, approved: 0, rejected: 0, deletePending: 0 } }, onLoad() { this.loadCategoryList(); this.loadData(); this.loadStatistics(); }, onShow() { this.loadData(); this.loadStatistics(); }, // 加载分类列表 async loadCategoryList() { try { const res = await categoryApi.getCategoryList(); if (res.code === 200) { this.setData({ categoryList: res.data || [] }); } } catch (error) { console.error('加载分类列表失败:', error); } }, // 加载数据 async loadData() { this.setData({ loading: true }); try { const res = await thirdPartyApi.getMyContentPage( this.data.current, this.data.size, this.data.keyword ); if (res.code === 200) { const total = res.data.total || 0; const totalPages = Math.ceil(total / this.data.size); this.setData({ contentList: res.data.records || [], total: total, totalPages: totalPages, loading: false }); } else { wx.showToast({ title: res.message || '加载失败', icon: 'none' }); this.setData({ loading: false }); } } catch (error) { console.error('加载数据失败:', error); wx.showToast({ title: '加载失败', icon: 'none' }); this.setData({ loading: false }); } }, // 加载统计信息 async loadStatistics() { try { const res = await thirdPartyApi.getStatistics(); if (res.code === 200) { this.setData({ statistics: res.data || {} }); } } catch (error) { console.error('加载统计信息失败:', error); } }, // 搜索 onSearchInput(e) { this.setData({ keyword: e.detail.value }); }, // 执行搜索 onSearch() { this.setData({ current: 1 }); this.loadData(); }, // 上传书籍 goToUpload() { wx.navigateTo({ url: '/pages/thirdPartyContentEdit/thirdPartyContentEdit' }); }, // 编辑书籍 editContent(e) { const contentId = e.currentTarget.dataset.id; wx.navigateTo({ url: `/pages/thirdPartyContentEdit/thirdPartyContentEdit?id=${contentId}` }); }, // 下架书籍 async offlineContent(e) { const contentId = e.currentTarget.dataset.id; wx.showModal({ title: '提示', content: '确定要下架这本书吗?下架后用户将无法看到。', success: async (res) => { if (res.confirm) { try { const result = await thirdPartyApi.offlineContent(contentId); if (result.code === 200) { wx.showToast({ title: '已下架', icon: 'success' }); this.loadData(); this.loadStatistics(); } else { wx.showToast({ title: result.message || '操作失败', icon: 'none' }); } } catch (error) { wx.showToast({ title: '操作失败', icon: 'none' }); } } } }); }, // 上架书籍 async onlineContent(e) { const contentId = e.currentTarget.dataset.id; try { const result = await thirdPartyApi.onlineContent(contentId); if (result.code === 200) { wx.showToast({ title: '已上架', icon: 'success' }); this.loadData(); this.loadStatistics(); } else { wx.showToast({ title: result.message || '操作失败', icon: 'none' }); } } catch (error) { wx.showToast({ title: '操作失败', icon: 'none' }); } }, // 申请删除 async requestDelete(e) { const contentId = e.currentTarget.dataset.id; wx.showModal({ title: '提示', content: '确定要申请删除这本书吗?需要超级管理员审核后才能删除。', success: async (res) => { if (res.confirm) { try { const result = await thirdPartyApi.requestDeleteContent(contentId); if (result.code === 200) { wx.showToast({ title: '删除申请已提交', icon: 'success' }); this.loadData(); this.loadStatistics(); } else { wx.showToast({ title: result.message || '操作失败', icon: 'none' }); } } catch (error) { wx.showToast({ title: '操作失败', icon: 'none' }); } } } }); }, // 分页 onPageChange(e) { const current = e.currentTarget.dataset.current || e.detail.current; this.setData({ current }); this.loadData(); }, // 下拉刷新 onPullDownRefresh() { this.setData({ current: 1 }); this.loadData(); this.loadStatistics(); setTimeout(() => { wx.stopPullDownRefresh(); }, 1000); } });