diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 31827b7..ad10e59 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 7A0420AA25619AEC00034941 /* Osago.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A0420A925619AEC00034941 /* Osago.swift */; }; 7A0420AD2561A0B100034941 /* OsagoController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A0420AC2561A0B100034941 /* OsagoController.swift */; }; 7A0420B12561A0E100034941 /* OsagoAddController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A0420B02561A0E100034941 /* OsagoAddController.swift */; }; + 7A0420B62568650C00034941 /* DkbmController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A0420B52568650C00034941 /* DkbmController.swift */; }; + 7A0420BA25693D2C00034941 /* dkbm.js in Resources */ = {isa = PBXBuildFile; fileRef = 7A0420B925693D2C00034941 /* dkbm.js */; }; 7A051611241412CA00FC55AC /* SwiftDate in Frameworks */ = {isa = PBXBuildFile; productRef = 7A051610241412CA00FC55AC /* SwiftDate */; }; 7A05161A2414FF0900FC55AC /* DateSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A0516192414FF0900FC55AC /* DateSection.swift */; }; 7A1090E824A394F100B4F0B2 /* AudioRecordCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A1090E724A394F100B4F0B2 /* AudioRecordCell.swift */; }; @@ -105,6 +107,8 @@ 7A0420A925619AEC00034941 /* Osago.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Osago.swift; sourceTree = ""; }; 7A0420AC2561A0B100034941 /* OsagoController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OsagoController.swift; sourceTree = ""; }; 7A0420B02561A0E100034941 /* OsagoAddController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OsagoAddController.swift; sourceTree = ""; }; + 7A0420B52568650C00034941 /* DkbmController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DkbmController.swift; sourceTree = ""; }; + 7A0420B925693D2C00034941 /* dkbm.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = dkbm.js; sourceTree = ""; }; 7A0516192414FF0900FC55AC /* DateSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateSection.swift; sourceTree = ""; }; 7A1090E724A394F100B4F0B2 /* AudioRecordCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioRecordCell.swift; sourceTree = ""; }; 7A1090E924A3A26300B4F0B2 /* AudioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayer.swift; sourceTree = ""; }; @@ -217,10 +221,19 @@ children = ( 7A0420AC2561A0B100034941 /* OsagoController.swift */, 7A0420B02561A0E100034941 /* OsagoAddController.swift */, + 7A0420B52568650C00034941 /* DkbmController.swift */, ); path = Osago; sourceTree = ""; }; + 7A0420B825693CEE00034941 /* JS */ = { + isa = PBXGroup; + children = ( + 7A0420B925693D2C00034941 /* dkbm.js */, + ); + path = JS; + sourceTree = ""; + }; 7A1146F423FDE7E500B424AF = { isa = PBXGroup; children = ( @@ -241,6 +254,7 @@ 7A1146FF23FDE7E500B424AF /* AutoCat */ = { isa = PBXGroup; children = ( + 7A0420B825693CEE00034941 /* JS */, 7A64AE6B2469DC6900ABE48E /* AutoCat.entitlements */, 7A3F07A924360D9100E59687 /* Extensions */, 7A6DD90424326788009DE740 /* Fonts */, @@ -497,6 +511,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 7A0420BA25693D2C00034941 /* dkbm.js in Resources */, 7ADF6C99250F872C00F237B2 /* RoadNumbers.otf in Resources */, 7A11470D23FDE7E600B424AF /* LaunchScreen.storyboard in Resources */, 7A6DD90A24329541009DE740 /* RoadNumbers2.0.otf in Resources */, @@ -529,6 +544,7 @@ 7A6DD90C24335A6D009DE740 /* FlagLayer.swift in Sources */, 7AE26A3524F31B0700625033 /* EventsController.swift in Sources */, 7AB67E8C2435C38700258F61 /* CustomTextField.swift in Sources */, + 7A0420B62568650C00034941 /* DkbmController.swift in Sources */, 7A27ADF5249FD2F90035F39E /* FileManagerExt.swift in Sources */, 7A1DC38E2517ED98002E9C99 /* BlockBarButtonItem.swift in Sources */, 7AE26A3324EEF9EC00625033 /* UIViewControllerExt.swift in Sources */, diff --git a/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist b/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist index 3d39954..4ada3d5 100644 --- a/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist @@ -14,35 +14,35 @@ isShown orderHint - 8 + 2 DifferenceKit (Playground) 2.xcscheme isShown orderHint - 9 + 3 DifferenceKit (Playground).xcscheme isShown orderHint - 7 + 1 Eureka (Playground) 1.xcscheme isShown orderHint - 5 + 8 Eureka (Playground) 2.xcscheme isShown orderHint - 6 + 9 Eureka (Playground) 3.xcscheme @@ -70,7 +70,7 @@ isShown orderHint - 4 + 7 GettingStarted (Playground) 1.xcscheme @@ -112,42 +112,42 @@ isShown orderHint - 12 + 13 Rx (Playground) 1.xcscheme isShown orderHint - 2 + 5 Rx (Playground) 2.xcscheme isShown orderHint - 3 + 6 Rx (Playground).xcscheme isShown orderHint - 1 + 4 SwiftDate (Playground) 1.xcscheme isShown orderHint - 11 + 12 SwiftDate (Playground) 2.xcscheme isShown orderHint - 13 + 10 SwiftDate (Playground) 3.xcscheme @@ -175,7 +175,7 @@ isShown orderHint - 10 + 11 SuppressBuildableAutocreation diff --git a/AutoCat/Controllers/Osago/DkbmController.swift b/AutoCat/Controllers/Osago/DkbmController.swift new file mode 100644 index 0000000..ecae7eb --- /dev/null +++ b/AutoCat/Controllers/Osago/DkbmController.swift @@ -0,0 +1,62 @@ +import UIKit +import WebKit +import PKHUD + +class DkbmController: UIViewController, WKScriptMessageHandler { + + private var webView: WKWebView! + private var captchaAdded = false + + var onDone: ((String) -> Void)? + + override func viewDidLoad() { + super.viewDidLoad() + + let config = WKWebViewConfiguration() + if let jsPath = Bundle.main.path(forResource: "dkbm", ofType: "js") { + let js = try? String(contentsOfFile: jsPath) + let contentController = WKUserContentController() + let script = WKUserScript(source: js!, injectionTime: .atDocumentEnd, forMainFrameOnly: false) + contentController.addUserScript(script) + contentController.add(self, name: "dkbmHandler") + config.userContentController = contentController + } + + self.webView = WKWebView(frame: .zero, configuration: config) + self.webView.translatesAutoresizingMaskIntoConstraints = false + self.view.addSubview(self.webView) + NSLayoutConstraint.activate([ + self.webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor), + self.webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor), + self.webView.topAnchor.constraint(equalTo: self.view.topAnchor), + self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor) + ]) + + //self.webView.isHidden = true + //HUD.show(.progress) + + let url = URL(string: "https://dkbm-web.autoins.ru/dkbm-web-1.0/policyInfo.htm")! + let request = URLRequest(url: url) + self.webView.load(request) + } + + // MARK: - WKScriptMessageHandler + + func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { + guard let msg = message.body as? [String:String] else { return } + print(msg) + + if let event = msg["event"] { + if event == "show-challenge" { + if self.captchaAdded { + HUD.hide() + self.webView.isHidden = false + } + } else if event == "show-checkbox" { + self.captchaAdded = true + } + } else if let token = msg["token"] { + self.onDone?(token) + } + } +} diff --git a/AutoCat/Controllers/Osago/OsagoAddController.swift b/AutoCat/Controllers/Osago/OsagoAddController.swift index c9aa474..45a1a64 100644 --- a/AutoCat/Controllers/Osago/OsagoAddController.swift +++ b/AutoCat/Controllers/Osago/OsagoAddController.swift @@ -1,5 +1,8 @@ import UIKit import Eureka +import PKHUD +import RxSwift +import RxCocoa enum OsagoCheckSource: Equatable, CustomStringConvertible { case plateNumber(number: String) @@ -17,26 +20,69 @@ enum OsagoCheckSource: Equatable, CustomStringConvertible { class OsagoAddController: FormViewController { + private let bag = DisposeBag() var checkSources: [OsagoCheckSource] = [] + var onDone: ((Vehicle) -> Void)? override func viewDidLoad() { super.viewDidLoad() self.title = "OSAGO check" form +++ Section("Check parameters") - <<< DateTimeInlineRow() { row in + <<< DateTimeInlineRow("date") { row in row.title = "Check date" row.value = Date() } - <<< PickerInlineRow() { row in + <<< PickerInlineRow("SourcePicker") { row in row.title = "Check by" - row.value = .plateNumber(number: "А123АА161") + row.value = self.checkSources.first row.options = self.checkSources } form +++ Section() <<< ButtonRow() { $0.title = "Check" }.onCellSelection { _, _ in - print("Check") +// HUD.show(.progress) +// Api.checkOsago(number: "Н133РО161", vin: nil, date: Date(), token: "") +// .observeOn(MainScheduler.instance) +// .subscribe { vehicle in +// HUD.hide() +// self.onDone?(vehicle) +// } onError: { err in +// HUD.show(error: err) +// } +// .disposed(by: self.bag) +// return + + + let controller = DkbmController() + controller.onDone = { token in + self.navigationController?.popViewController(animated: true, completion: { + guard let source = (self.form.rowBy(tag: "SourcePicker") as? PickerInlineRow)?.value, + let date = (self.form.rowBy(tag: "date") as? DateTimeInlineRow)?.value + else { return } + + var number, vin: String? + switch source { + case .plateNumber(let n): + number = n + case .vin(let v): + vin = v + } + HUD.show(.progress) + Api.checkOsago(number: number, vin: vin, date: date, token: token) + .observeOn(MainScheduler.instance) + .subscribe { vehicle in + HUD.hide() + self.onDone?(vehicle) + } onError: { err in + HUD.show(error: err) + } + .disposed(by: self.bag) + + }) + } + self.navigationController?.pushViewController(controller, animated: true) + //self.present(controller, animated: true) } } } diff --git a/AutoCat/Controllers/Osago/OsagoController.swift b/AutoCat/Controllers/Osago/OsagoController.swift index 2720d2f..303bf3e 100644 --- a/AutoCat/Controllers/Osago/OsagoController.swift +++ b/AutoCat/Controllers/Osago/OsagoController.swift @@ -1,11 +1,12 @@ import UIKit import Eureka +import PKHUD class OsagoController: FormViewController { var vehicle: Vehicle? { didSet { - self.form.allSections.forEach { $0.reload() } + self.updateInterface() } } @@ -24,6 +25,34 @@ class OsagoController: FormViewController { if let vin = vehicle.vin1, !vin.contains("*") { controller.checkSources.append(.vin(number: vin)) } + + controller.onDone = { vehicle in + self.navigationController?.popViewController(animated: true, completion: { + self.update(vehicle: vehicle) + }) + } + self.navigationController?.pushViewController(controller, animated: true) } + + func updateInterface() { + self.form.allSections.forEach { $0.reload() } + } + + func update(vehicle: Vehicle) { + do { + if let realm = self.vehicle?.realm { + try realm.write { + realm.add(vehicle, update: .all) + } + } else { + self.vehicle?.osagoContracts.removeAll() + self.vehicle?.osagoContracts.append(objectsIn: vehicle.osagoContracts) + } + self.updateInterface() + } catch { + HUD.show(error: error) + print(error) + } + } } diff --git a/AutoCat/JS/dkbm.js b/AutoCat/JS/dkbm.js new file mode 100644 index 0000000..c6aaeb8 --- /dev/null +++ b/AutoCat/JS/dkbm.js @@ -0,0 +1,28 @@ +const sitekey = '6Lf2uycUAAAAALo3u8D10FqNuSpUvUXlfP7BzHOk'; + +let verifyCallback = (response) => { + console.log('verifyCallback: ', response); + window.webkit.messageHandlers.dkbmHandler.postMessage({ token: response }); +}; + +window.addEventListener('load', (event) => { + if(window.top == window.self) { + let meta = document.createElement('meta'); meta + meta.setAttribute( 'name', 'viewport' ); + meta.setAttribute( 'content', 'width = device-width' ); + document.getElementsByTagName('head')[0].appendChild(meta); + + document.body.innerHTML = ''; + window.webkit.messageHandlers.dkbmHandler.postMessage({ event: 'show-checkbox' }); + grecaptcha.render(document.body, { + 'sitekey' : sitekey, + 'callback' : verifyCallback + }); + } else { + let checkbox = document.getElementById('recaptcha-anchor'); + if(checkbox) { + checkbox.click(); + window.webkit.messageHandlers.dkbmHandler.postMessage({ event: 'show-challenge', location: location.href }); + } + } +}); diff --git a/AutoCat/Models/Vehicle.swift b/AutoCat/Models/Vehicle.swift index 7ca84dc..6cf2a90 100644 --- a/AutoCat/Models/Vehicle.swift +++ b/AutoCat/Models/Vehicle.swift @@ -49,6 +49,7 @@ class VehiclePhoto: Object, Decodable { override var description: String { let formatter = DateFormatter() + formatter.timeZone = TimeZone(identifier:"GMT") formatter.dateStyle = .medium formatter.timeStyle = .none let date = Date(timeIntervalSince1970: self.date/1000) diff --git a/AutoCat/Utils/Api.swift b/AutoCat/Utils/Api.swift index ba88af3..b0ec4fa 100644 --- a/AutoCat/Utils/Api.swift +++ b/AutoCat/Utils/Api.swift @@ -255,4 +255,14 @@ class Api { public static func events(with filter: Filter) -> Single<[VehicleEvent]> { return self.makeGetRequest(api: "events", params: filter.queryDictionary()) } + + public static func checkOsago(number: String?, vin: String?, date: Date, token: String) -> Single { + let body = [ + "date": AnyEncodable(date.timeIntervalSince1970), + "number": AnyEncodable(number), + "vin": AnyEncodable(vin), + "token": AnyEncodable(token) + ] + return self.makeBodyRequest(api: "vehicles/checkOsago", body: body) + } }