u-album.vue 12 KB

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