Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // UserService.swift
- // isamtech
- //
- // Created by Diego Robles on 3/16/16.
- // Copyright © 2016 Diego Robles. All rights reserved.
- //
- import Foundation
- import Alamofire
- import SwiftyJSON
- class UserService: NSObject, IUserService {
- fileprivate var _user: User!
- fileprivate var _contacts: [Contact]!
- override init(){
- self._user = User()
- self._contacts = [Contact]()
- }
- func logIn(_ ilogin: String, ipassword: String, remember: Bool, completion: @escaping ((_ result :[String : AnyObject])->Void)) -> Void
- {
- let url = URL(string: "http://libs.samtech.cl/movil/SesionUsuario.asp")
- let parameters: [String : AnyObject] = ["ilogin" : ilogin as AnyObject, "ipassword" : ipassword as AnyObject, "app" : "Isamtech" as AnyObject]
- var msg = "Credenciales no válidas, intente de nuevo."
- var data : [String : AnyObject] = ["user" : "" as AnyObject, "vehicle" : "" as AnyObject]
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "message" : msg as AnyObject, "data": data as AnyObject]
- let url2 = URL(string:"http://libs.samtech.cl/movil/InsertaDeviceToken.asp")
- let parameters2: [String : AnyObject] = ["ilogin" : ilogin as AnyObject, "ipassword" : ipassword as AnyObject, "app" : "Isamtech" as AnyObject, "deviceToken" : LibraryAPI.sharedInstance.getDeviceToken() as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON
- {
- response in
- if response.result.isSuccess {
- let json = JSON(response.result.value!)
- print(json)
- Alamofire.request(url2!, method: .post, parameters: parameters2)
- if let users = json["users"].arrayObject {
- if users.count > 0 {
- for user_ in users{
- let user = user_ as AnyObject
- if user.value(forKey: "mensaje") as! String == "1" {
- self._user.ilogin = ilogin
- self._user.ipassword = ipassword
- self._user.type = user.value(forKey: "tipo_usuario")
- as! String == "1" ? 1 : 0
- self._user.state = user.value(forKey: "estado")
- as! String == "1" ? true : false
- self._user.hasSpeed = user.value(forKey: "contro_vel")
- as! String == "1" ? true : false
- self._user.contaUso = user.value(forKey: "conta_uso")
- as! String == "1" ? 1 : 0
- self._user.isDemo = user.value(forKey: "demo") as!
- String == "1" ? true : false
- self._user.hasNotifications = user.value(forKey: "notifi")
- as! String == "1" ? true : false
- self._user.total = Int(user.value(forKey: "total") as! String)
- self._user.isMasivo = user.value(forKey: "usuario") as! String
- == "MASIVO" ? true : false
- self._user.remember = remember
- data.updateValue(self._user, forKey: "user")
- if (self._user.total == 1){
- let vehicle: Vehicle = Vehicle(idGPS: user.value(forKey: "gps") as! String, plate: user.value(forKey: "patente") as! String, state: user.value(forKey: "estado") as! String == "1" ? true : false, name: "Vehicle", speed: user.value(forKey: "contro_vel") as! String, hasIgnition: false)
- vehicle.cu_state = user.value(forKey: "conta_uso") as! String == "1" ? true: false
- data.updateValue(vehicle, forKey: "vehicle")
- LibraryAPI.sharedInstance.setVehicle(vehicle)
- }
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- resultAPI.updateValue(data as AnyObject, forKey: "data")
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- }
- }
- }
- }
- else{
- msg = "Error en la conexión. Intente de nuevo"
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func requestPassword(_ ilogin: String){
- let url = URL(string: "http://libs.samtech.cl/movil/RecuperaContrasena.asp?")
- let parameters: [String : AnyObject] = ["ilogin" : ilogin as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{response in
- if response.result.isSuccess {
- if let jsonResponse = JSON(response.result.value!)["contrasena"]
- .arrayObject{
- for response_ in jsonResponse{
- let response = response_ as AnyObject
- let message = response.value(forKey: "mensaje") as! String
- NotificationCenter.default.post(
- name: Foundation.Notification.Name(rawValue: "requestPasswordResponse"),
- object: self, userInfo: ["mensaje" : message])
- }
- }
- } else {
- print(response.result.error)
- }
- }
- }
- func setCurrentUser(_ user: User){
- self._user = user
- }
- func getCurrentUser() -> User{
- return self._user
- }
- func sos(_ user: User, lat: String, lon: String){
- let url = URL(string: "http://libs.samtech.cl/movil/AlarmaSOS.asp?")
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject, "ipassword": user.ipassword as AnyObject,
- "latitud": lat as AnyObject, "longitud": lon as AnyObject]
- var message: String = String()
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{response in
- if response.result.isSuccess{
- if let jsonResponse = JSON(response.result.value!)["SOS"].arrayObject {
- for response_ in jsonResponse {
- let response = response_ as AnyObject
- message = response.value(forKey: "mensaje") as! String
- }
- NotificationCenter.default.post(
- name: Foundation.Notification.Name(rawValue: "getSosResponse"), object: self, userInfo:
- ["mensaje": message])
- }
- } else {
- }
- }
- }
- func getContacts(){
- let url = URL(string: "http://libs.samtech.cl/movil/contacto.asp?")
- let parameters: [String : AnyObject] = ["ilogin": LibraryAPI.sharedInstance.getCurrentUser().ilogin as AnyObject, "ipassword": LibraryAPI.sharedInstance.getCurrentUser().ipassword as AnyObject,
- "Sw": "1" as AnyObject, "id_cont": "" as AnyObject, "nombre": "" as AnyObject, "telefono": "" as AnyObject, "mail": "" as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{response in
- if response.result.isSuccess{
- if let jsonResponse = JSON(response.result.value!)["lista"].arrayObject {
- for contact_ in jsonResponse {
- let contact = contact_ as AnyObject
- let contactDTO: Contact = Contact(
- id: contact.value(forKey: "ID_cont") as! String,
- name: contact.value(forKey: "Nombre") as! String,
- phone: contact.value(forKey: "Telefono") as! String,
- email: contact.value(forKey: "eMail") as! String)
- self._contacts.append(contactDTO)
- }
- NotificationCenter.default.post(
- name: Foundation.Notification.Name(rawValue: "getContactsResponse"), object: self, userInfo:
- ["contacts": self._contacts])
- }
- } else {
- }
- }
- }
- func getContacts(_ completion: @escaping ((_ result :[String : AnyObject])->Void)) -> Void
- {
- let url = URL(string: "http://libs.samtech.cl/movil/contacto.asp?")
- let parameters: [String : AnyObject] = ["ilogin": LibraryAPI.sharedInstance.getCurrentUser().ilogin as AnyObject, "ipassword": LibraryAPI.sharedInstance.getCurrentUser().ipassword as AnyObject,
- "Sw": "1" as AnyObject, "id_cont": "" as AnyObject, "nombre": "" as AnyObject, "telefono": "" as AnyObject, "mail": "" as AnyObject]
- var msg = "error request"
- var contacts_dto : [Contact] = [Contact]()
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "data" : contacts_dto as AnyObject, "message" : msg as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess{
- if let jsonResponse = JSON(response.result.value!)["lista"].arrayObject {
- for contact_ in jsonResponse {
- let contact = contact_ as AnyObject
- let contactDTO: Contact = Contact(
- id: contact.value(forKey: "ID_cont") as! String,
- name: contact.value(forKey: "Nombre") as! String,
- phone: contact.value(forKey: "Telefono") as! String,
- email: contact.value(forKey: "eMail") as! String)
- contacts_dto.append(contactDTO)
- }
- if (contacts_dto.isEmpty) {
- let fake_user = Contact(
- id: "-1",
- name: "No tiene contactos disponibles",
- phone: "--",
- email: "--")
- contacts_dto.append(fake_user)
- msg = "empty response"
- }else {
- msg = "successful response"
- }
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- resultAPI.updateValue(contacts_dto as AnyObject, forKey: "data")
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- }else{
- msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func getUserList(_ completion: @escaping ((_ result :[String : AnyObject])->Void)) -> Void
- {
- let url: String = "http://libs.samtech.cl/movil/ConsultaUsuario.asp"
- let user: User = LibraryAPI.sharedInstance.getCurrentUser()
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject,"ipassword": user.ipassword as AnyObject]
- var msg = "error request"
- var usuarios_dto : [User] = [User]()
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "data" : usuarios_dto as AnyObject, "message" : msg as AnyObject]
- Alamofire.request(url, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess
- {
- if let jsonResponse = JSON(response.result.value!)["usuarios"].arrayObject {
- for usuario_ in jsonResponse {
- let usuario = usuario_ as AnyObject
- let userDTO : User = User(ilogin: usuario["Usuario"] as! String,
- ipassword: usuario["Clave"] as! String,
- tipo: 2,
- email: usuario["Mail"] as! String,
- nombre: usuario["Nombre"] as! String,
- fono: usuario["Fono"] as! String
- )
- usuarios_dto.append(userDTO)
- }
- if (usuarios_dto.isEmpty) {
- let fake_user = User(ilogin: "No tiene usuarios disponibles", ipassword: "", tipo: 0, email: "", nombre: "No tiene usuarios disponibles", fono: "")
- usuarios_dto.append(fake_user)
- msg = "empty response"
- }else {
- msg = "successful response"
- }
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- resultAPI.updateValue(usuarios_dto as AnyObject, forKey: "data")
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- }else{
- msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func ActualizarNotificaciones(_ accion_id: String, estado_id: String, notificacion_id: String, completion: @escaping ((_ result :[String : AnyObject])->Void)) -> Void
- {
- let url: String = "http://libs.samtech.cl/movil/Notificaciones.asp"
- let user: User = LibraryAPI.sharedInstance.getCurrentUser()
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject,"ipassword": user.ipassword as AnyObject, "accion": accion_id as AnyObject, "estado" : estado_id as AnyObject, "Id_notificacion" : notificacion_id as AnyObject]
- var msg = "error request"
- var notification_dto : [Notification] = [Notification]()
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "data" : notification_dto as AnyObject, "message" : msg as AnyObject]
- Alamofire.request(url, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess
- {
- if let jsonResponse = JSON(response.result.value!)["notificaciones"].arrayObject {
- if accion_id == "1" {
- for notificacion_ in jsonResponse {
- let notificacion = notificacion_ as AnyObject
- let notificacionDTO :Notification = Notification(gps: notificacion["GPS"] as! String, descripcion: notificacion["Descripcion"] as! String, leyenda: notificacion["Leyenda"] as! String, estado: notificacion["Estado"] as! String, esu_id: notificacion["Esu_id"] as! String,patente: "", fechaIni :"",fechaTer: "")
- notification_dto.append(notificacionDTO)
- }
- }
- if (notification_dto.isEmpty) {
- let fake_notification = Notification(
- gps: "-1",
- descripcion: "No tiene notificaciones",
- leyenda: "",
- estado: "",
- esu_id: "",
- patente: "",
- fechaIni :"",
- fechaTer: ""
- )
- notification_dto.append(fake_notification)
- msg = "empty response"
- }else {
- msg = "successful response"
- }
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- resultAPI.updateValue(notification_dto as AnyObject, forKey: "data")
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- }
- else{
- msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func getNamesPerPlates(_ gps_id: String, patente_id: String, nombre_id: String, accion_id: String, completion: @escaping ((_ result :[String : AnyObject])->Void)) -> Void
- {
- let url = String("http://libs.samtech.cl/movil/NombrePorPatente.asp")
- let user: User = LibraryAPI.sharedInstance.getCurrentUser()
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject,"ipassword": user.ipassword as AnyObject, "accion": accion_id as AnyObject, "patente": patente_id as AnyObject, "gps": gps_id as AnyObject ]
- var msg = "error request"
- var vehicles_dto : [Vehicle] = [Vehicle]()
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "data" : vehicles_dto as AnyObject, "message" : msg as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess
- {
- if let jsonResponse = JSON(response.result.value!)["NomPatente"].arrayObject {
- for vehicle_ in jsonResponse {
- let vehicle = vehicle_ as AnyObject
- let vehicle_dto = Vehicle(
- idGPS: vehicle.value(forKey: "GPS") as! String,
- plate: vehicle.value(forKey: "Patente") as! String,
- name: vehicle.value(forKey: "Nombre") as! String
- )
- vehicles_dto.append(vehicle_dto)
- }
- if (vehicles_dto.isEmpty) {
- let fake_vehicle = Vehicle(idGPS: "-1", plate: "--", name: "No tiene vehiculos disponibles")
- vehicles_dto.append(fake_vehicle)
- msg = "empty response"
- }else {
- msg = "successful response"
- }
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- resultAPI.updateValue(vehicles_dto as AnyObject, forKey: "data")
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- }
- else{
- msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func updateNamesPerPlates(_ gps_id: String, patente_id: String, nombre_id: String, completion: @escaping ((_ result :[String : AnyObject])->Void)) -> Void
- {
- let url = String("http://libs.samtech.cl/movil/NombrePorPatente.asp")
- let user: User = LibraryAPI.sharedInstance.getCurrentUser()
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject,"ipassword": user.ipassword as AnyObject, "accion": "2" as AnyObject, "patente": patente_id as AnyObject, "gps": gps_id as AnyObject, "Nombre": nombre_id as AnyObject]
- var msg = "error request"
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "message" : msg as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess
- {
- if let jsonResponse = JSON(response.result.value!)["NomPatente"].arrayObject {
- for response_ in jsonResponse {
- let r = response_ as AnyObject
- msg = r.value(forKey: "GPS") as! String
- if msg == "Patente Actualizada" {
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- }
- }
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- }
- else{
- msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func updateContact(_ contenido_id: String, nombre_id: String, telefono_id: String, mail_id: String, accion_id: String, completion: @escaping ((_ result :[String : AnyObject])->Void)) -> Void
- {
- let url = URL(string: "http://libs.samtech.cl/movil/contacto.asp")
- let user: User = LibraryAPI.sharedInstance.getCurrentUser()
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject, "ipassword": user.ipassword as AnyObject, "Sw": accion_id as AnyObject, "Id_cont": contenido_id as AnyObject, "Nombre": nombre_id as AnyObject, "Telefono": telefono_id as AnyObject, "Mail": mail_id as AnyObject]
- var msg = "error request"
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "message" : msg as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess{
- if let jsonResponse = JSON(response.result.value!)["lista"].arrayObject {
- for contact_ in jsonResponse {
- let contact = contact_ as AnyObject
- msg = contact.value(forKey: "Nombre") as! String
- if msg == "Usuario Registrado" || msg == "Contacto actualizado" || msg == "Contacto eliminado"
- {
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- }
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- }
- }else{
- msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func assignVehiclesPerUser(_ user_id: String, gps_id: String, patente_id: String, estado_id: String, accion_id: String, completion : @escaping ((_ result: [String: AnyObject])-> Void)) -> Void
- {
- let url = URL(string: "http://libs.samtech.cl/movil/AsignaVehiculosPorUsuario.asp")
- let user: User = LibraryAPI.sharedInstance.getCurrentUser()
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject,"ipassword": user.ipassword as AnyObject, "id": gps_id as AnyObject, "usuario":user_id as AnyObject, "patente":patente_id as AnyObject, "estado":estado_id as AnyObject, "accion":accion_id as AnyObject]
- var msg = "error request"
- var vehicles_dto : [Vehicle] = []
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "message" : msg as AnyObject, "data": vehicles_dto as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess{
- if let responses = JSON(response.result.value!)["vehiculos"].arrayObject {
- for response_ in responses {
- let response = response_ as AnyObject
- let vehicle_dto = Vehicle(idGPS: response.value(forKey: "GPS") as! String, plate: response.value(forKey: "Patente") as! String, state: response.value(forKey: "Estado") as! String == "1" ? true : false, name: "", speed: "")
- vehicles_dto.append(vehicle_dto)
- }
- resultAPI.updateValue(vehicles_dto as AnyObject, forKey: "data")
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- }
- }else{
- msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- func insertUser(_ user_id: String, name: String, password: String, mail: String, phone: String, type: String, completion: @escaping (_ result: [String:AnyObject]) -> Void) -> Void
- {
- let url = URL(string: "http://libs.samtech.cl/movil/InsertaUsuario.asp")
- let user: User = LibraryAPI.sharedInstance.getCurrentUser()
- let parameters: [String : AnyObject] = ["ilogin": user.ilogin as AnyObject,"ipassword": user.ipassword as AnyObject, "usuario": user_id as AnyObject, "Nombre_usu": name as AnyObject, "Clave_usuario": password as AnyObject, "Mail_usuario": mail as AnyObject, "Tipo_ingreso": type as AnyObject, "fono": phone as AnyObject, "tipo_usu": "2" as AnyObject, "app": "Isamtech" as AnyObject]
- var resultAPI : [String : AnyObject] = ["status" : false as AnyObject, "message" : "" as AnyObject, "success" : false as AnyObject]
- Alamofire.request(url!, method: .post, parameters: parameters).responseJSON{
- response in
- if response.result.isSuccess{
- if let responses = JSON(response.result.value!)["usuario"].arrayObject {
- for response_ in responses {
- let response = response_ as AnyObject
- let mensaje = response.value(forKey: "mensaje") as! String
- if(mensaje == "Usuario ingresados" || mensaje == "Usuario actualizado")
- {
- resultAPI.updateValue(true as AnyObject, forKey: "success")
- }
- resultAPI.updateValue(mensaje as AnyObject, forKey: "message")
- }
- resultAPI.updateValue(true as AnyObject, forKey: "status")
- }
- }else{
- let msg = "Error en la consulta, verifique su conexión."
- resultAPI.updateValue(msg as AnyObject, forKey: "message")
- }
- completion(resultAPI)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement