学员端小程序
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.

259 lines
9.7 KiB

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