import config from '@/utils/config';
import {
	getAccessToken,
} from "@/utils/auth.js"
import request from '@/utils/request';

// 上传文件
export function uploadFile(file, directory) {
	uni.showLoading({
		title: '上传中',
	});
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.default.apiUrl + config.default.apiUrlSuffix + '/infra/file/upload',
			filePath: file,
			name: 'file',
			header: {
				Accept: '*/*',
				'tenant-id': tenantId,
				Authorization: 'Bearer ' + getAccessToken(),
			},
			formData: {
				directory,
			},
			success: (uploadFileRes) => {
				let result = JSON.parse(uploadFileRes.data);
				if (result.error === 1) {
					uni.showToast({
						icon: 'none',
						title: result.msg,
					});
				} else {
					return resolve(result);
				}
			},
			fail: (error) => {
				console.log('上传失败：', error);
				return resolve(false);
			},
			complete: () => {
				uni.hideLoading();
			},
		});
	});
}

// 获取文件预签名地址
export function getFilePresignedUrl(name, directory) {
	return request({
		url: '/infra/file/presigned-url',
		method: 'GET',
		params: {
			name,
			directory,
		},
	});
}

// 创建文件
export function createFile(data) {
	return request({
		url: '/infra/file/create', // 请求的 URL
		method: 'POST', // 请求方法
		data: data, // 要发送的数据
	});
}