登录逻辑

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

View File

@@ -7,14 +7,14 @@ class UserInfoModel {
required this.address, required this.address,
}); });
int userId; String userId;
String username; String username;
String nickname; String? nickname;
String avatar; String? avatar;
String? address; String? address;
factory UserInfoModel.fromJson(Map<String, dynamic> json) => UserInfoModel( factory UserInfoModel.fromJson(Map<String, dynamic> json) => UserInfoModel(
userId: json['user_id'], userId: json['user_id'].toString(),
username: json['username'], username: json['username'],
nickname: json['nickname'], nickname: json['nickname'],
avatar: json['avatar'], avatar: json['avatar'],
@@ -22,10 +22,18 @@ class UserInfoModel {
); );
factory UserInfoModel.empty() => UserInfoModel( factory UserInfoModel.empty() => UserInfoModel(
userId: 0, userId: '',
username: '', username: '',
nickname: '', nickname: '',
avatar: '', avatar: '',
address: '', 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); return AuthModel.fromJson(result);
} catch (e) { } catch (e) {
UiTools.toast('区块链地址获取失败'); UiTools.toast('区块链地址获取失败 ${e.toString()}');
return null; return null;
} }
} }

View File

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

View File

@@ -1,5 +1,5 @@
class HttpOptions { 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 connectTimeout = 15000;
static const int receiveTimeout = 15000; static const int receiveTimeout = 15000;
} }

View File

@@ -46,93 +46,95 @@ class _AuthCreatePageState extends State<AuthCreatePage>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return SafeArea(
appBar: AppBar( child: Scaffold(
title: const Text('备份助记词'), appBar: AppBar(
actions: [ title: const Text('备份助记词'),
TextButton( actions: [
onPressed: () {}, TextButton(
child: const Text('跳过备份'), onPressed: () {},
), child: const Text('跳过备份'),
], ),
), ],
body: Column( ),
crossAxisAlignment: CrossAxisAlignment.start, body: Column(
children: [ crossAxisAlignment: CrossAxisAlignment.start,
Row( children: [
mainAxisAlignment: MainAxisAlignment.spaceBetween, Row(
children: [ mainAxisAlignment: MainAxisAlignment.spaceBetween,
TabBar( children: [
controller: _tabController, TabBar(
isScrollable: true, controller: _tabController,
indicatorColor: AppColors.primary, isScrollable: true,
indicatorWeight: 5, indicatorColor: AppColors.primary,
indicatorSize: TabBarIndicatorSize.label, indicatorWeight: 5,
tabs: const [ indicatorSize: TabBarIndicatorSize.label,
Tab( tabs: const [
text: 'English', Tab(
), text: 'English',
Tab( ),
text: '中文', Tab(
), text: '中文',
], ),
), ],
const Padding( ),
padding: EdgeInsets.only(right: 8.0), const Padding(
child: Text( padding: EdgeInsets.only(right: 8.0),
'请务必抄下助记词,确定之后将进行校验', child: Text(
style: TextStyle( '请务必抄下助记词,确定之后将进行校验',
fontSize: 12, style: TextStyle(
fontSize: 12,
),
), ),
), ),
),
],
),
Expanded(
child: TabBarView(
controller: _tabController,
children: [
_moArea(_englishMnemonic),
_moArea(_chineseMnemonic),
], ],
), ),
), Expanded(
Container( child: TabBarView(
height: Get.height * 0.382, controller: _tabController,
padding: const EdgeInsets.all(16), children: [
child: const Text( _moArea(_englishMnemonic),
'提示:请勿截图!如果有人获取您的助记词将直接获取您的资产,请抄写助记词并存放在安全的地方,我们会在下一屏幕进行校验,', _moArea(_chineseMnemonic),
],
),
), ),
), Container(
Row( height: Get.height * 0.382,
mainAxisAlignment: MainAxisAlignment.spaceAround, padding: const EdgeInsets.all(16),
children: [ child: const Text(
ElevatedButton( '提示:请勿截图!如果有人获取您的助记词将直接获取您的资产,请抄写助记词并存放在安全的地方,我们会在下一屏幕进行校验,',
onPressed: () {
_generateMnemonic();
},
child: const Text('更换助记词'),
), ),
ElevatedButton( ),
onPressed: () { Row(
var language = _tabController.index; mainAxisAlignment: MainAxisAlignment.spaceAround,
Get.toNamed( children: [
AuthRoutes.createVerify, ElevatedButton(
arguments: { onPressed: () {
'language': language == 0 ? 'english' : 'chinese', _generateMnemonic();
'mnemonic': },
language == 0 ? _englishMnemonic : _chineseMnemonic child: const Text('更换助记词'),
}, ),
); ElevatedButton(
}, onPressed: () {
child: const Text('开始备份'), var language = _tabController.index;
), Get.toNamed(
], AuthRoutes.createVerify,
), arguments: {
const SizedBox( 'language': language == 0 ? 'english' : 'chinese',
height: 16, '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( appBar: AppBar(
title: const Text('导入账户'), title: const Text('导入账户'),
), ),
body: Column( body: Padding(
children: [ padding: const EdgeInsets.all(16.0),
TextField( child: Column(
controller: _editingController, children: [
maxLines: 4, TextField(
decoration: const InputDecoration( controller: _editingController,
hintText: '请输入您的助记词', maxLines: 5,
border: OutlineInputBorder( decoration: const InputDecoration(
borderSide: BorderSide( hintText: '请输入您的助记词,以空格分割单词或汉字',
color: AppColors.border, border: OutlineInputBorder(
width: 0.4, borderSide: BorderSide(
color: AppColors.border,
width: 0.4,
),
), ),
), ),
), ),
), const SizedBox(height: 16),
const Text('支持导入所有遵循BIP标准生成的助记词'), const Text('支持导入所有遵循BIP标准生成的助记词'),
ElevatedButton( const SizedBox(height: 16),
onPressed: () async { ElevatedButton(
String? address = onPressed: () async {
HDWallet.mnemonicToAddress(_editingController.text); String? address =
if (address != null) { HDWallet.mnemonicToAddress(_editingController.text);
var result = await AuthService.to.login(address); if (address != null) {
if (result) { var result = await AuthService.to.login(address);
Get.offAllNamed(AppRoutes.app); 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( body: Column(
children: [ children: [
ElevatedButton( Row(
onPressed: () { mainAxisAlignment: MainAxisAlignment.spaceAround,
Get.toNamed(AuthRoutes.create); children: [
}, ElevatedButton(
child: const Text('创建账户'), onPressed: () {
), Get.toNamed(AuthRoutes.create);
ElevatedButton( },
onPressed: () { child: const Text('创建账户'),
Get.toNamed(AuthRoutes.import); ),
}, ElevatedButton(
child: const Text('导入账户'), onPressed: () {
Get.toNamed(AuthRoutes.import);
},
child: const Text('导入账户'),
),
],
), ),
], ],
), ),

View File

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

View File

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