index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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-shoucang"></text>
  20. {{item.collect_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. searchInfo: {},
  40. config: {},
  41. user: {},
  42. list: [],
  43. total: 0,
  44. skip: 0,
  45. limit: 6,
  46. page: 0,
  47. // 数据是否触底
  48. is_bottom: false,
  49. scrollTop: 0,
  50. }
  51. },
  52. onLoad: async function(e) {
  53. const that = this;
  54. that.searchToken();
  55. that.searchConfig();
  56. await that.searchOther();
  57. },
  58. onShow: async function() {
  59. const that = this;
  60. that.clearPage();
  61. await that.search();
  62. },
  63. onPullDownRefresh: async function() {
  64. const that = this;
  65. that.clearPage();
  66. await that.search();
  67. uni.stopPullDownRefresh();
  68. },
  69. methods: {
  70. searchToken() {
  71. const that = this;
  72. try {
  73. const res = uni.getStorageSync('token');
  74. if (res) that.$set(that, `user`, res);
  75. } catch (e) {
  76. uni.showToast({
  77. title: err.errmsg,
  78. icon: 'error',
  79. duration: 2000
  80. });
  81. }
  82. },
  83. searchConfig() {
  84. const that = this;
  85. try {
  86. const res = uni.getStorageSync('config');
  87. if (res) that.$set(that, `config`, res);
  88. } catch (e) {
  89. uni.showToast({
  90. title: err.errmsg,
  91. icon: 'error',
  92. duration: 2000
  93. });
  94. }
  95. },
  96. async search() {
  97. const that = this;
  98. if (!that.user._id) return
  99. let info = {
  100. skip: that.skip,
  101. limit: that.limit,
  102. user: that.user._id,
  103. type: '1'
  104. }
  105. const res = await that.$api(`/like/article`, 'GET', {
  106. ...info,
  107. ...that.searchInfo
  108. })
  109. if (res.errcode == '0') {
  110. let list = [...that.list, ...res.data];
  111. that.$set(that, `list`, list)
  112. that.$set(that, `total`, res.total)
  113. } else {
  114. uni.showToast({
  115. title: res.errmsg,
  116. });
  117. }
  118. },
  119. // 查看详情
  120. toInfo(item) {
  121. uni.navigateTo({
  122. url: `/pagesHome/article/index?id=${item.id||item._id}`
  123. })
  124. },
  125. // 取消收藏
  126. async toLike(item) {
  127. const that = this;
  128. let res;
  129. res = await that.$api(`/like/${item.collect}`, 'DELETE', {})
  130. if (res.errcode == '0') {
  131. that.clearPage();
  132. that.search()
  133. }
  134. },
  135. async searchOther() {
  136. const that = this;
  137. let res;
  138. },
  139. // 分页
  140. toPage(e) {
  141. const that = this;
  142. let list = that.list;
  143. let limit = that.limit;
  144. if (that.total > list.length) {
  145. uni.showLoading({
  146. title: '加载中',
  147. mask: true
  148. })
  149. let page = that.page + 1;
  150. that.$set(that, `page`, page)
  151. let skip = page * limit;
  152. that.$set(that, `skip`, skip)
  153. that.search();
  154. uni.hideLoading();
  155. } else that.$set(that, `is_bottom`, true)
  156. },
  157. toScroll(e) {
  158. const that = this;
  159. let up = that.scrollTop;
  160. that.$set(that, `scrollTop`, e.detail.scrollTop);
  161. let num = Math.sign(up - e.detail.scrollTop);
  162. if (num == 1) that.$set(that, `is_bottom`, false);
  163. },
  164. // 清空列表
  165. clearPage() {
  166. const that = this;
  167. that.$set(that, `list`, [])
  168. that.$set(that, `skip`, 0)
  169. that.$set(that, `limit`, 6)
  170. that.$set(that, `page`, 0)
  171. }
  172. }
  173. }
  174. </script>
  175. <style lang="scss" scoped>
  176. .main {
  177. display: flex;
  178. flex-direction: column;
  179. width: 100vw;
  180. height: 100vh;
  181. .one {
  182. position: relative;
  183. flex-grow: 1;
  184. background-color: var(--f9Color);
  185. margin: 2vw 0 0 0;
  186. .pubuBox {
  187. padding: 2vw;
  188. }
  189. .pubuItem {
  190. column-count: 2;
  191. column-gap: 2vw;
  192. }
  193. .list {
  194. box-sizing: border-box;
  195. border-radius: 2vw;
  196. overflow: hidden;
  197. background-color: var(--mainColor);
  198. break-inside: avoid;
  199. /*避免在元素内部插入分页符*/
  200. box-sizing: border-box;
  201. margin-bottom: 2vw;
  202. }
  203. .list image {
  204. width: 100%;
  205. }
  206. .title {
  207. padding: 2vw;
  208. .title_1 {
  209. font-size: var(--font14Size);
  210. line-height: 4vw;
  211. text-overflow: -o-ellipsis-lastline;
  212. overflow: hidden;
  213. text-overflow: ellipsis;
  214. display: -webkit-box;
  215. -webkit-line-clamp: 2;
  216. line-clamp: 2;
  217. -webkit-box-orient: vertical;
  218. min-height: 6vw;
  219. max-height: 20vw;
  220. }
  221. .title_2 {
  222. display: flex;
  223. justify-content: space-between;
  224. font-size: var(--font12Size);
  225. color: var(--f69Color);
  226. padding: 1vw 0;
  227. .right {
  228. display: flex;
  229. align-items: center;
  230. text:first-child {
  231. padding: 0 1vw 0 0;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. </style>