mirror of
https://github.com/qinglong-app/qinglong_app.git
synced 2025-10-09 16:48:19 +08:00
add task
This commit is contained in:
@@ -50,7 +50,7 @@ class _BaseStateWidgetState<T extends BaseViewModel> extends ConsumerState<BaseS
|
||||
if (viewModel.currentState == PageState.FAILED) {
|
||||
return Container(
|
||||
alignment: Alignment.center,
|
||||
child: Text("请求失败"),
|
||||
child: Text(viewModel.failReason ??""),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
|
||||
class ViewModel extends ChangeNotifier{}
|
||||
|
||||
class ViewModel extends ChangeNotifier {}
|
||||
|
||||
class BaseViewModel extends ViewModel {
|
||||
PageState currentState = PageState.START;
|
||||
String? failReason;
|
||||
|
||||
void loading({bool notify = false}) {
|
||||
currentState = PageState.LOADING;
|
||||
@@ -21,8 +20,9 @@ class BaseViewModel extends ViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
void failed({bool notify = false}) {
|
||||
void failed(String? reason, {bool notify = false}) {
|
||||
currentState = PageState.FAILED;
|
||||
failReason = reason;
|
||||
if (notify) {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
21
lib/base/http/api.dart
Normal file
21
lib/base/http/api.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:qinglong_app/base/http/http.dart';
|
||||
import 'package:qinglong_app/module/login/login_bean.dart';
|
||||
import 'package:qinglong_app/module/task/task_bean.dart';
|
||||
|
||||
import 'url.dart';
|
||||
|
||||
class Api {
|
||||
static Future<HttpResponse<LoginBean>> login(String userName, String passWord) async {
|
||||
return await Http.post<LoginBean>(
|
||||
Url.LOGIN,
|
||||
{
|
||||
"username": userName,
|
||||
"password": passWord,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static Future<HttpResponse<List<TaskBean>>> crons() async {
|
||||
return await Http.get<List<TaskBean>>(Url.TASKS, {"searchValue": ""});
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_log/dio_log.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -27,48 +30,111 @@ class Http {
|
||||
_dio?.interceptors.add(TokenInterceptor());
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> post<T>(String uri, Map<String, dynamic> json, {bool compute = true}) async {
|
||||
if (userInfoViewModel.host == null || userInfoViewModel.host!.isEmpty) {
|
||||
return HttpResponse(success: false, code: NOT_LOGIN, message: "未登录");
|
||||
}
|
||||
|
||||
static void _init() {
|
||||
if (_dio == null) {
|
||||
initDioConfig(UserInfoViewModel.getInstance().host!);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> get<T>(String uri, Map<String, String> json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.get(uri, queryParameters: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> post<T>(String uri, dynamic json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.post(uri, data: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> delete<T>(String uri, dynamic json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.delete(uri, data: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> put<T>(String uri, dynamic json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.put(uri, data: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
}
|
||||
|
||||
static HttpResponse<T> decodeResponse<T>(
|
||||
Response<dynamic> response,
|
||||
bool compute,
|
||||
) {
|
||||
late HttpResponse<T> result;
|
||||
|
||||
int code = 0;
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
if (compute) {
|
||||
try {
|
||||
if (response.data["code"] == 200) {
|
||||
try {
|
||||
if (response.data["code"] == 200) {
|
||||
if (response.data["data"] != null) {
|
||||
if (T is Void) {
|
||||
return HttpResponse<T>(
|
||||
success: true,
|
||||
code: 200,
|
||||
);
|
||||
}
|
||||
|
||||
dynamic data = response.data["data"];
|
||||
T bean = DeserializeAction.invokeJson(DeserializeAction<T>(data));
|
||||
result = HttpResponse<T>(success: true, code: 200, bean: bean);
|
||||
T t;
|
||||
if (T is String) {
|
||||
if (data is String) {
|
||||
t = data as T;
|
||||
} else {
|
||||
t = jsonEncode(data) as T;
|
||||
}
|
||||
return HttpResponse<T>(
|
||||
success: true,
|
||||
code: 200,
|
||||
bean: t,
|
||||
);
|
||||
} else {
|
||||
T bean;
|
||||
if (compute) {
|
||||
bean = DeserializeAction.invokeJson(DeserializeAction<T>(data));
|
||||
} else {
|
||||
bean = JsonConversion$Json.fromJson<T>(data);
|
||||
}
|
||||
return HttpResponse<T>(
|
||||
success: true,
|
||||
code: 200,
|
||||
bean: bean,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
result = HttpResponse<T>(success: false, code: -1000, message: "服务器返回数据异常");
|
||||
return HttpResponse<T>(
|
||||
success: false,
|
||||
code: 200,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
result = HttpResponse<T>(success: false, code: -1000, message: "json解析失败");
|
||||
} else {
|
||||
return HttpResponse<T>(
|
||||
success: false,
|
||||
code: -1000,
|
||||
message: "服务器返回数据异常",
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
return HttpResponse<T>(
|
||||
success: false,
|
||||
code: -1000,
|
||||
message: "json解析失败",
|
||||
);
|
||||
}
|
||||
} else {
|
||||
code = response.statusCode ?? 0;
|
||||
result = HttpResponse(success: false, code: code, message: response.statusMessage);
|
||||
return HttpResponse(
|
||||
success: false,
|
||||
code: code,
|
||||
message: response.statusMessage,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
class Url {
|
||||
static const LOGIN = "api/user/login";
|
||||
static const LOGIN = "/api/user/login";
|
||||
static const TASKS = "/api/crons";
|
||||
}
|
||||
|
||||
@@ -7,11 +7,15 @@ var themeProvider = ChangeNotifierProvider((ref) => ThemeViewModel());
|
||||
class ThemeViewModel extends ChangeNotifier {
|
||||
ThemeData currentTheme = lightTheme;
|
||||
|
||||
ThemeColors themeColor = LightartThemeColors();
|
||||
|
||||
void changeTheme() {
|
||||
if (currentTheme == darkTheme) {
|
||||
currentTheme = lightTheme;
|
||||
themeColor = LightartThemeColors();
|
||||
} else {
|
||||
currentTheme = darkTheme;
|
||||
themeColor = DartThemeColors();
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -23,3 +27,21 @@ ThemeData darkTheme = ThemeData.dark().copyWith(
|
||||
ThemeData lightTheme = ThemeData.light().copyWith(
|
||||
primaryColor: Color(0xFF0F77FE),
|
||||
);
|
||||
|
||||
abstract class ThemeColors {
|
||||
Color taskTitleColor();
|
||||
}
|
||||
|
||||
class LightartThemeColors extends ThemeColors {
|
||||
@override
|
||||
Color taskTitleColor() {
|
||||
return Color(0xff333333);
|
||||
}
|
||||
}
|
||||
|
||||
class DartThemeColors extends ThemeColors {
|
||||
@override
|
||||
Color taskTitleColor() {
|
||||
return Colors.white;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class UserInfoViewModel {
|
||||
|
||||
UserInfoViewModel._() {
|
||||
String userInfoJson = SpUtil.getString(sp_UserINfo);
|
||||
_host = SpUtil.getString(sp_Host);
|
||||
_host = SpUtil.getString(sp_Host,defValue: "http://49.234.59.95:5700");
|
||||
if (userInfoJson.isNotEmpty) {
|
||||
_token = userInfoJson;
|
||||
}
|
||||
@@ -34,4 +34,8 @@ class UserInfoViewModel {
|
||||
String? get token => _token;
|
||||
|
||||
String? get host => _host;
|
||||
|
||||
bool isLogined() {
|
||||
return token != null && token!.isNotEmpty;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user