WaterfallsFlow.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <view class="flex-1" v-if="column">
  3. <view>
  4. <view id="left" v-if="allList.length">
  5. <view v-for="(item,index) in allList" :key="index" class="wf-itemsss">
  6. <discoverFlowItem :items="item" v-if="fromType==1" :fromTo="fromTo" @changeLikeToggle="(item)=>changeLikeToggle(false, index, item)" ></discoverFlowItem>
  7. <WaterfallsFlowItem v-else :item="item" :isStore="isStore" :type="type"/>
  8. </view>
  9. </view>
  10. </view>
  11. </view>
  12. <view class="wf-page" v-else>
  13. <!-- left -->
  14. <view>
  15. <view id="left" v-if="leftList.length">
  16. <view v-for="(item,index) in leftList" :key="index" class="wf-item">
  17. <discoverFlowItem :items="item" v-if="fromType==1" :fromTo="fromTo" @changeLikeToggle="(item)=>changeLikeToggle(false, index, item)" ></discoverFlowItem>
  18. <WaterfallsFlowItem v-else :item="item" :isStore="isStore" :type="type"/>
  19. </view>
  20. </view>
  21. </view>
  22. <!-- right -->
  23. <view>
  24. <view id="right" v-if="rightList.length">
  25. <view v-for="(item,index) in rightList" :key="index" class="wf-item">
  26. <discoverFlowItem :items="item" v-if="fromType==1" :fromTo="fromTo" @changeLikeToggle="(item)=>changeLikeToggle(true, index, item)" ></discoverFlowItem>
  27. <WaterfallsFlowItem v-else :item="item" :isStore="isStore" :type="type"/>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. // +----------------------------------------------------------------------
  35. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  36. // +----------------------------------------------------------------------
  37. // | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
  38. // +----------------------------------------------------------------------
  39. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  40. // +----------------------------------------------------------------------
  41. // | Author: CRMEB Team <admin@crmeb.com>
  42. // +----------------------------------------------------------------------
  43. import WaterfallsFlowItem from '../WaterfallsFlowItem/WaterfallsFlowItem.vue'
  44. import discoverFlowItem from '@/components/discoverFlowItem/discoverFlowItem.vue'
  45. import {
  46. mapGetters
  47. } from "vuex";
  48. import {
  49. toLogin
  50. } from '@/libs/login.js';
  51. import useActivity from "@/mixins/useActivity";
  52. export default {
  53. mixins: [useActivity],
  54. components: {
  55. WaterfallsFlowItem,
  56. discoverFlowItem
  57. },
  58. computed: {
  59. ...mapGetters(['isLogin']),
  60. },
  61. props: {
  62. // 区分从发现列表、我的主页作品进去,点进去内容列表,home从我的主页点进去
  63. fromTo: {
  64. type: String,
  65. default: ''
  66. },
  67. // 区分瀑布流使用子组件,1逛逛瀑布流
  68. fromType: {
  69. type: Number || String,
  70. default: 0
  71. },
  72. // 瀑布流列表
  73. wfList: {
  74. type: Array || String,
  75. require: true
  76. },
  77. updateNum: {
  78. type: Number,
  79. default: 10
  80. },
  81. type: {
  82. type: Number,
  83. default: 0
  84. },
  85. isStore: {
  86. type: Number,
  87. default: 0
  88. },
  89. column: {
  90. type: Boolean,
  91. default: false
  92. }
  93. },
  94. data() {
  95. return {
  96. allList: [], // 全部列表
  97. leftList: [], // 左边列表
  98. rightList: [], // 右边列表
  99. mark: 0, // 列表标记
  100. boxHeight: [], // 下标0和1分别为左列和右列高度
  101. };
  102. },
  103. watch: {
  104. // 监听列表数据变化
  105. wfList: {
  106. handler(nVal, oVal) {
  107. // 如果数据为空或新的列表数据少于旧的列表数据(通常为下拉刷新或切换排序或使用筛选器),初始化变量
  108. if (!this.wfList.length ||
  109. (this.wfList.length === this.updateNum && this.wfList.length <= this.allList.length)) {
  110. this.allList = [];
  111. this.leftList = [];
  112. this.rightList = [];
  113. this.boxHeight = [];
  114. this.mark = 0;
  115. }
  116. // 如果列表有值,调用waterfall方法
  117. if (this.wfList.length) {
  118. this.allList = this.wfList;
  119. this.leftList = [];
  120. this.rightList = [];
  121. this.boxHeight = [];
  122. this.allList.forEach((v, i) => {
  123. if (this.allList.length < 3 || (this.allList.length <= 7 && this.allList.length - i >
  124. 1) || (this.allList.length > 7 && this.allList.length - i > 2)) {
  125. if (i % 2) {
  126. this.rightList.push(v);
  127. } else {
  128. this.leftList.push(v);
  129. }
  130. }
  131. });
  132. if (this.allList.length < 3) {
  133. this.mark = this.allList.length + 1;
  134. } else if (this.allList.length <= 7) {
  135. this.mark = this.allList.length - 1;
  136. } else {
  137. this.mark = this.allList.length - 2;
  138. }
  139. if (this.mark < this.allList.length) {
  140. this.waterFall()
  141. }
  142. }
  143. },
  144. immediate: true,
  145. deep: true
  146. },
  147. mounted() {},
  148. // 监听标记,当标记发生变化,则执行下一个item排序
  149. mark() {
  150. const len = this.allList.length;
  151. if (this.mark < len && this.mark !== 0 && this.boxHeight.length) {
  152. this.waterFall();
  153. }
  154. }
  155. },
  156. methods: {
  157. // 点赞
  158. changeLikeToggle(type, index) {
  159. if (this.isLogin == true) {
  160. if (type) {
  161. this.handleLikeToggle(this.rightList[index])
  162. } else {
  163. this.handleLikeToggle(this.leftList[index])
  164. }
  165. } else {
  166. toLogin()
  167. }
  168. },
  169. // 瀑布流排序
  170. waterFall() {
  171. const i = this.mark;
  172. if (i == 0) {
  173. // 初始化,从左边开始插入
  174. this.leftList.push(this.allList[i]);
  175. // 更新左边列表高度
  176. this.getViewHeight(0);
  177. } else if (i == 1) {
  178. // 第二个item插入,默认为右边插入
  179. this.rightList.push(this.allList[i]);
  180. // 更新右边列表高度
  181. this.getViewHeight(1);
  182. } else {
  183. // 根据左右列表高度判断下一个item应该插入哪边
  184. if (!this.boxHeight.length) {
  185. this.rightList.length < this.leftList.length ?
  186. this.rightList.push(this.allList[i]) :
  187. this.leftList.push(this.allList[i]);
  188. } else {
  189. const leftOrRight = this.boxHeight[0] > this.boxHeight[1] ? 1 : 0;
  190. if (leftOrRight) {
  191. this.rightList.push(this.allList[i])
  192. } else {
  193. this.leftList.push(this.allList[i])
  194. }
  195. }
  196. // 更新插入列表高度
  197. this.getViewHeight();
  198. }
  199. },
  200. // 获取列表高度
  201. getViewHeight() {
  202. // 使用nextTick,确保页面更新结束后,再请求高度
  203. this.$nextTick(() => {
  204. setTimeout(() => {
  205. uni.createSelectorQuery().in(this).select('#right').boundingClientRect(res => {
  206. res ? this.boxHeight[1] = res.height : '';
  207. uni.createSelectorQuery().in(this).select('#left').boundingClientRect(
  208. res => {
  209. res ? this.boxHeight[0] = res.height : '';
  210. this.mark = this.mark + 1;
  211. }).exec();
  212. }).exec();
  213. }, 100)
  214. })
  215. },
  216. // item点击
  217. itemTap(item) {
  218. this.$emit('itemTap', item)
  219. }
  220. }
  221. }
  222. </script>
  223. <style lang="scss" scoped>
  224. $page-padding: 12px;
  225. $grid-gap: 24rpx;
  226. .wf-page {
  227. display: grid;
  228. grid-template-columns: 1fr 1fr;
  229. grid-gap: $grid-gap;
  230. background-color: #f5f5f5;
  231. }
  232. .wf-item {
  233. width: calc((100vw - 2 * #{$page-padding} - #{$grid-gap}) / 2);
  234. padding-bottom: $grid-gap;
  235. //margin-bottom: 20rpx;
  236. }
  237. .wf-itemsss {
  238. // width: calc((100vw - 2 * #{$page-padding} - #{$grid-gap}) / 2);
  239. padding-bottom: $grid-gap;
  240. //margin-bottom: 20rpx;
  241. }
  242. .wf-page1 .wf-item {
  243. background-color: #fff;
  244. border-radius: 20rpx;
  245. padding-bottom: 0;
  246. }
  247. .wf-item-page {
  248. padding-bottom: 20rpx;
  249. }
  250. </style>