-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloud-mid-img.ts
80 lines (65 loc) · 2.09 KB
/
cloud-mid-img.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import cloud from '@lafjs/cloud'
import { Midjourney } from "midjourney";
const uuid = require('uuid');
const db = cloud.database()
const Message = db.collection('midjourney_task')
const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
});
const TaskStatus = {
NOT_START: 'NOT_START',
IN_PROGRESS: 'IN_PROGRESS',
FAILURE: 'FAILURE',
SUCCESS: 'SUCCESS',
};
const Action = {
IMAGINE: 'IMAGINE',
UPSCALE: 'UPSCALE',
VARIATION: 'VARIATION',
RESET: 'RESET',
};
async function resultData(code = 0, msg = '', data = null) {
return { code, msg, data }
}
export default async function (ctx: FunctionContext) {
let { prompt, webhook, state, action, content } = ctx.body;
if (!webhook || !prompt || !action) {
return resultData(400, '缺少必要的参数:prompt 或 webhook,action');
}
const lastMessage = await Message.where({
status: TaskStatus.NOT_START,
}).get()
const trans_result = await cloud.invoke('baiduTranslate', { body: { prompt: prompt } })
const id = uuid.v4()
const promptEn = trans_result.data
let prompts = `[${id}] ${promptEn}`
console.log(prompts)
client.Imagine(prompts)
const data = {
prompt,
taskId: id,
webhook,
promptEn,
action: action,
state,
status: TaskStatus.NOT_START,
submitTime: Date.now(),
createTime: getCurrentTime()
}
await Message.add(data)
// return { data2 }
return { prompt, promptEn, taskId: id, length: lastMessage.data.length }
}
function getCurrentTime() {
let now = Date.now();
// 定义你的时区偏移量,比如你在东八区(北京时间),偏移量应该是 +8
let timezoneOffsetHours = 8;
// 转换偏移量为毫秒
let timezoneOffsetMilliseconds = timezoneOffsetHours * 60 * 60 * 1000;
// 添加时区偏移量
let correctedTime = new Date(now + timezoneOffsetMilliseconds);
return correctedTime.toISOString();
}