u-album.vue 11 KB

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