This commit is contained in:
zhangzhan 2024-06-15 14:39:02 +08:00
parent 97c6d06a8f
commit f52dcdf657
5 changed files with 170 additions and 57 deletions

View File

@ -11,12 +11,11 @@ class BaseEntity {
//
factory BaseEntity.fromJson(json) {
Map<String, dynamic> responseData = jsonDecode(json);
int code = responseData["Code"];
int result = responseData["Result"];
String message = responseData["Message"]; //
dynamic data = responseData["Data"];
return BaseEntity(code: code, result: result, message: message, data: data);
Map<String, dynamic> responseData = json;
dynamic data = json["choices"][0]["message"]["content"];
return BaseEntity(data: data);
}
//

View File

@ -9,13 +9,13 @@ class NetworkConfig {
static int SELECT_INDEX = 0;
static List BASE_URLS = [
"http://117.50.182.144:5000/",
"http://117.50.182.144:5000/",
"http://117.50.182.144:5000/",
"http://117.50.182.144:5000",
"http://117.50.182.144:5000",
"http://117.50.182.144:5000",
];
static List BASE_URLS_AI = [
"http://117.50.182.144:5000/",
"http://117.50.182.144:5000",
];
@ -35,6 +35,6 @@ class NetworkConfig {
static const String login = "login"; //
static const String getHallEpgList = "/api/Epg/GetHallEpgList.ashx"; //
static const String chat = "/v1/chat/completions"; //
}

View File

@ -60,15 +60,7 @@ class RequestCenter {
responseType: ResponseType.json));
_dioLog!.interceptors.add(DioLogInterceptor());
// if (NetworkConfig.isAgent) {
// (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
// client.findProxy = (uri) {
// return "PROXY 192.168.1.231:8888";
// };
// //Https包设置
// client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
// };
// }
}
}
@ -77,7 +69,6 @@ class RequestCenter {
{RequestMethod? method}) async {
Map<String, dynamic> headers = {
"AppId": NetworkConfig.AppId,
/*"BossId": NetworkConfig.BossId,*/
"userId": NetworkConfig.userId,
"Version": NetworkConfig.Version,
"Language": NetworkConfig.Language
@ -133,22 +124,12 @@ class RequestCenter {
}
// post
Future<BaseEntity?> request(path, Map<String, dynamic> parmeters, Function(BaseEntity dataEntity) success, Function(ErrorEntity errorEntity) error,
Future<BaseEntity?> request(path, Object parmeters, Function(BaseEntity dataEntity) success, Function(ErrorEntity errorEntity) error,
{RequestMethod? method}) async {
Map<String, dynamic> headers = {
"AppId": NetworkConfig.AppId,
/*"BossId": NetworkConfig.BossId,*/
"userId": NetworkConfig.userId,
"Version": NetworkConfig.Version,
"Language": NetworkConfig.Language
};
parmeters.addAll(headers);
//
Map<String, dynamic> parmetersSign = sign(parmeters);
try {
FormData formData = FormData.fromMap(parmetersSign);
Response response = await _dio!.post(path, data: formData);
if (response != null && response.statusCode == 200) {
//FormData formData = FormData.fromMap(parmeters);
Response response = await _dio!.post(path, data: parmeters);
if (response.statusCode == 200) {
BaseEntity entity = BaseEntity.fromJson(response.data);
success(entity);
return entity;

View File

@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
@ -22,22 +21,22 @@ class HomeModel {
}
//
Future<void> getHallEpgList(int type) async {
RequestCenter.instance.request(NetworkConfig.getHallEpgList, {}, (BaseEntity dataEntity) {
//print("BaseEntity" + dataEntity.data.toString());
if (dataEntity.code == 0 && dataEntity.result == 0) {
Map<String, dynamic> json = jsonDecode(dataEntity.data);
HallEpgListBean data = HallEpgListBean.fromJson(json);
streamController.sink.add({
'code': "1", //
'data': data,
});
} else {
streamController.sink.add({
'code': "-1", //
'data': dataEntity.message,
});
}
Future<void> chat(message, character) async {
var nowTime = DateTime.now(); //
var nTime = nowTime.millisecondsSinceEpoch; //()13
RequestCenter.instance.request(NetworkConfig.chat, {
"messages": [
{"role": "user", "content": message}
],
"mode": "chat",
"character": character
}, (BaseEntity dataEntity) {
String json = dataEntity.data;
streamController.sink.add({
'code': "chat", //
'data': json,
});
}, (ErrorEntity errorEntity) {
streamController.sink.add({
'code': "0", //
@ -46,9 +45,6 @@ class HomeModel {
});
}
//
invokeNativeMethod(String method, Map<String, Object> map) async {
dynamic args;

View File

@ -1,4 +1,10 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'HomeModel.dart';
class Homepage extends StatefulWidget {
const Homepage({super.key});
@ -8,16 +14,147 @@ class Homepage extends StatefulWidget {
}
class _HomepageState extends State<Homepage> {
late StreamSubscription subscription;
final HomeModel viewModel = HomeModel();
final TextEditingController _controller1 = TextEditingController();
String text = "";
String data = '';
Future<void> chat() async {
viewModel.chat(text,'Assistant');
}
@override
void initState() {
// TODO: implement initState
super.initState();
//
_controller1.addListener(() {
print(_controller1.text);
});
subscription = viewModel.streamController.stream.listen((newData) {
String code = newData['code'];
if (code.isNotEmpty) {
if (code == "chat") {
//
data = newData['data'];
setState(() {
});
//EasyLoading.showToast(data);
print("data" + data.toString());
}
} else {
EasyLoading.dismiss();
}
});
}
void _textFieldChanged1(String str) {
text = str;
}
@override
void dispose() {
// TODO: implement dispose
subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("聊天"),
title: const Text("聊天"),
),
body: Container(
child: Text('你好'),
child: Column(
children: [
Text(data),
Container(
alignment: Alignment.center,
margin: const EdgeInsets.only(top: 100),
padding: EdgeInsets.only(left: 21),
height: 48,
width: MediaQuery.of(context).size.width - 60,
decoration: new BoxDecoration(
//
borderRadius: const BorderRadius.all(Radius.circular(24.0)),
border: Border.all(color: const Color(0xFFCACACA), width: 1),
//
),
child: TextField(
controller: _controller1,
keyboardType: TextInputType.number,
decoration: const InputDecoration.collapsed(hintText: '输入内容', hintStyle: TextStyle(fontSize: 16.0, color: Color(0xFF999999))),
textAlign: TextAlign.left,
//
//TextField右下角有一个输入数量的统计字符串
maxLines: 1,
//
style: const TextStyle(fontSize: 16.0, color: Color(0xFF999999)),
//
onChanged: _textFieldChanged1,
autofocus: false,
inputFormatters: [
LengthLimitingTextInputFormatter(255),
//
]),
),
Container(
margin: const EdgeInsets.only(top: 17),
height: 48,
width: MediaQuery.of(context).size.width - 60,
child: _LoginButton(),
),
],
),
),
);
}
_LoginButton() {
return Container(
height: 35,
alignment: Alignment.center,
decoration: const BoxDecoration(
//
gradient:
LinearGradient(colors: [Color(0xFF52B5FF), Color(0xFF6591FB)], begin: Alignment.centerLeft, end: Alignment.centerRight),
//
borderRadius: BorderRadius.all(Radius.circular(24)),
//
),
child: SizedBox(
width: double.infinity,
height: double.infinity,
child: ElevatedButton(
style: ButtonStyle(
elevation: MaterialStateProperty.all(0), //
backgroundColor: MaterialStateProperty.all(Color(0x00ffffff)),
shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(3.3))),
//
overlayColor: MaterialStateProperty.all(Color(0x4fffffff)),
),
onPressed: () {
chat();
},
child: Text(
"调用",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w600,
),
),
),
),
);
}
}