feat: complete leave approval MVP

This commit is contained in:
selfrelease
2026-07-18 19:20:07 +08:00
parent 2105fe3bac
commit 090a7e33ce
133 changed files with 7845 additions and 100 deletions
@@ -0,0 +1,30 @@
import 'package:aioa_mobile/core/auth/authenticated_http_client.dart';
import 'package:aioa_mobile/core/config/runtime_config.dart';
import 'package:aioa_mobile/features/assistant/data/leave_progress_repository.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final leaveProgressRepositoryProvider = Provider(
(ref) => LeaveProgressRepository(
client: ref.watch(authenticatedHttpClientProvider),
baseUrl: RuntimeConfig.apiBaseUrl,
),
);
final leaveProgressProvider =
AsyncNotifierProvider<LeaveProgressController, ProgressAnswer?>(
LeaveProgressController.new,
);
class LeaveProgressController extends AsyncNotifier<ProgressAnswer?> {
String _question = '';
@override
Future<ProgressAnswer?> build() async => null;
Future<void> ask(String question, {String? selectedRequestId}) async {
if (selectedRequestId == null) _question = question.trim();
state = const AsyncLoading();
state = await AsyncValue.guard(
() => ref
.read(leaveProgressRepositoryProvider)
.ask(_question, selectedRequestId: selectedRequestId),
);
}
}
@@ -0,0 +1,88 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
class ProgressCandidate {
const ProgressCandidate({
required this.id,
required this.type,
required this.status,
required this.startsAt,
required this.endsAt,
});
final String id, type, status;
final DateTime startsAt, endsAt;
factory ProgressCandidate.fromJson(Map<String, Object?> j) =>
ProgressCandidate(
id: j['id']! as String,
type: j['type']! as String,
status: j['status']! as String,
startsAt: DateTime.parse(j['startsAt']! as String),
endsAt: DateTime.parse(j['endsAt']! as String),
);
}
class ProgressAnswer {
const ProgressAnswer({
required this.requiresSelection,
required this.candidates,
this.answer,
this.request,
this.activeTasks = const [],
this.completedTasks = const [],
this.processEnded = false,
});
final bool requiresSelection, processEnded;
final List<ProgressCandidate> candidates;
final String? answer;
final ProgressCandidate? request;
final List<String> activeTasks, completedTasks;
factory ProgressAnswer.fromJson(Map<String, Object?> j) {
final p = j['progress'] as Map<String, Object?>?;
return ProgressAnswer(
requiresSelection: j['requiresSelection']! as bool,
candidates: ((j['candidates'] as List?) ?? const [])
.map(
(e) =>
ProgressCandidate.fromJson(Map<String, Object?>.from(e as Map)),
)
.toList(),
answer: j['answer'] as String?,
request: j['request'] == null
? null
: ProgressCandidate.fromJson(
Map<String, Object?>.from(j['request']! as Map),
),
activeTasks: ((p?['activeTaskNames'] as List?) ?? const [])
.cast<String>(),
completedTasks: ((p?['completedTaskNames'] as List?) ?? const [])
.cast<String>(),
processEnded: p?['processEnded'] as bool? ?? false,
);
}
}
class LeaveProgressRepository {
LeaveProgressRepository({required this.client, required this.baseUrl});
final http.Client client;
final String baseUrl;
Future<ProgressAnswer> ask(String text, {String? selectedRequestId}) async {
final response = await client.post(
Uri.parse('$baseUrl/ai/leave-progress-answers'),
headers: const {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: jsonEncode({
'text': text,
'timezone': 'Asia/Shanghai',
'selectedRequestId': ?selectedRequestId,
}),
);
if (response.statusCode < 200 || response.statusCode >= 300) {
throw Exception('查询失败(${response.statusCode}');
}
return ProgressAnswer.fromJson(
jsonDecode(response.body) as Map<String, Object?>,
);
}
}
@@ -1,9 +1,147 @@
import 'package:aioa_mobile/features/assistant/application/leave_progress_controller.dart';
import 'package:aioa_mobile/features/assistant/data/leave_progress_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
class AssistantPage extends StatelessWidget {
class AssistantPage extends ConsumerStatefulWidget {
const AssistantPage({super.key});
@override
ConsumerState<AssistantPage> createState() => _AssistantPageState();
}
class _AssistantPageState extends ConsumerState<AssistantPage> {
final _controller = TextEditingController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) =>
const Center(child: Text('AI 助手将在下一阶段接入'));
Widget build(BuildContext context) {
final result = ref.watch(leaveProgressProvider);
return Scaffold(
appBar: AppBar(title: const Text('AI 流程助手')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
const Text('询问本人请假流程进度,AI 只读查询,不会执行审批或修改申请。'),
const SizedBox(height: 12),
TextField(
controller: _controller,
minLines: 2,
maxLines: 4,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: '例如:我最近提交的年假审批到哪一步了?',
),
),
const SizedBox(height: 10),
FilledButton.icon(
onPressed: result.isLoading
? null
: () {
if (_controller.text.trim().isNotEmpty) {
ref
.read(leaveProgressProvider.notifier)
.ask(_controller.text);
}
},
icon: const Icon(Icons.auto_awesome),
label: const Text('查询进度'),
),
const SizedBox(height: 16),
result.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (e, _) => Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Text('查询失败:$e'),
),
),
data: (data) => data == null
? const SizedBox.shrink()
: _Result(
data: data,
onSelect: (id) => ref
.read(leaveProgressProvider.notifier)
.ask('', selectedRequestId: id),
),
),
],
),
);
}
}
class _Result extends StatelessWidget {
const _Result({required this.data, required this.onSelect});
final ProgressAnswer data;
final ValueChanged<String> onSelect;
@override
Widget build(BuildContext context) {
if (data.requiresSelection) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('找到多条可能的申请,请选择:'),
...data.candidates.map(
(c) => Card(
child: ListTile(
onTap: () => onSelect(c.id),
title: Text('${_type(c.type)} · ${_status(c.status)}'),
subtitle: Text(
'${DateFormat('MM-dd HH:mm').format(c.startsAt.toLocal())}${DateFormat('MM-dd HH:mm').format(c.endsAt.toLocal())}',
),
trailing: const Icon(Icons.chevron_right),
),
),
),
],
);
}
final request = data.request!;
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${_type(request.type)} · ${_status(request.status)}',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 10),
Text(data.answer ?? ''),
if (data.activeTasks.isNotEmpty) ...[
const SizedBox(height: 12),
Text('当前节点:${data.activeTasks.join('')}'),
],
if (data.completedTasks.isNotEmpty)
Text('已完成:${data.completedTasks.join('')}'),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: () => context.push('/leave/${request.id}'),
icon: const Icon(Icons.open_in_new),
label: const Text('查看申请详情'),
),
],
),
),
);
}
static String _type(String v) =>
{'PERSONAL': '事假', 'SICK': '病假', 'ANNUAL': '年假'}[v] ?? v;
static String _status(String v) =>
{
'DRAFT': '草稿',
'PENDING': '审批中',
'APPROVED': '已通过',
'REJECTED': '已驳回',
'WITHDRAWN': '已撤回',
}[v] ??
v;
}