detail.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const { Toast } = require('../../assets/libs/zanui/index');
  2. var app = getApp();
  3. Page(Object.assign({}, Toast, {
  4. data: {
  5. userInfo: null,
  6. postInfo: { article: {} },
  7. commentList: [],
  8. loading: false,
  9. nodata: true,
  10. nomore: false,
  11. form: { quotepid: 0, message: '', focus: false }
  12. },
  13. page: 1,
  14. onLoad: function (options) {
  15. var that = this;
  16. that.setData({ userInfo: app.globalData.userInfo });
  17. app.request('/post/detail', { id: options.id }, function (data, ret) {
  18. var content = data.postInfo.content;
  19. data.postInfo.article = app.towxml.toJson(content, 'html');
  20. data.commentList.forEach(function (item) {
  21. item.article = app.towxml.toJson(item.content, 'html');
  22. });
  23. that.setData({ postInfo: data.postInfo, commentList: data.commentList, nodata: data.commentList.length === 0 });
  24. that.page++;
  25. }, function (data, ret) {
  26. app.error(ret.msg);
  27. });
  28. },
  29. reply: function (event) {
  30. var that = this;
  31. var pid = event.currentTarget.dataset.pid;
  32. var username = event.currentTarget.dataset.username;
  33. that.setData({ form: { quotepid: pid, message: '@' + username + ' ', focus: true } });
  34. },
  35. login: function (event) {
  36. var that = this;
  37. app.login(function (data) {
  38. app.info('登录成功');
  39. that.setData({ userInfo: app.globalData.userInfo });
  40. });
  41. },
  42. formSubmit: function (event) {
  43. var that = this;
  44. var pid = event.currentTarget.dataset.pid;
  45. if (!app.globalData.userInfo) {
  46. app.error('请登录后再评论');
  47. return;
  48. }
  49. if (event.detail.value.message == '') {
  50. app.error('内容不能为空');
  51. return;
  52. }
  53. app.request('/comment/post', { post_id: this.data.postInfo.id, pid: this.data.form.quotepid, username: app.globalData.userInfo.nickName, avatar: app.globalData.userInfo.avatarUrl, content: event.detail.value.content }, function (data, ret) {
  54. app.success(ret.msg);
  55. that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
  56. if (that.data.commentList.length < 10) {
  57. that.page = 1;
  58. } else {
  59. that.data.commentList = that.data.commentList.slice(0, 10);
  60. that.page = 2;
  61. }
  62. that.onReachBottom();
  63. }, function (data, ret) {
  64. that.showZanToast(ret.msg);
  65. });
  66. },
  67. onReachBottom: function () {
  68. var that = this;
  69. this.loadComment(function (data) {
  70. if (data.commentList.length == 0) {
  71. //app.info("暂无更多数据");
  72. }
  73. });
  74. },
  75. loadComment: function (cb) {
  76. var that = this;
  77. if (that.data.nomore == true || that.data.loading == true) {
  78. return;
  79. }
  80. this.setData({ loading: true });
  81. app.request('/comment', { post_id: this.data.postInfo.id, page: this.page }, function (data, ret) {
  82. data.commentList.forEach(function (item) {
  83. item.article = app.towxml.toJson(item.content, 'html');
  84. });
  85. that.setData({
  86. loading: false,
  87. nodata: that.page == 1 && data.commentList.length == 0 ? true : false,
  88. nomore: that.page > 1 && data.commentList.length == 0 ? true : false,
  89. commentList: that.page > 1 ? that.data.commentList.concat(data.commentList) : data.commentList,
  90. });
  91. that.page++;
  92. typeof cb == 'function' && cb(data);
  93. }, function (data, ret) {
  94. that.setData({
  95. loading: false
  96. });
  97. app.error(ret.msg);
  98. });
  99. },
  100. share: function () {
  101. wx.showShareMenu({});
  102. },
  103. onShareAppMessage: function () {
  104. return {
  105. title: this.data.postInfo.title,
  106. desc: this.data.postInfo.intro,
  107. path: '/page/post/detail?id=' + this.data.postInfo.id
  108. }
  109. },
  110. }))