AI_Drawing/lib/tools/create/create_model.dart
2024-06-03 15:30:15 +08:00

205 lines
6.3 KiB
Dart

import 'dart:async';
import 'package:aiplot/bean/draw_template_bean.dart';
import '../../bean/text_to_image_bean.dart';
import '../../bean/user_draw_bean.dart';
import '../../bean/user_info_bean.dart';
import '../../network/BaseEntity.dart';
import '../../network/NetworkConfig.dart';
import '../../network/RequestCenter.dart';
class CreateModel {
StreamController streamController = StreamController.broadcast();
CreateModel() {
setup();
}
void setup() {
//初始化
}
//获取模板列表
Future<void> getDrawTemplateList() async {
RequestCenter.instance.requestPlay(NetworkConfig.getDrawTemplateList, {}, (BaseEntity dataEntity) {
if (dataEntity.code == 0) {
List<DrawTemplateBean> data = (dataEntity.data as List<dynamic>).map((e) => DrawTemplateBean.fromJson(e as Map<String, dynamic>)).toList();
streamController.sink.add({
'code': "getDrawTemplateList", //有数据
'data': data,
});
} else {
streamController.sink.add({
'code': "-1", //
'data': dataEntity.code,
});
}
}, (ErrorEntity errorEntity) {
streamController.sink.add({
'code': "0", //无数据
'data': errorEntity.code,
});
});
}
Future<void> getUserData() async {
RequestCenter.instance.requestPlay(NetworkConfig.getUserData, {}, (BaseEntity dataEntity) {
if (dataEntity.code == 0) {
Map<String, dynamic> json = dataEntity.data;
UserInfoBean data = UserInfoBean.fromJson(json);
NetworkConfig.userData = data;
double num = data.AllCurrency!;
NetworkConfig.allCurrency = num.truncate().toString();
NetworkConfig.currency = num;
NetworkConfig.usedRewardCount = data.UserAdLog!.UsedRewardCount;
NetworkConfig.totalRewardCount = data.UserAdLog!.TotalRewardCount;
NetworkConfig.invitationCode = data.InvitationCode;
streamController.sink.add({
'code': "getUserData", //有数据
'data': data,
});
} else {}
}, (ErrorEntity errorEntity) {
streamController.sink.add({
'code': "0", //无数据
'data': errorEntity.code,
});
});
}
//图片描述
Future<void> txt2Img(String prompt, int templateId, int sizeTemplateId, int followId) async {
RequestCenter.instance.requestPlay(NetworkConfig.txt2Img, {
"Prompt": prompt,
"TemplateId": templateId,
"SizeTemplateId": sizeTemplateId,
"FollowId": followId,
}, (BaseEntity dataEntity) {
if (dataEntity.code == 0) {
Map<String, dynamic> json = dataEntity.data;
TextToImageBean data = TextToImageBean.fromJson(json);
streamController.sink.add({
'code': "txt2Img", //有数据
'data': data,
});
} else if (dataEntity.code == -5) {
//免费生成到上限
streamController.sink.add({
'code': "upperLimit",
'data': dataEntity.message,
});
} else if (dataEntity.code == -6) {
//绘画点不足
streamController.sink.add({
'code': "InsufficientPoints",
'data': dataEntity.message,
});
} else {
streamController.sink.add({
'code': "-1", //
'data': dataEntity.message,
});
}
}, (ErrorEntity errorEntity) {
streamController.sink.add({
'code': "0", //无数据
'data': errorEntity.code,
});
});
}
//图片描述(与文生图数据结构相同)
Future<void> img2Img(String prompt, String initImg, int templateId, int sizeTemplateId, double denoisingStrength, int followId) async {
RequestCenter.instance.requestPlay(NetworkConfig.img2Img, {
"Prompt": prompt,
"InitImg": initImg,
"TemplateId": templateId,
"SizeTemplateId": sizeTemplateId,
"DenoisingStrength": denoisingStrength,
"FollowId": followId,
}, (BaseEntity dataEntity) {
if (dataEntity.code == 0) {
Map<String, dynamic> json = dataEntity.data;
TextToImageBean data = TextToImageBean.fromJson(json);
streamController.sink.add({
'code': "txt2Img", //有数据
'data': data,
});
} else if (dataEntity.code == -5) {
//免费生成到上限
streamController.sink.add({
'code': "upperLimit", //有数据
'data': dataEntity.message,
});
} else if (dataEntity.code == -6) {
//绘画点不足
streamController.sink.add({
'code': "InsufficientPoints",
'data': dataEntity.message,
});
} else {
streamController.sink.add({
'code': "-1", //
'data': dataEntity.message,
});
}
}, (ErrorEntity errorEntity) {
streamController.sink.add({
'code': "0", //无数据
'data': errorEntity.code,
});
});
}
//审核模式上传图片
Future<void> uploadImg(String initImg) async {
RequestCenter.instance.requestPlay(NetworkConfig.uploadImg, {
"ImgBase64": initImg,
}, (BaseEntity dataEntity) {
if (dataEntity.code == 0 && dataEntity.data != null) {
//Map<String, dynamic> json = dataEntity.data;
streamController.sink.add({
'code': "uploadOk", //有数据
'data': "",
});
} else {
streamController.sink.add({
'code': "uploadErr", //有数据
'data': dataEntity.message,
});
}
}, (ErrorEntity errorEntity) {
streamController.sink.add({
'code': "0", //无数据
'data': errorEntity.code,
});
});
}
//获取画廊
Future<void> getGalleryList() async {
RequestCenter.instance.requestPlay(NetworkConfig.getGalleryList, {"index": 1, "size": 30, "DrawLabele": 0}, (BaseEntity dataEntity) {
if (dataEntity.code == 0) {
List<UserDrawBean> data = (dataEntity.data as List<dynamic>).map((e) => UserDrawBean.fromJson(e as Map<String, dynamic>)).toList();
streamController.sink.add({
'code': "bannerGallery", //有数据
'data': data,
});
} else {
streamController.sink.add({
'code': "-1", //
'data': dataEntity.code,
});
}
}, (ErrorEntity errorEntity) {
streamController.sink.add({
'code': "0", //无数据
'data': errorEntity.code,
});
});
}
}