import request from '@/utils/request';

const BASE_URL = 'http://127.0.0.1:8080';
const CLIENT_ID = 'e48ac397bff4f031b14d6e671eee49c3';

/**
 * 上传文件至OSS
 * @param {string} filePath 本地临时文件路径
 * @returns {Promise} 解析结果含 { ossId, url, fileName }
 */
export function uploadFile(filePath) {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token') || '';
		uni.uploadFile({
			url: BASE_URL + '/resource/oss/upload',
			filePath,
			name: 'file',
			header: {
				clientid: CLIENT_ID,
				Authorization: 'Bearer ' + token
			},
			success(res) {
				try {
					const result = JSON.parse(res.data);
					if (result.code === 200) {
						resolve(result.data);
					} else {
						reject(new Error(result.msg || '上传失败'));
					}
				} catch (e) {
					reject(new Error('响应解析失败'));
				}
			},
			fail(err) {
				reject(new Error(err.errMsg || '上传失败'));
			}
		});
	});
}
