email signin
parent
81fefdaec4
commit
e71d167f3a
|
@ -40,7 +40,9 @@ Future<Map<String, dynamic>> signup(
|
|||
}
|
||||
|
||||
Future<Map<String, dynamic>> signinByUsername(
|
||||
String username, String password) async {
|
||||
String username,
|
||||
String password,
|
||||
) async {
|
||||
final formData = FormData.fromMap({
|
||||
'username': username,
|
||||
'password': password,
|
||||
|
@ -54,6 +56,23 @@ Future<Map<String, dynamic>> signinByUsername(
|
|||
return response.data;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> signinByEmail(
|
||||
String email,
|
||||
String code,
|
||||
) async {
|
||||
final formData = FormData.fromMap({
|
||||
'username': email,
|
||||
'password': code,
|
||||
});
|
||||
|
||||
Response response = await request.post(
|
||||
'/signin/email',
|
||||
data: formData,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> signinByToken() async {
|
||||
Response response = await request.get('/signin/token');
|
||||
return response.data;
|
||||
|
|
|
@ -6,6 +6,7 @@ import 'package:cached_network_image/cached_network_image.dart';
|
|||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'package:together_mobile/common/constants.dart';
|
||||
import 'package:together_mobile/database/hive_database.dart';
|
||||
import 'package:together_mobile/models/contact_model.dart';
|
||||
import 'package:together_mobile/models/init_get_it.dart';
|
||||
import 'package:together_mobile/models/route_state_model.dart';
|
||||
|
@ -206,7 +207,8 @@ class _FriendProfileScreenState extends State<FriendProfileScreen> {
|
|||
),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
onPressed: () async {
|
||||
await HiveDatabase.openMessageBox(widget.friendId);
|
||||
context.pushNamed(
|
||||
'Message',
|
||||
queryParameters: {
|
||||
|
|
|
@ -1,9 +1,19 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:cherry_toast/cherry_toast.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
|
||||
import '../../../components/common_widgets.dart';
|
||||
import 'package:together_mobile/common/constants.dart';
|
||||
import '/common/constants.dart';
|
||||
import '/request/signup_signin.dart';
|
||||
import '/components/common_widgets.dart';
|
||||
import '/database/hive_database.dart';
|
||||
import '/models/init_get_it.dart';
|
||||
import '/models/token_model.dart';
|
||||
import '/models/user_model.dart';
|
||||
|
||||
class EmailSigninBody extends StatefulWidget {
|
||||
const EmailSigninBody({super.key});
|
||||
|
@ -24,12 +34,24 @@ class _EmailSigninBodyState extends State<EmailSigninBody> {
|
|||
'code': false,
|
||||
};
|
||||
|
||||
Timer? _timer;
|
||||
int _count = 60;
|
||||
bool _isGetCode = false;
|
||||
|
||||
void _isInputError(String type, bool error) {
|
||||
setState(() {
|
||||
_isError[type] = error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
emailController.dispose();
|
||||
codeController.dispose();
|
||||
_timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
|
@ -56,6 +78,7 @@ class _EmailSigninBodyState extends State<EmailSigninBody> {
|
|||
type: 'email',
|
||||
labelText: '邮箱',
|
||||
isError: _isInputError,
|
||||
isSignup: false,
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
|
@ -78,10 +101,19 @@ class _EmailSigninBodyState extends State<EmailSigninBody> {
|
|||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {},
|
||||
child: const Text(
|
||||
'获取验证码',
|
||||
style: TextStyle(fontSize: 15.0),
|
||||
onPressed: () async {
|
||||
await _getCode(context);
|
||||
},
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor:
|
||||
_isGetCode ? kUnActivatedColor : kPrimaryColor,
|
||||
),
|
||||
child: Text(
|
||||
_isGetCode ? '已发送($_count)' : '获取验证码',
|
||||
style: TextStyle(
|
||||
color: _isGetCode ? kUnActivatedColor : kPrimaryColor,
|
||||
fontSize: 15.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
@ -92,14 +124,51 @@ class _EmailSigninBodyState extends State<EmailSigninBody> {
|
|||
height: kDefaultPadding,
|
||||
),
|
||||
CommonElevatedButton(
|
||||
onPressed: confirm,
|
||||
text: '确定',
|
||||
onPressed: _confirm,
|
||||
text: '登录',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void confirm() async {
|
||||
Future<void> _getCode(BuildContext context) async {
|
||||
if (_isGetCode) {
|
||||
_showWarningToast(context, '验证码还在有效期内,若没有收到邮件,请查看对应邮箱的垃圾箱');
|
||||
return;
|
||||
} else if (_isError['email']!) {
|
||||
_showWarningToast(context, '邮箱格式不正确');
|
||||
return;
|
||||
} else if (emailController.text.isEmpty) {
|
||||
_showWarningToast(context, '邮箱不能为空');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await askCode(emailController.text);
|
||||
} catch (e) {
|
||||
EasyLoading.showError('获取验证码失败,请稍后再试!');
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isGetCode = true;
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
setState(() {
|
||||
_count--;
|
||||
});
|
||||
|
||||
if (_count == 0) {
|
||||
timer.cancel();
|
||||
setState(() {
|
||||
_isGetCode = false;
|
||||
_count = 60;
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void _confirm() async {
|
||||
if (_isError['email']! || _isError['code']!) {
|
||||
CherryToast.error(
|
||||
title: const Text('邮箱或验证码不符合规范'),
|
||||
|
@ -113,25 +182,56 @@ class _EmailSigninBodyState extends State<EmailSigninBody> {
|
|||
if (!_isProgressShow) _isProgressShow = true;
|
||||
});
|
||||
|
||||
Map<String, dynamic>? result;
|
||||
late Map<String, dynamic> result;
|
||||
|
||||
EasyLoading.showInfo('正在登录中......');
|
||||
|
||||
if (_isProgressShow && !_isHttpSend) {
|
||||
_isHttpSend = true;
|
||||
// result = await signup();
|
||||
}
|
||||
|
||||
try {
|
||||
result = await signinByUsername(
|
||||
emailController.text,
|
||||
codeController.text,
|
||||
);
|
||||
result = await signinByEmail(emailController.text, codeController.text);
|
||||
} catch (e) {
|
||||
EasyLoading.showError('连接服务器失败,请稍后重试...');
|
||||
return;
|
||||
} finally {
|
||||
setState(() {
|
||||
if (result!['code'] == 10000) {
|
||||
_isHttpSend = false;
|
||||
_isProgressShow = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
emailController.dispose();
|
||||
codeController.dispose();
|
||||
super.dispose();
|
||||
if (result['code'] == 10200) {
|
||||
getIt<UserAccount>().init(result['data']);
|
||||
|
||||
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
|
||||
AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
|
||||
|
||||
Map<String, dynamic> res =
|
||||
await createToken(getIt.get<UserAccount>().id, androidDeviceInfo.id);
|
||||
getIt.get<Token>().init();
|
||||
getIt.get<Token>().updateToken(res['token']);
|
||||
|
||||
await HiveDatabase.init();
|
||||
EasyLoading.dismiss();
|
||||
// ignore: use_build_context_synchronously
|
||||
context.go('/chat');
|
||||
} else {
|
||||
EasyLoading.dismiss();
|
||||
// ignore: use_build_context_synchronously
|
||||
CherryToast.error(title: const Text('邮箱或验证码错误')).show(context);
|
||||
}
|
||||
}
|
||||
|
||||
void _showWarningToast(BuildContext context, String text) {
|
||||
CherryToast.warning(
|
||||
title: Text(text),
|
||||
toastDuration: const Duration(seconds: 2),
|
||||
animationDuration: const Duration(seconds: 1),
|
||||
).show(context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ class _SignupBodyState extends State<SignupBody> {
|
|||
onPressed: () {
|
||||
_confirm(context);
|
||||
},
|
||||
text: '确定',
|
||||
text: '注册',
|
||||
color: kSecondaryColor,
|
||||
),
|
||||
],
|
||||
|
|
|
@ -5,13 +5,13 @@ import 'package:go_router/go_router.dart';
|
|||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import 'package:together_mobile/database/hive_database.dart';
|
||||
import 'package:together_mobile/models/token_model.dart';
|
||||
import '../../../components/common_widgets.dart';
|
||||
import 'package:together_mobile/common/constants.dart';
|
||||
import 'package:together_mobile/request/signup_signin.dart';
|
||||
import 'package:together_mobile/models/init_get_it.dart';
|
||||
import 'package:together_mobile/models/user_model.dart';
|
||||
import '/database/hive_database.dart';
|
||||
import '/models/token_model.dart';
|
||||
import '/components/common_widgets.dart';
|
||||
import '/common/constants.dart';
|
||||
import '/request/signup_signin.dart';
|
||||
import '/models/init_get_it.dart';
|
||||
import '/models/user_model.dart';
|
||||
|
||||
class UsernameSigninBody extends StatefulWidget {
|
||||
const UsernameSigninBody({super.key});
|
||||
|
@ -143,8 +143,6 @@ class _UsernameSigninBodyState extends State<UsernameSigninBody> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (result['code'] == 10200) {
|
||||
getIt<UserAccount>().init(result['data']);
|
||||
|
||||
|
|
Loading…
Reference in New Issue