登录逻辑

This commit is contained in:
2022-10-21 14:03:48 +08:00
parent 30a9279ff1
commit 4bf03d234b
11 changed files with 154 additions and 135 deletions

BIN
assets/images/empty.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -1,16 +1,12 @@
import 'package:chat/models/user_info_model.dart';
class AuthModel {
String tokenType;
String accessToken;
String userSig;
String userID;
UserInfoModel userInfo;
String get userToken => '$tokenType $accessToken';
AuthModel({
required this.tokenType,
required this.accessToken,
required this.userID,
required this.userSig,
@@ -18,9 +14,8 @@ class AuthModel {
});
factory AuthModel.fromJson(Map<String, dynamic> json) => AuthModel(
tokenType: json['token_type'],
accessToken: json['access_token'],
userID: json['user_id'],
userID: json['user_id'].toString(),
userSig: json['user_sig'],
userInfo: UserInfoModel.fromJson(json['user_info']),
);

View File

@@ -7,14 +7,14 @@ class UserInfoModel {
required this.address,
});
int userId;
String userId;
String username;
String nickname;
String avatar;
String? nickname;
String? avatar;
String? address;
factory UserInfoModel.fromJson(Map<String, dynamic> json) => UserInfoModel(
userId: json['user_id'],
userId: json['user_id'].toString(),
username: json['username'],
nickname: json['nickname'],
avatar: json['avatar'],
@@ -22,10 +22,18 @@ class UserInfoModel {
);
factory UserInfoModel.empty() => UserInfoModel(
userId: 0,
userId: '',
username: '',
nickname: '',
avatar: '',
address: '',
);
Map<String, dynamic> toJson() => {
"userId": userId,
"username": username,
"nickname": nickname,
"avatar": avatar,
"address": address,
};
}

View File

@@ -14,7 +14,7 @@ class AuthProvider {
return AuthModel.fromJson(result);
} catch (e) {
UiTools.toast('区块链地址获取失败');
UiTools.toast('区块链地址获取失败 ${e.toString()}');
return null;
}
}

View File

@@ -25,6 +25,7 @@ class AuthService extends GetxService {
String get _userId => _box.read('userId') ?? '';
String get _userSig => _box.read('userSig') ?? '';
String get _userToken => _box.read('userToken') ?? '';
Map<String, dynamic> get _userInfo => _box.read('userInfo') ?? {};
Rx<UserInfoModel> userInfo = UserInfoModel.empty().obs;
@@ -37,6 +38,7 @@ class AuthService extends GetxService {
userSig = _userSig;
userId = _userId;
userToken = _userToken;
userInfo.value = UserInfoModel.fromJson(_userInfo);
}
}
@@ -46,11 +48,13 @@ class AuthService extends GetxService {
if (result != null) {
_box.write('userId', result.userID);
_box.write('userSig', result.userSig);
_box.write('userToken', result.userToken);
_box.write('userToken', result.accessToken);
_box.write('userInfo', result.userInfo.toJson());
userId = result.userID;
userSig = result.userSig;
userToken = result.userToken;
userToken = result.accessToken;
userInfo.value = result.userInfo;
isLogin.value = true;

View File

@@ -1,5 +1,5 @@
class HttpOptions {
static const String baseUrl = 'http://api.gl.shangkelian.cn/api/';
static const String baseUrl = 'https://zh-chat.cnskl.com/api/';
static const int connectTimeout = 15000;
static const int receiveTimeout = 15000;
}

View File

@@ -46,93 +46,95 @@ class _AuthCreatePageState extends State<AuthCreatePage>
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('备份助记词'),
actions: [
TextButton(
onPressed: () {},
child: const Text('跳过备份'),
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TabBar(
controller: _tabController,
isScrollable: true,
indicatorColor: AppColors.primary,
indicatorWeight: 5,
indicatorSize: TabBarIndicatorSize.label,
tabs: const [
Tab(
text: 'English',
),
Tab(
text: '中文',
),
],
),
const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Text(
'请务必抄下助记词,确定之后将进行校验',
style: TextStyle(
fontSize: 12,
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: const Text('备份助记词'),
actions: [
TextButton(
onPressed: () {},
child: const Text('跳过备份'),
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TabBar(
controller: _tabController,
isScrollable: true,
indicatorColor: AppColors.primary,
indicatorWeight: 5,
indicatorSize: TabBarIndicatorSize.label,
tabs: const [
Tab(
text: 'English',
),
Tab(
text: '中文',
),
],
),
const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Text(
'请务必抄下助记词,确定之后将进行校验',
style: TextStyle(
fontSize: 12,
),
),
),
),
],
),
Expanded(
child: TabBarView(
controller: _tabController,
children: [
_moArea(_englishMnemonic),
_moArea(_chineseMnemonic),
],
),
),
Container(
height: Get.height * 0.382,
padding: const EdgeInsets.all(16),
child: const Text(
'提示:请勿截图!如果有人获取您的助记词将直接获取您的资产,请抄写助记词并存放在安全的地方,我们会在下一屏幕进行校验,',
Expanded(
child: TabBarView(
controller: _tabController,
children: [
_moArea(_englishMnemonic),
_moArea(_chineseMnemonic),
],
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {
_generateMnemonic();
},
child: const Text('更换助记词'),
Container(
height: Get.height * 0.382,
padding: const EdgeInsets.all(16),
child: const Text(
'提示:请勿截图!如果有人获取您的助记词将直接获取您的资产,请抄写助记词并存放在安全的地方,我们会在下一屏幕进行校验,',
),
ElevatedButton(
onPressed: () {
var language = _tabController.index;
Get.toNamed(
AuthRoutes.createVerify,
arguments: {
'language': language == 0 ? 'english' : 'chinese',
'mnemonic':
language == 0 ? _englishMnemonic : _chineseMnemonic
},
);
},
child: const Text('开始备份'),
),
],
),
const SizedBox(
height: 16,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {
_generateMnemonic();
},
child: const Text('更换助记词'),
),
ElevatedButton(
onPressed: () {
var language = _tabController.index;
Get.toNamed(
AuthRoutes.createVerify,
arguments: {
'language': language == 0 ? 'english' : 'chinese',
'mnemonic':
language == 0 ? _englishMnemonic : _chineseMnemonic
},
);
},
child: const Text('开始备份'),
),
],
),
const SizedBox(
height: 16,
),
],
),
),
);
}

