<template>
	<view class="page" @longpress="handleSavePic">
    <image style="width: 100%;" mode="widthFix" src="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/97359202403161619147876.webp"></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},

		methods: {
			handleSavePic() {
				// 获取要保存的图片路径或URL
				let imageUrl =
				"https://lihai001.oss-cn-chengdu.aliyuncs.com/def/97359202403161619147876.webp"; // 这里使用了网络上的图片作为示例

				// #ifdef H5
				var a = document.createElement("a");
				a.download = imageUrl;
				a.href = imageUrl;
				document.body.appendChild(a);
				a.click();
				a.remove();
				// #endif

				// #ifndef H5
				let that = this;
				uni.downloadFile({
					url: imageUrl,
					success: async (res)=> {
						if (res.statusCode === 200) {
							let tempFilePath = res.tempFilePath; // 临时文件路径
              // #ifdef APP-PLUS
              let result = await this.$store.dispatch("permission/requestPermissions",
                'WRITE_EXTERNAL_STORAGE')
              if (result !== 1) return;
              // #endif
							uni.saveImageToPhotosAlbum({
								filePath: tempFilePath,
								success() {
									return that.$util.Tips({
										title: '图片已保存至相册!'
									});
								},
								fail(err) {
									console.error('保存失败', err);
								}
							});
						} else {
							console.error('下载失败', res.statusCode);
						}
					},
					fail(err) {
						console.error('下载失败', err);
					}
				});
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	.page {
	}
</style>