From 1a2ebdb20bfea26d16c9e5ca7e2350929316e81b Mon Sep 17 00:00:00 2001 From: fmk Date: Sat, 9 Oct 2021 10:27:00 +0800 Subject: [PATCH 1/2] safety --- lib/main_page.dart | 27 ++++++++++++++------------ lib/retrofit/min_api.dart | 4 ++-- lib/retrofit/retrofit_api.dart | 4 ++-- lib/store/store_order.dart | 24 ++++++++++++++++++++++- lib/view_widget/login_tips_dialog.dart | 10 +++++----- 5 files changed, 47 insertions(+), 22 deletions(-) diff --git a/lib/main_page.dart b/lib/main_page.dart index b939c58b..5a432a99 100644 --- a/lib/main_page.dart +++ b/lib/main_page.dart @@ -105,13 +105,15 @@ class _MainPage extends State with WidgetsBindingObserver { if (Platform.isAndroid) { xgFlutterPlugin.getXgAndroidApi().addNativeEventHandler( - MyNativeEventHandler( - (String title, String message, String customContent, int type) { - print("xgPushClickAction2: $customContent"); - SharedPreferences.getInstance().then((value) { - value.setString("pushData", customContent); - }); - })); + MyNativeEventHandler( + (String title, String message, String customContent, int type) { + print("xgPushClickAction2: $customContent"); + SharedPreferences.getInstance().then((value) { + value.setString("pushData", customContent); + }); + }, + ), + ); } /// @typed: 1文章 2活动 3店铺 4积分商品 5订单 @@ -147,8 +149,7 @@ class _MainPage extends State with WidgetsBindingObserver { }, xgPushDidBindWithIdentifier: (event) async { print("xgPushDidBindWithIdentifier: ${event.toString()}"); return event; - } - ); + }); } pushRoute() async { @@ -203,9 +204,11 @@ class _MainPage extends State with WidgetsBindingObserver { SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); if (sharedPreferences.getString("token") == null || sharedPreferences.getString("token") == "") return; - BaseData baseDate = await ApiService(Dio(), - context: context, token: sharedPreferences.getString('token')) - .queryInfo(); + BaseData baseDate = await ApiService( + Dio(), + context: context, + token: sharedPreferences.getString('token'), + ).queryInfo().catchError((onError) {}); sharedPreferences.setString('user', jsonEncode(baseDate.data)); String mobile = baseDate.data.phone; diff --git a/lib/retrofit/min_api.dart b/lib/retrofit/min_api.dart index b6ad97de..d062a919 100644 --- a/lib/retrofit/min_api.dart +++ b/lib/retrofit/min_api.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/generated/l10n.dart'; +import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/view_widget/login_tips_dialog.dart'; import 'package:retrofit/retrofit.dart'; @@ -107,6 +108,5 @@ abstract class MinApiService { //剩余部分 print(msg); } - - + } \ No newline at end of file diff --git a/lib/retrofit/retrofit_api.dart b/lib/retrofit/retrofit_api.dart index a3fe779f..24a4ee97 100644 --- a/lib/retrofit/retrofit_api.dart +++ b/lib/retrofit/retrofit_api.dart @@ -274,11 +274,11 @@ abstract class ApiService { @GET("/home/brand") Future queryHomeBrand(); - ///小程序会员登录 + /// 小程序会员登录 @GET("/auth/mini/login/{storeId}") Future minLogin(@Path("storeId") String storeId); - /// + /// 各种消费记录 @POST("/member/listBill") Future>> queryBillInfo( @Body() Map param); diff --git a/lib/store/store_order.dart b/lib/store/store_order.dart index 975305de..2e41f84b 100644 --- a/lib/store/store_order.dart +++ b/lib/store/store_order.dart @@ -8,6 +8,8 @@ import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/activity.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/store_info.dart'; +import 'package:huixiang/retrofit/data/user_entity.dart'; +import 'package:huixiang/retrofit/min_api.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/store/store_view/store_activity.dart'; import 'package:huixiang/store/store_view/store_info.dart'; @@ -38,6 +40,7 @@ class _StoreOrderPage extends State with TickerProviderStateMixin /*, AutomaticKeepAliveClientMixin */ { TabController tabcontroller; ApiService apiService; + MinApiService minService; StoreInfo storeInfo; RefreshController refreshController; List _widgetOptions; @@ -63,9 +66,29 @@ class _StoreOrderPage extends State ), ]; + minLogin(); + queryStoreInfo(); } + minLogin() { + apiService.minLogin(storeInfo.id) + .catchError((onError){}).then((baseData) { + if (baseData != null && baseData.isSuccess) { + UserEntity userEntity = UserEntity.fromJson(baseData.data); + String minToken = userEntity.token; + SharedPreferences.getInstance().then((value) => { + value.setString('minToken', minToken), + }); + minService = MinApiService( + Dio(), + context: context, + token: minToken, + ); + } + }); + } + queryStoreInfo() async { final SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService( @@ -73,7 +96,6 @@ class _StoreOrderPage extends State context: context, token: value.getString('token'), ); - BaseData baseData = await apiService .queryStoreInfo(widget.arguments["id"]) .catchError((error) { diff --git a/lib/view_widget/login_tips_dialog.dart b/lib/view_widget/login_tips_dialog.dart index f3b6e43f..9feeb194 100644 --- a/lib/view_widget/login_tips_dialog.dart +++ b/lib/view_widget/login_tips_dialog.dart @@ -5,7 +5,6 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'login_tips.dart'; class LoginTipsDialog { - static LoginTipsDialog _instance; factory LoginTipsDialog() { @@ -33,8 +32,10 @@ class LoginTipsDialog { value.setString("mobile", ""); value.setString("nick", ""); }); - Navigator.of(context).pushNamed('/router/login_page', - arguments: {"login": "login"}); + Navigator.of(context).pushNamed( + '/router/login_page', + arguments: {"login": "login"}, + ); }, ), onDismiss: () { @@ -43,5 +44,4 @@ class LoginTipsDialog { clickBgDismissTemp: false, ); } - -} \ No newline at end of file +} From 6201ef03a4e9c9888e12ecf6729468a4fc15e332 Mon Sep 17 00:00:00 2001 From: fmk Date: Sat, 9 Oct 2021 11:05:52 +0800 Subject: [PATCH 2/2] safety --- lib/login/login_page.dart | 19 ++++++++++--------- lib/retrofit/min_api.dart | 8 ++------ lib/retrofit/retrofit_api.dart | 5 ++++- lib/store/store_order.dart | 9 ++++++--- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/login/login_page.dart b/lib/login/login_page.dart index 63c291b1..10072fce 100644 --- a/lib/login/login_page.dart +++ b/lib/login/login_page.dart @@ -83,6 +83,7 @@ class _MyLoginPageState extends State with TickerProviderStateMixin { double offsetBtn = 0; double rota = 1.04; Image image; + buildImageInfo(BuildContext context) async { image = Image.asset("assets/image/laoban.png"); double screenWidth = MediaQuery.of(this.context).size.width; @@ -94,8 +95,7 @@ class _MyLoginPageState extends State with TickerProviderStateMixin { if ((width / screenWidth) < rota) { rota = (screenWidth * 1.04) / width; } - if (mounted) - setState(() {}); + if (mounted) setState(() {}); })); } @@ -129,9 +129,9 @@ class _MyLoginPageState extends State with TickerProviderStateMixin { changeAlpha = (changeAlpha > 255 ? 255 : changeAlpha); changeAlpha = (changeAlpha < 0 ? 0 : changeAlpha); alpha = (initAlpha + - scrollController.offset / - (height - chaHeight) * - (255 - initAlpha)) + scrollController.offset / + (height - chaHeight) * + (255 - initAlpha)) .toInt(); alpha = (alpha > 255 ? 255 : alpha); alpha = (alpha < 0 ? 0 : alpha); @@ -308,8 +308,10 @@ class _MyLoginPageState extends State with TickerProviderStateMixin { saveUserJson(value.data); eventBus.fire(EventType(3)); - Navigator.of(context) - .pushNamedAndRemoveUntil('/router/main_page', (route) => false); + Navigator.of(context).pushNamedAndRemoveUntil( + '/router/main_page', + (route) => false, + ); } else { SmartDialog.showToast("${value.msg}", alignment: Alignment.center); } @@ -379,10 +381,9 @@ class _MyLoginPageState extends State with TickerProviderStateMixin { @override Widget build(BuildContext context) { - try { buildImageInfo(this.context); - } catch(ex) {} + } catch (ex) {} if (animatedContainer != null && !animatedContainer.isAnimating) { animatedContainer.forward(); diff --git a/lib/retrofit/min_api.dart b/lib/retrofit/min_api.dart index d062a919..64514542 100644 --- a/lib/retrofit/min_api.dart +++ b/lib/retrofit/min_api.dart @@ -97,16 +97,12 @@ abstract class MinApiService { } static void p(String msg) { - //因为String的length是字符数量不是字节数量所以为了防止中文字符过多, - // 把4*1024的MAX字节打印长度改为1000字符数 int maxStrLength = 900; - //大于1000时 while (msg.length > maxStrLength) { debugPrint(msg.substring(0, maxStrLength)); msg = msg.substring(maxStrLength); } - //剩余部分 - print(msg); + debugPrint(msg); } - + } \ No newline at end of file diff --git a/lib/retrofit/retrofit_api.dart b/lib/retrofit/retrofit_api.dart index 24a4ee97..47280305 100644 --- a/lib/retrofit/retrofit_api.dart +++ b/lib/retrofit/retrofit_api.dart @@ -89,7 +89,10 @@ abstract class ApiService { EasyLoading.dismiss(); } debugPrint("code = ${response.statusCode}"); - p(jsonEncode(response.data)); + // p(jsonEncode(response.data)); + + debugPrint(jsonEncode(response.data), wrapWidth: response.data.toString().length); + Map map = response.data; if (map["code"] != 0) { EasyLoading.dismiss(); diff --git a/lib/store/store_order.dart b/lib/store/store_order.dart index c5dd59d4..bbe9a348 100644 --- a/lib/store/store_order.dart +++ b/lib/store/store_order.dart @@ -65,13 +65,12 @@ class _StoreOrderPage extends State ), ]; - minLogin(); - queryStoreInfo(); } + /// 小程序登录 minLogin() { - apiService.minLogin(storeInfo.id) + apiService.minLogin(widget.arguments["id"]) .catchError((onError){}).then((baseData) { if (baseData != null && baseData.isSuccess) { UserEntity userEntity = UserEntity.fromJson(baseData.data); @@ -88,6 +87,7 @@ class _StoreOrderPage extends State }); } + /// 查询店铺信息 queryStoreInfo() async { final SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService( @@ -95,6 +95,9 @@ class _StoreOrderPage extends State context: context, token: value.getString('token'), ); + + minLogin(); + BaseData baseData = await apiService .queryStoreInfo(widget.arguments["id"]) .catchError((error) {