294 lines
9.5 KiB
Dart
Executable File
294 lines
9.5 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:nhance_app_pwa/customAppBar/toastHelper.dart';
|
|
import 'package:nhance_app_pwa/models/environment.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'dart:convert';
|
|
|
|
class ApiService {
|
|
final BuildContext context;
|
|
String? _token;
|
|
|
|
ApiService(this.context) {
|
|
_initializeToken();
|
|
}
|
|
|
|
Future<void> _initializeToken() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
_token = prefs.getString('_postToken') ?? '';
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getActiveAndInactivePolicyDetails(
|
|
String clientId,
|
|
String empCode,
|
|
String status,
|
|
String branchID,
|
|
String mobileNo) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeeActiveOrInactivePolicy?client_id=$clientId&emp_code=$empCode&type=$status&client_branch_id=$branchID&mobile_no=$mobileNo');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getClaimsHistoryToApi(
|
|
String ticket_type_id) async {
|
|
print("getCashDepositDetailsToApi1");
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}claimView?ticket_id=$ticket_type_id');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getAdvertisementImageToApi() async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getAdvertisementImage');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getCashlessAndReimbursementToApi() async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getFEContent');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getSelfEmployeeProfileDetails(
|
|
String clientId, String empCode, String branchID) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeeProfile?emp_code=$empCode&client_id=$clientId&client_branch_id=$branchID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getTrackClaimsList(String empPrimaryId) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}/get_ticket_data?emp_id=$empPrimaryId');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getTicketListByMobile(String empMobileNo) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}ticketList?mobile=$empMobileNo');
|
|
|
|
final url1 = Uri.parse('${Environment.apiUrl}ticketList?mobile=1234567890');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getTicketConverList(String thz_id) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url1 = Uri.parse('${Environment.apiUrl}ticketList?mobile=$thz_id');
|
|
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}ticketConversationList?thz_id=$thz_id');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getWellnessLink() async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getWellnessURL');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> fetchDepartmentList(String clientId, String empCode) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}/get_ticket_type?client_id=$clientId&emp_code=$empCode');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendTicketFormDataToApi(
|
|
Map<String, dynamic> formData) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}ticketSave');
|
|
// Convert formData to Map<String, String>
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendClaimsFormDataToApi(
|
|
Map<String, dynamic> formData) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}/initiateClaim');
|
|
// Convert formData to Map<String, String>
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendClaimsMessageToApi(
|
|
Map<String, dynamic> formData) async {
|
|
print("sendClaimsMessageToApi");
|
|
final url = Uri.parse('${Environment.apiUrl}ticketConversationSave');
|
|
// final url = Uri.parse('${Environment.apiUrlTicket}/messages/create');
|
|
// Convert formData to Map<String, String>
|
|
print("sendClaimsMessageToApi1");
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
Future<void> _clearLocalStorageAndRedirect() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.clear();
|
|
// Assuming you have access to the context
|
|
ToastHelper.showErrorToast(context, 'Session Out');
|
|
Navigator.pushNamed(context, 'login');
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getBotDetails(
|
|
Map<String, dynamic> formData) async {
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getChatResponse');
|
|
// Convert formData to Map<String, String>
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
print(url);
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
// Future<Map<String, dynamic>> getBotDetails(String requestFor, is_option,
|
|
// option, returnMessage, mobileNo, policyID) async {
|
|
// print('requestFor: $requestFor');
|
|
// print('is_option: $is_option');
|
|
// print('option: $option');
|
|
// print('returnMessage: $returnMessage');
|
|
// print('mobileNo: $mobileNo');
|
|
// print('policyID: $policyID');
|
|
// if (_token == null) {
|
|
// await _initializeToken();
|
|
// }
|
|
// final url;
|
|
// if (is_option != '0') {
|
|
// url = Uri.parse(
|
|
// '${Environment.apiUrl}getChatResponse?request_for=$requestFor&is_option=$is_option&option=$option&mobile_no=$mobileNo&policy_id=$policyID');
|
|
// } else {
|
|
// url = Uri.parse(
|
|
// '${Environment.apiUrl}getChatResponse?request_for=$requestFor&is_option=$is_option&text=$returnMessage&value=$option&mobile_no=$mobileNo&policy_id=$policyID');
|
|
// }
|
|
// print(url);
|
|
//
|
|
// final headers = {
|
|
// 'Authorization': 'Bearer $_token' ?? '',
|
|
// };
|
|
// final response = await _makeGetRequest(url, headers);
|
|
// return response;
|
|
// }
|
|
|
|
Future<Map<String, dynamic>> _makeGetRequest(
|
|
Uri url, Map<String, String> headers) async {
|
|
final response = await http.get(url, headers: headers);
|
|
return _handleResponse(response);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _makePostRequest(
|
|
Uri url, Map<String, String> body, Map<String, String> headers) async {
|
|
final response = await http.post(url, headers: headers, body: body);
|
|
return _handleResponse(response);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _handleResponse(http.Response response) async {
|
|
if (response.statusCode == 200) {
|
|
return jsonDecode(response.body);
|
|
} else if (response.statusCode == 401) {
|
|
await _clearLocalStorageAndRedirect();
|
|
return {};
|
|
} else {
|
|
throw Exception('Failed to load data');
|
|
}
|
|
}
|
|
}
|