user_dao.dart
3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'package:wow_english/common/core/user_util.dart';
import 'package:wow_english/models/user_entity.dart';
import 'package:wow_english/utils/log_util.dart';
import '../request_client.dart';
enum SmsType { login, change_passWord, stdDestroy }
class UserDao {
/// 登录
static Future<UserEntity?> login(phoneNumber, type, checkKey, checkNumber) async {
var params = {'phoneNum': phoneNumber, 'type': type, checkKey: checkNumber};
var data = await requestClient.post<UserEntity>(
Apis.login,
data: params,
);
if (data != null) {
UserUtil.saveUser(data);
// 由于userInfo接口不会返回token,所以这里需要再次保存一下token
final token = data.token;
//登录成功后zip一下getUserInfo,因为进入首页需要的信息在userinfo里,保证进入首页数据是最新的
data = await getUserInfo();
data?.token = token;
UserUtil.saveUser(data);
}
return data;
}
/// 登出
static Future logout() async {
var result = await requestClient.post(Apis.logout);
UserUtil.logout();
return result;
}
/// 注销账号
static Future deleteAccount() async {
var result = await requestClient.put(Apis.deleteAccount);
UserUtil.logout();
return result;
}
/// 发送验证码
/// [msgType] 消息类型 登录:login, 更改密码:change_passWord, 账号注销:stdDestroy
static Future sendCode(phoneNumber, {smsType = 'login'}) async {
final params = {'phoneNum': phoneNumber, 'smsType': smsType};
await requestClient.post(Apis.sendSmsCode, data: params);
}
/// 设置密码,初始化密码
/// [password] 密码
static Future initPassword(String password) async {
final params = {'password': password};
await requestClient.post(Apis.initPassword, data: params);
}
/// 修改密码
/// [phoneNum] 手机号
/// [password] 密码
/// [smsCode] 短信验证码
static Future changePassword(String phoneNum, String password, String smsCode) async {
final params = {'phoneNum': phoneNum, 'password': password, 'code': smsCode};
await requestClient.post(Apis.changePassword, data: params);
}
/// 忘记密码
/// [phoneNum] 手机号
/// [password] 密码
/// [smsCode] 短信验证码
static Future resetPassword(String phoneNum, String password, String smsCode) async {
final params = {'phoneNum': phoneNum, 'password': password, 'code': smsCode};
await requestClient.post(Apis.resetPassword, data: params);
}
/// 验证短信验证码
/// [msgType] 消息类型 登录:login, 更改密码:change_passWord, 账号注销:stdDestroy
static Future checkSmsCode(String phoneNum, String smsCode, String msgType) async {
final params = {'phoneNum': phoneNum, 'code': smsCode, 'msgType': msgType};
await requestClient.post(Apis.checkSmsCode, data: params);
}
/// 获取用户信息
static Future<UserEntity?> getUserInfo() async {
return await requestClient.get(Apis.getUserInfo);
}
/// 更新用户信息,返回即成功,无body
static Future updateUserInfo(UserEntity userEntity) async {
Log.d('updateUserInfo, userEntity: $userEntity');
return await requestClient.put(Apis.setUserInfo, data: userEntity.toUpdateJson());
}
/// 更新用户信息指定字段
static Future updateUserInfoField({String? name, int? age, int? gender, String? avatarUrl}) async {
Log.d('name=$name, age=$age, gender=$gender, avatarUrl=$avatarUrl');
final Map<String, dynamic> data = <String, dynamic>{};
if (name != null) {
data['name'] = name;
}
if (age != null) {
data['age'] = age;
}
if (gender != null) {
data['gender'] = gender;
}
if (avatarUrl != null) {
data['avatarUrl'] = avatarUrl;
}
return await requestClient.put(Apis.setUserInfo, data: data);
}
}