index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. const app = getApp();
  2. import HTTP from "../../../requestFn/Api"
  3. Page({
  4. data: {
  5. garden_id: null,
  6. article_list: [],
  7. page: 1,
  8. page_size: 10,
  9. hasMore: true,
  10. comment_page: 1,
  11. class_list: [],
  12. comment_list: [],
  13. class: {},
  14. files: [],
  15. inputValue: '',
  16. tabbar: {},
  17. article_height: 50,
  18. oldHeight: 50
  19. },
  20. onLoad() {
  21. const {unreadCountNeig} = app.globalData;
  22. this.setData({unreadCountNeig});
  23. getApp().watch('unreadCountNeig',this.watchBack);
  24. app.editTabbar();
  25. },
  26. watchBack(name, value) {
  27. let data = {};
  28. data[name] = value;
  29. this.setData(data);
  30. },
  31. onShow() {
  32. const {
  33. garden_id
  34. } = app.globalData;
  35. if (garden_id) {
  36. this.setData({
  37. garden_id
  38. })
  39. }
  40. this.init();
  41. },
  42. init() {
  43. this.setData({
  44. page: 1,
  45. hasMore: true
  46. })
  47. this.getArticle(true);
  48. this.getNeighbor_list();
  49. },
  50. getArticle(flag = false) {
  51. const that = this;
  52. let {
  53. page,
  54. page_size,
  55. article_list,
  56. garden_id
  57. } = that.data;
  58. HTTP.NeighborArticle({
  59. garden_id,
  60. page,
  61. page_size,
  62. is_me: false
  63. }).then(res => {
  64. that.setData({
  65. article_list: flag ? res.list : [...article_list, ...res.list],
  66. page: ++page,
  67. hasMore: res.list.length === page_size
  68. })
  69. })
  70. },
  71. onReachBottom() {
  72. if (!this.data.hasMore) {
  73. wx.showToast({
  74. title: '没有更多数据了',
  75. icon: 'none'
  76. })
  77. return console.log('没有更多数据了');
  78. }
  79. this.getArticle()
  80. },
  81. deleteArticle(e) {
  82. const that = this;
  83. const {
  84. id
  85. } = e.currentTarget.dataset;
  86. const {
  87. garden_id
  88. } = that.data;
  89. console.log(id);
  90. wx.showModal({
  91. title: "提示",
  92. content: "是否删除该文章",
  93. success: function (res) {
  94. if (res.confirm) {
  95. HTTP.DeleteArticle({
  96. method: 'delete',
  97. garden_id,
  98. id
  99. }).then(res => {
  100. if (res.code == 0) {
  101. setTimeout(() => {
  102. wx.showToast({
  103. title: '删除成功',
  104. icon: "success"
  105. })
  106. }, 500)
  107. that.init()
  108. }
  109. })
  110. }
  111. }
  112. })
  113. },
  114. like(e) {
  115. const that = this;
  116. const {
  117. garden_id,
  118. article_list
  119. } = that.data;
  120. const {
  121. like,
  122. like_id,
  123. like_type,
  124. article_id
  125. } = e.currentTarget.dataset;
  126. HTTP.Like({
  127. like: !like,
  128. like_id,
  129. like_type: like_type - 0,
  130. garden_id
  131. }).then(res => {
  132. if (res.code == 0) {
  133. if (like_type == 1) {
  134. article_list.map(item => {
  135. if (item.id == like_id) {
  136. item.like = !item.like;
  137. if (item.like) {
  138. item.likes++
  139. } else {
  140. item.likes--
  141. }
  142. }
  143. })
  144. that.setData({
  145. article_list
  146. })
  147. } else {
  148. article_list.map(item => {
  149. if (item.id == article_id) {
  150. item.comment_list.map(key => {
  151. if (key.id == like_id) {
  152. key.like = !key.like
  153. if (key.like) {
  154. key.likes++
  155. } else {
  156. key.likes--
  157. }
  158. }
  159. })
  160. }
  161. })
  162. that.setData({
  163. article_list
  164. })
  165. // that.get_Neighbor_comment(article_id, like_id)
  166. }
  167. }
  168. })
  169. },
  170. getNeighbor_list() {
  171. const {
  172. garden_id
  173. } = this.data;
  174. HTTP.Neighbor_class({
  175. garden_id
  176. }).then(res => {
  177. this.setData({
  178. class_list: res.list
  179. })
  180. })
  181. },
  182. getNeighbor_cpmment(e) {
  183. const that = this;
  184. const {
  185. article_id
  186. } = e.currentTarget.dataset
  187. that.get_Neighbor_comment(article_id)
  188. },
  189. get_Neighbor_comment(article_id, pid = '', flag = false) {
  190. console.log(article_id, pid, flag);
  191. const that = this;
  192. const {
  193. garden_id,
  194. comment_page
  195. } = this.data;
  196. HTTP.NeighborComment({
  197. comment_page,
  198. page_size: 10,
  199. garden_id,
  200. article_id,
  201. pid
  202. }).then(res => {
  203. const commentTag = [];
  204. const arr = that.data.article_list;
  205. arr.map(item => {
  206. if (item.id == article_id) {
  207. if (item.comment_list && flag) {
  208. item.comment_list = that.removeDuplicates(item.comment_list.concat(res.list));
  209. for (let i = 0; i < item.comment_list.length; i++) {
  210. commentTag.push(false)
  211. }
  212. item.commentTag = commentTag
  213. } else {
  214. item.comment_list = res.list
  215. for (let i = 0; i < res.list.length; i++) {
  216. commentTag.push(false)
  217. }
  218. item.commentTag = commentTag
  219. }
  220. }
  221. })
  222. console.log(arr);
  223. that.setData({
  224. article_list: arr
  225. })
  226. console.log(that.data.article_list);
  227. })
  228. },
  229. previewImage(e) {
  230. const {
  231. id
  232. } = e.currentTarget;
  233. const files = this.data.article_list.filter(item => item.id == id)
  234. console.log(files);
  235. this.setData({
  236. files: files[0].pics
  237. })
  238. wx.previewImage({
  239. current: this.data.files[e.currentTarget.dataset.index],
  240. urls: this.data.files,
  241. });
  242. },
  243. addComment(e) {
  244. const that = this;
  245. const {
  246. garden_id,
  247. inputValue
  248. } = that.data;
  249. const {
  250. pid,
  251. article_id
  252. } = e.currentTarget.dataset;
  253. HTTP.NeighborComment({
  254. method: 'post',
  255. content: inputValue,
  256. pid,
  257. garden_id,
  258. pics: []
  259. }).then(res => {
  260. if (res.code == 0) {
  261. wx.showToast({
  262. title: '评论成功',
  263. icon: "success",
  264. success() {
  265. that.get_Neighbor_comment(article_id)
  266. }
  267. })
  268. }
  269. })
  270. },
  271. showComment(e) {
  272. const {
  273. index,
  274. id,
  275. article_id
  276. } = e.currentTarget.dataset;
  277. const {
  278. article_list
  279. } = this.data;
  280. article_list.map(item => {
  281. if (item.id == article_id) {
  282. item.commentTag[index] = true;
  283. }
  284. })
  285. this.setData({
  286. article_list
  287. })
  288. },
  289. hiddenComment(e) {
  290. const {
  291. index,
  292. article_id
  293. } = e.currentTarget.dataset;
  294. const {
  295. article_list
  296. } = this.data;
  297. console.log(article_list);
  298. article_list.map(item => {
  299. if (item.id == article_id) {
  300. item.commentTag[index] = false
  301. }
  302. })
  303. this.setData({
  304. article_list
  305. })
  306. },
  307. deleteComment(e) {
  308. console.log(e);
  309. const that = this;
  310. wx.showModal({
  311. title: "提示",
  312. content: "是否删除该条评论",
  313. success: function (res) {
  314. if (res.confirm) {
  315. const {
  316. garden_id
  317. } = that.data;
  318. const {
  319. id,
  320. article_id
  321. } = e.currentTarget.dataset
  322. HTTP.DeleteComment({
  323. garden_id,
  324. id
  325. }).then(res => {
  326. if (res.code == 0) {
  327. that.get_Neighbor_comment(article_id)
  328. }
  329. })
  330. }
  331. }
  332. })
  333. },
  334. goAddArticle() {
  335. wx.navigateTo({
  336. url: '/page/neighbor/pages/addArticle/addArticle',
  337. })
  338. },
  339. getComment_son(e) {
  340. const {
  341. article_id,
  342. pid
  343. } = e.currentTarget.dataset
  344. this.get_Neighbor_comment(article_id, pid, true)
  345. },
  346. removeDuplicates(arr) {
  347. let newObj = {};
  348. arr = arr.reduce((preVal, curVal) => {
  349. newObj[curVal.id] ? '' : newObj[curVal.id] = preVal.push(curVal);
  350. return preVal
  351. }, [])
  352. return arr
  353. },
  354. /**
  355. * 页面相关事件处理函数--监听用户下拉动作
  356. */
  357. onPullDownRefresh: function () {
  358. app.onRefresh();
  359. this.init();
  360. },
  361. getCommentValue(e) {
  362. this.setData({
  363. inputValue: e.detail.value
  364. })
  365. },
  366. goDetail(e) {
  367. const {
  368. item
  369. } = e.currentTarget.dataset;
  370. let data = item;
  371. console.log(data);
  372. wx.navigateTo({
  373. url: `../../neighbor/pages/Detail/Detail`,
  374. success: function (res) {
  375. res.eventChannel.emit('acceptDataFromOpenerPage', data)
  376. }
  377. })
  378. }
  379. })