168 lines
4.8 KiB
Dart
168 lines
4.8 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../../app.dart';
|
|
|
|
class AuthApi {
|
|
static String className = 'AuthApi';
|
|
|
|
static Future<bool> registerUser(Map<String, dynamic> params) async {
|
|
final String fnName = 'registerUser';
|
|
|
|
debugPrint('class: $className, method: $fnName, params: $params');
|
|
try {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
const String path = Constants.BASE_URL + 'customer/register';
|
|
|
|
final response = await HttpUtil().post(path, queryParameters: params);
|
|
|
|
prefs.remove(Constants.TOKEN);
|
|
prefs.setString(Constants.TOKEN, response['token']);
|
|
|
|
return true;
|
|
} on DioError catch (error) {
|
|
debugPrint('ERROR: class: $className, method: $fnName, error: $error ');
|
|
|
|
int? errCode = error.response?.statusCode;
|
|
|
|
if (errCode == 401 || errCode == 422) {
|
|
showSnack('warning'.tr, 'phone_registered'.tr, SnackType.WARNING);
|
|
}
|
|
return false;
|
|
} catch (error) {
|
|
debugPrint('registerUser error: $error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<String?> updateUser(Map<String, dynamic> params) async {
|
|
try {
|
|
const String path = Constants.BASE_URL + 'customer/profile';
|
|
|
|
final response = await HttpUtil().put(path, queryParameters: params);
|
|
return response['message'];
|
|
} catch (error) {
|
|
debugPrint('updateUser error: $error');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<bool> resetPassword(Map<String, dynamic> params) async {
|
|
final String fnName = 'resetPassword';
|
|
try {
|
|
const String path = Constants.BASE_URL + 'customer/reset_password';
|
|
|
|
debugPrint('class: $className, method: $fnName, params: $params');
|
|
final response = await HttpUtil().post(path, queryParameters: params);
|
|
|
|
debugPrint('response resetPassword $response');
|
|
|
|
return true;
|
|
} catch (error) {
|
|
debugPrint('resetPassword error: $error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<bool> updatePassword(Map<String, dynamic> params) async {
|
|
final String fnName = 'updatePassword';
|
|
try {
|
|
const String path = Constants.BASE_URL + 'customer/update_password';
|
|
debugPrint('class: $className, method: $fnName, params: $params');
|
|
|
|
final response = await HttpUtil().post(path, queryParameters: params);
|
|
|
|
debugPrint('response updatePassword $response');
|
|
return true;
|
|
} catch (error) {
|
|
debugPrint('updatePassword error: $error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<bool> login(Map<String, dynamic> params) async {
|
|
try {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
const String path = Constants.BASE_URL + 'customer/login';
|
|
|
|
final response = await HttpUtil().post(path, queryParameters: params);
|
|
|
|
prefs.remove(Constants.TOKEN);
|
|
prefs.setString(Constants.TOKEN, response['token']);
|
|
|
|
return true;
|
|
} on DioError catch (error) {
|
|
int? errCode = error.response?.statusCode;
|
|
|
|
if (errCode == 401 || errCode == 422) {
|
|
showSnack('error'.tr, 'invalid_credentials'.tr, SnackType.ERROR);
|
|
}
|
|
return false;
|
|
} catch (error) {
|
|
debugPrint('login error: $error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<UserModel?> getCurrentUser() async {
|
|
try {
|
|
const String path = Constants.BASE_URL + 'v1/customer/get';
|
|
|
|
final response = await HttpUtil().get(path: path);
|
|
|
|
return UserModel.fromJson(response['data']);
|
|
} on DioError catch (error) {
|
|
int? errCode = error.response?.statusCode;
|
|
|
|
if (errCode == 401) {
|
|
showSnack('warning'.tr, 'please_login'.tr, SnackType.WARNING);
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
debugPrint('getCurrentUser error: $error');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/* static Future<String?> getVerificationCode(String phone) async {
|
|
final String fnName = 'getVerificationCode';
|
|
|
|
try {
|
|
final Map<String, String> params = {
|
|
'phone': phone,
|
|
};
|
|
|
|
debugPrint('class: $className, method: $fnName, params: $params');
|
|
|
|
const String path = 'http://sms.digital-tps.tk/api/verify';
|
|
|
|
final response = await HttpUtil().postForm(path, data: params);
|
|
|
|
return response['verify_code'].toString();
|
|
} catch (e) {
|
|
debugPrint('ERROR: class: $className, method: $fnName, error: $e ');
|
|
return null;
|
|
}
|
|
} */
|
|
|
|
static Future<bool> deleteAccount() async {
|
|
final String fnName = 'deleteAccount';
|
|
|
|
try {
|
|
debugPrint('class: $className, method: $fnName');
|
|
const String path = Constants.BASE_URL + 'customer/delete';
|
|
|
|
await HttpUtil().delete(path);
|
|
|
|
return true;
|
|
} catch (e) {
|
|
debugPrint('ERROR: class: $className, method: $fnName, error: $e ');
|
|
return false;
|
|
}
|
|
}
|
|
}
|