OOAttanceCheckInController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // OOAttanceCheckInController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/17.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class OOAttanceCheckInController: UITableViewController {
  11. private lazy var viewModel:OOAttandanceViewModel = {
  12. return OOAttandanceViewModel()
  13. }()
  14. var checkinForm:OOAttandanceMobileCheckinForm = OOAttandanceMobileCheckinForm()
  15. var myButton:UIButton?
  16. var feature : OOAttandanceFeature?
  17. private lazy var headerView:OOAttanceHeaderView = {
  18. let view = Bundle.main.loadNibNamed("OOAttanceHeaderView", owner: self, options: nil)?.first as! OOAttanceHeaderView
  19. view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 280)
  20. return view
  21. }()
  22. private lazy var promptView:OOAttanceCheckinPromptView = {
  23. let view = Bundle.main.loadNibNamed("OOAttanceCheckinPromptView", owner: self, options: nil)?.first as! OOAttanceCheckinPromptView
  24. view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 44)
  25. return view
  26. }()
  27. override func viewWillAppear(_ animated: Bool) {
  28. super.viewWillAppear(animated)
  29. headerView.startBMKMapViewService()
  30. NotificationCenter.default.addObserver(self, selector: #selector(locationReceive(_:)), name: OONotification.location.notificationName, object: nil)
  31. if myButton != nil {
  32. myButton?.isHidden = false
  33. }
  34. }
  35. override func viewWillDisappear(_ animated: Bool) {
  36. super.viewWillDisappear(animated)
  37. headerView.stopBMKMapViewService()
  38. NotificationCenter.default.removeObserver(self)
  39. if myButton != nil {
  40. myButton?.isHidden = true
  41. }
  42. }
  43. override func viewDidAppear(_ animated: Bool) {
  44. super.viewDidAppear(animated)
  45. }
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. tableView.register(UINib.init(nibName: "OOAttanceItemCell", bundle: nil), forCellReuseIdentifier: "OOAttanceItemCell")
  49. getCurrentCheckinList()
  50. getMyRecords()
  51. self.perform(#selector(createButton), with: nil, afterDelay: 0)
  52. getWorkPlace()
  53. }
  54. //创建打卡按钮
  55. @objc private func createButton() {
  56. let window = UIApplication.shared.windows[0]
  57. myButton = UIButton(type: .custom)
  58. myButton?.frame = CGRect(x: kScreenW - 90, y: kScreenH - 150, width: 70, height: 70)
  59. myButton?.setTitle("打卡", for: .normal)
  60. myButton?.setTitle("打卡", for: .disabled)
  61. myButton?.titleLabel?.font = UIFont(name: "PingFangSC-Medium", size: 14.0)!
  62. myButton?.theme_backgroundColor = ThemeColorPicker(keyPath: "Base.base_color")
  63. myButton?.setBackgroundColor(UIColor.gray, forState: .disabled)
  64. myButton?.isEnabled = false
  65. myButton?.layer.cornerRadius = 35
  66. myButton?.layer.masksToBounds = true
  67. myButton?.addTarget(self, action: #selector(postCheckinButton(_:)), for: .touchUpInside)
  68. window.addSubview(myButton!)
  69. }
  70. //删除打卡按钮
  71. private func removeButton() {
  72. if myButton != nil {
  73. myButton?.removeFromSuperview()
  74. myButton = nil
  75. }
  76. }
  77. @objc private func locationReceive(_ notification:Notification){
  78. if let result = notification.object as? BMKReverseGeoCodeSearchResult {
  79. checkinForm.recordAddress = result.address
  80. checkinForm.desc = result.sematicDescription
  81. checkinForm.longitude = String(result.location.longitude)
  82. checkinForm.latitude = String(result.location.latitude)
  83. checkinForm.empNo = O2AuthSDK.shared.myInfo()?.employee
  84. checkinForm.empName = O2AuthSDK.shared.myInfo()?.name
  85. let currenDate = Date()
  86. checkinForm.recordDateString = currenDate.toString("yyyy-MM-dd")
  87. checkinForm.signTime = currenDate.toString("HH:mm:ss")
  88. checkinForm.optMachineType = UIDevice.deviceModelReadable()
  89. checkinForm.optSystemName = "\(UIDevice.systemName()) \(UIDevice.systemVersion())"
  90. // 打卡按钮启用
  91. myButton?.isEnabled = true
  92. headerView.addSubview(promptView)
  93. DDLogDebug("checkForm set completed")
  94. }else{
  95. //打卡按钮禁用
  96. myButton?.isEnabled = false
  97. promptView.removeFromSuperview()
  98. }
  99. }
  100. @objc private func postCheckinButton(_ sender:UIButton){
  101. if self.feature != nil {
  102. if self.feature?.signSeq ?? -1 < 1 {
  103. self.showError(title: "当前不需要打卡!")
  104. return
  105. }
  106. }
  107. self.showLoading(title: "打卡中...")
  108. checkinForm.checkin_type = self.feature?.checkinType ?? ""
  109. viewModel.postMyCheckin(checkinForm) { (result) in
  110. self.hideLoading()
  111. switch result {
  112. case .ok(_):
  113. DispatchQueue.main.async {
  114. self.showSuccess(title: "打卡成功")
  115. self.getCurrentCheckinList()
  116. self.getMyRecords()
  117. }
  118. break
  119. case .fail(let errorMessage):
  120. DispatchQueue.main.async {
  121. self.showError(title: "打卡失败,\n\(errorMessage)")
  122. }
  123. break
  124. default:
  125. break
  126. }
  127. }
  128. }
  129. // @objc private func changePostion(_ pan:UIPanGestureRecognizer){
  130. //
  131. // }
  132. func getWorkPlace() {
  133. viewModel.getLocationWorkPlace { (myResult) in
  134. switch myResult {
  135. case .ok(let result):
  136. DDLogDebug("有打卡位置了。。。。。。")
  137. let model = result as? [OOAttandanceWorkPlace]
  138. DispatchQueue.main.async {
  139. self.headerView.workPlaces = model
  140. }
  141. break
  142. case .fail(let s):
  143. self.showError(title: "错误:\n\(s)")
  144. break
  145. default:
  146. break
  147. }
  148. }
  149. }
  150. func getMyRecords() {
  151. viewModel.listMyRecords { (result) in
  152. switch result {
  153. case .ok(let record):
  154. let model = record as? OOMyAttandanceRecords
  155. if let feature = model?.feature {
  156. self.feature = feature
  157. }
  158. break
  159. case .fail(let err):
  160. DDLogError(err)
  161. break
  162. default:
  163. break
  164. }
  165. }
  166. }
  167. func getCurrentCheckinList() {
  168. var model = CommonPageModel()
  169. model.pageSize = 200
  170. let bean = OOAttandanceMobileQueryBean()
  171. bean.empName = O2AuthSDK.shared.myInfo()?.distinguishedName
  172. let currentDate = Date().toString("yyyy-MM-dd")
  173. bean.startDate = currentDate
  174. bean.endDate = currentDate
  175. self.showLoading()
  176. viewModel.getMyCheckinList(model, bean) { (myResult) in
  177. self.hideLoading()
  178. switch myResult {
  179. case .fail(let s):
  180. self.showError(title: "错误:\n\(s)")
  181. DispatchQueue.main.async {
  182. self.tableView.reloadData()
  183. }
  184. break
  185. case .reload:
  186. DispatchQueue.main.async {
  187. self.tableView.reloadData()
  188. }
  189. default:
  190. break
  191. }
  192. }
  193. }
  194. override func didReceiveMemoryWarning() {
  195. super.didReceiveMemoryWarning()
  196. // Dispose of any resources that can be recreated.
  197. }
  198. // MARK: - Table view data source
  199. override func numberOfSections(in tableView: UITableView) -> Int {
  200. // #warning Incomplete implementation, return the number of sections
  201. return viewModel.numberOfSections()
  202. }
  203. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  204. // #warning Incomplete implementation, return the number of rows
  205. return viewModel.numberOfRowsInSection(section)
  206. }
  207. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  208. let cell = tableView.dequeueReusableCell(withIdentifier: "OOAttanceItemCell", for: indexPath) as! (OOAttanceItemCell & Configurable)
  209. let item = viewModel.nodeForIndexPath(indexPath)
  210. cell.config(withItem: item)
  211. return cell
  212. }
  213. override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  214. if section == 0 {
  215. return headerView
  216. }
  217. return nil
  218. }
  219. override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  220. if section == 0 {
  221. let view = Bundle.main.loadNibNamed("OOAttanceFooterView", owner: self, options: nil)?.first as! OOAttanceFooterView
  222. return view
  223. }
  224. return nil
  225. }
  226. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  227. if section == 0 {
  228. return 280.0
  229. }
  230. return 10
  231. }
  232. override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  233. if section == 0 {
  234. return 50.0
  235. }
  236. return 10
  237. }
  238. deinit {
  239. DDLogDebug("deinit 这里是checkin controller 。。。。。。。。。")
  240. }
  241. }