<template>
	<view class="content">
		<view class="" style="display: flex;padding: 40rpx 20rpx 0 20rpx;">
			<u-section title="精彩瞬间" :right="false" color="#303133"></u-section>
			<text style="font-size: 24rpx;color: #ccc;margin-left: 30rpx;">MOMENT</text>
		</view>
		<image src="../../../static/img/vlog/icon/fengexian.png" mode="" style="width: 100%;height: 15rpx;"></image>
		<view class="row">
			<view class="item" v-for="(item,index) in list" :key="index">
				<image :src="item.thumbImageUrl" mode="widthFix"></image>
				<view class="bottom">
					<text class="text1">¥0</text>
					<text class="text2">¥0</text>
					<u-button class="button" size="mini" @click="handleDownload(item.thumbImageUrl)">
						免费下载
					</u-button>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				list: [], //相片列表
				checkImg: '', //选中的图片
			}
		},
		onLoad() { //代替 vue 里面的 created
			this.getPhotoData()
		},
		onReady() { //代替 vue 里面的 mounted
		},
		methods: {
			handleDownload(url) { // 下载功能
				uni.showLoading({
					title: '下载中',
					mask: true
				})
				let fileName = new Date().valueOf() //获取时间戳
				uni.downloadFile({ //下载文件资源到本地
					url,
					filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.jpg', //filePath指定文件下载后存储的路径，wx.env.USER_DATA_PATH,时间戳为文件名
					success: res => { //下载到本地成功
						let filePath = res.filePath
						uni.saveImageToPhotosAlbum({ //保存视频到系统相册。
							filePath,
							success: file => { //保存成功
								//删除本地缓存
								let fileMgr = uni.getFileSystemManager()
								fileMgr.unlink({
									filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.jpg',
								})
								uni.showToast({
									title: '下载成功',
									icon: 'success',
									mask: true
								})
							},
							fail: err => {
								uni.hideLoading()
								//拒绝授权时显示
								if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
									uni.showModal({
										title: '提示',
										content: '需要您授权保存相册',
										showCancel: false,
										success: data => {
											//打开权限设置
											uni.openSetting({
												success: setting => {
													if (setting.authSetting['scope.writePhotosAlbum']) {
														uni.showModal({
															title: '提示',
															content: '获取权限成功,再次点击下载即可保存',
															showCancel: false,
														})
													} else {
														uni.showModal({
															title: '提示',
															ontent: '获取权限失败，将无法保存到相册哦',
															showCancel: false
														})
													}
												},
											})
										}
									})
								}
							}
						})
					},
					fail: err => { //下载失败
						uni.hideLoading()
						if (err.errMsg == 'downloadFile:fail createDownloadTask:fail url not in domain list') {
							uni.showToast({
								title: '服务器错误，请联系相关管理员',
								icon: 'none',
								mask: true
							})
						}
					}
				})
			},
			getPhotoData() { //获取用户vlog图片
				this.$request('wechatUser/myPage/getVlogRecord', {
					openid: uni.getStorageSync('openid'),
					productType: 1
				}).then(res => {
					if (res.code === '00') {
						this.list = res.data
					} else {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
						})
					}
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		box-sizing: border-box;
		margin: 0 auto;
		background-color: #fff;
		height: 100%;

		.row {
			display: flex;
			width: 100%;
			justify-content: space-between;
			flex-wrap: wrap;

			.item {
				width: 50%;
				position: relative;
				padding: 15rpx;

				image {
					width: 100%;
					border-radius: 10rpx;
				}

				.bottom {
					font-weight: bold;
					align-items: center;

					.text1 {
						font-size: 24rpx;
						text-decoration: line-through;
						color: #ccc;
					}

					.text2 {
						font-size: 32rpx;
						color: #EE520E;
						margin-left: 10rpx;
					}

					.button {
						float: right;
					}
				}
			}
		}
	}
</style>
