import http from '@/utils/request'
import {
	UrlPath
} from "@/utils/commonuni"
export const download = (id) => {
	console.log('download-:' + id)
	doDownload(id, `${process.env.VUE_APP_OSS_URL}/download`).then(response => {
		// 提取文件名
		let filename = response.headers['content-disposition'].match(
			/filename=(.*)/
		)[1]
		filename = decodeURI(filename)
		filename = filename.replaceAll('"', '')
		// 将二进制流转为blob
		const blob = new Blob([response.data])
		if (typeof window.navigator.msSaveBlob !== 'undefined') {
			// 兼容IE，window.navigator.msSaveBlob：以本地方式保存文件
			window.navigator.msSaveBlob(blob, filename)
		} else {
			// 创建新的URL并指向File对象或者Blob对象的地址
			const blobURL = window.URL.createObjectURL(blob)
			// 创建a标签，用于跳转至下载链接
			const tempLink = document.createElement('a')
			tempLink.style.display = 'none'
			tempLink.href = blobURL
			tempLink.setAttribute('download', filename)
			// 兼容：某些浏览器不支持HTML5的download属性
			if (typeof tempLink.download === 'undefined') {
				tempLink.setAttribute('target', '_blank')
			}
			// 挂载a标签
			document.body.appendChild(tempLink)
			tempLink.click()
			document.body.removeChild(tempLink)
			// 释放blob URL地址
			window.URL.revokeObjectURL(blobURL)
		}
	})
}


const doDownload = (id, url) => {
	return http.get(url, {
		id: id,
		isFile: true
	})

}
export const uploadImageModelValue = (id) => {
	var ids = null
	id = JSON.parse(JSON.stringify(id))
	if (typeof(id) == 'string') {
		ids = id.split(",")
	} else if (id instanceof Array) {
		ids = JSON.parse(JSON.stringify(id))
	} else {
		return []
	}
	var urlList = []
	for (var i = 0; i < ids.length; i++) {
		var urlObj = {}
		urlObj.url = UrlPath.getBaseUrl() + "oss/preview?id=" + ids[i]+"&code=" + uni.getStorageSync("token")
		urlList.push(urlObj)
	}
	return urlList;
}