uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.5 KiB

3 years ago
  1. <template>
  2. <view class="u-album">
  3. <view
  4. class="u-album__row"
  5. ref="u-album__row"
  6. v-for="(arr, index) in showUrls"
  7. :forComputedUse="albumWidth"
  8. :key="index"
  9. >
  10. <view
  11. class="u-album__row__wrapper"
  12. v-for="(item, index1) in arr"
  13. :key="index1"
  14. :style="[imageStyle(index + 1, index1 + 1)]"
  15. @tap="onPreviewTap(getSrc(item))"
  16. >
  17. <image
  18. :src="getSrc(item)"
  19. :mode="urls.length === 1 ? (imageHeight > 0 ? singleMode : 'widthFix') : multipleMode"
  20. :style="[{
  21. width: $u.addUnit(imageWidth),
  22. height: $u.addUnit(imageHeight)
  23. }]"
  24. ></image>
  25. <view
  26. v-if="showMore && urls.length > rowCount * showUrls.length && index === showUrls.length - 1 && index1 === showUrls[showUrls.length - 1].length - 1"
  27. class="u-album__row__wrapper__text"
  28. >
  29. <u--text
  30. :text="`+${urls.length - maxCount}`"
  31. color="#fff"
  32. :size="multipleSize * 0.3"
  33. align="center"
  34. customStyle="justify-content: center"
  35. ></u--text>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import props from './props.js';
  43. // #ifdef APP-NVUE
  44. // 由于weex为阿里的KPI业绩考核的产物,所以不支持百分比单位,这里需要通过dom查询组件的宽度
  45. const dom = uni.requireNativePlugin('dom')
  46. // #endif
  47. /**
  48. * Album 相册
  49. * @description 本组件提供一个类似相册的功能让开发者开发起来更加得心应手减少重复的模板代码
  50. * @tutorial https://www.uviewui.com/components/album.html
  51. *
  52. * @property {Array} urls 图片地址列表 Array<String>|Array<Object>形式
  53. * @property {String} keyName 指定从数组的对象元素中读取哪个属性作为图片地址
  54. * @property {String | Number} singleSize 单图时图片长边的长度 默认 180
  55. * @property {String | Number} multipleSize 多图时图片边长 默认 70
  56. * @property {String | Number} space 多图时图片水平和垂直之间的间隔 默认 6
  57. * @property {String} singleMode 单图时图片缩放裁剪的模式 默认 'scaleToFill'
  58. * @property {String} multipleMode 多图时图片缩放裁剪的模式 默认 'aspectFill'
  59. * @property {String | Number} maxCount 取消按钮的提示文字 默认 9
  60. * @property {Boolean} previewFullImage 是否可以预览图片 默认 true
  61. * @property {String | Number} rowCount 每行展示图片数量如设置singleSize和multipleSize将会无效 默认 3
  62. * @property {Boolean} showMore 超出maxCount时是否显示查看更多的提示 默认 true
  63. *
  64. * @event {Function} albumWidth 某些特殊的情况下需要让文字与相册的宽度相等这里事件的形式对外发送 回调参数 width
  65. * @example <u-album :urls="urls2" @albumWidth="width => albumWidth = width" multipleSize="68" ></u-album>
  66. */
  67. export default {
  68. name: 'u-album',
  69. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  70. data() {
  71. return {
  72. // 单图的宽度
  73. singleWidth: 0,
  74. // 单图的高度
  75. singleHeight: 0,
  76. // 单图时,如果无法获取图片的尺寸信息,让图片宽度默认为容器的一定百分比
  77. singlePercent: 0.6,
  78. }
  79. },
  80. watch: {
  81. urls: {
  82. immediate: true,
  83. handler(newVal) {
  84. if (newVal.length === 1) {
  85. this.getImageRect()
  86. }
  87. }
  88. }
  89. },
  90. computed: {
  91. imageStyle() {
  92. return (index1, index2) => {
  93. const {
  94. space,
  95. rowCount,
  96. multipleSize,
  97. urls
  98. } = this, {
  99. addUnit,
  100. addStyle
  101. } = uni.$u,
  102. rowLen = this.showUrls.length,
  103. allLen = this.urls.length
  104. const style = {
  105. marginRight: addUnit(space),
  106. marginBottom: addUnit(space)
  107. }
  108. // 如果为最后一行,则每个图片都无需下边框
  109. if (index1 === rowLen) style.marginBottom = 0
  110. // 每行的最右边一张和总长度的最后一张无需右边框
  111. if (index2 === rowCount || index1 === rowLen && index2 === this.showUrls[index1 - 1].length) style.marginRight =
  112. 0
  113. return style
  114. }
  115. },
  116. // 将数组划分为二维数组
  117. showUrls() {
  118. const arr = []
  119. this.urls.map((item, index) => {
  120. // 限制最大展示数量
  121. if(index + 1 <= this.maxCount) {
  122. // 计算该元素为第几个素组内
  123. const itemIndex = Math.floor(index / this.rowCount)
  124. // 判断对应的索引是否存在
  125. if (!arr[itemIndex]) {
  126. arr[itemIndex] = []
  127. }
  128. arr[itemIndex].push(item)
  129. }
  130. })
  131. return arr
  132. },
  133. imageWidth() {
  134. return this.urls.length === 1 ? this.singleWidth : this.multipleSize
  135. },
  136. imageHeight() {
  137. return this.urls.length === 1 ? this.singleHeight : this.multipleSize
  138. },
  139. // 此变量无实际用途,仅仅是为了利用computed特性,让其在urls长度等变化时,重新计算图片的宽度
  140. // 因为用户在某些特殊的情况下,需要让文字与相册的宽度相等,所以这里事件的形式对外发送
  141. albumWidth() {
  142. let width = 0
  143. if(this.urls.length === 1) {
  144. width = this.singleWidth
  145. } else {
  146. width = this.showUrls[0].length * this.multipleSize + this.space * (this.showUrls[0].length - 1)
  147. }
  148. this.$emit('albumWidth', width)
  149. return width
  150. }
  151. },
  152. methods: {
  153. // 预览图片
  154. onPreviewTap(url) {
  155. const urls = this.urls.map(item => {
  156. return this.getSrc(item)
  157. })
  158. uni.previewImage({
  159. current: url,
  160. urls
  161. });
  162. },
  163. // 获取图片的路径
  164. getSrc(item) {
  165. return uni.$u.test.object(item) ? this.keyName && item[this.keyName] || item.src : item
  166. },
  167. // 单图时,获取图片的尺寸
  168. // 在小程序中,需要将网络图片的的域名添加到小程序的download域名才可能获取尺寸
  169. // 在没有添加的情况下,让单图宽度默认为盒子的一定宽度(singlePercent)
  170. getImageRect() {
  171. const src = this.getSrc(this.urls[0])
  172. uni.getImageInfo({
  173. src,
  174. success: (res) => {
  175. // 判断图片横向还是竖向展示方式
  176. const isHorizotal = res.width >= res.height
  177. this.singleWidth = isHorizotal ? this.singleSize : res.width / res.height * this.singleSize
  178. this.singleHeight = !isHorizotal ? this.singleSize : res.height / res.width * this.singleWidth
  179. },
  180. fail: () => {
  181. this.getComponentWidth()
  182. }
  183. })
  184. },
  185. // 获取组件的宽度
  186. async getComponentWidth() {
  187. // 延时一定时间,以获取dom尺寸
  188. await uni.$u.sleep(30)
  189. // #ifndef APP-NVUE
  190. this.$uGetRect('.u-album__row').then(size => {
  191. this.singleWidth = size.width * this.singlePercent
  192. })
  193. // #endif
  194. // #ifdef APP-NVUE
  195. // 这里ref="u-album__row"所在的标签为通过for循环出来,导致this.$refs['u-album__row']是一个数组
  196. const ref = this.$refs['u-album__row'][0]
  197. ref && dom.getComponentRect(ref, (res) => {
  198. this.singleWidth = res.size.width * this.singlePercent
  199. })
  200. // #endif
  201. },
  202. }
  203. }
  204. </script>
  205. <style lang="scss">
  206. @import "../../libs/css/components.scss";
  207. .u-album {
  208. @include flex(column);
  209. &__row {
  210. @include flex(row);
  211. flex-wrap: wrap;
  212. &__wrapper {
  213. position: relative;
  214. &__text {
  215. position: absolute;
  216. top: 0;
  217. left: 0;
  218. right: 0;
  219. bottom: 0;
  220. background-color: rgba(0, 0, 0, 0.3);
  221. @include flex(row);
  222. justify-content: center;
  223. align-items: center;
  224. }
  225. }
  226. }
  227. }
  228. </style>