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.

263 lines
9.9 KiB

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