| ¶Ô±ÈÐÂÎļþ | 
|  |  |  | 
|---|
|  |  |  | <template> | 
|---|
|  |  |  | <view class="u-album"> | 
|---|
|  |  |  | <view | 
|---|
|  |  |  | class="u-album__row" | 
|---|
|  |  |  | ref="u-album__row" | 
|---|
|  |  |  | v-for="(arr, index) in showUrls" | 
|---|
|  |  |  | :forComputedUse="albumWidth" | 
|---|
|  |  |  | :key="index" | 
|---|
|  |  |  | > | 
|---|
|  |  |  | <view | 
|---|
|  |  |  | class="u-album__row__wrapper" | 
|---|
|  |  |  | v-for="(item, index1) in arr" | 
|---|
|  |  |  | :key="index1" | 
|---|
|  |  |  | :style="[imageStyle(index + 1, index1 + 1)]" | 
|---|
|  |  |  | @tap="previewFullImage ? onPreviewTap(getSrc(item)) : ''" | 
|---|
|  |  |  | > | 
|---|
|  |  |  | <image | 
|---|
|  |  |  | :src="getSrc(item)" | 
|---|
|  |  |  | :mode=" | 
|---|
|  |  |  | urls.length === 1 | 
|---|
|  |  |  | ? imageHeight > 0 | 
|---|
|  |  |  | ? singleMode | 
|---|
|  |  |  | : 'widthFix' | 
|---|
|  |  |  | : multipleMode | 
|---|
|  |  |  | " | 
|---|
|  |  |  | :style="[ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | width: imageWidth, | 
|---|
|  |  |  | height: imageHeight | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ]" | 
|---|
|  |  |  | ></image> | 
|---|
|  |  |  | <view | 
|---|
|  |  |  | v-if=" | 
|---|
|  |  |  | showMore && | 
|---|
|  |  |  | urls.length > rowCount * showUrls.length && | 
|---|
|  |  |  | index === showUrls.length - 1 && | 
|---|
|  |  |  | index1 === showUrls[showUrls.length - 1].length - 1 | 
|---|
|  |  |  | " | 
|---|
|  |  |  | class="u-album__row__wrapper__text" | 
|---|
|  |  |  | > | 
|---|
|  |  |  | <u--text | 
|---|
|  |  |  | :text="`+${urls.length - maxCount}`" | 
|---|
|  |  |  | color="#fff" | 
|---|
|  |  |  | :size="multipleSize * 0.3" | 
|---|
|  |  |  | align="center" | 
|---|
|  |  |  | customStyle="justify-content: center" | 
|---|
|  |  |  | ></u--text> | 
|---|
|  |  |  | </view> | 
|---|
|  |  |  | </view> | 
|---|
|  |  |  | </view> | 
|---|
|  |  |  | </view> | 
|---|
|  |  |  | </template> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | <script> | 
|---|
|  |  |  | import props from './props.js' | 
|---|
|  |  |  | // #ifdef APP-NVUE | 
|---|
|  |  |  | // ç±äºweex为é¿éçKPIä¸ç»©èæ ¸ç产ç©ï¼æä»¥ä¸æ¯æç¾åæ¯åä½ï¼è¿ééè¦éè¿domæ¥è¯¢ç»ä»¶ç宽度 | 
|---|
|  |  |  | const dom = uni.requireNativePlugin('dom') | 
|---|
|  |  |  | // #endif | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Album ç¸å | 
|---|
|  |  |  | * @description æ¬ç»ä»¶æä¾ä¸ä¸ªç±»ä¼¼ç¸åçåè½ï¼è®©å¼åè
å¼åèµ·æ¥æ´å å¾å¿åºæãåå°éå¤ç模æ¿ä»£ç  | 
|---|
|  |  |  | * @tutorial https://www.uviewui.com/components/album.html | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @property {Array}           urls             å¾çå°åå表 Array<String>|Array<Object>å½¢å¼ | 
|---|
|  |  |  | * @property {String}          keyName          æå®ä»æ°ç»ç对象å
ç´ ä¸è¯»ååªä¸ªå±æ§ä½ä¸ºå¾çå°å | 
|---|
|  |  |  | * @property {String | Number} singleSize       å徿¶ï¼å¾çé¿è¾¹çé¿åº¦  ï¼é»è®¤ 180 ï¼ | 
|---|
|  |  |  | * @property {String | Number} multipleSize     å¤å¾æ¶ï¼å¾ç边鿠ï¼é»è®¤ 70 ï¼ | 
|---|
|  |  |  | * @property {String | Number} space            å¤å¾æ¶ï¼å¾çæ°´å¹³ååç´ä¹é´çé´é ï¼é»è®¤ 6 ï¼ | 
|---|
|  |  |  | * @property {String}          singleMode       å徿¶ï¼å¾ç缩æ¾è£åªç模张ï¼é»è®¤ 'scaleToFill' ï¼ | 
|---|
|  |  |  | * @property {String}          multipleMode     å¤å¾æ¶ï¼å¾ç缩æ¾è£åªç模张ï¼é»è®¤ 'aspectFill' ï¼ | 
|---|
|  |  |  | * @property {String | Number} maxCount         åæ¶æé®çæç¤ºæå ï¼é»è®¤ 9 ï¼ | 
|---|
|  |  |  | * @property {Boolean}         previewFullImage æ¯å¦å¯ä»¥é¢è§å¾ç ï¼é»è®¤ true ï¼ | 
|---|
|  |  |  | * @property {String | Number} rowCount         æ¯è¡å±ç¤ºå¾çæ°éï¼å¦è®¾ç½®ï¼singleSizeåmultipleSizeå°ä¼æ æ   ï¼é»è®¤ 3 ï¼ | 
|---|
|  |  |  | * @property {Boolean}         showMore         è¶
åºmaxCountæ¶æ¯å¦æ¾ç¤ºæ¥çæ´å¤çæç¤º ï¼é»è®¤ true ï¼ | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @event    {Function}        albumWidth       æäºç¹æ®çæ
åµä¸ï¼éè¦è®©æåä¸ç¸åç宽度ç¸çï¼è¿éäºä»¶çå½¢å¼å¯¹å¤åé  ï¼åè°åæ° width ï¼ | 
|---|
|  |  |  | * @example <u-album :urls="urls2" @albumWidth="width => albumWidth = width" multipleSize="68" ></u-album> | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | export default { | 
|---|
|  |  |  | name: 'u-album', | 
|---|
|  |  |  | mixins: [uni.$u.mpMixin, uni.$u.mixin, props], | 
|---|
|  |  |  | data() { | 
|---|
|  |  |  | return { | 
|---|
|  |  |  | // åå¾ç宽度 | 
|---|
|  |  |  | singleWidth: 0, | 
|---|
|  |  |  | // åå¾çé«åº¦ | 
|---|
|  |  |  | singleHeight: 0, | 
|---|
|  |  |  | // å徿¶ï¼å¦ææ æ³è·åå¾çç尺寸信æ¯ï¼è®©å¾ç宽度é»è®¤ä¸ºå®¹å¨çä¸å®ç¾åæ¯ | 
|---|
|  |  |  | singlePercent: 0.6 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | watch: { | 
|---|
|  |  |  | urls: { | 
|---|
|  |  |  | immediate: true, | 
|---|
|  |  |  | handler(newVal) { | 
|---|
|  |  |  | if (newVal.length === 1) { | 
|---|
|  |  |  | this.getImageRect() | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | computed: { | 
|---|
|  |  |  | imageStyle() { | 
|---|
|  |  |  | return (index1, index2) => { | 
|---|
|  |  |  | const { space, rowCount, multipleSize, urls } = this, | 
|---|
|  |  |  | { addUnit, addStyle } = uni.$u, | 
|---|
|  |  |  | rowLen = this.showUrls.length, | 
|---|
|  |  |  | allLen = this.urls.length | 
|---|
|  |  |  | const style = { | 
|---|
|  |  |  | marginRight: addUnit(space), | 
|---|
|  |  |  | marginBottom: addUnit(space) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // å¦æä¸ºæåä¸è¡ï¼åæ¯ä¸ªå¾ç齿 éä¸è¾¹æ¡ | 
|---|
|  |  |  | if (index1 === rowLen) style.marginBottom = 0 | 
|---|
|  |  |  | // æ¯è¡çæå³è¾¹ä¸å¼ åæ»é¿åº¦çæåä¸å¼ æ éå³è¾¹æ¡ | 
|---|
|  |  |  | if ( | 
|---|
|  |  |  | index2 === rowCount || | 
|---|
|  |  |  | (index1 === rowLen && | 
|---|
|  |  |  | index2 === this.showUrls[index1 - 1].length) | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | style.marginRight = 0 | 
|---|
|  |  |  | return style | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | // å°æ°ç»åå为äºç»´æ°ç» | 
|---|
|  |  |  | showUrls() { | 
|---|
|  |  |  | const arr = [] | 
|---|
|  |  |  | this.urls.map((item, index) => { | 
|---|
|  |  |  | // é嶿大å±ç¤ºæ°é | 
|---|
|  |  |  | if (index + 1 <= this.maxCount) { | 
|---|
|  |  |  | // è®¡ç®è¯¥å
素为第å ä¸ªç´ ç»å | 
|---|
|  |  |  | const itemIndex = Math.floor(index / this.rowCount) | 
|---|
|  |  |  | // å¤æå¯¹åºçç´¢å¼æ¯å¦åå¨ | 
|---|
|  |  |  | if (!arr[itemIndex]) { | 
|---|
|  |  |  | arr[itemIndex] = [] | 
|---|
|  |  |  | } | 
|---|
|  |  |  | arr[itemIndex].push(item) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | return arr | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | imageWidth() { | 
|---|
|  |  |  | return uni.$u.addUnit( | 
|---|
|  |  |  | this.urls.length === 1 ? this.singleWidth : this.multipleSize | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | imageHeight() { | 
|---|
|  |  |  | return uni.$u.addUnit( | 
|---|
|  |  |  | this.urls.length === 1 ? this.singleHeight : this.multipleSize | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | // æ¤åéæ å®é
ç¨éï¼ä»
ä»
æ¯ä¸ºäºå©ç¨computedç¹æ§ï¼è®©å
¶å¨urlsé¿åº¦çååæ¶ï¼éæ°è®¡ç®å¾çç宽度 | 
|---|
|  |  |  | // å ä¸ºç¨æ·å¨æäºç¹æ®çæ
åµä¸ï¼éè¦è®©æåä¸ç¸åç宽度ç¸çï¼æä»¥è¿éäºä»¶çå½¢å¼å¯¹å¤åé | 
|---|
|  |  |  | albumWidth() { | 
|---|
|  |  |  | let width = 0 | 
|---|
|  |  |  | if (this.urls.length === 1) { | 
|---|
|  |  |  | width = this.singleWidth | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | width = | 
|---|
|  |  |  | this.showUrls[0].length * this.multipleSize + | 
|---|
|  |  |  | this.space * (this.showUrls[0].length - 1) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.$emit('albumWidth', width) | 
|---|
|  |  |  | return width | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | methods: { | 
|---|
|  |  |  | // é¢è§å¾ç | 
|---|
|  |  |  | onPreviewTap(url) { | 
|---|
|  |  |  | const urls = this.urls.map((item) => { | 
|---|
|  |  |  | return this.getSrc(item) | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | uni.previewImage({ | 
|---|
|  |  |  | current: url, | 
|---|
|  |  |  | urls | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | // è·åå¾ççè·¯å¾ | 
|---|
|  |  |  | getSrc(item) { | 
|---|
|  |  |  | return uni.$u.test.object(item) | 
|---|
|  |  |  | ? (this.keyName && item[this.keyName]) || item.src | 
|---|
|  |  |  | : item | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | // å徿¶ï¼è·åå¾çç尺寸 | 
|---|
|  |  |  | // å¨å°ç¨åºä¸ï¼éè¦å°ç½ç»å¾çççååæ·»å å°å°ç¨åºçdownloadååæå¯è½è·å尺寸 | 
|---|
|  |  |  | // å¨æ²¡ææ·»å çæ
åµä¸ï¼è®©åå¾å®½åº¦é»è®¤ä¸ºçåçä¸å®å®½åº¦(singlePercent) | 
|---|
|  |  |  | getImageRect() { | 
|---|
|  |  |  | const src = this.getSrc(this.urls[0]) | 
|---|
|  |  |  | uni.getImageInfo({ | 
|---|
|  |  |  | src, | 
|---|
|  |  |  | success: (res) => { | 
|---|
|  |  |  | // å¤æå¾ç横åè¿æ¯ç«åå±ç¤ºæ¹å¼ | 
|---|
|  |  |  | const isHorizotal = res.width >= res.height | 
|---|
|  |  |  | this.singleWidth = isHorizotal | 
|---|
|  |  |  | ? this.singleSize | 
|---|
|  |  |  | : (res.width / res.height) * this.singleSize | 
|---|
|  |  |  | this.singleHeight = !isHorizotal | 
|---|
|  |  |  | ? this.singleSize | 
|---|
|  |  |  | : (res.height / res.width) * this.singleWidth | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | fail: () => { | 
|---|
|  |  |  | this.getComponentWidth() | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | // è·åç»ä»¶ç宽度 | 
|---|
|  |  |  | async getComponentWidth() { | 
|---|
|  |  |  | // å»¶æ¶ä¸å®æ¶é´ï¼ä»¥è·ådom尺寸 | 
|---|
|  |  |  | await uni.$u.sleep(30) | 
|---|
|  |  |  | // #ifndef APP-NVUE | 
|---|
|  |  |  | this.$uGetRect('.u-album__row').then((size) => { | 
|---|
|  |  |  | this.singleWidth = size.width * this.singlePercent | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | // #endif | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // #ifdef APP-NVUE | 
|---|
|  |  |  | // è¿éref="u-album__row"æå¨çæ ç¾ä¸ºéè¿for循ç¯åºæ¥ï¼å¯¼è´this.$refs['u-album__row']æ¯ä¸ä¸ªæ°ç» | 
|---|
|  |  |  | const ref = this.$refs['u-album__row'][0] | 
|---|
|  |  |  | ref && | 
|---|
|  |  |  | dom.getComponentRect(ref, (res) => { | 
|---|
|  |  |  | this.singleWidth = res.size.width * this.singlePercent | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | // #endif | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | </script> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | <style lang="scss" scoped> | 
|---|
|  |  |  | @import '../../libs/css/components.scss'; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | .u-album { | 
|---|
|  |  |  | @include flex(column); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | &__row { | 
|---|
|  |  |  | @include flex(row); | 
|---|
|  |  |  | flex-wrap: wrap; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | &__wrapper { | 
|---|
|  |  |  | position: relative; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | &__text { | 
|---|
|  |  |  | position: absolute; | 
|---|
|  |  |  | top: 0; | 
|---|
|  |  |  | left: 0; | 
|---|
|  |  |  | right: 0; | 
|---|
|  |  |  | bottom: 0; | 
|---|
|  |  |  | background-color: rgba(0, 0, 0, 0.3); | 
|---|
|  |  |  | @include flex(row); | 
|---|
|  |  |  | justify-content: center; | 
|---|
|  |  |  | align-items: center; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | </style> | 
|---|