thirdPartyContent.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // pages/thirdPartyContent/thirdPartyContent.js
  2. const thirdPartyApi = require('../../api/thirdPartyContent');
  3. const categoryApi = require('../../api/category');
  4. Page({
  5. data: {
  6. contentList: [],
  7. categoryList: [],
  8. loading: false,
  9. current: 1,
  10. size: 10,
  11. total: 0,
  12. totalPages: 0,
  13. keyword: '',
  14. statistics: {
  15. pending: 0,
  16. approved: 0,
  17. rejected: 0,
  18. deletePending: 0
  19. }
  20. },
  21. onLoad() {
  22. this.loadCategoryList();
  23. this.loadData();
  24. this.loadStatistics();
  25. },
  26. onShow() {
  27. this.loadData();
  28. this.loadStatistics();
  29. },
  30. // 加载分类列表
  31. async loadCategoryList() {
  32. try {
  33. const res = await categoryApi.getCategoryList();
  34. if (res.code === 200) {
  35. this.setData({
  36. categoryList: res.data || []
  37. });
  38. }
  39. } catch (error) {
  40. console.error('加载分类列表失败:', error);
  41. }
  42. },
  43. // 加载数据
  44. async loadData() {
  45. this.setData({ loading: true });
  46. try {
  47. const res = await thirdPartyApi.getMyContentPage(
  48. this.data.current,
  49. this.data.size,
  50. this.data.keyword
  51. );
  52. if (res.code === 200) {
  53. const total = res.data.total || 0;
  54. const totalPages = Math.ceil(total / this.data.size);
  55. this.setData({
  56. contentList: res.data.records || [],
  57. total: total,
  58. totalPages: totalPages,
  59. loading: false
  60. });
  61. } else {
  62. wx.showToast({
  63. title: res.message || '加载失败',
  64. icon: 'none'
  65. });
  66. this.setData({ loading: false });
  67. }
  68. } catch (error) {
  69. console.error('加载数据失败:', error);
  70. wx.showToast({
  71. title: '加载失败',
  72. icon: 'none'
  73. });
  74. this.setData({ loading: false });
  75. }
  76. },
  77. // 加载统计信息
  78. async loadStatistics() {
  79. try {
  80. const res = await thirdPartyApi.getStatistics();
  81. if (res.code === 200) {
  82. this.setData({
  83. statistics: res.data || {}
  84. });
  85. }
  86. } catch (error) {
  87. console.error('加载统计信息失败:', error);
  88. }
  89. },
  90. // 搜索
  91. onSearchInput(e) {
  92. this.setData({
  93. keyword: e.detail.value
  94. });
  95. },
  96. // 执行搜索
  97. onSearch() {
  98. this.setData({ current: 1 });
  99. this.loadData();
  100. },
  101. // 上传书籍
  102. goToUpload() {
  103. wx.navigateTo({
  104. url: '/pages/thirdPartyContentEdit/thirdPartyContentEdit'
  105. });
  106. },
  107. // 编辑书籍
  108. editContent(e) {
  109. const contentId = e.currentTarget.dataset.id;
  110. wx.navigateTo({
  111. url: `/pages/thirdPartyContentEdit/thirdPartyContentEdit?id=${contentId}`
  112. });
  113. },
  114. // 下架书籍
  115. async offlineContent(e) {
  116. const contentId = e.currentTarget.dataset.id;
  117. wx.showModal({
  118. title: '提示',
  119. content: '确定要下架这本书吗?下架后用户将无法看到。',
  120. success: async (res) => {
  121. if (res.confirm) {
  122. try {
  123. const result = await thirdPartyApi.offlineContent(contentId);
  124. if (result.code === 200) {
  125. wx.showToast({
  126. title: '已下架',
  127. icon: 'success'
  128. });
  129. this.loadData();
  130. this.loadStatistics();
  131. } else {
  132. wx.showToast({
  133. title: result.message || '操作失败',
  134. icon: 'none'
  135. });
  136. }
  137. } catch (error) {
  138. wx.showToast({
  139. title: '操作失败',
  140. icon: 'none'
  141. });
  142. }
  143. }
  144. }
  145. });
  146. },
  147. // 上架书籍
  148. async onlineContent(e) {
  149. const contentId = e.currentTarget.dataset.id;
  150. try {
  151. const result = await thirdPartyApi.onlineContent(contentId);
  152. if (result.code === 200) {
  153. wx.showToast({
  154. title: '已上架',
  155. icon: 'success'
  156. });
  157. this.loadData();
  158. this.loadStatistics();
  159. } else {
  160. wx.showToast({
  161. title: result.message || '操作失败',
  162. icon: 'none'
  163. });
  164. }
  165. } catch (error) {
  166. wx.showToast({
  167. title: '操作失败',
  168. icon: 'none'
  169. });
  170. }
  171. },
  172. // 申请删除
  173. async requestDelete(e) {
  174. const contentId = e.currentTarget.dataset.id;
  175. wx.showModal({
  176. title: '提示',
  177. content: '确定要申请删除这本书吗?需要超级管理员审核后才能删除。',
  178. success: async (res) => {
  179. if (res.confirm) {
  180. try {
  181. const result = await thirdPartyApi.requestDeleteContent(contentId);
  182. if (result.code === 200) {
  183. wx.showToast({
  184. title: '删除申请已提交',
  185. icon: 'success'
  186. });
  187. this.loadData();
  188. this.loadStatistics();
  189. } else {
  190. wx.showToast({
  191. title: result.message || '操作失败',
  192. icon: 'none'
  193. });
  194. }
  195. } catch (error) {
  196. wx.showToast({
  197. title: '操作失败',
  198. icon: 'none'
  199. });
  200. }
  201. }
  202. }
  203. });
  204. },
  205. // 分页
  206. onPageChange(e) {
  207. const current = e.currentTarget.dataset.current || e.detail.current;
  208. this.setData({ current });
  209. this.loadData();
  210. },
  211. // 下拉刷新
  212. onPullDownRefresh() {
  213. this.setData({ current: 1 });
  214. this.loadData();
  215. this.loadStatistics();
  216. setTimeout(() => {
  217. wx.stopPullDownRefresh();
  218. }, 1000);
  219. }
  220. });