查询数据

This commit is contained in:
2025-11-12 18:02:04 +08:00
parent 412f2786a2
commit c4f34f3e00
8 changed files with 206 additions and 133 deletions

View File

@@ -2,14 +2,17 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/common/common.dart';
import 'package:getx_scaffold/common/services/http.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:intl/intl.dart';
import 'package:ln_jq_app/common/model/base_model.dart';
import 'package:ln_jq_app/common/styles/theme.dart';
import 'package:ln_jq_app/pages/b_page/site/controller.dart';
import 'package:ln_jq_app/storage_service.dart'; // 用于日期格式化
import 'package:ln_jq_app/storage_service.dart';
class ReservationController extends GetxController {
/// --- 状态变量 ---
import '../../../common/styles/theme.dart'; // 用于日期格式化
class ReservationController extends GetxController with BaseControllerMixin {
@override
String get builderId => 'reservation';
// 【修改】使用 Rx 变量,让 GetX 的 Obx/GetX 能够自动监听变化UI更新更简单
// 日期,默认为今天
@@ -27,7 +30,7 @@ class ReservationController extends GetxController {
final TextEditingController amountController = TextEditingController();
// 车牌号
final TextEditingController plateNumberController = TextEditingController();
final TextEditingController plateNumberController = TextEditingController(text: "浙F");
// 加氢站
final List<String> stationOptions = [
@@ -152,7 +155,6 @@ class ReservationController extends GetxController {
},
);
// 安全校验
if (response == null || response.data == null) {
showToast('暂时无法获取预约数据');
hasReservationData = false;
@@ -163,12 +165,8 @@ class ReservationController extends GetxController {
final baseModel = BaseModel<dynamic>.fromJson(response.data);
if (baseModel.code == 0 && baseModel.data != null) {
// 【核心修改】处理接口返回的列表数据
final dataMap = baseModel.data as Map<String, dynamic>;
final List<dynamic> listFromServer = dataMap['list'] ?? [];
// 使用 .map() 遍历列表,将每个 item 转换为一个 ReservationModel 对象
reservationList = listFromServer.map((item) {
return ReservationModel.fromJson(item as Map<String, dynamic>);
}).toList();
@@ -176,18 +174,15 @@ class ReservationController extends GetxController {
// 根据列表是否为空来更新 hasReservationData 状态
hasReservationData = reservationList.isNotEmpty;
} else {
// 接口返回业务错误
showToast(baseModel.message);
hasReservationData = false;
reservationList = []; // 清空列表
}
} catch (e) {
// 捕获网络或解析异常
showToast('获取预约数据失败');
hasReservationData = false;
reservationList = []; // 清空列表
} finally {
// 无论成功失败最后都要关闭加载动画并更新UI
dismissLoading();
}
@@ -344,6 +339,103 @@ class ReservationController extends GetxController {
);
}
String phone = "";
String name = "";
String leftHydrogen = "";
String workEfficiency = "";
//累计数据
String fillingWeight = "";
String fillingTimes = "";
String plateNumber = "沪AGZ8967";
@override
void onInit() {
phone = StorageService.to.phone ?? "";
name = StorageService.to.name ?? "";
getCatinfo();
getJqinfo();
// getSiteList();
super.onInit();
}
void getJqinfo() async {
try {
HttpService.to.setBaseUrl(AppTheme.test_service_url);
var responseData = await HttpService.to.get(
'appointment/truck/history-filling-summary?vin=LSFGL23Z2ND214377'
);
if (responseData == null || responseData.data == null) {
showToast('服务暂不可用,请稍后');
return;
}
var result = BaseModel.fromJson(responseData.data);
fillingWeight = "${result.data["fillingWeight"]}${result.data["fillingWeightUnit"]}";
fillingTimes = "${result.data["fillingTimes"]}${result.data["fillingTimesUnit"]}";
updateUi();
} catch (e) {
} finally {
HttpService.to.setBaseUrl(AppTheme.test_service_url);
}
}
void getCatinfo() async {
try {
HttpService.to.setBaseUrl(AppTheme.car_service_url);
var responseData = await HttpService.to.post(
'VehicleData/getHydrogenInfoByPlateNumber',
data: {
'userName': "xll@lingniu",
'password': "4q%3!l6s0p",
'plateNumber': plateNumber,
},
);
if (responseData == null || responseData.data == null) {
showToast('服务暂不可用,请稍后');
return;
}
var result = BaseModel.fromJson(responseData.data);
leftHydrogen = result.data["leftHydrogen"].toString();
workEfficiency = result.data["workEfficiency"].toString();
updateUi();
} catch (e) {
} finally {
HttpService.to.setBaseUrl(AppTheme.test_service_url);
}
}
void getSiteList() async {
final originalHeaders = Map<String, dynamic>.from(HttpService.to.dio.options.headers);
try {
HttpService.to.setBaseUrl(AppTheme.jiaqing_service_url);
HttpService.to.dio.options.headers['appId'] = '97ad10eeb6b346f79e0d6ffd81e4d3c3';
var responseData = await HttpService.to.get("hydrogen/queryHydrogenSiteInfo");
if (responseData == null && responseData!.data == null) {
showToast('暂时无法获取站点信息');
return;
}
try {
var result = BaseModel.fromJson(responseData.data);
// showToast(result.data["data"].toString());
} catch (e) {
showToast('数据异常');
}
} catch (e) {
} finally {
HttpService.to.setBaseUrl(AppTheme.test_service_url);
HttpService.to.dio.options.headers = originalHeaders;
}
}
@override
void onClose() {
amountController.dispose();

View File

@@ -1,10 +1,9 @@
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:getx_scaffold/common/widgets/text_x.dart';
// import 'package:getx_scaffold/getx_scaffold.dart'; // 如果不使用其中的扩展,可以注释掉
import 'package:getx_scaffold/getx_scaffold.dart';
import '../../../storage_service.dart';
import 'controller.dart';
class ReservationPage extends GetView<ReservationController> {
@@ -12,33 +11,35 @@ class ReservationPage extends GetView<ReservationController> {
@override
Widget build(BuildContext context) {
// 【修改】使用 Get.put 来初始化 Controller而不是在 GetBuilder 中。这是更推荐的做法。
// Get.lazyPut 会在第一次使用时才创建实例。
Get.lazyPut(() => ReservationController());
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildUserInfoCard(),
const SizedBox(height: 12),
_buildCarInfoCard(),
const SizedBox(height: 12),
_buildReservationFormCard(context), // 将 context 传入
const SizedBox(height: 12),
_buildTipsCard(),
],
),
),
return GetBuilder<ReservationController>(
init: ReservationController(),
id: 'reservation',
builder: (_) {
return Scaffold(
backgroundColor: Colors.grey[100],
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildUserInfoCard(),
const SizedBox(height: 16),
_buildCarInfoCard(),
const SizedBox(height: 16),
_buildReservationFormCard(context),
],
),
),
);
},
);
}
/// 构建顶部用户信息卡片
/// 构建用户信息卡片
Widget _buildUserInfoCard() {
return Card(
elevation: 2,
elevation: 2, // 轻微的阴影
shadowColor: Colors.black.withOpacity(0.1),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
child: Column(
children: [
@@ -52,17 +53,17 @@ class ReservationPage extends GetView<ReservationController> {
child: Icon(Icons.person, color: Colors.white, size: 30),
),
const SizedBox(width: 12),
const Expanded(
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'王小龙',
controller.name,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
SizedBox(height: 4),
Text(
'15888332828',
controller.phone,
style: TextStyle(color: Colors.grey, fontSize: 12),
),
],
@@ -98,7 +99,10 @@ class ReservationPage extends GetView<ReservationController> {
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [_buildStatItem('0Kg', '累计加氢'), _buildStatItem('0次', '加氢次数')],
children: [
_buildStatItem(controller.fillingWeight, '累计加氢'),
_buildStatItem(controller.fillingTimes, '加氢次数'),
],
),
),
],
@@ -129,11 +133,11 @@ class ReservationPage extends GetView<ReservationController> {
Expanded(
child: Column(
children: [
_buildInfoRow('车牌号:', '扫码绑定'),
_buildInfoRow('车牌号: ${controller.plateNumber}', '扫码绑定'),
const SizedBox(height: 12),
_buildInfoRow('剩余氢量:', '0Kg'),
_buildInfoRow('剩余氢量:', '${controller.leftHydrogen}Kg'),
const SizedBox(height: 12),
_buildInfoRow('百公里氢耗:', '0KG/100KM'),
_buildInfoRow('百公里氢耗:', '${controller.workEfficiency}KG/100KM'),
],
),
),
@@ -215,7 +219,8 @@ class ReservationPage extends GetView<ReservationController> {
_buildTextField(
label: '车牌号',
controller: controller.plateNumberController,
hint: '请输入车牌号',
hint: '请输入车牌号', // 修改提示文案
enabled: false, // 设置为不可编辑
),
_buildStationSelector(),
const SizedBox(height: 24),
@@ -310,6 +315,7 @@ class ReservationPage extends GetView<ReservationController> {
required TextEditingController controller,
required String hint,
TextInputType? keyboardType,
bool enabled = true,
}) {
return Padding(
padding: const EdgeInsets.only(bottom: 12.0),
@@ -321,10 +327,14 @@ class ReservationPage extends GetView<ReservationController> {
TextFormField(
controller: controller,
keyboardType: keyboardType,
enabled: enabled, // 使用 enabled 参数
decoration: InputDecoration(
hintText: hint,
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8.0)),
contentPadding: const EdgeInsets.symmetric(horizontal: 12.0),
// 【新增】禁用时,使用填充色
filled: !enabled,
fillColor: Colors.grey[100],
),
),
],
@@ -344,7 +354,7 @@ class ReservationPage extends GetView<ReservationController> {
Text('加氢站', style: TextStyle(color: Colors.grey[600], fontSize: 14)),
TextButton(
onPressed: () {
showToast("msg");
controller.getSiteList();
},
child: const Text('刷新'),
),
@@ -382,18 +392,13 @@ class ReservationPage extends GetView<ReservationController> {
),
iconStyleData: const IconStyleData(
icon: Icon(Icons.arrow_drop_down),
iconSize: 24,
iconSize: 20,
),
dropdownStyleData: DropdownStyleData(
maxHeight: 200,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)),
offset: const Offset(0, -5),
elevation: 8,
),
menuItemStyleData: const MenuItemStyleData(
height: 55, // 控制每个下拉选项的高度
padding: EdgeInsets.symmetric(horizontal: 14),
),
menuItemStyleData: const MenuItemStyleData(height: 40),
),
),
),
@@ -402,78 +407,12 @@ class ReservationPage extends GetView<ReservationController> {
);
}
///用于构建 DropdownMenuItem 内部的自定义 Widget
Widget _buildDropdownItem(String value) {
bool isSpecial = value.contains('银河路');
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Flexible(
child: Text(
value,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
overflow: TextOverflow.ellipsis,
),
),
if (isSpecial) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: Colors.orange[100],
borderRadius: BorderRadius.circular(4),
),
child: const Text(
'维修中',
style: TextStyle(color: Colors.orange, fontSize: 10),
),
),
],
],
),
const SizedBox(height: 2),
const Text(
'江苏省苏州市常熟市东南街道银河路80号 | ¥45.00/kg',
style: TextStyle(color: Colors.grey, fontSize: 11),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
],
);
}
/// 构建提示信息卡片
Widget _buildTipsCard() {
return Card(
elevation: 2,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
_buildTipItem(Icons.info_outline, '请提前30分钟到达加氢站'),
const SizedBox(height: 10),
_buildTipItem(Icons.rule, '请确保车辆证件齐全'),
const SizedBox(height: 10),
_buildTipItem(Icons.headset_mic_outlined, '如有疑问请联系客服: 400-123-4567'),
],
),
),
);
}
// 提示信息卡片中的列表项
Widget _buildTipItem(IconData icon, String text) {
Widget _buildDropdownItem(String item) {
return Row(
children: [
Icon(icon, color: Colors.blue, size: 20),
const Icon(Icons.local_gas_station_outlined, size: 18, color: Colors.grey),
const SizedBox(width: 10),
Expanded(
child: Text(text, style: const TextStyle(fontSize: 12, color: Colors.black54)),
),
Text(item, style: const TextStyle(fontSize: 14)),
],
);
}

View File

@@ -187,11 +187,20 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
String idCard = result.data['idCard'] ?? '';
String name = result.data['name'] ?? '';
String phone = result.data['phone'] ?? '';
/*{
"token": "a615cf38a066473ebf8e9a956fc51928",
"idCard": "330324200010241024",
"name": "王小龙",
"phone": "15888331828"
}*/
await StorageService.to.saveLoginInfo(
token: token,
userId: phone,
userId: "",
channel: "driver",
idCard: idCard,
name: name,
phone: phone,
);
dismissLoading();