mirror of
https://github.com/asjqkkkk/flutter-todos.git
synced 2025-08-06 14:19:24 +08:00
105 lines
2.6 KiB
Dart
105 lines
2.6 KiB
Dart
import 'package:todo_list/json/all_beans.dart';
|
|
import 'package:todo_list/json/weather_bean.dart';
|
|
|
|
import 'api_strategy.dart';
|
|
export 'package:dio/dio.dart';
|
|
|
|
class ApiService {
|
|
factory ApiService() => _getInstance();
|
|
|
|
static ApiService get instance => _getInstance();
|
|
static ApiService _instance;
|
|
|
|
static final int SUCCEED = 0;
|
|
static final int FAILED = 1;
|
|
|
|
ApiService._internal() {
|
|
//初始化
|
|
}
|
|
|
|
static ApiService _getInstance() {
|
|
if (_instance == null) {
|
|
_instance = new ApiService._internal();
|
|
}
|
|
return _instance;
|
|
}
|
|
|
|
//获取图片
|
|
void getPhotos({
|
|
Function success,
|
|
Function failed,
|
|
Function error,
|
|
Map<String, String> params,
|
|
CancelToken token,
|
|
}) {
|
|
ApiStrategy.getInstance().get(
|
|
"https://api.unsplash.com/photos/",
|
|
(data) {
|
|
if (data.toString().contains("errors")) {
|
|
failed(data);
|
|
} else {
|
|
List<PhotoBean> beans = PhotoBean.fromMapList(data);
|
|
success(beans);
|
|
}
|
|
},
|
|
params: params,
|
|
errorCallBack: (errorMessage) {
|
|
error(errorMessage);
|
|
},
|
|
token: token,
|
|
);
|
|
}
|
|
|
|
//提交建议
|
|
void postSuggestion(Map<String, String> params, Function success,
|
|
Function failed, Function error, CancelToken token) {
|
|
postCommon(params, success, failed, error, "fUser/suggestion", token);
|
|
}
|
|
|
|
//通用的请求
|
|
void postCommon(Map<String, String> params, Function success, Function failed,
|
|
Function error, String url, CancelToken token) {
|
|
ApiStrategy.getInstance().post(
|
|
url,
|
|
(data) {
|
|
CommonBean commonBean = CommonBean.fromMap(data);
|
|
if (commonBean.status == 0) {
|
|
success(commonBean);
|
|
} else {
|
|
failed(commonBean);
|
|
}
|
|
},
|
|
params: params,
|
|
errorCallBack: (errorMessage) {
|
|
error(errorMessage);
|
|
},
|
|
token: token);
|
|
}
|
|
|
|
//天气获取
|
|
void getWeatherNow(
|
|
Function success,
|
|
Function failed,
|
|
Function error,
|
|
Map<String, String> params,
|
|
CancelToken token,
|
|
) {
|
|
ApiStrategy.getInstance().get(
|
|
"https://free-api.heweather.com/s6/weather/now",
|
|
(data) {
|
|
WeatherBean weatherBean = WeatherBean.fromMap(data);
|
|
if (weatherBean.HeWeather6[weatherBean.HeWeather6.length - 1].status == "ok") {
|
|
success(weatherBean);
|
|
} else {
|
|
failed(weatherBean);
|
|
}
|
|
},
|
|
params: params,
|
|
errorCallBack: (errorMessage) {
|
|
error(errorMessage);
|
|
},
|
|
token: token,
|
|
);
|
|
}
|
|
}
|