WaterfallsFlow-bak.vue 6.6 KB

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