View File

@@ -37,36 +37,41 @@ class _AuthImportPageState extends State<AuthImportPage> {
appBar: AppBar(
title: const Text('导入账户'),
),
body: Column(
children: [
TextField(
controller: _editingController,
maxLines: 4,
decoration: const InputDecoration(
hintText: '请输入您的助记词',
border: OutlineInputBorder(
borderSide: BorderSide(
color: AppColors.border,
width: 0.4,
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _editingController,
maxLines: 5,
decoration: const InputDecoration(
hintText: '请输入您的助记词,以空格分割单词或汉字',
border: OutlineInputBorder(
borderSide: BorderSide(
color: AppColors.border,
width: 0.4,
),
),
),
),
),
const Text('支持导入所有遵循BIP标准生成的助记词'),
ElevatedButton(
onPressed: () async {
String? address =
HDWallet.mnemonicToAddress(_editingController.text);
if (address != null) {
var result = await AuthService.to.login(address);
if (result) {
Get.offAllNamed(AppRoutes.app);
const SizedBox(height: 16),
const Text('支持导入所有遵循BIP标准生成的助记词'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () async {
String? address =
HDWallet.mnemonicToAddress(_editingController.text);
if (address != null) {
var result = await AuthService.to.login(address);
if (result) {
Get.offAllNamed(AppRoutes.app);
}
}
}
},
child: const Text('开始导入'),
),
],
},
child: const Text('开始导入'),
),
],
),
),
);
}

View File

@@ -18,17 +18,22 @@ class _AuthPageState extends State<AuthPage> {
),
body: Column(
children: [
ElevatedButton(
onPressed: () {
Get.toNamed(AuthRoutes.create);
},
child: const Text('创建账户'),
),
ElevatedButton(
onPressed: () {
Get.toNamed(AuthRoutes.import);
},
child: const Text('导入账户'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {
Get.toNamed(AuthRoutes.create);
},
child: const Text('创建账户'),
),
ElevatedButton(
onPressed: () {
Get.toNamed(AuthRoutes.import);
},
child: const Text('导入账户'),
),
],
),
],
),

View File

@@ -49,7 +49,7 @@ class _QuickReplyBarState extends State<QuickReplyBar> {
border: QuickReplyBar._border,
focusedBorder: QuickReplyBar._border,
disabledBorder: QuickReplyBar._border,
hintText: widget.comment?.user?.nickname.isNotEmpty ?? false
hintText: widget.comment?.user?.nickname != null
? '回复:${widget.comment?.user?.nickname}'
: '评论',
filled: true,

View File

@@ -27,7 +27,7 @@ class _UserPageState extends State<UserPage> {
),
Column(
children: [
Text(_.userInfo.value.nickname),
Text(_.userInfo.value.nickname ?? ''),
Text('${_.userInfo.value.address}'),
],
),