You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.9 KiB

import { reactive } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { getModelToMake, getImgHistory, makeImg } from "@/apis/api";
export default function () {
const token = uni.getStorageSync("token");
const { screenWidth } = uni.getWindowInfo();
const data: any = reactive({
moduleId: "",
promptId: "",
token: token,
modelImg: "", //模版图片
modelTitle: "", //模版标题
memoList: [], //模版描述
starNum: 0, //消耗金豆数量
pageNum: 1,
pageSize: 10,
totalPage: 0,
imgHistoryList: [],
loadContent: {
contentdown: "点击加载更多",
contentrefresh: "正在加载...",
contentnomore: "没有更多数据了~",
},
loadStatus: "more",
tempImgUrl: "", //上传文件路径
isMaking: false, //正在制作
isDisabled: false, //按钮在上传时禁用
imgWidth: Math.floor((screenWidth - 60) / 3), //历史图片适配宽度
});
onLoad((options: any) => {
data.moduleId = options.modelId;
data.promptId = options.promptId;
console.log(data);
getModelToMakeFn();
getImgHistoryFn();
});
//获取模板数据
function getModelToMakeFn() {
const param = {
model_id: data.moduleId,
prompt_id: data.promptId,
token: data.token,
};
getModelToMake(param).then((res: any) => {
if (res.success) {
data.modelImg = res.img_url;
data.modelTitle = res.record.model_name;
data.starNum = res.record.star;
const memo = res.record.memo;
if (memo && memo !== "") {
data.memoList = memo.split("。");
}
} else {
uni.showToast({
icon: "none",
title: "数据获取失败!",
});
}
});
}
//获取历史相册
function getImgHistoryFn() {
const param = {
page: data.pageNum,
limit: data.pageSize,
token: data.token,
};
getImgHistory(param).then((res: any) => {
if (res.success) {
data.imgHistoryList = res.data;
// changeLoadStatus();
} else {
//changeLoadStatus();
uni.showToast({
icon: "none",
title: "历史相册数据获取失败!",
});
}
});
}
//改变底部loading状态
// function changeLoadStatus() {
// if (data.pageNum == data.totalPage || data.totalPage == 0) {
// data.loadStatus = "no-more";
// } else {
// data.loadStatus = "more";
// }
// }
//上传图片
function toUpload() {
uni.chooseImage({
count: 1,
success: (res) => {
data.tempImgUrl = res.tempFilePaths[0];
},
fail: (err) => {
uni.showToast({
icon: "none",
title: "图片上传失败!",
});
},
});
}
//删除临时上传图片
function deleteTemp() {
data.tempImgUrl = "";
}
//历史转临时上传文件
function convertTemp(url: string) {
data.tempImgUrl = url;
}
//立即制作
function toMake() {
if (data.tempImgUrl && data.tempImgUrl !== "") {
const param = {
model_id: data.moduleId,
prompt_id: data.promptId,
token: data.token,
source_img_url: data.tempImgUrl,
count: 1,
};
data.isMaking = true;
makeImg(param).then((res: any) => {
data.isMaking = false;
if (res.success) {
uni.showToast({
duration: 3500,
icon: "none",
title: res.estimation_finish_time,
});
data.isDisabled = true;
setTimeout(() => {
uni.navigateBack();
}, 3500);
} else {
uni.showToast({
icon: "none",
title: res.message,
});
}
});
} else {
uni.showToast({
icon: "none",
title: "请上传图片!",
});
}
}
return {
data,
toMake,
toUpload,
deleteTemp,
convertTemp,
};
}