75 lines
3.3 KiB
JavaScript
75 lines
3.3 KiB
JavaScript
import { addConsumptionHistory } from '../school/api.js'
|
||
import outside from './outPlatforms/outside.js'
|
||
import { modelData,CostData } from '../config/Config.js';
|
||
|
||
async function getTaskResult(task) { // 创建一个函数,用于获取comfyui(runninghub)的任务结果得到其费用
|
||
const body = JSON.stringify({
|
||
"apiKey": modelData[task.info.AIGC][task.info.platform].apikey,
|
||
"taskId": task.remoteTaskId
|
||
})
|
||
console.log('获取任务结果参数', task.remoteTaskId)
|
||
for (let i = 0; i < 13; i++){
|
||
const response = await fetch('https://www.runninghub.cn/task/openapi/outputs', {
|
||
method: 'POST',
|
||
headers:{'Host':'www.runninghub.cn','Content-Type':'application/json'},
|
||
body: body
|
||
});
|
||
const res = await response.json()
|
||
console.log(res)
|
||
if ( res.code === 0 && res.msg === 'success'){
|
||
return res
|
||
}
|
||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||
}
|
||
// console.log('生成时长:',res.data.taskCostTime,'三方平台费用:',res.data.thirdPartyConsumeMoney)
|
||
}
|
||
|
||
async function defaultBody(task, file) {
|
||
return {
|
||
platformCode: `${task.info.platformId}`,
|
||
platformId: task.info.platformId,
|
||
taskRootId: task.info.taskRootId, // 根任务ID
|
||
parentTaskId: task.info.taskType === 1 ? '0' : (task.info.parentTaskId || '0'), // 记录父任务 ID
|
||
taskId: task.taskId,
|
||
title: task.info.title || '',
|
||
modelName: task.info.modelName || '',
|
||
chargeCode: task.info.chargeCode,
|
||
quantity: 1,
|
||
status: file.type, // 需要在获取结果后判断 1成功 2失败
|
||
fileType: task.info.fileType || 'image', // 文件类型
|
||
fileUrl: file.files || '',
|
||
chargeType: task.info.chargeType, // 需要在提交时判断
|
||
taskType: task.info.taskType, //任务类型:1 - 初始模特穿衣生成(仅根任务可用),2 - 对话修改,3 - 生成视频,4 - 生成模特,5 - 生成姿势,6 - 生成背景
|
||
type: 2, // 需要在提交时判断
|
||
actualAmount: task.info.cost,
|
||
createTime: task.info.createTime || '', // 创建时间
|
||
parentCreateTime: task.info.parentCreateTime || '', // 父任务创建时间
|
||
parentIndex: task.info.parentIndex || 0 // 父任务图片索引
|
||
}
|
||
}
|
||
|
||
export async function record(task) { // 创建一个函数,用于记录任务
|
||
const file = await outside[task.info.platform].getTaskResult(task.resultData)
|
||
let errorMessage = null
|
||
|
||
if(file.type === 2){
|
||
errorMessage = file.message
|
||
return false
|
||
}
|
||
if (task.info.platform === 'comfyui' && task.info.type === 2){
|
||
let res = null
|
||
res = await getTaskResult(task)
|
||
// console.log('生成时长:',res.data.taskCostTime,'三方平台费用:',res.data.thirdPartyConsumeMoney)
|
||
task.info.cost = Math.round(Number(res.data[0].taskCostTime) * 100 * CostData[task.info.platform] + Number(res.data[0].thirdPartyConsumeMoney || 0) * 100)
|
||
console.log('任务费用:',task.info.cost)
|
||
}
|
||
|
||
let body = null
|
||
body = await defaultBody(task, file)
|
||
|
||
console.log('记录信息\n', body)
|
||
const success = await addConsumptionHistory(body,task.token)
|
||
console.log('记录任务成功', success.data)
|
||
return 'succes'; // 返回 后端任务ID与对应的平台任务ID, “后端任务ID” 该ID为当前后端创建,非外部平台返回的任务ID
|
||
}
|