growup.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const util = require('../../utils/util.js');
  2. const config = require('../../utils/config.js')
  3. const app = getApp();
  4. Page({
  5. data: {
  6. userInfo: app.globalData.userInfo,
  7. list: [],
  8. childs: [],
  9. lists: [],
  10. finished: false,
  11. pageSize: 10,
  12. pageIndex: 1,
  13. finishedText: '',
  14. img: config.imgPath,
  15. },
  16. onShow: function () {
  17. this.setData({ userInfo: app.globalData.userInfo, finished: false, pageIndex: 1, finishedText: '' });
  18. this.getChilds();
  19. this.listInfo();
  20. },
  21. getChilds() {
  22. let that = this;
  23. if (!app.globalData.userInfo) return false;
  24. util.ajax({ func: "v2/user/grow/childs", load: false }, function (res) {
  25. if (res.code == 0) {
  26. res.data.forEach((item, index) => {
  27. res.data[index].avatar = item.avatar ? item.avatar : '/images/default_logo.jpg';
  28. });
  29. that.setData({ childs: res.data });
  30. } else
  31. util.showTips(res.reason)
  32. })
  33. },
  34. listInfo() {
  35. let that = this;
  36. util.ajax({ func: 'v2/comments/grow/list', load: false }, function (res) {
  37. if (res.code == 0) {
  38. let lists = res.data;
  39. lists.forEach((item, index) => {
  40. lists[index].tags = item.content.match(/#([^#]+)#/g);
  41. lists[index].content = item.content.replace(/#([^#]+)#/g, "").replace(/<br\/>/g, '\n');
  42. lists[index].ftags = item.fcontent ? item.fcontent.match(/#([^#]+)#/g) : [];
  43. lists[index].fcontent = item.fcontent ? item.fcontent.replace(/#([^#]+)#/g, "").replace(/<br\/>/g, '\n') : '';
  44. })
  45. that.setData({ lists })
  46. that.getList(1);
  47. } else
  48. util.showTips(res.reason)
  49. })
  50. },
  51. setLike(e) {
  52. let info = e.currentTarget.dataset.info, index = e.currentTarget.dataset.index;
  53. if (info.likeStatus == 1) return false;
  54. let that = this;
  55. if (app.globalData.userInfo) {
  56. util.ajax({
  57. func: "v2/user/comments/like",
  58. data: { cid: info.cid },
  59. method: "POST",
  60. load: true
  61. }, function (res) {
  62. if (res.code == 0) {
  63. let list = that.data.list;
  64. list[index].likeStatus = 1;
  65. list[index].likeCnt = list[index].likeCnt + 1;
  66. if (list[index].commentsLikes.length >= 6) list[index].commentsLikes.pop();
  67. list[index].commentsLikes.unshift({ avatar: that.data.userInfo.avatar });
  68. that.setData({ list })
  69. } else {
  70. util.showTips(res.reason)
  71. }
  72. })
  73. } else {
  74. // 静默登录
  75. util.silentLogin().then(resr => {
  76. util.ajax({
  77. func: "v2/user/comments/like",
  78. data: { cid: info.cid },
  79. method: "POST",
  80. load: true
  81. }, function (res) {
  82. if (res.code == 0) {
  83. let list = that.data.list;
  84. list[index].likeStatus = 1;
  85. list[index].likeCnt = list[index].likeCnt + 1;
  86. if (list[index].commentsLikes.length >= 6) list[index].commentsLikes.pop();
  87. list[index].commentsLikes.unshift({ avatar: that.data.userInfo.avatar });
  88. that.setData({ list })
  89. } else {
  90. util.showTips(res.reason)
  91. }
  92. })
  93. })
  94. }
  95. },
  96. getList(type) {
  97. wx.hideLoading();
  98. let data = this.data.lists.slice(this.data.pageSize * (this.data.pageIndex - 1), this.data.pageSize * this.data.pageIndex);
  99. let list = type ? data : [...this.data.list, ...data];
  100. this.setData({ list });
  101. if (this.data.list.length >= this.data.lists.length || this.data.list.length < this.data.pageSize) {
  102. this.setData({ finishedText: this.data.list.length < this.data.pageSize ? '' : "~~已经到底了~~", finished: true })
  103. } else {
  104. }
  105. },
  106. clickImg(arr, index) {
  107. ImagePreview({ images: arr, startPosition: index, lazyLoad: true, closeOnPopstate: true, showIndex: false, showIndicators: true });
  108. },
  109. childsBinderror(e) {
  110. var childs = this.data.childs, index = e.currentTarget.dataset.index;
  111. childs[index].avatar = '/images/default_logo.jpg'
  112. this.setData({ childs });
  113. },
  114. binderror(e) {
  115. var list = this.data.list, index = e.currentTarget.dataset.index;
  116. list[index].ownerAvatar = '/images/default_logo.jpg'
  117. this.setData({ list });
  118. },
  119. imgListBinderror(e) {
  120. var list = this.data.list, index = e.currentTarget.dataset.index, i = e.currentTarget.dataset.i;
  121. list[index].imageList[i] = '/images/noimg.png';
  122. this.setData({ list });
  123. },
  124. activeLogoBinderror(e) {
  125. var list = this.data.list, index = e.currentTarget.dataset.index;
  126. list[index].article.logo = '/images/noimg.png'
  127. this.setData({ list });
  128. },
  129. userBinderror(e) {
  130. var list = this.data.list, index = e.currentTarget.dataset.index, i = e.currentTarget.dataset.i;
  131. list[index].commentsLikes[i].avatar = '/images/default_logo.jpg'
  132. this.setData({ list });
  133. },
  134. previewImage(e) {
  135. let index = e.currentTarget.dataset.index, i = e.target.dataset.i, list = this.data.list;
  136. this.setData({ onshow: false });
  137. wx.previewImage({
  138. current: list[index].imageList[i], // 当前显示图片的http链接
  139. urls: list[index].imageList // 需要预览的图片http链接列表
  140. })
  141. },
  142. onReachBottom() {
  143. if (this.data.finished) return false
  144. wx.showLoading();
  145. this.setData({ pageIndex: this.data.pageIndex + 1 });
  146. this.getList();
  147. },
  148. onShareAppMessage() {
  149. return {
  150. title: '这里,藏着孩子的未来',
  151. path: '/pages/home/growup'
  152. }
  153. }
  154. })