提交 6fcbd5a6 authored 作者: songchuancai's avatar songchuancai

对接登录接口

上级 a89b102e
class User {
final String username;
final String password;
final String? token;
User({required this.username, required this.password});
User({
required this.username,
required this.password,
this.token,
});
Map<String, dynamic> toJson() {
return {
'username': username,
'password': password,
'token': token,
};
}
......@@ -15,6 +21,7 @@ class User {
return User(
username: json['username'],
password: json['password'],
token: json['token'],
);
}
}
import 'package:flutter/material.dart';
import 'package:allen/models/user.dart';
import 'package:allen/services/storage_service.dart';
import 'package:allen/services/api_service.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
......@@ -24,12 +25,31 @@ class _LoginPageState extends State<LoginPage> {
return;
}
final user = User(username: username, password: password);
try {
final response = await ApiService.login(username, password);
if (response['result'] == 'success') {
final token = response['data'];
final user = User(username: username, password: password, token: token);
await StorageService.saveUser(user);
if (mounted) {
Navigator.of(context).pushReplacementNamed('/home');
}
} else {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('登录失败,请检查用户名和密码')),
);
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('登录失败,请稍后重试')),
);
}
}
}
@override
......
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
static const String baseUrl =
'https://knowledge-web.apps.iytcloud.com/console/api';
static Future<Map<String, dynamic>> login(
String email, String password) async {
final response = await http.post(
Uri.parse('$baseUrl/login/'),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: jsonEncode({
'email': email,
'password': password,
}),
);
return jsonDecode(response.body);
}
}
......@@ -132,10 +132,10 @@ packages:
dependency: "direct main"
description:
name: http
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.13.6"
version: "1.2.2"
http_parser:
dependency: transitive
description:
......
......@@ -30,13 +30,12 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^1.1.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
speech_to_text: ^6.1.1
http: ^0.13.5
flutter_tts: ^3.6.3
animate_do: ^3.0.2
shared_preferences: ^2.0.15
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论