From f52dcdf65714c3ef90a2feacd27097b52f61afa7 Mon Sep 17 00:00:00 2001 From: zhangzhan <1372350891@qq.com> Date: Sat, 15 Jun 2024 14:39:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/network/BaseEntity.dart | 11 ++- lib/network/NetworkConfig.dart | 10 +-- lib/network/RequestCenter.dart | 29 ++----- lib/tools/HomeModel.dart | 36 ++++----- lib/tools/HomePage.dart | 141 ++++++++++++++++++++++++++++++++- 5 files changed, 170 insertions(+), 57 deletions(-) diff --git a/lib/network/BaseEntity.dart b/lib/network/BaseEntity.dart index af31442..70a986e 100644 --- a/lib/network/BaseEntity.dart +++ b/lib/network/BaseEntity.dart @@ -11,12 +11,11 @@ class BaseEntity { // 数据解析 factory BaseEntity.fromJson(json) { - Map 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 responseData = json; + + + dynamic data = json["choices"][0]["message"]["content"]; + return BaseEntity(data: data); } // 数据解析 diff --git a/lib/network/NetworkConfig.dart b/lib/network/NetworkConfig.dart index aa0d24c..82d8e40 100644 --- a/lib/network/NetworkConfig.dart +++ b/lib/network/NetworkConfig.dart @@ -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"; //聊天 } diff --git a/lib/network/RequestCenter.dart b/lib/network/RequestCenter.dart index d534d17..208703e 100644 --- a/lib/network/RequestCenter.dart +++ b/lib/network/RequestCenter.dart @@ -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 headers = { "AppId": NetworkConfig.AppId, - /*"BossId": NetworkConfig.BossId,*/ "userId": NetworkConfig.userId, "Version": NetworkConfig.Version, "Language": NetworkConfig.Language @@ -133,22 +124,12 @@ class RequestCenter { } // 网络请求默认为post - Future request(path, Map parmeters, Function(BaseEntity dataEntity) success, Function(ErrorEntity errorEntity) error, + Future request(path, Object parmeters, Function(BaseEntity dataEntity) success, Function(ErrorEntity errorEntity) error, {RequestMethod? method}) async { - Map headers = { - "AppId": NetworkConfig.AppId, - /*"BossId": NetworkConfig.BossId,*/ - "userId": NetworkConfig.userId, - "Version": NetworkConfig.Version, - "Language": NetworkConfig.Language - }; - parmeters.addAll(headers); - //签名加密 - Map 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; diff --git a/lib/tools/HomeModel.dart b/lib/tools/HomeModel.dart index fa9523b..528346e 100644 --- a/lib/tools/HomeModel.dart +++ b/lib/tools/HomeModel.dart @@ -1,4 +1,3 @@ - import 'dart:async'; import 'dart:convert'; import 'package:flutter/services.dart'; @@ -22,22 +21,22 @@ class HomeModel { } //首页大厅数据 - Future getHallEpgList(int type) async { - RequestCenter.instance.request(NetworkConfig.getHallEpgList, {}, (BaseEntity dataEntity) { - //print("BaseEntity" + dataEntity.data.toString()); - if (dataEntity.code == 0 && dataEntity.result == 0) { - Map 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 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 map) async { dynamic args; diff --git a/lib/tools/HomePage.dart b/lib/tools/HomePage.dart index 1eee1ca..0350edc 100644 --- a/lib/tools/HomePage.dart +++ b/lib/tools/HomePage.dart @@ -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 { + late StreamSubscription subscription; + final HomeModel viewModel = HomeModel(); + final TextEditingController _controller1 = TextEditingController(); + String text = ""; + String data = ''; + Future 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, + ), + ), + ), + ), + ); + } }