11import 'dart:async' ;
2- import 'dart:convert' ;
3-
4- import 'package:collection/collection.dart' ;
52import 'package:dio/dio.dart' ;
63import 'package:flutter_openim_sdk/flutter_openim_sdk.dart' ;
74import 'package:get/get.dart' ;
85import 'package:openim_common/openim_common.dart' ;
9- import 'package:sprintf/sprintf.dart' ;
10-
11- import 'utils/api_service.dart' ;
126
137class Apis {
148 static Options get imTokenOptions => Options (headers: {'token' : DataSp .imToken});
@@ -42,21 +36,11 @@ class Apis {
4236 'verifyCode' : verificationCode,
4337 });
4438 final cert = LoginCertificate .fromJson (data! );
45- ApiService ().setToken (cert.imToken);
4639
4740 return cert;
48- } catch (e, _) {
49- final t = e as (int , String ? )? ;
50-
51- if (t == null ) {
52- Logger .print ('e:$e ' );
41+ } catch (e, s) {
42+ _catchErrorHelper (e, s);
5343
54- return Future .error (e);
55- }
56- final errCode = t.$1;
57- final errMsg = t.$2;
58- _kickoff (errCode);
59- Logger .print ('e:$errCode s:$errMsg ' );
6044 return Future .error (e);
6145 }
6246 }
@@ -95,15 +79,11 @@ class Apis {
9579 });
9680
9781 final cert = LoginCertificate .fromJson (data! );
98- ApiService ().setToken (cert.imToken);
9982
10083 return cert;
10184 } catch (e, s) {
102- final t = e as (int , String ? );
103- final errCode = t.$1;
104- final errMsg = t.$2;
105- _kickoff (errCode);
106- Logger .print ('e:$errCode s:$errMsg ' );
85+ _catchErrorHelper (e, s);
86+
10787 return Future .error (e);
10888 }
10989 }
@@ -129,11 +109,7 @@ class Apis {
129109 options: chatTokenOptions,
130110 );
131111 } catch (e, s) {
132- final t = e as (int , String ? );
133- final errCode = t.$1;
134- final errMsg = t.$2;
135- _kickoff (errCode);
136- Logger .print ('e:$errCode s:$errMsg ' );
112+ _catchErrorHelper (e, s);
137113 }
138114 }
139115
@@ -155,11 +131,8 @@ class Apis {
155131 );
156132 return true ;
157133 } catch (e, s) {
158- final t = e as (int , String ? );
159- final errCode = t.$1;
160- final errMsg = t.$2;
161- _kickoff (errCode);
162- Logger .print ('e:$errCode s:$errMsg ' );
134+ _catchErrorHelper (e, s);
135+
163136 return false ;
164137 }
165138 }
@@ -228,18 +201,15 @@ class Apis {
228201 options: chatTokenOptions,
229202 );
230203 } catch (e, s) {
231- final t = e as (int , String ? );
232- final errCode = t.$1;
233- final errMsg = t.$2;
234- _kickoff (errCode);
235- Logger .print ('e:$errCode s:$errMsg ' );
204+ _catchErrorHelper (e, s);
236205 }
237206 }
238207
239208 static Future <List <FriendInfo >> searchFriendInfo (
240209 String keyword, {
241210 int pageNumber = 1 ,
242211 int showNumber = 10 ,
212+ bool showErrorToast = true ,
243213 }) async {
244214 try {
245215 final data = await HttpUtil .post (
@@ -249,18 +219,16 @@ class Apis {
249219 'keyword' : keyword,
250220 },
251221 options: chatTokenOptions,
222+ showErrorToast: showErrorToast,
252223 );
253224 if (data['users' ] is List ) {
254225 return (data['users' ] as List ).map ((e) => FriendInfo .fromJson (e)).toList ();
255226 }
256227 return [];
257228 } catch (e, s) {
258- final t = e as (int , String ? );
259- final errCode = t.$1;
260- final errMsg = t.$2;
261- _kickoff (errCode);
262- Logger .print ('e:$errCode s:$errMsg ' );
263- return [];
229+ _catchErrorHelper (e, s);
230+
231+ rethrow ;
264232 }
265233 }
266234
@@ -284,11 +252,8 @@ class Apis {
284252 }
285253 return null ;
286254 } catch (e, s) {
287- final t = e as (int , String ? );
288- final errCode = t.$1;
289- final errMsg = t.$2;
290- _kickoff (errCode);
291- Logger .print ('e:$errCode s:$errMsg ' );
255+ _catchErrorHelper (e, s);
256+
292257 return [];
293258 }
294259 }
@@ -312,11 +277,8 @@ class Apis {
312277 }
313278 return null ;
314279 } catch (e, s) {
315- final t = e as (int , String ? );
316- final errCode = t.$1;
317- final errMsg = t.$2;
318- _kickoff (errCode);
319- Logger .print ('e:$errCode s:$errMsg ' );
280+ _catchErrorHelper (e, s);
281+
320282 return [];
321283 }
322284 }
@@ -348,7 +310,8 @@ class Apis {
348310 IMViews .showToast (StrRes .sentSuccessfully);
349311 return true ;
350312 }).catchError ((e, s) {
351- Logger .print ('e:$e s:$s ' );
313+ _catchErrorHelper (e, s);
314+
352315 return false ;
353316 });
354317 }
@@ -365,7 +328,9 @@ class Apis {
365328 final signaling = SignalingCertificate .fromJson (value)..roomID = roomID;
366329 return signaling;
367330 }).catchError ((e, s) {
368- Logger .print ('e:$e s:$s ' );
331+ _catchErrorHelper (e, s);
332+
333+ throw e;
369334 });
370335 }
371336
@@ -413,23 +378,24 @@ class Apis {
413378 return {'discoverPageURL' : Config .discoverPageURL, 'allowSendMsgNotFriend' : Config .allowSendMsgNotFriend};
414379 }
415380
416- static void _catchError (Object e, StackTrace s, {bool forceBack = true }) {
417- if (e is ApiException ) {
418- var msg = '${e .code }' .tr;
419- if (msg.isEmpty || e.code.toString () == msg) {
420- msg = e.message ?? 'Unkonw error' ;
421- } else if (e.code == 1004 ) {
422- msg = sprintf (msg, [StrRes .meeting]);
423- }
424-
425- IMViews .showToast (msg);
381+ static void _catchErrorHelper (Object e, StackTrace s) {
382+ if (e is (int , String ? )) {
383+ final errCode = e.$1;
384+ final errMsg = e.$2;
385+ _kickoff (errCode);
426386
427- if ((e.code == 10010 || e.code == 10002 ) && forceBack) {
428- DataSp .removeLoginCertificate ();
429- Get .offAllNamed ('/login' );
430- }
387+ Logger .print ('e:$errCode s:$errMsg ' );
431388 } else {
432- IMViews .showToast (e.toString ());
389+ _catchError (e, s);
390+ }
391+ }
392+
393+ static void _catchError (Object e, StackTrace s, {bool forceBack = true }) {
394+ IMViews .showToast (e.toString ());
395+
396+ if (forceBack) {
397+ DataSp .removeLoginCertificate ();
398+ Get .offAllNamed ('/login' );
433399 }
434400 }
435401}
0 commit comments