api_auth_provider.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:dio/dio.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:telnow_mobile_new/src/injector/injector.dart';
  7. import 'package:telnow_mobile_new/src/model/login/login_body.dart';
  8. import 'package:telnow_mobile_new/src/model/refreshtoken/refresh_token_body.dart';
  9. import 'package:telnow_mobile_new/src/model/token/token.dart';
  10. import 'package:telnow_mobile_new/src/layouts/components/template.dart';
  11. import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';
  12. import 'package:telnow_mobile_new/src/utils/ui_service.dart';
  13. import 'package:telnow_mobile_new/src/utils/dio_logging_interceptors.dart';
  14. import 'package:telnow_mobile_new/src/utils/U.dart';
  15. import 'package:easy_localization/easy_localization.dart';
  16. import 'package:http/http.dart' as http;
  17. const String host = '';
  18. // const String host = 'bridge3.telmessenger.com';
  19. // const String host = '192.168.100.19:8080';
  20. // const String host = '192.168.100.18:8080';
  21. class ApiAuthProvider {
  22. final Dio _dio = Dio();
  23. final SharedPreferencesManager _sharedPreferencesManager = locator<SharedPreferencesManager>();
  24. final String displayVersion = '4.0.17'; //versi aplikasi untuk di tampilkan
  25. final int currentVersion = 40; //versi aplikasi yang digunakan untuk pengecekan versi
  26. final String buildNumber = '2539.01';
  27. // final String companyName = '999';
  28. // final String _baseUrl = 'http://139.162.7.140:9090/';
  29. // final String _baseUrl = 'http://192.168.100.14:8080/'; //irma
  30. // final String _baseUrl = 'http://192.168.100.68:8080/'; //mita
  31. // final String _baseUrl = 'http://192.168.100.121:8080/'; //abi
  32. // String companyCode = '001';
  33. late String baseUrl = 'https://$host/';
  34. // final String baseUrl = 'http://$host/';
  35. final String clientId = 'inf-G52G4op8N8';
  36. final String clientSecret = '3JskYu5zxlXRDv6g';
  37. // final String clientIdWeb = 'web-apHca0ncOX';
  38. // final String clientSecretWeb = '1qeIwW8Wu9AF4DRF';
  39. bool isDebug = false;
  40. ApiAuthProvider() {
  41. // print('ApiAuthProvider called');
  42. // print(U.getAccessCode());
  43. // isDebug = _sharedPreferencesManager.getBool(SharedPreferencesManager.keyIsDebug);
  44. // if (_sharedPreferencesManager
  45. // .isKeyExists(SharedPreferencesManager.keyAccessCode)) {
  46. // _dio.options.baseUrl = baseUrl + U.decryptAESCryptoJS(U.getAccessCode(), U.passphrase);
  47. // // _dio.options.baseUrl = baseUrl + U.getAccessCode();
  48. // _dio.interceptors.add(DioLoggingInterceptors(_dio));
  49. // }
  50. init();
  51. }
  52. init(){
  53. // print('ApiAuthProvider called');
  54. // print("U.getAccessCode() ==> ${U.getAccessCode()}");
  55. isDebug = _sharedPreferencesManager.getBool(SharedPreferencesManager.keyIsDebug)??false;
  56. if (_sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyAccessCode)! && _sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyBaseUrl)!) {
  57. baseUrl = U.decodeBase64Url(U.getBaseUrl()!);
  58. _dio.options.baseUrl = baseUrl + U.decodeBase64Url(Uri.decodeComponent(U.getAccessCode()!));
  59. // _dio.options.baseUrl = baseUrl + U.getAccessCode();
  60. _dio.interceptors.add(DioLoggingInterceptors(_dio));
  61. }
  62. }
  63. Future<Token> loginUser(LoginBody loginBody) async {
  64. try {
  65. // print("======base url==========");
  66. // print(baseUrl + U.decryptAESCryptoJS(U.getAccessCode(), U.passphrase));
  67. var loginData = loginBody.toJson();
  68. var strData = "";
  69. var i = 0;
  70. loginData.forEach((key, value) {
  71. strData = '$strData${i == 0 ? '' : '&'}$key=${Uri.encodeComponent(value)}';
  72. i++;
  73. });
  74. // print(strData);
  75. final response = await _dio.post('/oauth/token?$strData',
  76. options: Options(
  77. headers: {
  78. 'Accept': 'application/json',
  79. 'Authorization': 'Basic ${base64Encode(
  80. utf8.encode('$clientId:$clientSecret'),
  81. )}'
  82. },
  83. ));
  84. return Token.fromJson(response.data);
  85. } on DioException catch (error) {
  86. // print(error.response);
  87. if (error.response == null) {
  88. try {
  89. final result = await InternetAddress.lookup('google.com');
  90. if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  91. return Token.withError('errorConnection'.tr());
  92. }
  93. } on SocketException catch (_) {
  94. return Token.withError('noInternet'.tr());
  95. }
  96. } else if (error.response?.statusCode == 400) {
  97. if (error.response?.data['error_description'] == 'User account has expired') return Token.withError('expAccount'.tr());
  98. return Token.withError('invalidLogin'.tr());
  99. } else if (error.response?.statusCode == 401) {
  100. if (error.response?.data['error_description'] ==
  101. 'Invalid phone number') {
  102. return Token.withError('invalidPhone'.tr());
  103. }
  104. return Token.withError('expAccount'.tr());
  105. } else if (error.response!.statusCode! >= 500) {
  106. return Token.withError('errorConnection'.tr());
  107. } else {
  108. return Token.withError('errorServer'.tr());
  109. }
  110. return Token.withError('$error');
  111. }
  112. }
  113. Future<Token> refreshAuth(RefreshTokenBody refreshTokenBody) async {
  114. try {
  115. // print(refreshTokenBody.toJson());
  116. var refreshData = refreshTokenBody.toJson();
  117. var strData = "";
  118. var i = 0;
  119. refreshData.forEach((key, value) {
  120. strData = '$strData${i == 0 ? '' : '&'}$key=${Uri.encodeComponent(value)}';
  121. i++;
  122. });
  123. // print(strData);
  124. final response = await _dio.post(
  125. '/oauth/token?$strData',
  126. options: Options(
  127. headers: {
  128. 'Accept': 'application/json',
  129. 'Authorization': 'Basic ${base64Encode(
  130. utf8.encode('$clientId:$clientSecret'),
  131. )}',
  132. },
  133. ),
  134. );
  135. return Token.fromJson(response.data);
  136. } on DioException catch (error) {
  137. // print(error.response.statusCode);
  138. return Token.withError('$error');
  139. } catch(error){
  140. return Token.withError('$error');
  141. }
  142. }
  143. Future<dynamic> getJsonData(String path, var params, context,
  144. {bool secondCheck = false}) async {
  145. try {
  146. Response<String> response = await _dio.getUri(
  147. Uri(path: path, queryParameters: params),
  148. options: Options(
  149. headers: {
  150. 'Accept': 'application/json',
  151. 'requirestoken': true,
  152. },
  153. ),
  154. );
  155. return json.decode(response.data!);
  156. } on DioException catch (error) {
  157. debugPrint(error.response.toString());
  158. if (error.response == null) {
  159. try {
  160. final result = await InternetAddress.lookup('google.com');
  161. if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  162. handlingError(context, 1); //error server
  163. }
  164. } on SocketException catch (_) {
  165. // handlingError(context, ApiError.NO_INTERNET); //no internet
  166. }
  167. } else if (error.response!.statusCode! >= 500) {
  168. if (secondCheck) {
  169. // print('secondCheck');
  170. handlingError(context, 2); //error connection
  171. } else {
  172. await Future.delayed(Duration(milliseconds: 200));
  173. return getData(path, params, secondCheck: true);
  174. }
  175. //error server
  176. } else if (error.response?.statusCode == 401) {
  177. handlingError(context, 3); //error auth
  178. } else {
  179. if (secondCheck) {
  180. // print('secondCheck');
  181. handlingError(context, 2); //error connection
  182. } else {
  183. await Future.delayed(Duration(milliseconds: 200));
  184. return getData(path, params, secondCheck: true);
  185. }
  186. }
  187. return Future.error(error);
  188. } catch (error) {
  189. debugPrint(error.toString());
  190. handlingError(context, 1);
  191. return Future.error(error);
  192. }
  193. }
  194. Future getData(String path, var params, {bool secondCheck = false}) async {
  195. try {
  196. Response<String> response = await _dio.getUri(
  197. Uri(path: path, queryParameters: params),
  198. options: Options(
  199. headers: {
  200. 'requirestoken': true,
  201. },
  202. ),
  203. );
  204. return json.decode(response.data!);
  205. } on DioException catch (error) {
  206. bool isOpen = UIService.isCurrentRouteInactive;
  207. if (error.response == null) {
  208. try {
  209. final result = await InternetAddress.lookup('google.com');
  210. if (!isOpen && result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  211. UIService.handlingError(ErrorType.noInternet); //error server
  212. }
  213. } on SocketException catch (_) {
  214. }
  215. } else if (!isOpen && error.response!.statusCode! >= 500) {
  216. UIService.handlingError(ErrorType.serverError); //error server
  217. } else if (!isOpen && error.response?.statusCode == 401) {
  218. UIService.handlingError(ErrorType.invalidAccount); //error auth
  219. } else {
  220. if(!isOpen){
  221. if (secondCheck) {
  222. UIService.handlingError(ErrorType.connectionError); //error connection
  223. } else {
  224. return getData(path, params, secondCheck: true);
  225. }
  226. }
  227. }
  228. return null;
  229. }
  230. }
  231. Future postData(String path, var params, var data) async {
  232. try {
  233. Response response = await _dio.postUri(
  234. Uri(path: path, queryParameters: params),
  235. data: data,
  236. options: Options(
  237. headers: {
  238. 'Accept': 'application/json',
  239. 'requirestoken': true,
  240. },
  241. ),
  242. );
  243. return response.data;
  244. } on DioException catch (error) {
  245. debugPrint('Error post: ${error.toString()}');
  246. if (error.response == null) {
  247. try {
  248. final result = await InternetAddress.lookup('google.com');
  249. if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  250. UIService.showError('errorConnection'.tr());
  251. // showError(context, 'errorConnection'.tr());
  252. }
  253. } on SocketException catch (_) {
  254. UIService.showError('noInternet'.tr());
  255. // showError(context, 'noInternet'.tr());
  256. }
  257. } else if (error.response!.statusCode! >= 500) {
  258. UIService.showError('errorConnection'.tr());
  259. // showError(context, 'errorConnection'.tr());
  260. } else if (error.response?.statusCode == 401) {
  261. UIService.handlingError(ErrorType.invalidAccount);
  262. // handlingError(context, 3); //error auth
  263. } else if (error.response?.statusCode == 422) {
  264. if(error.response?.data['message'] == 'Worktime did not found'){
  265. UIService.showError('notFoundWorktime'.tr());
  266. // showError(context, 'notFoundWorktime'.tr());
  267. } else if(error.response?.data['message'] == 'Cant send broadcast message, you have not permission.'){
  268. UIService.showError('broadcastPermission'.tr());
  269. // showError(context, 'broadcastPermission'.tr());
  270. } else if(error.response?.data['message'] == 'Cant send broadcast message, you have not permission.'){
  271. UIService.showError('broadcastPermission'.tr());
  272. // showError(context, 'broadcastPermission'.tr());
  273. } else if(error.response?.data['message'] == 'request did not match informant rights'){
  274. UIService.showError('idNotMatch'.tr().replaceAll("#ID", data['user_id']));
  275. // showError(context, 'idNotMatch'.tr().replaceAll("#ID", data['user_id']));
  276. } else if(error.response?.data['message'] == 'Informant user id not found.' ||
  277. error.response?.data['message'] == 'invalid user informant requested'
  278. ){
  279. UIService.showError('idNotFound'.tr());
  280. // showError(context, 'idNotFound'.tr());
  281. } else if(error.response?.data['message'] == 'You are not registered as receptionist or room attendant.'){
  282. UIService.showError('informantNotRegistered'.tr());
  283. // showError(context, 'informantNotRegistered'.tr());
  284. } else if(error.response?.data['message'] == 'invalid parent ticket'){
  285. UIService.showError('invalidParentTicket'.tr());
  286. // showError(context, 'invalidParentTicket'.tr());
  287. } else {
  288. UIService.showError(error.response?.data['message']);
  289. // showError(context, error.response?.data['message']);
  290. }
  291. } else {
  292. UIService.showError('errorServer'.tr());
  293. // showError(context, 'errorServer'.tr());
  294. }
  295. return null;
  296. }
  297. }
  298. Future postDataNoAuth(String path) async {
  299. try {
  300. // print(path);
  301. Response response = await _dio.postUri(Uri(path: path));
  302. return response.data;
  303. } on DioException catch (error) {
  304. debugPrint(error.response.toString());
  305. return null;
  306. }
  307. }
  308. Future patchData(String path, var data, {var params}) async {
  309. try {
  310. Response response = await _dio.patchUri(
  311. Uri(path: path, queryParameters: params),
  312. data: data,
  313. options: Options(
  314. headers: {
  315. 'Accept': 'application/json',
  316. 'requirestoken': true,
  317. },
  318. ),
  319. );
  320. return response.data;
  321. } on DioException catch (error) {
  322. if (error.response == null) {
  323. try {
  324. final result = await InternetAddress.lookup('google.com');
  325. if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  326. UIService.showError('errorConnection'.tr());
  327. }
  328. } on SocketException catch (_) {
  329. UIService.showError('noInternet'.tr());
  330. }
  331. } else if (error.response!.statusCode! >= 500) {
  332. UIService.showError('errorConnection'.tr());
  333. } else if (error.response?.statusCode == 401) {
  334. UIService.handlingError(ErrorType.invalidAccount); //error auth
  335. } else if (error.response?.statusCode == 422) {
  336. if (error.response?.data['message'] == 'Old Password Not Match.') {
  337. UIService.showError('wrongOldPass'.tr());
  338. } else if (error.response?.data['message'] == 'New password already in used.') {
  339. UIService.showError('alreadyUsePass'.tr());
  340. } else {
  341. UIService.showError(error.response?.data['message']);
  342. }
  343. } else {
  344. UIService.showError('errorServer'.tr());
  345. }
  346. return null;
  347. }
  348. }
  349. Future<dynamic> getJsonDataNoAuth(String path, {int n = 0}) async {
  350. // print('sini');
  351. var thisUrl = U.decodeBase64Url(U.getBaseUrl()!) + U.decodeBase64Url(Uri.decodeComponent(U.getAccessCode()!));
  352. // print("url : ${thisUrl + path}");
  353. try{
  354. var response = await http.get(Uri.parse(thisUrl + path)).timeout(Duration(seconds: 5));
  355. // print("data: ${response.body}");
  356. if(response.statusCode == 200){
  357. return Future.value(json.decode(response.body));
  358. }
  359. else{
  360. return Future.error(json.decode(response.body));
  361. }
  362. } on TimeoutException catch (error) {
  363. // print("err timeout men");
  364. // Fluttertoast.showToast(msg: 'invalid_bridge'.tr());
  365. return Future.error(error);
  366. } catch (error) {
  367. // print("err catch: $error");
  368. if (n < 3) {
  369. return getJsonDataNoAuth(path, n: n+1);
  370. } else {
  371. return Future.error(error);
  372. }
  373. }
  374. // try {
  375. // Response<String> response = await _dio.getUri(Uri(path: path));
  376. // return json.decode(response.data!);
  377. // } catch (error) {
  378. // if (n < 3) {
  379. // print("retry $n");
  380. // return getJsonDataNoAuth(path, n: n+1);
  381. // } else {
  382. // print("return error: after $n trial");
  383. // if (path.contains("/api/license")) {
  384. // U.clearAccessCode();
  385. // }
  386. // return Future.error(error);
  387. // }
  388. // }
  389. }
  390. Future getDataNoAuth(String path, {int n = 0}) async {
  391. try {
  392. Response<String> response = await _dio.getUri(Uri(path: path));
  393. return json.decode(response.data!);
  394. } catch (error) {
  395. // print('trien $n times!');
  396. if (n < 3) {
  397. getDataNoAuth(path, n: n+1);
  398. } else {
  399. // print("error after trial $n times: ${error.response}");
  400. return isDebug ? Future.error(error) : null;
  401. }
  402. }
  403. }
  404. Future downloadImage(String path, String savePath) async {
  405. try {
  406. var response = _dio.download(path, savePath);
  407. return response;
  408. } catch (e) {
  409. return e;
  410. }
  411. }
  412. String getServiceAsset(String key){
  413. return '${_dio.options.baseUrl}/assets/lotties/$key';
  414. }
  415. }