message_model.dart
7.46 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
229
230
231
232
233
234
235
236
237
238
239
240
241
import 'package:flutter/material.dart';
import 'package:one_key/constant/constant.dart';
import 'package:one_key/constant/error.dart';
import 'package:one_key/constant/state.dart';
import 'package:one_key/models/message.dart';
import 'package:one_key/models/new_unmessage_model.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/utils/utils.dart';
class MessageModel extends ChangeNotifier {
List<NewUnmessage> messages = []; //消息
PageState pageStateUn = PageState.Loading; //未处理
PageState pageState = PageState.Loading; //已处理
// 未处理消息总数
int onlineTotal = 0; //在线消息
int inlineTotal = 0; //内部消息
int orderTotal = 0; //提前点餐
int checkTotal = 0; //在线买单
int allUnNum = 0; //全部未读
initData() {
messages = []; //消息
pageStateUn = PageState.Loading; //未处理
pageState = PageState.Loading; //已处理
}
//获取已处理消息
getMessages(BuildContext context, Map<String, String> params,
{showLoading = true}) async {
if (showLoading) {
pageState = PageState.Loading;
messages = [];
notifyListeners();
}
BaseResp<List> baseResp = await DioUtil()
.request<List>(context, Method.get, HttpUrl.new_get_message_list,
data: params, showLoading: false)
.catchError((error) {
print(error.toString());
if (error is MyError) {
switch (error.type) {
case 1:
pageState = PageState.ConnectivityError;
break;
case 2:
pageState = PageState.ConnectivityTimeOut;
break;
default:
pageState = PageState.ConnectivityError;
break;
}
}
notifyListeners();
});
if (baseResp != null && baseResp.code == Constant.status_success) {
if (params['offset'] == "0") {
messages = baseResp.data.map<NewUnmessage>((value) {
return NewUnmessage.fromJson(value);
}).toList();
} else {
messages.addAll(baseResp.data.map<NewUnmessage>((value) {
return NewUnmessage.fromJson(value);
}).toList());
}
// if (params["type_id"] == "10") {
// //在线预订
// messages.removeWhere((message) {
// if (message.message_type.toString() == "10") {
// //在线预订
// if (message.status == "4") {
// //已取消
// return true;
// }
// return false;
// } else if (message.message_type.toString() == "11") {
// //取消在线预订
// return false;
// }
// return true;
// });
// }
if (messages.isEmpty) {
pageState = PageState.Empty;
} else {
pageState = PageState.Success;
}
notifyListeners();
}
}
//获取未处理消息数目
getNewGetUntreatedMsgNum(BuildContext context, Map<String, String> params,
{showLoading = true}) async {
if (showLoading) {
pageStateUn = PageState.Loading;
messages = [];
notifyListeners();
}
BaseResp<dynamic> baseResp = await DioUtil()
.request<dynamic>(
context, Method.get, HttpUrl.new_get_untreated_msg_list,
data: params, showLoading: false)
.catchError((error) {
print(error.toString());
PageState pageStateTemp;
if (error is MyError) {
switch (error.type) {
case 1:
pageStateUn = PageState.ConnectivityError;
break;
case 2:
pageStateUn = PageState.ConnectivityTimeOut;
break;
default:
pageStateUn = PageState.ConnectivityError;
break;
}
}
notifyListeners();
});
if (baseResp != null && baseResp.code == Constant.status_success) {
allUnNum = baseResp.data['count'];
notifyListeners();
}
}
//获取未处理消息和数目
getNewGetUntreatedMsgList(BuildContext context, Map<String, String> params,
{showLoading = true}) async {
if (showLoading) {
pageStateUn = PageState.Loading;
messages = [];
notifyListeners();
}
BaseResp<dynamic> baseResp = await DioUtil()
.request<dynamic>(
context, Method.get, HttpUrl.new_get_untreated_msg_list,
data: params, showLoading: false)
.catchError((error) {
print(error.toString());
PageState pageStateTemp;
if (error is MyError) {
switch (error.type) {
case 1:
pageStateUn = PageState.ConnectivityError;
break;
case 2:
pageStateUn = PageState.ConnectivityTimeOut;
break;
default:
pageStateUn = PageState.ConnectivityError;
break;
}
}
notifyListeners();
});
if (baseResp != null && baseResp.code == Constant.status_success) {
allUnNum = baseResp.data['count'];
if (params['offset'] == "0") {
messages = baseResp.data['list'].map<NewUnmessage>((value) {
return NewUnmessage.fromJson(value);
}).toList();
} else {
messages.addAll(baseResp.data['list'].map<NewUnmessage>((value) {
return NewUnmessage.fromJson(value);
}).toList());
}
// if(params["type_id"]=="10"){//在线预订
// messages.removeWhere((message) {
// if (message.message_type.toString() == "10") {//在线预订
// if (message.status == "4") {//已取消
// return true;
// }
// return false;
// } else if (message.message_type.toString() == "11") {//取消在线预订
// return false;
// }
// return true;
// });
// }
if (messages.isEmpty) {
//未处理
pageStateUn = PageState.Empty;
} else {
//未处理
pageStateUn = PageState.Success;
}
notifyListeners();
}
}
//获取未处理消息数目
getUnMsgNum(BuildContext context, Map<String, String> params) async {
BaseResp<Map<String, dynamic>> baseResp = await DioUtil()
.request<Map<String, dynamic>>(
context, Method.get, HttpUrl.get_message_num,
data: params, showLoading: false)
.catchError((error) {
print(error.toString());
});
if (baseResp != null && baseResp.code == Constant.status_success) {
onlineTotal = int.parse(baseResp.data['online_total'].toString());
inlineTotal = int.parse(baseResp.data['inline_total'].toString());
orderTotal = int.parse(baseResp.data['order_total'].toString());
checkTotal = int.parse(baseResp.data['check_total'].toString());
notifyListeners();
}
}
//设置消息状态
setMessageStatus(BuildContext context, Map<String, String> params) async {
BaseResp<dynamic> baseResp = await DioUtil()
.request<dynamic>(
context, Method.get, HttpUrl.push_msg_set_message_status,
data: params, showLoading: false)
.catchError((error) {
print(error.toString());
});
if (baseResp.code == Constant.status_success) {
} else {
Utils.showToast(baseResp.msg);
}
}
//根据消息id移除消息
removeUntreatedMessageByMid(String mid) {
messages.removeWhere((element) => element.id == mid);
if (messages.length == 0) {
pageStateUn = PageState.Empty;
}
notifyListeners();
}
}