OOAttanceSettingController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // OOAttanceSettingController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/14.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class OOAttanceSettingController: UIViewController {
  11. var mapView:BMKMapView!
  12. var locService:BMKLocationManager!
  13. var searchAddress:BMKGeoCodeSearch!
  14. var settingBean:OOAttandanceNewWorkPlace = OOAttandanceNewWorkPlace()
  15. private var annotation:BMKPointAnnotation!
  16. private var isAdmin = false
  17. private var viewModel:OOAttandanceViewModel = {
  18. return OOAttandanceViewModel()
  19. }()
  20. private lazy var dataView:OOAttandanceSettingDataView = {
  21. let view = Bundle.main.loadNibNamed("OOAttandanceSettingDataView", owner: self
  22. , options: nil)?.first as! OOAttandanceSettingDataView
  23. return view
  24. }()
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. // title = "设置"
  28. // navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(closeWindow))
  29. // navigationItem.rightBarButtonItem = UIBarButtonItem(title: "地点管理", style: .plain, target: self, action: #selector(navWorkPlaceManager(_:)))
  30. loadAdmin()
  31. //增加mapView
  32. commonMapView()
  33. commonDataView()
  34. }
  35. override func viewWillAppear(_ animated: Bool) {
  36. super.viewWillAppear(animated)
  37. dataView.isHidden = false
  38. self.addNotificationObserver(OONotification.newWorkPlace.stringValue, selector: #selector(createNewWorkPlace(_:)))
  39. self.addKeyboardWillShowNotification()
  40. self.addKeyboardWillHideNotification()
  41. }
  42. override func viewWillDisappear(_ animated: Bool) {
  43. super.viewDidDisappear(animated)
  44. self.removeNotificationObserver(OONotification.newWorkPlace.stringValue)
  45. self.removeKeyboardWillHideNotification()
  46. self.removeKeyboardWillShowNotification()
  47. dataView.isHidden = true
  48. }
  49. override func keyboardWillShowWithFrame(_ frame: CGRect) {
  50. //输入框view向上移动
  51. print("keyboardWillShowWithFrame = ",frame)
  52. UIView.animate(withDuration: 0.5) {
  53. //self.dataView.origin.y = kScreenH - 125 - frame.size.height
  54. self.dataView.frame = CGRect(x: 0, y: kScreenH - 125 - frame.size.height, width: SCREEN_WIDTH, height: 125)
  55. }
  56. }
  57. override func keyboardWillHideWithFrame(_ frame: CGRect) {
  58. print("keyboardWillHideWithFrame = ",frame)
  59. UIView.animate(withDuration: 0.5) {
  60. //self.dataView.origin.y = kScreenH - 125 - 50
  61. self.dataView.frame = CGRect(x: 0, y: SCREEN_HEIGHT - 125 - 50, width: SCREEN_WIDTH, height: 125)
  62. }
  63. }
  64. //
  65. private func loadAdmin() {
  66. let distName = O2AuthSDK.shared.myInfo()?.distinguishedName ?? ""
  67. viewModel.getAttendanceAdmin().then { (admins) in
  68. admins.forEach({ (admin) in
  69. if admin.adminName == distName {
  70. self.isAdmin = true
  71. }
  72. })
  73. DDLogDebug("是否是管理员:\(self.isAdmin)")
  74. }
  75. .catch { (myerror) in
  76. DDLogError(myerror.localizedDescription)
  77. }
  78. }
  79. @objc private func createNewWorkPlace(_ notification:Notification){
  80. DDLogDebug("接收到消息。。。。。。。。。。。。。。。。。。。。。。")
  81. if self.isAdmin == false {
  82. self.showError(title: "你不是管理员,无操作权限")
  83. return
  84. }
  85. if let obj = notification.object as? (String,String,String) {
  86. settingBean.placeName = obj.0
  87. settingBean.placeAlias = obj.1
  88. settingBean.errorRange = obj.2
  89. }
  90. self.showLoading(title: "创建打卡地址...")
  91. viewModel.postCheckinLocation(settingBean) { (resultType) in
  92. self.hideLoading()
  93. switch resultType {
  94. case .ok(_):
  95. self.showSuccess(title: "打卡地址设置成功")
  96. break
  97. case .fail(let errorMessage):
  98. self.showError(title: "打卡地址设置失败\n\(errorMessage)" )
  99. break
  100. default:
  101. break
  102. }
  103. }
  104. }
  105. @objc private func navWorkPlaceManager(_ sender:Any?){
  106. if self.isAdmin {
  107. let destVC = OOAttandanceWorkPlaceController(nibName: "OOAttandanceWorkPlaceController", bundle: nil)
  108. self.pushVC(destVC)
  109. }else {
  110. self.showMessage(msg: "你不是管理员,无操作权限")
  111. }
  112. }
  113. func commonMapView() {
  114. mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
  115. mapView.zoomLevel = 19
  116. mapView.showMapPoi = true
  117. mapView.showIndoorMapPoi = true
  118. view.addSubview(mapView)
  119. mapView.delegate = self
  120. //mapView.showsUserLocation = true
  121. //locService.desiredAccuracy = 100
  122. locService = BMKLocationManager()
  123. locService.delegate = self
  124. locService.startUpdatingLocation()
  125. searchAddress = BMKGeoCodeSearch()
  126. searchAddress.delegate = self
  127. }
  128. func commonDataView() {
  129. let window = UIApplication.shared.windows.last
  130. dataView.frame = CGRect(x: 0, y: SCREEN_HEIGHT - 125 - 50, width: SCREEN_WIDTH, height: 125)
  131. window?.addSubview(dataView)
  132. }
  133. @objc func closeWindow() {
  134. self.tabBarController?.navigationController?.dismiss(animated: true, completion: nil)
  135. }
  136. override func didReceiveMemoryWarning() {
  137. super.didReceiveMemoryWarning()
  138. // Dispose of any resources that can be recreated.
  139. }
  140. deinit {
  141. mapView.delegate = nil
  142. locService.delegate = nil
  143. locService.stopUpdatingLocation()
  144. searchAddress.delegate = nil
  145. }
  146. }
  147. extension OOAttanceSettingController:BMKMapViewDelegate {
  148. func mapView(_ mapView: BMKMapView!, onClickedMapBlank coordinate: CLLocationCoordinate2D) {
  149. mapView.superview?.endEditing(true)
  150. //单击
  151. if annotation == nil {
  152. annotation = BMKPointAnnotation()
  153. annotation.coordinate = coordinate
  154. annotation.title = "公司打卡地点";
  155. mapView.addAnnotation(annotation)
  156. }else{
  157. annotation.coordinate = coordinate
  158. annotation.title = "公司打卡地点";
  159. }
  160. //反向查询具体地址名称
  161. let re = BMKReverseGeoCodeSearchOption()
  162. re.location = coordinate
  163. let flag = searchAddress.reverseGeoCode(re)
  164. DDLogDebug("coordinate searchAddress \(flag)")
  165. }
  166. func mapView(_ mapView: BMKMapView!, onClickedMapPoi mapPoi: BMKMapPoi!) {
  167. let re = BMKReverseGeoCodeSearchOption()
  168. let coordinate = mapPoi.pt
  169. re.location = coordinate
  170. let flag = searchAddress.reverseGeoCode(re)
  171. DDLogDebug("mapPoi searchAddress \(flag)")
  172. }
  173. func mapView(_ mapView: BMKMapView!, didSelect view: BMKAnnotationView!) {
  174. mapView.centerCoordinate = view.annotation.coordinate
  175. }
  176. }
  177. extension OOAttanceSettingController:BMKLocationManagerDelegate {
  178. // func willStartLocatingUser() {
  179. // DDLogDebug("willStartLocatingUser")
  180. // MBProgressHUD_JChat.showMessage(message:"正在定位中,请稍候", toView: self.mapView)
  181. // }
  182. func bmkLocationManager(_ manager: BMKLocationManager, didUpdate location: BMKLocation?, orError error: Error?) {
  183. if let loc = location?.location {
  184. DDLogDebug("设置 当前位置,\(loc.coordinate.latitude),\(loc.coordinate.longitude)")
  185. let user = BMKUserLocation()
  186. user.location = loc
  187. mapView.updateLocationData(user)
  188. mapView.centerCoordinate = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
  189. //定位完成停止定位
  190. locService.stopUpdatingLocation()
  191. }
  192. }
  193. // func didUpdate(_ userLocation: BMKUserLocation!) {
  194. // DDLogDebug("当前位置,\(userLocation.location.coordinate.latitude),\(userLocation.location.coordinate.longitude)")
  195. // mapView.updateLocationData(userLocation)
  196. // mapView.centerCoordinate = userLocation.location.coordinate
  197. // //定位完成停止定位
  198. // locService.stopUpdatingLocation()
  199. // }
  200. //
  201. // func didStopLocatingUser() {
  202. //
  203. // MBProgressHUD_JChat.hide(forView: self.mapView, animated: true)
  204. // }
  205. }
  206. extension OOAttanceSettingController:BMKGeoCodeSearchDelegate {
  207. func onGetReverseGeoCodeResult(_ searcher: BMKGeoCodeSearch!, result: BMKReverseGeoCodeSearchResult!, errorCode error: BMKSearchErrorCode) {
  208. dataView.workPlaceNameTextField.text = result.address
  209. dataView.workAliasNameTextField.text = result.sematicDescription
  210. //设置settingBean
  211. settingBean.placeName = result.address
  212. settingBean.placeAlias = result.sematicDescription
  213. settingBean.creator = O2AuthSDK.shared.myInfo()?.distinguishedName
  214. settingBean.longitude = String(result.location.longitude)
  215. settingBean.latitude = String(result.location.latitude)
  216. settingBean.errorRange = "0"
  217. }
  218. }