maramlee-waterfalls-flow.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <view class="waterfalls-box" :style="{ height: height + 'px' }">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <view
  5. v-for="(item, index) of list"
  6. class="waterfalls-list"
  7. :key="item[idKey]"
  8. :id="'waterfalls-list-id-' + item[idKey]"
  9. :ref="'waterfalls-list-id-' + item[idKey]"
  10. :style="{
  11. '--offset': offset + 'px',
  12. '--cols': cols,
  13. top: allPositionArr[index].top || 0,
  14. left: allPositionArr[index].left || 0,
  15. }"
  16. @click="$emit('wapper-lick', item)"
  17. >
  18. <view class="pictrue">
  19. <image
  20. class="waterfalls-list-image"
  21. mode="aspectFill"
  22. :class="{ single }"
  23. :style="imageStyle"
  24. :src="item[imageSrcKey] || ' '"
  25. @load="imageLoadHandle(index)"
  26. @error="imageLoadHandle(index)"
  27. @click="$emit('image-click', item)"
  28. />
  29. <view class="masks acea-row row-center-wrapper" v-if="item.stock<=0">
  30. <view class="bg">
  31. <view>暂时</view>
  32. <view>售罄</view>
  33. </view>
  34. </view>
  35. <video
  36. v-if="product_video_status && item.video_link"
  37. :src="item.video_link"
  38. :controls="false"
  39. :show-center-play-btn="false"
  40. :id="`video${item.id}`"
  41. :poster="item[imageSrcKey] || ''"
  42. class="video"
  43. objectFit="cover"
  44. loop
  45. muted
  46. ></video>
  47. <view class="activityFrame" v-if="item.activity_frame.image" :style="'background-image: url('+item.activity_frame.image+');'"></view>
  48. </view>
  49. <slot name="slot{{index}}" />
  50. </view>
  51. <!-- #endif -->
  52. <!-- #ifndef MP-WEIXIN -->
  53. <view
  54. v-for="(item, index) of list"
  55. class="waterfalls-list"
  56. :key="item[idKey]"
  57. :id="'waterfalls-list-id-' + item[idKey]"
  58. :ref="'waterfalls-list-id-' + item[idKey]"
  59. :style="{
  60. '--offset': offset + 'px',
  61. '--cols': cols,
  62. ...listStyle,
  63. ...(allPositionArr[index] || {}),
  64. }"
  65. @click="$emit('wapper-lick', item)"
  66. >
  67. <view class="pictrue">
  68. <image
  69. class="waterfalls-list-image"
  70. :class="{ single }"
  71. mode="aspectFill"
  72. :style="imageStyle"
  73. :src="item[imageSrcKey] || ' '"
  74. @load="imageLoadHandle(index)"
  75. @error="imageLoadHandle(index)"
  76. @click="$emit('image-click', item)"
  77. />
  78. <view class="masks acea-row row-center-wrapper" v-if="item.stock<=0">
  79. <view class="bg">
  80. <view>暂时</view>
  81. <view>售罄</view>
  82. </view>
  83. </view>
  84. <!-- #ifdef H5 -->
  85. <video
  86. v-if="product_video_status && item.video_link"
  87. :src="item.video_link"
  88. :controls="false"
  89. :show-center-play-btn="false"
  90. :id="`video${item.id}`"
  91. :poster="item[imageSrcKey] || ''"
  92. objectFit="cover"
  93. class="video"
  94. loop
  95. muted
  96. ></video>
  97. <!-- #endif -->
  98. <view class="activityFrame" v-if="item.activity_frame.image" :style="'background-image: url('+item.activity_frame.image+');'"></view>
  99. </view>
  100. <slot v-bind="item" />
  101. </view>
  102. <!-- #endif -->
  103. </view>
  104. </template>
  105. <script>
  106. import store from '../../store';
  107. import {
  108. diyProduct
  109. } from '@/api/store.js';
  110. import {
  111. mapMutations
  112. } from 'vuex';
  113. export default {
  114. props: {
  115. list: { type: Array, required: true },
  116. // offset 间距,单位为 px
  117. offset: { type: Number, default: 10 },
  118. // 列表渲染的 key 的键名,值必须唯一,默认为 id
  119. idKey: { type: String, default: "id" },
  120. // 图片 src 的键名
  121. imageSrcKey: { type: String, default: "image" },
  122. // 列数
  123. cols: { type: Number, default: 2, validator: (num) => num >= 2 },
  124. imageStyle: { type: Object },
  125. // 是否是单独的渲染图片的样子,只控制图片圆角而已
  126. single: { type: Boolean, default: false },
  127. // #ifndef MP-WEIXIN
  128. listStyle: { type: Object },
  129. // #endif
  130. },
  131. data() {
  132. return {
  133. topArr: [], // left, right 多个时依次表示第几列的数据
  134. allPositionArr: [], // 保存所有的位置信息
  135. allHeightArr: [], // 保存所有的 height 信息
  136. height: 0, // 外层包裹高度
  137. oldNum: 0,
  138. num: 0,
  139. updateCount: 0,
  140. product_video_status: false
  141. };
  142. },
  143. watch: {
  144. list: {
  145. handler(newValue, oldValue) {
  146. // #ifndef APP-PLUS
  147. if (!newValue.length) {
  148. return;
  149. }
  150. this.$nextTick(() => {
  151. this.updateCount++;
  152. if (!this.product_video_status) {
  153. return;
  154. }
  155. uni.getNetworkType({
  156. success: (res) => {
  157. if (['wifi', 'unknown'].includes(res.networkType)) {
  158. // 监听
  159. this.observeVideo();
  160. }
  161. if (['2g', '3g', '4g', '5g'].includes(res.networkType)) {
  162. if (!this.$store.state.app.autoplay) {
  163. if (this.updateCount != 1) {
  164. return;
  165. }
  166. return uni.showModal({
  167. content: '当前使用移动网络,是否继续播放视频?',
  168. success: (res) => {
  169. if (res.confirm) {
  170. // 监听
  171. this.SET_AUTOPLAY(true);
  172. this.observeVideo();
  173. }
  174. }
  175. });
  176. }
  177. // 监听
  178. this.observeVideo();
  179. }
  180. }
  181. });
  182. });
  183. // #endif
  184. },
  185. immediate: true
  186. }
  187. },
  188. created() {
  189. let that = this;
  190. this.refresh();
  191. let product_video_status = null;
  192. try{
  193. product_video_status = JSON.parse(uni.getStorageSync('product_video_status'));
  194. }catch(e){
  195. //TODO handle the exception
  196. }
  197. if (typeof product_video_status == 'boolean') {
  198. this.product_video_status = product_video_status;
  199. } else{
  200. this.getdiyProduct();
  201. }
  202. },
  203. methods: {
  204. ...mapMutations(['SET_AUTOPLAY']),
  205. observeVideo() {
  206. let observer = uni.createIntersectionObserver(this, { observeAll: true });
  207. observer.relativeToViewport().observe('.video', res => {
  208. if (res.intersectionRatio) {
  209. setTimeout(() => {
  210. uni.createVideoContext(res.id, this).play();
  211. }, 200)
  212. } else{
  213. setTimeout(() => {
  214. uni.createVideoContext(res.id, this).pause();
  215. }, 200)
  216. }
  217. });
  218. this.$once('hook:beforeDestroy', () => {
  219. observer.disconnect();
  220. });
  221. },
  222. // div商品详情
  223. getdiyProduct() {
  224. diyProduct().then(res => {
  225. uni.setStorageSync('product_video_status',JSON.stringify(res.data.product_video_status))
  226. this.product_video_status = res.data.product_video_status;
  227. })
  228. },
  229. imageLoadHandle(index) {
  230. if(!this.list.length){
  231. return
  232. }
  233. const id = "waterfalls-list-id-" + this.list[index][this.idKey],
  234. query = uni.createSelectorQuery().in(this);
  235. query
  236. .select("#" + id)
  237. .fields({ size: true }, (data) => {
  238. this.num++;
  239. this.$set(this.allHeightArr, index, data.height);
  240. if (this.num === this.list.length) {
  241. for (let i = this.oldNum; i < this.num; i++) {
  242. const getTopArrMsg = () => {
  243. let arrtmp = [...this.topArr].sort((a, b) => a - b);
  244. return {
  245. shorterIndex: this.topArr.indexOf(arrtmp[0]),
  246. shorterValue: arrtmp[0],
  247. longerIndex: this.topArr.indexOf(arrtmp[this.cols - 1]),
  248. longerValue: arrtmp[this.cols - 1],
  249. };
  250. };
  251. const { shorterIndex, shorterValue } = getTopArrMsg();
  252. const position = {
  253. top: shorterValue + "px",
  254. left: (data.width + this.offset) * shorterIndex + "px",
  255. };
  256. this.$set(this.allPositionArr, i, position);
  257. this.topArr[shorterIndex] =
  258. shorterValue + this.allHeightArr[i] + this.offset;
  259. this.height = getTopArrMsg().longerValue - this.offset;
  260. }
  261. this.oldNum = this.num;
  262. // 完成渲染 emit `image-load` 事件
  263. this.$emit("image-load");
  264. }
  265. })
  266. .exec();
  267. },
  268. refresh() {
  269. let arr = [];
  270. for (let i = 0; i < this.cols; i++) {
  271. arr.push(0);
  272. }
  273. this.topArr = arr;
  274. this.num = 0;
  275. this.oldNum = 0;
  276. this.height = 0;
  277. },
  278. },
  279. };
  280. </script>
  281. <style lang="scss" scoped>
  282. // 这里可以自行配置
  283. $border-radius: 10px;
  284. .waterfalls-box {
  285. position: relative;
  286. width: 100%;
  287. overflow: hidden;
  288. .waterfalls-list {
  289. width: 346rpx;
  290. position: absolute;
  291. background-color: #fff;
  292. border-radius: $border-radius;
  293. // 防止刚开始渲染时堆叠在第一幅图的地方
  294. left: calc(-50% - var(--offset));
  295. .pictrue{
  296. position: relative;
  297. .masks{
  298. position: absolute;
  299. top: 0;
  300. left: 0;
  301. right: 0;
  302. bottom: 0;
  303. background: rgba(0, 0, 0, 0.2);
  304. border-radius: 20rpx 20rpx 0 0;
  305. .bg{
  306. width: 152rpx;
  307. height: 152rpx;
  308. background: #000000;
  309. opacity: 0.6;
  310. color: #fff;
  311. font-size: 32rpx;
  312. border-radius: 50%;
  313. padding: 34rpx 0;
  314. text-align: center;
  315. }
  316. }
  317. .activityFrame{
  318. border-radius: $border-radius $border-radius 0 0;
  319. }
  320. }
  321. .waterfalls-list-image {
  322. width: 100%;
  323. height: 346rpx !important;
  324. will-change: transform;
  325. border-radius: $border-radius $border-radius 0 0;
  326. display: block;
  327. &.single {
  328. border-radius: $border-radius;
  329. }
  330. }
  331. }
  332. .video {
  333. position: absolute;
  334. top: 0;
  335. left: 0;
  336. width: 100%;
  337. height: 346rpx !important;
  338. border-radius: $border-radius $border-radius 0 0;
  339. }
  340. }
  341. </style>