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

327 lines
7.1 KiB

  1. import http from '../../../utils/api'
  2. const baseUrl = require('../../../utils/baseUrl')
  3. Page({
  4. data: {
  5. postId: null, // 存储帖子ID
  6. post: null,
  7. postHf: [], // 帖子回复列表
  8. replyContent: '',
  9. baseUrl: baseUrl,
  10. replyTarget: {
  11. type: '', // 'post'或'reply'或'comment'
  12. id: '', // 回复或评论的ID
  13. nickName: '',
  14. replyIndex: null,
  15. commentIndex: null,
  16. parentId: null // 父级ID(用于二级评论)
  17. },
  18. replyPlaceholder: '输入您的回复...',
  19. isInputFocused: false,
  20. inputTransformY: '0',
  21. isSubmitting: false,
  22. showPreview: false,
  23. previewImages: [],
  24. previewIndex: 0,
  25. loading: false,
  26. scrollToId: '',
  27. keyboardHeight: 0
  28. },
  29. onLoad: function (options) {
  30. const postId = options.id
  31. this.setData({
  32. postId: postId
  33. })
  34. this.loadPostDetail(postId);
  35. this.getforumReply(postId)
  36. // 监听键盘高度变化
  37. wx.onKeyboardHeightChange(res => {
  38. if (res.height > 0) {
  39. this.setData({
  40. keyboardHeight: res.height
  41. });
  42. }
  43. });
  44. },
  45. // 加载帖子详情
  46. loadPostDetail: function (postId) {
  47. this.setData({
  48. loading: true
  49. });
  50. http.forumDetails({
  51. data: {
  52. id: postId
  53. },
  54. success: res => {
  55. console.log('帖子详情:', res);
  56. const postData = res.data
  57. const images = postData.images ? postData.images.split(',') : []
  58. this.setData({
  59. post: postData,
  60. images: images,
  61. loading: false
  62. });
  63. },
  64. fail: err => {
  65. console.error('加载帖子详情失败:', err);
  66. wx.showToast({
  67. title: '加载失败',
  68. icon: 'none'
  69. });
  70. this.setData({
  71. loading: false
  72. });
  73. }
  74. })
  75. },
  76. // 帖子回复列表
  77. getforumReply(postId) {
  78. http.forumReply({
  79. data: {
  80. questionId: postId
  81. },
  82. success: res => {
  83. console.log('回复列表:', res);
  84. this.setData({
  85. postHf: res.rows || []
  86. })
  87. },
  88. fail: err => {
  89. console.error('加载回复列表失败:', err);
  90. this.setData({
  91. postHf: []
  92. })
  93. }
  94. })
  95. },
  96. // 滚动监听
  97. onScroll: function (e) {
  98. // 可以在这里实现滚动相关逻辑
  99. },
  100. // 输入框获取焦点
  101. onInputFocus: function (e) {
  102. this.setData({
  103. isInputFocused: true,
  104. inputTransformY: `-${e.detail.height}px`
  105. });
  106. },
  107. // 输入框失去焦点
  108. onInputBlur: function () {
  109. this.setData({
  110. isInputFocused: false,
  111. inputTransformY: '0'
  112. });
  113. },
  114. // 回复输入
  115. onReplyInput: function (e) {
  116. this.setData({
  117. replyContent: e.detail.value
  118. });
  119. },
  120. // 回复一级评论
  121. replyToUser: function (e) {
  122. console.log(567,e);
  123. const {
  124. type,
  125. index,
  126. username
  127. } = e.currentTarget.dataset;
  128. const replyItem = this.data.postHf[index];
  129. this.setData({
  130. replyTarget: {
  131. type: 'reply', // 回复一级评论
  132. id: replyItem.id,
  133. nickName: username,
  134. replyIndex: parseInt(index),
  135. parentId: null // 一级评论没有父级ID
  136. },
  137. replyPlaceholder: `回复 @${username}...`,
  138. replyContent: '',
  139. isInputFocused: true
  140. });
  141. // 滚动到对应回复位置
  142. setTimeout(() => {
  143. this.setData({
  144. scrollToId: `reply-${replyItem.id}`
  145. });
  146. }, 100);
  147. },
  148. // 清除回复目标
  149. clearReplyTarget: function () {
  150. this.setData({
  151. replyTarget: {
  152. type: '',
  153. id: '',
  154. nickName: '',
  155. replyIndex: null,
  156. commentIndex: null,
  157. parentId: null
  158. },
  159. replyPlaceholder: '输入您的回复...',
  160. replyContent: ''
  161. });
  162. },
  163. // 提交回复
  164. submitReply: function () {
  165. const {
  166. replyContent,
  167. replyTarget,
  168. postId
  169. } = this.data;
  170. const content = replyContent.trim();
  171. if (!content) {
  172. wx.showToast({
  173. title: '请输入内容',
  174. icon: 'none'
  175. });
  176. return;
  177. }
  178. if (content.length > 500) {
  179. wx.showToast({
  180. title: '回复内容不能超过500字',
  181. icon: 'none'
  182. });
  183. return;
  184. }
  185. this.setData({
  186. isSubmitting: true
  187. });
  188. // 准备提交数据
  189. const submitData = {
  190. questionId: postId, // 帖子ID
  191. content: content // 回复内容
  192. };
  193. // 如果有回复目标,添加父级ID
  194. if (replyTarget.type === 'reply' && replyTarget.id) {
  195. submitData.parentId = replyTarget.id; // 回复一级评论
  196. } else if (replyTarget.type === 'comment' && replyTarget.id) {
  197. submitData.parentId = replyTarget.id; // 回复二级评论
  198. }
  199. // 如果type为'post'或为空,则表示直接回复帖子,parentId为空
  200. console.log('提交数据:', submitData);
  201. // 调用提交接口
  202. http.commentReply({
  203. data: submitData,
  204. success: res => {
  205. console.log('回复成功:', res);
  206. // 提交成功后的处理
  207. this.handleReplySuccess(content, replyTarget);
  208. // 刷新回复列表
  209. setTimeout(() => {
  210. this.getforumReply(postId);
  211. }, 500);
  212. },
  213. fail: err => {
  214. console.error('回复失败:', err);
  215. wx.showToast({
  216. title: '回复失败,请重试',
  217. icon: 'none',
  218. duration: 2000
  219. });
  220. this.setData({
  221. isSubmitting: false
  222. });
  223. }
  224. });
  225. },
  226. // 处理回复成功后的UI更新
  227. handleReplySuccess: function (content, replyTarget) {
  228. // 清空输入框和回复目标
  229. this.setData({
  230. replyContent: '',
  231. replyTarget: {
  232. type: '',
  233. id: '',
  234. nickName: '',
  235. replyIndex: null,
  236. commentIndex: null,
  237. parentId: null
  238. },
  239. replyPlaceholder: '输入您的回复...',
  240. isInputFocused: false,
  241. isSubmitting: false
  242. });
  243. // 如果是直接回复帖子,滚动到底部
  244. if (!replyTarget.type || replyTarget.type === 'post') {
  245. setTimeout(() => {
  246. wx.pageScrollTo({
  247. scrollTop: 9999,
  248. duration: 300
  249. });
  250. }, 300);
  251. }
  252. },
  253. // 图片预览
  254. previewImage: function (e) {
  255. const imgIndex = e.currentTarget.dataset.imgIndex;
  256. const images = this.data.images;
  257. this.setData({
  258. showPreview: true,
  259. previewImages: images,
  260. previewIndex: imgIndex
  261. });
  262. },
  263. // 图片预览滑动
  264. onSwiperChange: function (e) {
  265. this.setData({
  266. previewIndex: e.detail.current
  267. });
  268. },
  269. // 隐藏预览
  270. hidePreview: function () {
  271. this.setData({
  272. showPreview: false
  273. });
  274. },
  275. // 下拉刷新
  276. onPullDownRefresh: function () {
  277. if (this.data.postId) {
  278. this.loadPostDetail(this.data.postId);
  279. this.getforumReply(this.data.postId);
  280. }
  281. wx.stopPullDownRefresh();
  282. },
  283. // 页面上拉触底
  284. onReachBottom: function () {
  285. // 这里可以实现加载更多回复的逻辑
  286. console.log('加载更多回复');
  287. },
  288. // 页面卸载
  289. onUnload: function () {
  290. // 移除键盘高度监听
  291. wx.offKeyboardHeightChange();
  292. }
  293. });