thirdPartyContentEdit.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // pages/thirdPartyContentEdit/thirdPartyContentEdit.js
  2. const thirdPartyApi = require('../../api/thirdPartyContent');
  3. const categoryApi = require('../../api/category');
  4. Page({
  5. data: {
  6. contentId: null,
  7. isEdit: false,
  8. categoryList: [],
  9. categoryIndex: 0,
  10. selectedCategoryName: '请选择分类',
  11. form: {
  12. title: '',
  13. author: '',
  14. categoryId: null,
  15. coverUrl: '',
  16. description: '',
  17. publisher: '',
  18. contentType: 1,
  19. totalChapters: 0,
  20. status: 1,
  21. isFree: 0
  22. }
  23. },
  24. async onLoad(options) {
  25. // 先加载分类列表
  26. await this.loadCategoryList();
  27. if (options.id) {
  28. this.setData({
  29. contentId: parseInt(options.id),
  30. isEdit: true
  31. });
  32. this.loadContentDetail();
  33. }
  34. },
  35. // 加载分类列表
  36. async loadCategoryList() {
  37. try {
  38. const res = await categoryApi.getCategoryList();
  39. if (res.code === 200) {
  40. const categoryList = res.data || [];
  41. let categoryIndex = 0;
  42. let selectedCategoryName = '请选择分类';
  43. // 如果是编辑模式,找到当前分类的索引
  44. if (this.data.form.categoryId) {
  45. const index = categoryList.findIndex(cat => cat.categoryId === this.data.form.categoryId);
  46. if (index >= 0) {
  47. categoryIndex = index;
  48. selectedCategoryName = categoryList[index].categoryName;
  49. }
  50. }
  51. this.setData({
  52. categoryList: categoryList,
  53. categoryIndex: categoryIndex,
  54. selectedCategoryName: selectedCategoryName
  55. });
  56. }
  57. } catch (error) {
  58. console.error('加载分类列表失败:', error);
  59. }
  60. },
  61. // 加载书籍详情
  62. async loadContentDetail() {
  63. wx.showLoading({ title: '加载中...' });
  64. try {
  65. const res = await thirdPartyApi.getContentDetail(this.data.contentId);
  66. if (res.code === 200) {
  67. const form = {
  68. title: res.data.title || '',
  69. author: res.data.author || '',
  70. categoryId: res.data.categoryId || null,
  71. coverUrl: res.data.coverUrl || '',
  72. description: res.data.description || '',
  73. publisher: res.data.publisher || '',
  74. contentType: res.data.contentType || 1,
  75. totalChapters: res.data.totalChapters || 0,
  76. status: res.data.status || 1,
  77. isFree: res.data.isFree || 0
  78. };
  79. // 更新分类选择
  80. let categoryIndex = 0;
  81. let selectedCategoryName = '请选择分类';
  82. if (form.categoryId) {
  83. const index = this.data.categoryList.findIndex(cat => cat.categoryId === form.categoryId);
  84. if (index >= 0) {
  85. categoryIndex = index;
  86. selectedCategoryName = this.data.categoryList[index].categoryName;
  87. }
  88. }
  89. this.setData({
  90. form: form,
  91. categoryIndex: categoryIndex,
  92. selectedCategoryName: selectedCategoryName
  93. });
  94. wx.setNavigationBarTitle({
  95. title: '编辑书籍'
  96. });
  97. } else {
  98. wx.showToast({
  99. title: res.message || '加载失败',
  100. icon: 'none'
  101. });
  102. }
  103. } catch (error) {
  104. wx.showToast({
  105. title: '加载失败',
  106. icon: 'none'
  107. });
  108. } finally {
  109. wx.hideLoading();
  110. }
  111. },
  112. // 表单输入
  113. onInput(e) {
  114. const field = e.currentTarget.dataset.field;
  115. let value = e.detail.value;
  116. // 如果是数字字段,转换为数字
  117. if (field === 'totalChapters') {
  118. value = value ? parseInt(value) || 0 : 0;
  119. }
  120. this.setData({
  121. [`form.${field}`]: value
  122. });
  123. },
  124. // 选择分类
  125. onCategoryChange(e) {
  126. const index = parseInt(e.detail.value);
  127. const category = this.data.categoryList[index];
  128. if (category) {
  129. this.setData({
  130. 'form.categoryId': category.categoryId,
  131. categoryIndex: index,
  132. selectedCategoryName: category.categoryName
  133. });
  134. }
  135. },
  136. // 选择内容类型
  137. onContentTypeChange(e) {
  138. this.setData({
  139. 'form.contentType': parseInt(e.detail.value) + 1
  140. });
  141. },
  142. // 选择状态
  143. onStatusChange(e) {
  144. this.setData({
  145. 'form.status': parseInt(e.detail.value) + 1
  146. });
  147. },
  148. // 选择是否免费
  149. onFreeChange(e) {
  150. this.setData({
  151. 'form.isFree': parseInt(e.detail.value)
  152. });
  153. },
  154. // 提交
  155. async onSubmit() {
  156. // 验证必填项
  157. if (!this.data.form.title || !this.data.form.title.trim()) {
  158. wx.showToast({
  159. title: '请输入书名',
  160. icon: 'none'
  161. });
  162. return;
  163. }
  164. if (!this.data.form.author || !this.data.form.author.trim()) {
  165. wx.showToast({
  166. title: '请输入作者',
  167. icon: 'none'
  168. });
  169. return;
  170. }
  171. if (!this.data.form.categoryId) {
  172. wx.showToast({
  173. title: '请选择分类',
  174. icon: 'none'
  175. });
  176. return;
  177. }
  178. wx.showLoading({ title: this.data.isEdit ? '保存中...' : '上传中...' });
  179. try {
  180. let res;
  181. if (this.data.isEdit) {
  182. res = await thirdPartyApi.updateContent(this.data.contentId, this.data.form);
  183. } else {
  184. res = await thirdPartyApi.uploadContent(this.data.form);
  185. }
  186. if (res.code === 200) {
  187. wx.showToast({
  188. title: res.message || (this.data.isEdit ? '保存成功' : '上传成功'),
  189. icon: 'success'
  190. });
  191. setTimeout(() => {
  192. wx.navigateBack();
  193. }, 1500);
  194. } else {
  195. wx.showToast({
  196. title: res.message || '操作失败',
  197. icon: 'none'
  198. });
  199. }
  200. } catch (error) {
  201. console.error('提交失败:', error);
  202. wx.showToast({
  203. title: '操作失败',
  204. icon: 'none'
  205. });
  206. } finally {
  207. wx.hideLoading();
  208. }
  209. }
  210. });