Skip to content

Commit d84cef8

Browse files
committed
[bug] 容错高德 api 调用次数上线无法继续访问问题
Signed-off-by: xiaweizi <1012126908@qq.com>
1 parent c4ade5b commit d84cef8

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

lib/app/utils/print_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:flutter/widgets.dart';
33
typedef WeatherPrint = void Function(String message,
44
{int wrapWidth, String tag});
55

6-
const DEBUG = false;
6+
const DEBUG = true;
77

88
WeatherPrint weatherPrint = debugPrintThrottled;
99

lib/app/utils/toast.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ToastUtils {
1818

1919
static show(String msg, BuildContext context,
2020
{int duration = 1,
21-
int gravity = 0,
21+
int gravity = 2,
2222
Color backgroundColor = const Color(0xAA000000),
2323
Color textColor = Colors.white,
2424
double backgroundRadius = 20,

lib/bloc/city/city_bloc.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:flutter_dynamic_weather/app/res/weather_type.dart';
1111
import 'package:flutter_dynamic_weather/app/utils/location_util.dart';
1212
import 'package:flutter_dynamic_weather/app/utils/print_utils.dart';
1313
import 'package:flutter_dynamic_weather/app/utils/shared_preference_util.dart';
14+
import 'package:flutter_dynamic_weather/app/utils/toast.dart';
1415
import 'package:flutter_dynamic_weather/event/change_index_envent.dart';
1516
import 'package:flutter_dynamic_weather/model/city_model_entity.dart';
1617
import 'package:flutter_dynamic_weather/model/weather_model_entity.dart';
@@ -43,9 +44,11 @@ class CityBloc extends Bloc<CityEvent, CityState> {
4344
cityModel.displayedName = WeatherUtil.getCityName(cityModel);
4445
UmengAnalyticsPlugin.event(AnalyticsConstant.locatedCityName, label: "${cityModel.displayedName}");
4546
List<CityModel> cityModels = [];
46-
if (cityModel.latitude != 0.0 && cityModel.longitude != 0.0) {
47-
cityModels = await insertCityMode(cityModel);
47+
if (cityModel.latitude == 0.0 || cityModel.longitude == 0.0) {
48+
ToastUtils.show("高德 API 调用上限, 默认添加北京,请见谅", globalKey.currentContext, duration: 5);
49+
cityModel = _buildDefault();
4850
}
51+
cityModels = await insertCityMode(cityModel);
4952
weatherPrint('定位成功 location: $cityModel');
5053
eventBus.fire(MainBgChangeEvent());
5154
Navigator.of(globalKey.currentContext).pop();
@@ -100,6 +103,19 @@ class CityBloc extends Bloc<CityEvent, CityState> {
100103
}
101104
}
102105

106+
CityModel _buildDefault() {
107+
CityModel cityModel = CityModel(
108+
latitude: 39.904989,
109+
longitude: 116.405285,
110+
country: "中国",
111+
province: "北京市",
112+
district: "东城区",
113+
displayedName: "北京市",
114+
isLocated: true,
115+
);
116+
return cityModel;
117+
}
118+
103119
Future<List<CityModel>> insertCityMode(CityModel cityModel) async {
104120
weatherPrint("开始插入城市数据 $cityModel");
105121
List<CityModel> cityModels = await SPUtil.getCityModels();

lib/views/pages/home/main_app_bar.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class _MainAppBarState extends State<MainAppBar> {
9494
super.dispose();
9595
}
9696

97+
98+
9799
@override
98100
Widget build(BuildContext context) {
99101
return Container(
@@ -103,7 +105,11 @@ class _MainAppBarState extends State<MainAppBar> {
103105
IconButton(
104106
padding: EdgeInsets.only(left: 20),
105107
onPressed: () async {
106-
Navigator.of(context).pushNamed(Router.manager);
108+
if (widget.cityModels == null && widget.cityModels.isEmpty) {
109+
Navigator.of(context).pushNamed(Router.search);
110+
} else {
111+
Navigator.of(context).pushNamed(Router.manager);
112+
}
107113
},
108114
icon: Icon(
109115
Icons.add,

lib/views/pages/search/search_page.dart

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class _SearchPageState extends State<SearchPage> {
6363
});
6464
List<CityData> cityData = [];
6565
var result = await WeatherApi().searchCity(keywords);
66+
weatherPrint("search result $result");
6667
if (keywords != _keyWords) {
6768
return;
6869
}
@@ -93,6 +94,9 @@ class _SearchPageState extends State<SearchPage> {
9394
});
9495
}
9596
});
97+
} else if (result.info == "INVALID_REQUEST") {
98+
weatherPrint("高德 API 调用次数上限");
99+
ToastUtils.show("由于高德 API 调用次数上限,无法搜索,请见谅", context, duration: 5, gravity: 2);
96100
}
97101
}
98102
if (cityData.isEmpty) {
@@ -130,19 +134,37 @@ class _SearchPageState extends State<SearchPage> {
130134
}
131135
}
132136

137+
CityModel _buildDefault() {
138+
CityModel cityModel = CityModel(
139+
latitude: 39.904989,
140+
longitude: 116.405285,
141+
country: "中国",
142+
province: "北京市",
143+
district: "东城区",
144+
displayedName: "北京市",
145+
);
146+
return cityModel;
147+
}
148+
133149
Future<void> onItemClick(CityData cityData, bool fromList) async {
134150
showAppDialog();
135151
var result = await WeatherApi().reGeo(cityData.center);
152+
weatherPrint("item click regeo result: $result");
136153
if (result == null) {
137154
Navigator.of(context).pop();
138155
ToastUtils.show("添加失败请重试", context);
139156
return;
140157
}
141158
CityModel cityModel = await parseCityModel(result, cityData);
142-
if (fromList) {
143-
cityModel.displayedName = "${WeatherUtil.getCityName(cityModel)}";
159+
if (cityModel == null) {
160+
ToastUtils.show("高德 API 调用上限, 默认添加北京,请见谅", context, duration: 5);
161+
cityModel = _buildDefault();
144162
} else {
145-
cityModel.displayedName = cityData.name;
163+
if (fromList) {
164+
cityModel.displayedName = "${WeatherUtil.getCityName(cityModel)}";
165+
} else {
166+
cityModel.displayedName = cityData.name;
167+
}
146168
}
147169
BlocProvider.of<CityBloc>(context).add(InsertCityData(cityModel));
148170
await Future.delayed(Duration(milliseconds: 20));
@@ -171,6 +193,8 @@ class _SearchPageState extends State<SearchPage> {
171193
addressComponent["city"] is String) {
172194
cityModel.city = addressComponent["city"];
173195
}
196+
} else {
197+
return null;
174198
}
175199
return cityModel;
176200
}

0 commit comments

Comments
 (0)