请求
This commit is contained in:
parent
97c6d06a8f
commit
f52dcdf657
|
|
@ -11,12 +11,11 @@ class BaseEntity {
|
||||||
|
|
||||||
// 数据解析
|
// 数据解析
|
||||||
factory BaseEntity.fromJson(json) {
|
factory BaseEntity.fromJson(json) {
|
||||||
Map<String, dynamic> responseData = jsonDecode(json);
|
Map<String, dynamic> responseData = json;
|
||||||
int code = responseData["Code"];
|
|
||||||
int result = responseData["Result"];
|
|
||||||
String message = responseData["Message"]; //错误描述
|
dynamic data = json["choices"][0]["message"]["content"];
|
||||||
dynamic data = responseData["Data"];
|
return BaseEntity(data: data);
|
||||||
return BaseEntity(code: code, result: result, message: message, data: data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 数据解析
|
// 数据解析
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,13 @@ class NetworkConfig {
|
||||||
static int SELECT_INDEX = 0;
|
static int SELECT_INDEX = 0;
|
||||||
|
|
||||||
static List BASE_URLS = [
|
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 = [
|
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 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));
|
responseType: ResponseType.json));
|
||||||
_dioLog!.interceptors.add(DioLogInterceptor());
|
_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 {
|
{RequestMethod? method}) async {
|
||||||
Map<String, dynamic> headers = {
|
Map<String, dynamic> headers = {
|
||||||
"AppId": NetworkConfig.AppId,
|
"AppId": NetworkConfig.AppId,
|
||||||
/*"BossId": NetworkConfig.BossId,*/
|
|
||||||
"userId": NetworkConfig.userId,
|
"userId": NetworkConfig.userId,
|
||||||
"Version": NetworkConfig.Version,
|
"Version": NetworkConfig.Version,
|
||||||
"Language": NetworkConfig.Language
|
"Language": NetworkConfig.Language
|
||||||
|
|
@ -133,22 +124,12 @@ class RequestCenter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 网络请求默认为post
|
// 网络请求默认为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 {
|
{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 {
|
try {
|
||||||
FormData formData = FormData.fromMap(parmetersSign);
|
//FormData formData = FormData.fromMap(parmeters);
|
||||||
Response response = await _dio!.post(path, data: formData);
|
Response response = await _dio!.post(path, data: parmeters);
|
||||||
if (response != null && response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
BaseEntity entity = BaseEntity.fromJson(response.data);
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
||||||
success(entity);
|
success(entity);
|
||||||
return entity;
|
return entity;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
@ -22,22 +21,22 @@ class HomeModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
//首页大厅数据
|
//首页大厅数据
|
||||||
Future<void> getHallEpgList(int type) async {
|
Future<void> chat(message, character) async {
|
||||||
RequestCenter.instance.request(NetworkConfig.getHallEpgList, {}, (BaseEntity dataEntity) {
|
var nowTime = DateTime.now(); //获取当前时间
|
||||||
//print("BaseEntity" + dataEntity.data.toString());
|
var nTime = nowTime.millisecondsSinceEpoch; //单位是毫秒(千分之一秒),13位时间戳
|
||||||
if (dataEntity.code == 0 && dataEntity.result == 0) {
|
RequestCenter.instance.request(NetworkConfig.chat, {
|
||||||
Map<String, dynamic> json = jsonDecode(dataEntity.data);
|
"messages": [
|
||||||
HallEpgListBean data = HallEpgListBean.fromJson(json);
|
{"role": "user", "content": message}
|
||||||
|
],
|
||||||
|
"mode": "chat",
|
||||||
|
"character": character
|
||||||
|
}, (BaseEntity dataEntity) {
|
||||||
|
String json = dataEntity.data;
|
||||||
|
|
||||||
streamController.sink.add({
|
streamController.sink.add({
|
||||||
'code': "1", //有数据
|
'code': "chat", //有数据
|
||||||
'data': data,
|
'data': json,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
streamController.sink.add({
|
|
||||||
'code': "-1", //
|
|
||||||
'data': dataEntity.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, (ErrorEntity errorEntity) {
|
}, (ErrorEntity errorEntity) {
|
||||||
streamController.sink.add({
|
streamController.sink.add({
|
||||||
'code': "0", //无数据
|
'code': "0", //无数据
|
||||||
|
|
@ -46,9 +45,6 @@ class HomeModel {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 获取原生的值
|
// 获取原生的值
|
||||||
invokeNativeMethod(String method, Map<String, Object> map) async {
|
invokeNativeMethod(String method, Map<String, Object> map) async {
|
||||||
dynamic args;
|
dynamic args;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,10 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||||
|
|
||||||
|
import 'HomeModel.dart';
|
||||||
|
|
||||||
class Homepage extends StatefulWidget {
|
class Homepage extends StatefulWidget {
|
||||||
const Homepage({super.key});
|
const Homepage({super.key});
|
||||||
|
|
@ -8,16 +14,147 @@ class Homepage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _HomepageState extends State<Homepage> {
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text("聊天"),
|
title: const Text("聊天"),
|
||||||
),
|
),
|
||||||
body: Container(
|
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