综合办公系统
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.

uv-album.vue 9.4KB

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