IHGuestInfoManager.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // IHGuestInfoManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/2/13.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import Photos
  10. import AVFoundation
  11. import PKHUD
  12. let kNotifactionRegisterSuccess = "kNotifactionRegisterSuccess"
  13. class IHGuestInfoManager: NSObject {
  14. // private var vc = UIViewController()
  15. private weak var vc : UIViewController?
  16. let imagePicker = UIImagePickerController()
  17. lazy var mainView: IHGuestInfoView = {
  18. let mainView = Bundle.main.loadNibNamed("IHGuestInfoView", owner: nil, options: nil)?.last as! IHGuestInfoView
  19. mainView.delegate = self
  20. return mainView
  21. }()
  22. /// 懒加载
  23. lazy var addressPicker: IHRoomPicker = {
  24. // let nib = UINib(nibName: "IHRoomPicker", bundle: nil)
  25. // let picker = nib.instantiate(withOwner: nil, options: nil).first as! IHRoomPicker
  26. let picker = IHRoomPicker()
  27. picker.modalSize = (width: .full, height: .custom(size: 300))
  28. picker.modalPosition = .bottomCenter
  29. // picker.cornerRadius = 10
  30. /// 该回调方法可以在本类任意地方写
  31. picker.selectedRoomCompleted = { [weak self] (build, floor, room,roomId) in
  32. self?.mainView.roomNumber = build + "-" + floor + "-" + room
  33. AppShare.register_roomId = roomId
  34. }
  35. return picker
  36. }()
  37. func bindController(_ vc: UIViewController) {
  38. self.vc = vc
  39. createUI()
  40. requestdata()
  41. }
  42. deinit {
  43. NotificationCenter.default.removeObserver(self)
  44. }
  45. func createUI() {
  46. let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight))
  47. self.vc?.view.addSubview(scrollView)
  48. scrollView.contentSize = CGSize(width: 0, height: 812)
  49. scrollView.addSubview(mainView)
  50. setNavigationBar()
  51. imagePicker.delegate = self
  52. self.imagePicker.modalPresentationStyle = .fullScreen
  53. }
  54. func setNavigationBar() {
  55. }
  56. func requestdata() {
  57. //请求酒店数据
  58. IHLoginService.share.getHotelList(requstSuccess: { (hotellist) in
  59. self.mainView.hotellist = hotellist
  60. }) {
  61. }
  62. }
  63. }
  64. extension IHGuestInfoManager:IHGuestInfoViewDelegate{
  65. //选择照片
  66. func goToPickerAvatar() {
  67. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  68. alert.addAction(UIAlertAction(title: "相册", style: .default, handler: { (button) in
  69. if self.checkAlbum() == true{
  70. self.presentToImagePickerController(.photoLibrary)
  71. }else{
  72. self.showAlart(title: "提示", message: "相册权限未启用,请进入系统[设置]>[隐私]>[照片]打开开关启用相册功能")
  73. }
  74. }))
  75. alert.addAction(UIAlertAction(title: "相机", style: .default, handler: { (button) in
  76. //判断是否支持使用相册
  77. if UIImagePickerController.isSourceTypeAvailable(.camera){
  78. if self.checkCamera() == true {
  79. self.presentToImagePickerController(.camera)
  80. }else{
  81. self.showAlart(title: "提示", message: "相机权限未开启,请进入系统[设置]>[隐私]>[相机]打开开关,开启相机功能")
  82. return
  83. }
  84. }else{
  85. g_showHUD("该设备不支持拍照功能")
  86. }
  87. }))
  88. alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
  89. self.vc?.present(alert, animated: true, completion: nil)
  90. }
  91. func presentToImagePickerController(_ type : UIImagePickerController.SourceType) {
  92. self.imagePicker.sourceType = type
  93. self.vc?.present(self.imagePicker, animated: true, completion: nil)
  94. }
  95. func showAlart(title:String,message:String) {
  96. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  97. let action = UIAlertAction(title: "I know", style: .default) { (action) in
  98. }
  99. alert.addAction(action)
  100. self.vc?.present(alert, animated: true, completion: nil)
  101. }
  102. func checkAlbum() -> Bool {
  103. let status = PHPhotoLibrary.authorizationStatus()
  104. if status == .denied || status == .restricted {
  105. return false
  106. }
  107. return true
  108. }
  109. func checkCamera() -> Bool {
  110. let status = AVCaptureDevice.authorizationStatus(for: .video)
  111. if status == .restricted || status == .denied {
  112. return false
  113. }
  114. return true
  115. }
  116. func guestInfoConfirm(_ name: String, age: String) {
  117. if AppShare.register_roomId == "" {
  118. g_showHUD("请选择您的房间")
  119. return
  120. }
  121. if AppShare.register_avatar == "" {
  122. g_showHUD("请选择您的头像")
  123. return
  124. }
  125. let username = AppShare.register_username
  126. let password = AppShare.register_password
  127. let name = AppShare.register_name
  128. let age = AppShare.register_age
  129. let roomId = AppShare.register_roomId
  130. let avatar = AppShare.register_avatar
  131. IHLoginService.share.registerUser(username, password: password, name: name, age: age, roomId: roomId, avatar: avatar) {
  132. log.debug("guestInfoConfirm")
  133. let success = IHSignupSuccess()
  134. success.show(above: self.vc, completion: nil)
  135. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionRegisterSuccess), object: username)
  136. }
  137. }
  138. func guestInfoConfirm() {
  139. }
  140. func selectedHotel(hotelId: String) {
  141. addressPicker.hotelId = hotelId
  142. // picker.hotelId = hotelId
  143. }
  144. //选择题房间信息
  145. func goToselectedRoomNumber(_ hotelId :String?) {
  146. addressPicker.hotelId = hotelId
  147. self.vc?.present(addressPicker, animated: true, completion: nil)
  148. // self.vc.present(picker, animated: true, completion: nil)
  149. }
  150. }
  151. //MARK : UIImagePickerControllerDelegate
  152. extension IHGuestInfoManager : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  153. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  154. guard let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
  155. //imageView.image = pickedImage
  156. log.debug("pickedImage - \(pickedImage)")
  157. NotificationCenter.default.post(name: NSNotification.Name(kIHwGuestInfoNotificationGetDeviceImage), object: pickedImage)
  158. self.vc?.dismiss(animated: true, completion: nil)
  159. //上传图片
  160. //let imageData = pickedImage.pngData()
  161. HUD.show(.progress)
  162. IHNewEquipmentService.share.uploadImage(file: pickedImage, requestSuccess: { (path) in
  163. log.debug("图片地址 - \(path)")
  164. AppShare.register_avatar = path
  165. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHGuestInfoUploadImageFinished), object: path)
  166. HUD.flash(.success, delay: 1)
  167. }) {
  168. log.debug("上传失败")
  169. HUD.flash(.error, delay: 1)
  170. }
  171. }
  172. }