user_model.dart
7.79 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:one_key/constant/constant.dart';
import 'package:one_key/models/user.dart';
import 'package:one_key/net/base_resp.dart';
import 'package:one_key/net/dio_util.dart';
import 'package:one_key/net/http_url.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/log_util.dart';
import 'package:one_key/utils/navigator_util.dart';
import 'package:one_key/utils/utils.dart';
import 'package:one_key/widgets/MyDivider.dart';
import 'package:one_key/widgets/v_empty_view.dart';
import '../app.dart';
class UserModel with ChangeNotifier {
User _user;
String _registrationId;//极光推送registrationId
String _token;//信鸽推送token
User get user => _user;
set registrationId(String registrationId)=>_registrationId=registrationId;
String get registrationId =>_registrationId;
String get token => _token;
set token(String value) {
_token = value;
}
/// 初始化 User
void initUser() {
if (App.sp.containsKey('user')) {
String s = App.sp.getString('user');
_user = User.fromJson(json.decode(s));
}
}
/// 登录
Future<String> login(BuildContext context, Map<String, String> params) async {
BaseResp<List> baseResp = await DioUtil()
.request<List>(context, Method.get, HttpUrl.app_user_login,
data: params)
.catchError((error) {
print(error.toString());
});
if (baseResp.code != Constant.status_success) {
return baseResp.msg ?? '登录失败,请检查账号密码';
} else {
Map<String, dynamic> seller = baseResp.data[0]["seller"];
Map<String, dynamic> user = baseResp.data[0]["user"];
Map<String, dynamic> userMap = {
"openid": user['id'],
"account": user['account'],
"role_name": user['role_name'],
"sellerid": seller['id'],
"sellercode": params['seller_code'],
"sellername": seller['sellername'],
"sellerphone": seller['seller_tel'],
"sellerAddr": '',
"scatter": 0,
"room": 0,
"cassette": 0,
"morning": "",
"noon": "",
"night": "",
};
_saveUserInfo(User.fromJson(userMap));
return getSellerInfo(
context,
{"seller_id": seller['id'], "seller_openid": seller['openid']},
userMap);
}
}
///获取商家信息
Future<String> getSellerInfo(BuildContext context, Map<String, String> params,
Map<String, dynamic> userMap) async {
BaseResp<Map<String, dynamic>> baseResp = await DioUtil()
.request<Map<String, dynamic>>(context, Method.get, HttpUrl.sellerinfo,
data: params)
.catchError((error) {
print(error.toString());
});
if (baseResp.code != Constant.status_success) {
return baseResp.msg ?? '登录失败,请检查账号密码';
} else {
Map<String, dynamic> seller = baseResp.data['seller'];
Map<String, dynamic> daily = baseResp.data["daily"];
Map<String, dynamic> user = Map.of(userMap);
user['sellerAddr'] = seller['real_address'] ?? seller['map_address'];
user['scatter'] = baseResp.data['scatter'];
user['room'] = baseResp.data['room'];
user['cassette'] = baseResp.data['cassette'];
user['morning'] = daily['morning'];
user['noon'] = daily['noon'];
user['night'] = daily['night'];
_saveUserInfo(User.fromJson(user));
App.sp.setBool("isLogin", true);
new JPush().resumePush();
NavigatorUtil.goHomePage(context);
return null;
}
}
///初始化推送token
userTokenInit(BuildContext context, Map<String, String> params) async {
BaseResp<dynamic> baseResp = await DioUtil()
.request<dynamic>(context, Method.get, HttpUrl.user_token_init,
data: params)
.catchError((error) {
print(error.toString());
});
if (baseResp.code == Constant.status_success) {
}
}
//版本更新弹框
showLoginOutDialog(BuildContext pageContext) {
showDialog(
context: pageContext,
builder: (context) {
Widget widget = Container(
decoration: BoxDecoration(
color: MyColours.color_ffffff,
borderRadius: BorderRadius.all(
Radius.circular(ScreenUtil().setWidth(30)))),
padding: EdgeInsets.only(top: ScreenUtil().setWidth(88)),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
child: Text(
"确定要退出登录吗?",
style: TextStyle(
fontSize: ScreenUtil().setSp(36),
color: MyColours.color_353535),
),
),
VEmptyView(66),
MyDivider(double.infinity, ScreenUtil().setWidth(1)),
Container(
height: ScreenUtil().setWidth(94),
child: Row(
children: <Widget>[
Expanded(
child: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Text(
"取消",
style: TextStyle(
fontSize: ScreenUtil().setSp(34),
color: MyColours.color_777777),
textAlign: TextAlign.center,
),
),
),
MyDivider(ScreenUtil().setWidth(1),ScreenUtil().setWidth(94)),
Expanded(
child: GestureDetector(
onTap: () {
Navigator.of(context).pop();
loginOut(pageContext, {"seller_code":user.sellercode,"account":user.account});
},
child: Text(
"确定",
style: TextStyle(
fontSize: ScreenUtil().setSp(34),
color: MyColours.color_2bb5b7),
textAlign: TextAlign.center,
),
),
)
],
),
)
],
),
);
return Center(child: UnconstrainedBox(
constrainedAxis: Axis.vertical,
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: ScreenUtil().setWidth(560)),
child: DefaultTextStyle(
style: TextStyle(decoration: TextDecoration.none),
child: widget,
)
),
),);
return AlertDialog(
content: widget,
contentPadding: EdgeInsets.zero,
);
});
}
///退出登录
loginOut(BuildContext context, Map<String, String> params) async {
BaseResp<int> baseResp = await DioUtil()
.request<int>(context, Method.get, HttpUrl.login_out, data: params)
.catchError((error) {
print(error.toString());
});
if (baseResp.code != Constant.status_success) {
Utils.showToast(baseResp.msg ?? "退出登录失败,请稍后重试");
} else {
App.sp.setBool("isLogin", false);
new JPush().stopPush();
NavigatorUtil.goLoginPage(context);
}
}
/// 保存用户信息到 sp
_saveUserInfo(User user) {
_user = user;
App.sp.setString('user', json.encode(user.toJson()));
}
}