请求
This commit is contained in:
parent
97c6d06a8f
commit
f52dcdf657
|
|
@ -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);
|
||||
}
|
||||
|
||||
// 数据解析
|
||||
|
|
|
|||
|
|
@ -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"; //聊天
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user