JavaScript 示例
Node.js 完整示例代码。
安装依赖
bash
npm install node-fetch form-data完整代码
javascript
const fs = require('fs');
const crypto = require('crypto');
const FormData = require('form-data');
// 配置
const API_KEY = 'YOUR_API_KEY';
const BASE_URL = 'https://openapi.kujiale.com';
const APP_UID = 'user123';
async function getUploadToken() {
const url = `${BASE_URL}/v2/aholo/upload/token?appuid=${APP_UID}`;
const resp = await fetch(url, {
method: 'POST',
headers: { 'Authorization': `Bearer ${API_KEY}` }
});
const data = await resp.json();
if (data.c !== '0') throw new Error('获取 Token 失败');
return data.d;
}
async function uploadFile(globalDomain, ousToken, filePath) {
// 计算 MD5
const fileBuffer = fs.readFileSync(filePath);
const md5 = crypto.createHash('md5').update(fileBuffer).digest('hex');
// 上传
const form = new FormData();
form.append('file', fs.createReadStream(filePath));
form.append('md5', md5);
const url = `https://${globalDomain}/ous/api/v2/single/upload`;
const resp = await fetch(url, {
method: 'POST',
headers: { 'ous-token-v2': ousToken },
body: form
});
const result = await resp.json();
if (result.c !== '0') throw new Error('上传失败');
// 轮询状态
const statusUrl = `https://${globalDomain}/ous/api/v2/upload/status`;
for (let i = 0; i < 50; i++) {
const statusResp = await fetch(statusUrl, {
headers: { 'ous-token-v2': ousToken }
});
const data = await statusResp.json();
if (data.d.status === 5) return data.d.url;
if (data.d.status === 6 || data.d.status === 8) {
throw new Error('上传失败');
}
await new Promise(r => setTimeout(r, 200));
}
throw new Error('上传超时');
}
async function create3DGenProject(imageUrl, prompt) {
const url = `${BASE_URL}/v2/aholo/project/create-3d-gen?appuid=${APP_UID}`;
const resp = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
projectName: 'AI Generated 3D',
prompt,
resources: [{
name: 'input.jpg',
type: 0,
url: imageUrl,
meta: { width: 1920, height: 1080 }
}]
})
});
const data = await resp.json();
if (data.c !== '0') throw new Error('创建项目失败');
return data.d;
}
async function getProjectInfo(projectId) {
const url = `${BASE_URL}/v2/aholo/project/info?appuid=${APP_UID}&projectId=${projectId}`;
const resp = await fetch(url, {
headers: { 'Authorization': `Bearer ${API_KEY}` }
});
return await resp.json();
}
async function waitForResult(projectId, timeout = 120) {
for (let i = 0; i < timeout / 3; i++) {
const info = await getProjectInfo(projectId);
const status = info.d.task.status;
if (status === 3) return info.d.task.result;
if (status === 4) throw new Error('项目处理失败');
await new Promise(r => setTimeout(r, 3000));
}
throw new Error('等待超时');
}
async function main() {
console.log('1. 获取上传 Token...');
const { globalDomain, ousToken } = await getUploadToken();
console.log('2. 上传图片...');
const imageUrl = await uploadFile(globalDomain, ousToken, 'input.jpg');
console.log(` 图片 URL: ${imageUrl}`);
console.log('3. 创建 3D 生成项目...');
const projectId = await create3DGenProject(imageUrl, '生成客厅空间');
console.log(` 项目 ID: ${projectId}`);
console.log('4. 等待处理完成...');
const result = await waitForResult(projectId);
console.log('5. 结果文件:');
console.log(` PLY: ${result.plyPath}`);
console.log(` SPZ: ${result.spzPath}`);
console.log(` SOG: ${result.sogPath}`);
}
main().catch(console.error);运行
bash
node aholo_example.js