index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <view class="main">
  3. <view class="one">
  4. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  5. <view class="list-scroll-view">
  6. <!-- 瀑布流布局列表 -->
  7. <view class="pubuBox">
  8. <view class="pubuItem">
  9. <view class="list" v-for="(item, index) in list" :key="index" @tap="toInfo(item)">
  10. <image :src="item.file&&item.file.length>0?item.file[0].url:''" mode="widthFix">
  11. </image>
  12. <view class="title"> <!-- 这是没有高度的父盒子(下半部分) -->
  13. <view class="title_1">{{ item.title ||'暂无'}}</view>
  14. <view class="title_2">
  15. <view class="left">
  16. {{item.contact_name||'暂无昵称'}}
  17. </view>
  18. <view class="right" @tap.stop="toLike(item)">
  19. <text class="iconfont icon-aixin1"></text>
  20. {{item.like_num||'0'}}
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="is_bottom" v-if="is_bottom">
  28. <text>{{config.bottom_title||'到底了!'}}</text>
  29. </view>
  30. </view>
  31. </scroll-view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. export default {
  37. data() {
  38. return {
  39. config: {},
  40. user: {},
  41. list: [],
  42. total: 0,
  43. skip: 0,
  44. limit: 6,
  45. page: 0,
  46. // 数据是否触底
  47. is_bottom: false,
  48. scrollTop: 0,
  49. }
  50. },
  51. onLoad: async function(e) {
  52. const that = this;
  53. that.searchToken();
  54. that.searchConfig();
  55. },
  56. onShow: async function() {
  57. const that = this;
  58. that.clearPage();
  59. await that.search();
  60. },
  61. onPullDownRefresh: async function() {
  62. const that = this;
  63. that.clearPage();
  64. await that.search();
  65. uni.stopPullDownRefresh();
  66. },
  67. methods: {
  68. searchToken() {
  69. const that = this;
  70. try {
  71. const res = uni.getStorageSync('token');
  72. if (res) that.$set(that, `user`, res);
  73. } catch (e) {
  74. uni.showToast({
  75. title: err.errmsg,
  76. icon: 'error',
  77. duration: 2000
  78. });
  79. }
  80. },
  81. searchConfig() {
  82. const that = this;
  83. try {
  84. const res = uni.getStorageSync('config');
  85. if (res) that.$set(that, `config`, res);
  86. } catch (e) {
  87. uni.showToast({
  88. title: err.errmsg,
  89. icon: 'error',
  90. duration: 2000
  91. });
  92. }
  93. },
  94. async search() {
  95. const that = this;
  96. if (!that.user._id) return
  97. let info = {
  98. skip: that.skip,
  99. limit: that.limit,
  100. user: that.user._id,
  101. type: '0'
  102. }
  103. const res = await that.$api(`/like/article`, 'GET', {
  104. ...info,
  105. ...that.searchInfo
  106. })
  107. if (res.errcode == '0') {
  108. let list = [...that.list, ...res.data];
  109. that.$set(that, `list`, list)
  110. that.$set(that, `total`, res.total)
  111. } else {
  112. uni.showToast({
  113. title: res.errmsg,
  114. });
  115. }
  116. },
  117. // 查看详情
  118. toInfo(item) {
  119. uni.navigateTo({
  120. url: `/pagesHome/article/index?id=${item.id||item._id}`
  121. })
  122. },
  123. // 取消点赞
  124. async toLike(item) {
  125. const that = this;
  126. let res;
  127. res = await that.$api(`/like/${item.like}`, 'DELETE', {})
  128. if (res.errcode == '0') {
  129. that.clearPage();
  130. that.search()
  131. }
  132. },
  133. // 分页
  134. toPage(e) {
  135. const that = this;
  136. let list = that.list;
  137. let limit = that.limit;
  138. if (that.total > list.length) {
  139. uni.showLoading({
  140. title: '加载中',
  141. mask: true
  142. })
  143. let page = that.page + 1;
  144. that.$set(that, `page`, page)
  145. let skip = page * limit;
  146. that.$set(that, `skip`, skip)
  147. that.search();
  148. uni.hideLoading();
  149. } else that.$set(that, `is_bottom`, true)
  150. },
  151. toScroll(e) {
  152. const that = this;
  153. let up = that.scrollTop;
  154. that.$set(that, `scrollTop`, e.detail.scrollTop);
  155. let num = Math.sign(up - e.detail.scrollTop);
  156. if (num == 1) that.$set(that, `is_bottom`, false);
  157. },
  158. // 清空列表
  159. clearPage() {
  160. const that = this;
  161. that.$set(that, `list`, [])
  162. that.$set(that, `skip`, 0)
  163. that.$set(that, `limit`, 6)
  164. that.$set(that, `page`, 0)
  165. }
  166. }
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. .main {
  171. display: flex;
  172. flex-direction: column;
  173. width: 100vw;
  174. height: 100vh;
  175. .one {
  176. position: relative;
  177. flex-grow: 1;
  178. background-color: var(--f9Color);
  179. margin: 2vw 0 0 0;
  180. .pubuBox {
  181. padding: 2vw;
  182. }
  183. .pubuItem {
  184. column-count: 2;
  185. column-gap: 2vw;
  186. }
  187. .list {
  188. box-sizing: border-box;
  189. border-radius: 2vw;
  190. overflow: hidden;
  191. background-color: var(--mainColor);
  192. break-inside: avoid;
  193. /*避免在元素内部插入分页符*/
  194. box-sizing: border-box;
  195. margin-bottom: 2vw;
  196. }
  197. .list image {
  198. width: 100%;
  199. }
  200. .title {
  201. padding: 2vw;
  202. .title_1 {
  203. font-size: var(--font14Size);
  204. line-height: 4vw;
  205. text-overflow: -o-ellipsis-lastline;
  206. overflow: hidden;
  207. text-overflow: ellipsis;
  208. display: -webkit-box;
  209. -webkit-line-clamp: 2;
  210. line-clamp: 2;
  211. -webkit-box-orient: vertical;
  212. min-height: 6vw;
  213. max-height: 20vw;
  214. }
  215. .title_2 {
  216. display: flex;
  217. justify-content: space-between;
  218. font-size: var(--font12Size);
  219. color: var(--f69Color);
  220. padding: 1vw 0;
  221. .right {
  222. display: flex;
  223. align-items: center;
  224. text:first-child {
  225. padding: 0 1vw 0 0;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. .scroll-view {
  233. position: absolute;
  234. top: 0;
  235. left: 0;
  236. right: 0;
  237. bottom: 0;
  238. .list-scroll-view {
  239. display: flex;
  240. flex-direction: column;
  241. }
  242. }
  243. .is_bottom {
  244. width: 100%;
  245. text-align: center;
  246. text {
  247. padding: 2vw 0;
  248. display: inline-block;
  249. color: var(--f85Color);
  250. font-size: var(--font14Size);
  251. }
  252. }
  253. </style>