与牧同行-兽医端小程序
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.

190 lines
4.9 KiB

  1. import http from '../../../utils/api';
  2. const baseUrl = require('../../../utils/baseUrl');
  3. Page({
  4. data: {
  5. baseUrl: baseUrl,
  6. // 表单数据
  7. formData: {
  8. title: '',
  9. summary: '',
  10. categoryName: '',
  11. tags: '', // 改为字符串格式,如 "传染病,疫苗"
  12. content: ''
  13. },
  14. // 分类列表 - 直接从接口返回的 rows 赋值
  15. categoryList: [],
  16. categoryIndex: -1,
  17. // 标签列表 - 直接从接口返回的 rows 赋值
  18. tagList: [],
  19. // 已选中的标签对象数组
  20. selectedTags: [],
  21. // 提交状态
  22. submitting: false
  23. },
  24. onLoad(options) {
  25. this.getCategoryList();
  26. this.getTagList();
  27. },
  28. // 输入处理
  29. onInput(e) {
  30. const { field } = e.currentTarget.dataset;
  31. const { value } = e.detail;
  32. this.setData({
  33. [`formData.${field}`]: value
  34. });
  35. },
  36. // 获取文章分类 - 直接使用 res.rows
  37. getCategoryList() {
  38. http.articleZd({
  39. data: {
  40. dictType: 'vet_experience_category'
  41. },
  42. success: (res) => {
  43. // 直接使用 res.rows 赋值
  44. if (res.rows && Array.isArray(res.rows)) {
  45. this.setData({ categoryList: res.rows });
  46. } else {
  47. console.error('分类数据格式错误', res);
  48. wx.showToast({ title: '分类加载失败', icon: 'none' });
  49. }
  50. },
  51. fail: (err) => {
  52. console.error('分类接口异常', err);
  53. wx.showToast({ title: '网络错误', icon: 'none' });
  54. }
  55. });
  56. },
  57. // 获取文章标签 - 直接使用 res.rows
  58. getTagList() {
  59. http.videoZd({
  60. data: {
  61. dictType: 'vet_experience_tag'
  62. },
  63. success: (res) => {
  64. // 直接使用 res.rows 赋值
  65. if (res.rows && Array.isArray(res.rows)) {
  66. this.setData({ tagList: res.rows });
  67. } else {
  68. console.error('标签数据格式错误', res);
  69. wx.showToast({ title: '标签加载失败', icon: 'none' });
  70. }
  71. },
  72. fail: (err) => {
  73. console.error('标签接口异常', err);
  74. wx.showToast({ title: '网络错误', icon: 'none' });
  75. }
  76. });
  77. },
  78. // 分类选择
  79. onCategoryChange(e) {
  80. const index = parseInt(e.detail.value, 10);
  81. const selectedCategory = this.data.categoryList[index];
  82. this.setData({
  83. categoryIndex: index,
  84. [`formData.categoryName`]: selectedCategory.dictLabel
  85. });
  86. },
  87. // 标签点击切换
  88. toggleTag(e) {
  89. const tagItem = e.currentTarget.dataset.item;
  90. let selectedTags = [...this.data.selectedTags];
  91. const index = selectedTags.findIndex(t => t.dictValue === tagItem.dictValue);
  92. if (index > -1) {
  93. selectedTags.splice(index, 1);
  94. } else {
  95. selectedTags.push(tagItem);
  96. }
  97. // 生成逗号分隔的标签字符串,如 "传染病,疫苗"
  98. const tagString = selectedTags.map(item => item.dictLabel).join(',');
  99. this.setData({
  100. selectedTags: selectedTags,
  101. [`formData.tags`]: tagString
  102. });
  103. },
  104. // 表单提交
  105. formSubmit(e) {
  106. const { title, categoryName, content } = this.data.formData;
  107. // 表单验证
  108. if (!title || title.trim() === '') {
  109. this.showError('请填写文章标题');
  110. return;
  111. }
  112. if (!categoryName) {
  113. this.showError('请选择文章分类');
  114. return;
  115. }
  116. if (this.data.selectedTags.length === 0) {
  117. this.showError('请至少选择一个标签');
  118. return;
  119. }
  120. if (!content || content.trim() === '') {
  121. this.showError('请填写文章内容');
  122. return;
  123. }
  124. this.setData({ submitting: true });
  125. // 构建提交数据 - tags 已经是字符串格式
  126. const postData = {
  127. title: this.data.formData.title,
  128. summary: this.data.formData.summary || '',
  129. categoryName: this.data.formData.categoryName,
  130. tags: this.data.formData.tags, // 已经是 "传染病,疫苗" 格式
  131. content: this.data.formData.content
  132. };
  133. console.log('提交数据:', postData); // 调试用
  134. http.shareAdd({
  135. data: postData,
  136. success: (res) => {
  137. if(res.code == 200){
  138. wx.showToast({
  139. title: '发布成功',
  140. icon: 'success',
  141. duration: 2000,
  142. success: () => {
  143. setTimeout(() => {
  144. wx.navigateBack();
  145. }, 1500);
  146. }
  147. })
  148. }else{
  149. console.error('发布失败', err);
  150. this.showError('发布失败,请重试');
  151. this.setData({ submitting: false });
  152. }
  153. },
  154. fail: (err) => {
  155. console.error('发布失败', err);
  156. this.showError('发布失败,请重试');
  157. this.setData({ submitting: false });
  158. },
  159. complete: () => {
  160. setTimeout(() => {
  161. this.setData({ submitting: false });
  162. }, 3000);
  163. }
  164. });
  165. },
  166. // 错误提示
  167. showError(msg) {
  168. wx.showToast({
  169. title: msg,
  170. icon: 'none',
  171. duration: 2000
  172. });
  173. }
  174. });