From fd66b06c7ede8d7826f580d534bf4aa4ca704193 Mon Sep 17 00:00:00 2001 From: zhangmeng <494089941@qq.com> Date: Tue, 15 Jun 2021 12:11:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E7=94=9F=E6=B4=BB=E7=BC=B4?= =?UTF-8?q?=E8=B4=B9=E6=94=AF=E4=BB=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/constants/api.dart | 9 ++ lib/models/pay/pay_model.dart | 69 ++++++++++ lib/models/pay/pay_model.g.dart | 31 +++++ lib/pages/life_pay/life_pay_page.dart | 124 +++++++++++------- lib/pages/life_pay/pay_util.dart | 96 ++++++++++++++ .../life_pay/widget/life_pay_detail_page.dart | 2 +- lib/provider/user_provider.dart | 7 +- lib/ui/home/home_notification.dart | 3 +- .../market/goods/goods_order_detail_page.dart | 1 - lib/ui/profile/house/house_func.dart | 2 +- pubspec.lock | 7 + pubspec.yaml | 3 + tool/_project_manage.dart | 6 + 13 files changed, 304 insertions(+), 56 deletions(-) create mode 100644 lib/models/pay/pay_model.dart create mode 100644 lib/models/pay/pay_model.g.dart create mode 100644 lib/pages/life_pay/pay_util.dart diff --git a/lib/constants/api.dart b/lib/constants/api.dart index 8fd2e36c..d3b0defd 100644 --- a/lib/constants/api.dart +++ b/lib/constants/api.dart @@ -21,6 +21,7 @@ class API { static _Market market = _Market(); static _News news = _News(); static _Search search = _Search(); + static _Pay pay = _Pay(); } class _Login { @@ -419,3 +420,11 @@ class _Facility { String get detailType => '/user/facilitiesAppointment/findFacilitiesByCategoryId'; } + +class _Pay { + ///日常缴费 支付宝支付 + String get dailyPayMentAliPay => '/user/alipay/dailyPaymentAlipay'; + + ///日常缴费 查询支付宝订单状态 + String get dailPayMentCheck => '/user/alipay/dailyPaymentCheckAlipay'; +} diff --git a/lib/models/pay/pay_model.dart b/lib/models/pay/pay_model.dart new file mode 100644 index 00000000..c5f2ec65 --- /dev/null +++ b/lib/models/pay/pay_model.dart @@ -0,0 +1,69 @@ +import 'package:equatable/equatable.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'pay_model.g.dart'; + +@JsonSerializable() +class PayModel extends Equatable { + @JsonKey(name: 'alipay_trade_app_pay_response') + final AliPayTradeAppPayResponse aliPayTradeAppPayResponse; + final String sign; + @JsonKey(name: 'sign_type') + final String signType; + PayModel({ + required this.aliPayTradeAppPayResponse, + required this.sign, + required this.signType, + }); + factory PayModel.fromJson(Map json) => + _$PayModelFromJson(json); + @override + List get props => [aliPayTradeAppPayResponse, sign, signType]; +} + +@JsonSerializable() +class AliPayTradeAppPayResponse extends Equatable { + final String code; + final String msg; + @JsonKey(name: 'app_id') + final String appId; + @JsonKey(name: 'out_trade_no') + final String outTradeNo; + @JsonKey(name: 'trade_no') + final String tradeNo; + @JsonKey(name: 'total_amount') + final String totalAmount; + @JsonKey(name: 'seller_id') + final String sellerId; + final String charset; + final String timestamp; + AliPayTradeAppPayResponse({ + required this.code, + required this.msg, + required this.appId, + required this.outTradeNo, + required this.tradeNo, + required this.totalAmount, + required this.sellerId, + required this.charset, + required this.timestamp, + }); + + factory AliPayTradeAppPayResponse.fromJson(Map json) => + _$AliPayTradeAppPayResponseFromJson(json); + + @override + List get props { + return [ + code, + msg, + appId, + outTradeNo, + tradeNo, + totalAmount, + sellerId, + charset, + timestamp, + ]; + } +} diff --git a/lib/models/pay/pay_model.g.dart b/lib/models/pay/pay_model.g.dart new file mode 100644 index 00000000..751bffe5 --- /dev/null +++ b/lib/models/pay/pay_model.g.dart @@ -0,0 +1,31 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'pay_model.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +PayModel _$PayModelFromJson(Map json) { + return PayModel( + aliPayTradeAppPayResponse: AliPayTradeAppPayResponse.fromJson( + json['alipay_trade_app_pay_response'] as Map), + sign: json['sign'] as String, + signType: json['sign_type'] as String, + ); +} + +AliPayTradeAppPayResponse _$AliPayTradeAppPayResponseFromJson( + Map json) { + return AliPayTradeAppPayResponse( + code: json['code'] as String, + msg: json['msg'] as String, + appId: json['app_id'] as String, + outTradeNo: json['out_trade_no'] as String, + tradeNo: json['trade_no'] as String, + totalAmount: json['total_amount'] as String, + sellerId: json['seller_id'] as String, + charset: json['charset'] as String, + timestamp: json['timestamp'] as String, + ); +} diff --git a/lib/pages/life_pay/life_pay_page.dart b/lib/pages/life_pay/life_pay_page.dart index fd20bf0b..905a5754 100644 --- a/lib/pages/life_pay/life_pay_page.dart +++ b/lib/pages/life_pay/life_pay_page.dart @@ -1,3 +1,6 @@ +import 'package:aku_community/pages/life_pay/pay_util.dart'; +import 'package:aku_community/utils/network/base_model.dart'; +import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -17,7 +20,6 @@ import 'package:aku_community/provider/app_provider.dart'; import 'package:aku_community/ui/profile/house/pick_my_house_page.dart'; import 'package:aku_community/utils/bee_parse.dart'; import 'package:aku_community/utils/headers.dart'; -import 'package:aku_community/utils/network/base_model.dart'; import 'package:aku_community/utils/network/net_util.dart'; import 'package:aku_community/widget/bee_divider.dart'; import 'package:aku_community/widget/bee_scaffold.dart'; @@ -49,6 +51,7 @@ class _LifePayPageState extends State { bool get allSelect => ((_models.length == _selectYears.length) && (_models.length != 0)); + @override void initState() { super.initState(); @@ -58,6 +61,7 @@ class _LifePayPageState extends State { @override void dispose() { _controller?.dispose(); + BotToast.closeAllLoading(); super.dispose(); } @@ -140,12 +144,6 @@ class _LifePayPageState extends State { _selectPay[index].ids.forEach((element) { _ids.remove(element); }); - if (_count < 0) { - _count = 0; - } - if (_totalCost < 0) { - _totalCost = 0; - } } else { _selectYears.add(index); _totalCost += (_selectPay[index].payTotal); @@ -187,7 +185,8 @@ class _LifePayPageState extends State { fontWeight: FontWeight.bold), children: [ TextSpan( - text: '¥ ${_selectPay[index].payTotal}', + text: + '¥ ${_selectPay[index].payTotal.toStringAsFixed(2)}', style: TextStyle( color: kDangerColor, fontSize: 28.sp, @@ -201,11 +200,23 @@ class _LifePayPageState extends State { children: [ GestureDetector( onTap: () async { + if (_selectYears.contains(index)) { + _totalCost -= _selectPay[index].payTotal; + _count -= _selectPay[index].payCount; + _selectPay[index].ids.forEach((element) { + _ids.remove(element); + }); + } + List payMent = await (Get.to( () => LifePayDetailPage(model: _models[index]))); _selectPay[index].payCount = payMent[0]; _selectPay[index].payTotal = payMent[1]; _selectPay[index].ids = payMent[2]; + if (_selectYears.contains(index)) { + _totalCost += _selectPay[index].payTotal; + _count += _selectPay[index].payCount; + } setState(() {}); }, child: Container( @@ -257,6 +268,58 @@ class _LifePayPageState extends State { return _list; } + _allSelectOption() { + //若已全选则清空已选年份数组 + if (_models.length == _selectYears.length) { + _selectYears.clear(); + _ids.clear(); + _totalCost = 0; + _count = 0; + } else { + for (var i = 0; i < _models.length; i++) { + if (!_selectYears.contains(i)) { + _selectYears.add(i); + } + } + _totalCost = 0; + _count = 0; + _ids.clear(); + for (var item in _selectPay) { + _totalCost += item.payTotal; + _count += item.payCount; + _ids.addAll(item.ids); + } + } + setState(() {}); + } + + Widget _payButton() { + return MaterialButton( + elevation: 0, + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(37.w)), + color: kPrimaryColor, + padding: EdgeInsets.symmetric(horizontal: 50.w, vertical: 15.w), + onPressed: () async { + Function cancel = BotToast.showLoading(); + BaseModel baseModel = + await NetUtil().post('/user/alipay/dailyPaymentAlipay', params: { + "ids": _ids, + "payType": 1, //暂时写死 等待后续补充 + "payPrice": _totalCost.toStringAsFixed(2) + }); + if (baseModel.status ?? false) { + bool result = await PayUtil() + .callAliPay(baseModel.message!, API.pay.dailPayMentCheck); + if (result) { + Get.off(PayFinishPage()); + } + } + cancel(); + }, + child: '去缴费'.text.black.size(32.sp).bold.make(), + ); + } + @override Widget build(BuildContext context) { final appProvider = Provider.of(context); @@ -322,28 +385,7 @@ class _LifePayPageState extends State { children: [ GestureDetector( onTap: () { - //若已全选则清空已选年份数组 - if (_models.length == _selectYears.length) { - _selectYears.clear(); - _ids.clear(); - _totalCost = 0; - _count = 0; - } else { - for (var i = 0; i < _models.length; i++) { - if (!_selectYears.contains(i)) { - _selectYears.add(i); - } - } - _totalCost = 0; - _count = 0; - _ids.clear(); - for (var item in _selectPay) { - _totalCost += item.payTotal; - _count += item.payCount; - _ids.addAll(item.ids); - } - } - setState(() {}); + _allSelectOption(); }, child: AnimatedContainer( duration: Duration(milliseconds: 300), @@ -379,7 +421,7 @@ class _LifePayPageState extends State { fontWeight: FontWeight.bold), children: [ TextSpan( - text: '$_totalCost', + text: _totalCost.toStringAsFixed(2), style: TextStyle( color: kDangerColor, fontSize: 32.sp, @@ -389,25 +431,7 @@ class _LifePayPageState extends State { ], ), 24.w.widthBox, - MaterialButton( - elevation: 0, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(37.w)), - color: kPrimaryColor, - padding: EdgeInsets.symmetric(horizontal: 50.w, vertical: 15.w), - onPressed: () async { - BaseModel baseModel = - await NetUtil().post('/user/dailyPayment/pay', params: { - "ids": _ids, - "payType": 1, //暂时写死 等待后续补充 - "payPrice": _totalCost - }); - if (baseModel.status ?? false) { - Get.off(() => PayFinishPage()); - } - }, - child: '去缴费'.text.black.size(32.sp).bold.make(), - ), + _payButton(), ], ), ), diff --git a/lib/pages/life_pay/pay_util.dart b/lib/pages/life_pay/pay_util.dart new file mode 100644 index 00000000..771c2cc2 --- /dev/null +++ b/lib/pages/life_pay/pay_util.dart @@ -0,0 +1,96 @@ +import 'dart:convert'; + +import 'package:aku_community/models/pay/pay_model.dart'; +import 'package:aku_community/utils/network/net_util.dart'; +import 'package:bot_toast/bot_toast.dart'; +import 'package:dio/dio.dart'; +import 'package:power_logger/power_logger.dart'; +import 'package:tobias/tobias.dart'; + +enum PAYTYPE { + ///支付宝 + ALI, + + ///微信 + WX, + + ///现金 + CASH, + + ///pos + POS +} + +class PayUtil { + void resultSatus(String status) { + switch (status) { + case '8000': + BotToast.showText(text: '正在处理中'); + break; + case '4000': + BotToast.showText(text: '订单支付失败'); + break; + case '5000': + BotToast.showText(text: '重复请求'); + break; + case '6001': + BotToast.showText(text: ' 用户中途取消'); + break; + case '6002': + BotToast.showText(text: '网络连接出错'); + break; + case '6004': + BotToast.showText(text: '支付结果未知,请查询商户订单列表中订单的支付状态'); + break; + default: + BotToast.showText(text: '其他支付错误'); + break; + } + } + + String _resultStatus = ''; + + ///传入订单信息和确认订单请求地址 + Future callAliPay(String order, String path) async { + Map result = await aliPay(order); + _resultStatus = result['resultStatus']; + if (_resultStatus == '9000') { + String _res = result['result']; + PayModel _model = PayModel.fromJson(jsonDecode(_res)); + bool _confirmResult = await _confirmPayResult( + path, _model.aliPayTradeAppPayResponse.outTradeNo); + return _confirmResult; + } else { + resultSatus(_resultStatus); + return false; + } + } + + Future _confirmPayResult(String path, String code) async { + try { + int status = 0; + for (var i = 0; i < 3; i++) { + await Future.delayed(Duration(milliseconds: 1000), () async { + Response response = await NetUtil().dio!.get(path, queryParameters: { + "code": code, + }); + status = response.data['status'] as int; + }); + if (status == 2) { + break; + } + } + if (status == 2) { + BotToast.showText(text: '交易成功'); + return true; + } else { + BotToast.showText(text: '交易失败 错误码${status}'); + return false; + } + } catch (e) { + BotToast.showText(text: '网络请求错误'); + LoggerData.addData(e); + return false; + } + } +} diff --git a/lib/pages/life_pay/widget/life_pay_detail_page.dart b/lib/pages/life_pay/widget/life_pay_detail_page.dart index 555234c8..f206e6f8 100644 --- a/lib/pages/life_pay/widget/life_pay_detail_page.dart +++ b/lib/pages/life_pay/widget/life_pay_detail_page.dart @@ -236,7 +236,7 @@ class _LifePayDetailPageState extends State { fontWeight: FontWeight.bold), children: [ TextSpan( - text: '$_payTotal', + text: '${_payTotal.toStringAsFixed(2)}', style: TextStyle( color: kDangerColor, fontSize: 32.sp, diff --git a/lib/provider/user_provider.dart b/lib/provider/user_provider.dart index 884a7867..dd41a36d 100644 --- a/lib/provider/user_provider.dart +++ b/lib/provider/user_provider.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flustars/flustars.dart'; import 'package:get/get.dart'; import 'package:jpush_flutter/jpush_flutter.dart'; +import 'package:power_logger/power_logger.dart'; import 'package:provider/provider.dart'; import 'package:aku_community/constants/api.dart'; @@ -52,7 +53,11 @@ class UserProvider extends ChangeNotifier { Future updateProfile() async { _userInfoModel = await SignFunc.getUserInfo(); if (_userInfoModel != null && !kIsWeb && !Platform.isMacOS) { - await JPush().setAlias(_userInfoModel?.id.toString() ?? ''); + try { + await JPush().setAlias(_userInfoModel!.id.toString()); + } catch (e) { + LoggerData.addData(e); + } } notifyListeners(); } diff --git a/lib/ui/home/home_notification.dart b/lib/ui/home/home_notification.dart index b242d1ae..235fc437 100644 --- a/lib/ui/home/home_notification.dart +++ b/lib/ui/home/home_notification.dart @@ -46,8 +46,7 @@ class _HomeNotificationState extends State { .toList(), repeatForever: true, ), - ), - Spacer(), + ).expand(), MaterialButton( shape: StadiumBorder(), padding: EdgeInsets.symmetric(horizontal: 12.w), diff --git a/lib/ui/market/goods/goods_order_detail_page.dart b/lib/ui/market/goods/goods_order_detail_page.dart index eb5628bc..7b4d7f0f 100644 --- a/lib/ui/market/goods/goods_order_detail_page.dart +++ b/lib/ui/market/goods/goods_order_detail_page.dart @@ -81,7 +81,6 @@ class _GoodsOrderDetailPageState extends State { ), bottomNavi: BottomButton( onPressed: () async { - final cancel = BotToast.showLoading(); BaseModel baseModel = await NetUtil().post( API.market.appointment, diff --git a/lib/ui/profile/house/house_func.dart b/lib/ui/profile/house/house_func.dart index d392f999..29dd2127 100644 --- a/lib/ui/profile/house/house_func.dart +++ b/lib/ui/profile/house/house_func.dart @@ -13,7 +13,7 @@ class HouseFunc { } ///查询用户所拥有的房屋信息 - static Future get passedHouses async{ + static Future> get passedHouses async{ BaseModel model = await NetUtil().get(API.user.passedHouseList); if (!model.status!) return []; return (model.data as List).map((e) => PassedHouseListModel.fromJson(e)).toList(); diff --git a/pubspec.lock b/pubspec.lock index 211fe3b1..6c492146 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1061,6 +1061,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.0.0" + tobias: + dependency: "direct main" + description: + name: tobias + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.0" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 8fc32750..22436fb5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,9 @@ dependencies: #用户存储路径 path_provider: ^2.0.1 + #支付宝支付功能 + tobias: ^2.1.0 + #微信支付 fluwx: ^3.4.2 get: ^4.1.4 velocity_x: ^3.0.0 diff --git a/tool/_project_manage.dart b/tool/_project_manage.dart index 19bf399d..64bd0a3d 100644 --- a/tool/_project_manage.dart +++ b/tool/_project_manage.dart @@ -41,3 +41,9 @@ void gen() async { arguments: ['build'], ); } + +@Task('build runner clean') +void genClean() async { + await Pub.runAsync('build_runner', + arguments: ['build', '--delete-conflicting-outputs']); +}