213 lines
6.0 KiB
Dart
213 lines
6.0 KiB
Dart
import 'package:ar_tourism_flutter_unity/pages/publish/orderDetail.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'dart:async';
|
||
import 'package:dio/dio.dart';
|
||
import 'dart:convert';
|
||
|
||
import '../../apis/app.dart';
|
||
import '../../components/appbar.dart';
|
||
import '../../components/gradientButton.dart';
|
||
|
||
class PayPage extends StatefulWidget {
|
||
final String orderId;
|
||
final double totalPrice;
|
||
const PayPage({super.key, required this.orderId,required this.totalPrice});
|
||
|
||
@override
|
||
State<PayPage> createState() => _PayPageState();
|
||
}
|
||
|
||
class _PayPageState extends State<PayPage> {
|
||
late Timer _timer;
|
||
int _remainingTime = 5 * 60; // 5分钟倒计时
|
||
String selectedPayment = 'weixin'; // 默认选择微信支付
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_startTimer();
|
||
}
|
||
|
||
void _startTimer() {
|
||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||
setState(() {
|
||
if (_remainingTime > 0) {
|
||
_remainingTime--;
|
||
} else {
|
||
_timer.cancel();
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
// 添加支付处理方法
|
||
Future<void> _handlePayment() async {
|
||
print("准备支付金额: ¥${widget.totalPrice.toStringAsFixed(2)}");
|
||
final response = await userApi.getPayCallback(widget.orderId);
|
||
print("@@@@@@@@@@@response: ${response}");
|
||
|
||
if (response['code'] == 200) {
|
||
final orderId = response['orderId'].toString(); // 将整数转换为字符串
|
||
print("@@@@@@@@@@@orderId: ${orderId}");
|
||
|
||
// 跳转到订单详情页并清除之前所有页面的历史记录
|
||
Navigator.pushReplacement(
|
||
context,
|
||
MaterialPageRoute(
|
||
builder: (context) => OrderDetailPage(orderId: orderId),
|
||
),
|
||
);
|
||
} else {
|
||
if (mounted) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
SnackBar(content: Text(response['msg'] ?? '支付失败,请重试')),
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 添加关闭订单方法
|
||
Future<void> _closeOrder() async {
|
||
try {
|
||
print("准备关闭订单,订单号: ${widget.orderId}");
|
||
|
||
// 创建 FormData,直接传递订单号字符串
|
||
var formData = FormData.fromMap({
|
||
"mchOrderNo": widget.orderId
|
||
});
|
||
|
||
print("关闭订单请求参数: mchOrderNo=${widget.orderId}");
|
||
|
||
final response = await userApi.closeOrder(formData);
|
||
print("关闭订单响应: $response");
|
||
|
||
if (response['code'] == 200) {
|
||
print("订单关闭成功");
|
||
} else {
|
||
print("订单关闭失败: ${response['msg']}");
|
||
}
|
||
} catch (e) {
|
||
print("关闭订单异常: $e");
|
||
}
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_timer.cancel();
|
||
super.dispose();
|
||
}
|
||
|
||
String get _formattedTime {
|
||
final minutes = (_remainingTime ~/ 60).toString().padLeft(2, '0');
|
||
final seconds = (_remainingTime % 60).toString().padLeft(2, '0');
|
||
return '$minutes:$seconds';
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return PopScope(
|
||
canPop: true,
|
||
onPopInvoked: (didPop) async {
|
||
if (didPop) {
|
||
await _closeOrder();
|
||
}
|
||
},
|
||
child: Scaffold(
|
||
appBar: CustomAppBar(
|
||
title: '支付订单',
|
||
),
|
||
body: Container(
|
||
padding: const EdgeInsets.only(top: 30),
|
||
width: double.infinity,
|
||
height: double.infinity,
|
||
decoration: const BoxDecoration(color: Color(0xff0C0C16)),
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
const Text(
|
||
'实付金额',
|
||
style: TextStyle(
|
||
color: Color.fromRGBO(255, 255, 255, 0.5), fontSize: 14),
|
||
),
|
||
const SizedBox(height: 8),
|
||
Text(
|
||
'¥${widget.totalPrice.toStringAsFixed(2)}',
|
||
style: TextStyle(color: Colors.white, fontSize: 32),
|
||
),
|
||
const SizedBox(height: 16),
|
||
Text(
|
||
'剩余支付时间: $_formattedTime',
|
||
style: const TextStyle(color: Color(0xffEE7371), fontSize: 12),
|
||
),
|
||
const SizedBox(height: 24),
|
||
_buildPaymentOption('支付宝', 'images/zfb.png'),
|
||
const SizedBox(height: 16),
|
||
_buildPaymentOption('微信', 'images/wx.png'),
|
||
const Spacer(),
|
||
GradientButton(
|
||
text: '立即支付',
|
||
onPressed: () {
|
||
print("点击了立即支付按钮");
|
||
_handlePayment();
|
||
|
||
},
|
||
),
|
||
const SizedBox(
|
||
height: 30,
|
||
)
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildPaymentOption(String title, String imagePath) {
|
||
String paymentId = title == '微信' ? 'weixin' : 'alipay';
|
||
|
||
return Container(
|
||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||
decoration: BoxDecoration(
|
||
color: const Color(0xff1F212E),
|
||
borderRadius: BorderRadius.circular(8),
|
||
),
|
||
padding: const EdgeInsets.all(16),
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||
children: [
|
||
Row(
|
||
children: [
|
||
Image.asset(
|
||
imagePath,
|
||
width: 24,
|
||
height: 24,
|
||
),
|
||
const SizedBox(width: 16),
|
||
Text(
|
||
title,
|
||
style: const TextStyle(color: Colors.white, fontSize: 16),
|
||
),
|
||
],
|
||
),
|
||
Radio(
|
||
value: paymentId,
|
||
groupValue: selectedPayment,
|
||
onChanged: (value) {
|
||
setState(() {
|
||
selectedPayment = value.toString();
|
||
});
|
||
},
|
||
activeColor: const Color(0xFF96FBE9),
|
||
fillColor: MaterialStateProperty.resolveWith<Color>((states) {
|
||
if (states.contains(MaterialState.selected)) {
|
||
return const Color(0xFF96FBE9);
|
||
}
|
||
return Colors.white;
|
||
}),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|