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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio_log/overlay_draggable_button.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:qinglong_app/base/theme.dart';
|
||||
import 'package:qinglong_app/module/login/login_page.dart';
|
||||
import 'package:qinglong_app/utils/sp_utils.dart';
|
||||
|
||||
import 'base/routes.dart';
|
||||
import 'base/userinfo_viewmodel.dart';
|
||||
@@ -15,9 +17,9 @@ late UserInfoViewModel userInfoViewModel;
|
||||
|
||||
var logger = Logger();
|
||||
|
||||
void main() {
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await SpUtil.getInstance();
|
||||
userInfoViewModel = UserInfoViewModel.getInstance();
|
||||
runApp(
|
||||
ProviderScope(
|
||||
@@ -48,8 +50,13 @@ class MyApp extends ConsumerWidget {
|
||||
onGenerateRoute: (setting) {
|
||||
return Routes.generateRoute(setting);
|
||||
},
|
||||
// home: ref.read<UserInfoViewModel>(userInfoProvider).userInfoBean != null ? const HomePage() : LoginPage(),
|
||||
home: LoginPage(),
|
||||
home: Builder(
|
||||
builder: (context) {
|
||||
showDebugBtn(context, btnColor: Colors.blue);
|
||||
return userInfoViewModel.isLogined() ? const HomePage() : LoginPage();
|
||||
},
|
||||
),
|
||||
// home: LoginPage(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
showDebugBtn(context, btnColor: Colors.blue);
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:qinglong_app/base/base_viewmodel.dart';
|
||||
import 'package:qinglong_app/base/http/api.dart';
|
||||
import 'package:qinglong_app/base/http/http.dart';
|
||||
import 'package:qinglong_app/base/http/url.dart';
|
||||
import 'package:qinglong_app/main.dart';
|
||||
@@ -19,13 +20,7 @@ class LoginViewModel extends ViewModel {
|
||||
msg = "";
|
||||
|
||||
notifyListeners();
|
||||
HttpResponse<LoginBean> response = await Http.post<LoginBean>(
|
||||
Url.LOGIN,
|
||||
{
|
||||
"username": userName,
|
||||
"password": password,
|
||||
},
|
||||
);
|
||||
HttpResponse<LoginBean> response = await Api.login(userName, password);
|
||||
|
||||
if (response.success) {
|
||||
userInfoViewModel.updateToken(response.bean?.token ?? "");
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:json_conversion_annotation/json_conversion_annotation.dart';
|
||||
|
||||
@JsonConversion()
|
||||
@@ -7,14 +9,14 @@ class TaskBean {
|
||||
String? schedule;
|
||||
bool? saved;
|
||||
String? sId;
|
||||
int? created;
|
||||
num? created;
|
||||
int? status;
|
||||
String? timestamp;
|
||||
int? isSystem;
|
||||
int? isDisabled;
|
||||
String? logPath;
|
||||
int? isPinned;
|
||||
int? lastExecutionTime;
|
||||
num? lastExecutionTime;
|
||||
int? lastRunningTime;
|
||||
String? pid;
|
||||
|
||||
@@ -36,21 +38,26 @@ class TaskBean {
|
||||
this.pid});
|
||||
|
||||
TaskBean.fromJson(Map<String, dynamic> json) {
|
||||
name = json['name'];
|
||||
command = json['command'];
|
||||
schedule = json['schedule'];
|
||||
saved = json['saved'];
|
||||
sId = json['_id'];
|
||||
created = json['created'];
|
||||
status = json['status'];
|
||||
timestamp = json['timestamp'];
|
||||
isSystem = json['isSystem'];
|
||||
isDisabled = json['isDisabled'];
|
||||
logPath = json['log_path'];
|
||||
isPinned = json['isPinned'];
|
||||
lastExecutionTime = json['last_execution_time'];
|
||||
lastRunningTime = json['last_running_time'];
|
||||
pid = json['pid'];
|
||||
try {
|
||||
name = json['name'].toString();
|
||||
command = json['command'].toString();
|
||||
schedule = json['schedule'].toString();
|
||||
saved = json['saved'];
|
||||
sId = json['_id'].toString();
|
||||
created = json['created'];
|
||||
status = json['status'];
|
||||
timestamp = json['timestamp'].toString();
|
||||
isSystem = json['isSystem'];
|
||||
isDisabled = json['isDisabled'];
|
||||
logPath = json['log_path'].toString();
|
||||
isPinned = json['isPinned'];
|
||||
lastExecutionTime = json['last_execution_time'];
|
||||
lastRunningTime = json['last_running_time'];
|
||||
pid = json['pid'].toString();
|
||||
} catch (e) {
|
||||
print(jsonEncode(json));
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
|
||||
@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
import 'package:qinglong_app/base/base_state_widget.dart';
|
||||
import 'package:qinglong_app/base/theme.dart';
|
||||
import 'package:qinglong_app/module/task/task_bean.dart';
|
||||
import 'package:qinglong_app/module/task/task_viewmodel.dart';
|
||||
|
||||
class TaskPage extends StatefulWidget {
|
||||
@@ -19,18 +21,18 @@ class _TaskPageState extends State<TaskPage> {
|
||||
builder: (context, model, child) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
return Future.delayed(Duration(seconds: 1), () {});
|
||||
return model.loadData(false);
|
||||
},
|
||||
child: ListView.separated(
|
||||
padding: EdgeInsets.symmetric(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 15,
|
||||
),
|
||||
itemBuilder: (context, index) {
|
||||
return TaskItemCell();
|
||||
return TaskItemCell(model.list[index]);
|
||||
},
|
||||
itemCount: model.list.length,
|
||||
separatorBuilder: (BuildContext context, int index) {
|
||||
return SizedBox(
|
||||
return const SizedBox(
|
||||
height: 10,
|
||||
);
|
||||
},
|
||||
@@ -45,11 +47,13 @@ class _TaskPageState extends State<TaskPage> {
|
||||
}
|
||||
}
|
||||
|
||||
class TaskItemCell extends StatelessWidget {
|
||||
const TaskItemCell({Key? key}) : super(key: key);
|
||||
class TaskItemCell extends ConsumerWidget {
|
||||
final TaskBean bean;
|
||||
|
||||
const TaskItemCell(this.bean, {Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return Slidable(
|
||||
key: const ValueKey(0),
|
||||
startActionPane: ActionPane(
|
||||
@@ -98,34 +102,49 @@ class TaskItemCell extends StatelessWidget {
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: const [
|
||||
children: [
|
||||
Text(
|
||||
"东东农场",
|
||||
bean.name ?? "",
|
||||
maxLines: 1,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w500,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
color: bean.isDisabled == 1 ? Color(0xffF85152) : ref.watch(themeProvider).themeColor.taskTitleColor(),
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
const SizedBox(
|
||||
width: 5,
|
||||
),
|
||||
SizedBox(
|
||||
width: 15,
|
||||
height: 15,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
bean.status == 1
|
||||
? const SizedBox.shrink()
|
||||
: const SizedBox(
|
||||
width: 15,
|
||||
height: 15,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
Text(
|
||||
"${bean.lastRunningTime ?? "-"}",
|
||||
maxLines: 1,
|
||||
style: const TextStyle(
|
||||
overflow: TextOverflow.ellipsis,
|
||||
color: Color(0xff999999),
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
Spacer(),
|
||||
Text("2020/12/12 12:23"),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 5,
|
||||
),
|
||||
const Text(
|
||||
"12/12/12",
|
||||
style: TextStyle(
|
||||
Text(
|
||||
bean.schedule ?? "",
|
||||
maxLines: 1,
|
||||
style: const TextStyle(
|
||||
overflow: TextOverflow.ellipsis,
|
||||
color: Color(0xff999999),
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,31 +1,37 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:qinglong_app/base/base_viewmodel.dart';
|
||||
import 'package:qinglong_app/base/http/api.dart';
|
||||
import 'package:qinglong_app/base/http/http.dart';
|
||||
import 'package:qinglong_app/module/task/task_bean.dart';
|
||||
|
||||
var taskProvider = ChangeNotifierProvider((ref) => TaskViewModel());
|
||||
|
||||
class TaskViewModel extends BaseViewModel {
|
||||
List<String> list = [];
|
||||
List<TaskBean> list = [];
|
||||
|
||||
void loadData() {
|
||||
loading(notify: true);
|
||||
Future<void> loadData([isLoading = true]) async {
|
||||
if (isLoading) {
|
||||
loading(notify: true);
|
||||
}
|
||||
|
||||
HttpResponse<List<TaskBean>> result = await Api.crons();
|
||||
|
||||
if (result.success && result.bean != null) {
|
||||
list.clear();
|
||||
list.addAll(result.bean!);
|
||||
|
||||
list.sort((a, b) {
|
||||
return a.isDisabled! - b.isDisabled!;
|
||||
});
|
||||
|
||||
list.sort((a, b) {
|
||||
return a.status! - b.status!;
|
||||
});
|
||||
|
||||
Future.delayed(Duration(seconds: 2), () {
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
list.add("sdfsf");
|
||||
success();
|
||||
});
|
||||
} else {
|
||||
list.clear();
|
||||
failed(result.message, notify: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
lib/utils/extension.dart
Normal file
12
lib/utils/extension.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
extension ContextExt on BuildContext {
|
||||
T read<T>(ProviderBase<T> provider) {
|
||||
return (this as WidgetRef).read<T>(provider);
|
||||
}
|
||||
|
||||
T watch<T>(ProviderBase<T> provider) {
|
||||
return (this as WidgetRef).watch<T>(provider);
|
||||
}
|
||||
}
|
||||
@@ -16,4 +16,7 @@ import 'package:qinglong_app/module/login/login_viewmodel.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
testWidgets("", (_){
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user