WaterfallsFlow.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 :column="column" :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" :isDynamics="isDynamics" :userFair="userFair" :isManage="isManage"/>
  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" :isDynamics="isDynamics" :userFair="userFair" :isManage="isManage"/>
  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. isDynamics: {
  94. type: Boolean,
  95. default: false
  96. },
  97. userFair: {
  98. type: Boolean,
  99. default: false
  100. },
  101. isManage: {
  102. type: Number,
  103. default: 0
  104. }
  105. },
  106. data() {
  107. return {
  108. allList: [], // 全部列表
  109. leftList: [], // 左边列表
  110. rightList: [], // 右边列表
  111. mark: 0, // 列表标记
  112. boxHeight: [], // 下标0和1分别为左列和右列高度
  113. };
  114. },
  115. watch: {
  116. // 监听列表数据变化
  117. wfList: {
  118. handler(nVal, oVal) {
  119. // 如果数据为空或新的列表数据少于旧的列表数据(通常为下拉刷新或切换排序或使用筛选器),初始化变量
  120. if (!this.wfList.length ||
  121. (this.wfList.length === this.updateNum && this.wfList.length <= this.allList.length)) {
  122. this.allList = [];
  123. this.leftList = [];
  124. this.rightList = [];
  125. this.boxHeight = [];
  126. this.mark = 0;
  127. }
  128. // 如果列表有值,调用waterfall方法
  129. if (this.wfList.length) {
  130. this.allList = this.wfList;
  131. this.leftList = [];
  132. this.rightList = [];
  133. this.boxHeight = [];
  134. this.allList.forEach((v, i) => {
  135. if (this.allList.length < 3 || (this.allList.length <= 7 && this.allList.length - i >
  136. 1) || (this.allList.length > 7 && this.allList.length - i > 2)) {
  137. if (i % 2) {
  138. this.rightList.push(v);
  139. } else {
  140. this.leftList.push(v);
  141. }
  142. }
  143. });
  144. if (this.allList.length < 3) {
  145. this.mark = this.allList.length + 1;
  146. } else if (this.allList.length <= 7) {
  147. this.mark = this.allList.length - 1;
  148. } else {
  149. this.mark = this.allList.length - 2;
  150. }
  151. if (this.mark < this.allList.length) {
  152. this.waterFall()
  153. }
  154. }
  155. },
  156. immediate: true,
  157. deep: true
  158. },
  159. mounted() {
  160. conolse.log(this.isDynamics, this.userFair, 1111111111111)
  161. },
  162. // 监听标记,当标记发生变化,则执行下一个item排序
  163. mark() {
  164. const len = this.allList.length;
  165. if (this.mark < len && this.mark !== 0 && this.boxHeight.length) {
  166. this.waterFall();
  167. }
  168. }
  169. },
  170. methods: {
  171. // 点赞
  172. changeLikeToggle(type, index) {
  173. if (this.isLogin == true) {
  174. if (type) {
  175. this.handleLikeToggle(this.rightList[index])
  176. } else {
  177. this.handleLikeToggle(this.leftList[index])
  178. }
  179. } else {
  180. toLogin()
  181. }
  182. },
  183. // 瀑布流排序
  184. waterFall() {
  185. const i = this.mark;
  186. if (i == 0) {
  187. // 初始化,从左边开始插入
  188. this.leftList.push(this.allList[i]);
  189. // 更新左边列表高度
  190. this.getViewHeight(0);
  191. } else if (i == 1) {
  192. // 第二个item插入,默认为右边插入
  193. this.rightList.push(this.allList[i]);
  194. // 更新右边列表高度
  195. this.getViewHeight(1);
  196. } else {
  197. // 根据左右列表高度判断下一个item应该插入哪边
  198. if (!this.boxHeight.length) {
  199. this.rightList.length < this.leftList.length ?
  200. this.rightList.push(this.allList[i]) :
  201. this.leftList.push(this.allList[i]);
  202. } else {
  203. const leftOrRight = this.boxHeight[0] > this.boxHeight[1] ? 1 : 0;
  204. if (leftOrRight) {
  205. this.rightList.push(this.allList[i])
  206. } else {
  207. this.leftList.push(this.allList[i])
  208. }
  209. }
  210. // 更新插入列表高度
  211. this.getViewHeight();
  212. }
  213. },
  214. // 获取列表高度
  215. getViewHeight() {
  216. // 使用nextTick,确保页面更新结束后,再请求高度
  217. this.$nextTick(() => {
  218. setTimeout(() => {
  219. uni.createSelectorQuery().in(this).select('#right').boundingClientRect(res => {
  220. res ? this.boxHeight[1] = res.height : '';
  221. uni.createSelectorQuery().in(this).select('#left').boundingClientRect(
  222. res => {
  223. res ? this.boxHeight[0] = res.height : '';
  224. this.mark = this.mark + 1;
  225. }).exec();
  226. }).exec();
  227. }, 100)
  228. })
  229. },
  230. // item点击
  231. itemTap(item) {
  232. this.$emit('itemTap', item)
  233. }
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. $page-padding: 12px;
  239. $grid-gap: 24rpx;
  240. .wf-page {
  241. display: grid;
  242. grid-template-columns: 1fr 1fr;
  243. grid-gap: $grid-gap;
  244. background-color: #f5f5f5;
  245. }
  246. .wf-item {
  247. width: calc((100vw - 2 * #{$page-padding} - #{$grid-gap}) / 2);
  248. padding-bottom: $grid-gap;
  249. //margin-bottom: 20rpx;
  250. }
  251. .wf-itemsss {
  252. // width: calc((100vw - 2 * #{$page-padding} - #{$grid-gap}) / 2);
  253. padding-bottom: $grid-gap;
  254. //margin-bottom: 20rpx;
  255. }
  256. .wf-page1 .wf-item {
  257. background-color: #fff;
  258. border-radius: 20rpx;
  259. padding-bottom: 0;
  260. }
  261. .wf-item-page {
  262. padding-bottom: 20rpx;
  263. }
  264. </style>