SPersonViewController.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // SPersonViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/10/14.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Eureka
  10. import Alamofire
  11. import AlamofireImage
  12. import AlamofireObjectMapper
  13. import ObjectMapper
  14. import BSImagePicker
  15. import Photos
  16. import CocoaLumberjack
  17. class SPersonViewController: FormViewController {
  18. var person:O2PersonInfo? = nil
  19. var updateFlag:Bool = false
  20. var myIconView:UIImageView = UIImageView()
  21. private let viewModel: O2PersonalViewModel = {
  22. return O2PersonalViewModel()
  23. }()
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. //右边按钮
  27. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "更改", style: .plain, target: self, action: #selector(self.submitPersonUpdateAction(sender:)))
  28. ImageRow.defaultCellUpdate = { cell, row in
  29. cell.accessoryView?.layer.cornerRadius = 17
  30. cell.accessoryView?.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
  31. cell.textLabel?.font = setting_content_textFont
  32. cell.textLabel?.textColor = setting_content_textColor
  33. }
  34. LabelRow.defaultCellUpdate = {
  35. cell,row in
  36. cell.textLabel?.font = setting_content_textFont
  37. cell.textLabel?.textColor = setting_content_textColor
  38. cell.accessoryType = .disclosureIndicator
  39. }
  40. EmailRow.defaultCellUpdate = {
  41. cell,row in
  42. cell.textLabel?.font = setting_content_textFont
  43. cell.textLabel?.textColor = setting_content_textColor
  44. }
  45. PhoneRow.defaultCellUpdate = {
  46. cell,row in
  47. cell.textLabel?.font = setting_content_textFont
  48. cell.textLabel?.textColor = setting_content_textColor
  49. }
  50. TextRow.defaultCellUpdate = {
  51. cell,row in
  52. //修改输入法顶部的“完成”按钮的颜色字体
  53. if cell.inputAccessoryView?.isKind(of: NavigationAccessoryView.self) == true {
  54. let view = cell.textField.inputAccessoryView as? NavigationAccessoryView
  55. view?.doneButton.setTitleTextAttributes([
  56. NSAttributedString.Key.font:navbar_item_font,
  57. NSAttributedString.Key.foregroundColor: base_blue_color
  58. ], for:UIControl.State())
  59. }
  60. cell.textLabel?.font = setting_content_textFont
  61. cell.textLabel?.textColor = setting_content_textColor
  62. }
  63. ButtonRow.defaultCellUpdate = {
  64. cell,row in
  65. cell.textLabel?.font = setting_item_textFont
  66. cell.textLabel?.theme_textColor = ThemeColorPicker(keyPath: "Base.base_color")
  67. }
  68. ActionSheetRow<String>.defaultCellUpdate = {
  69. cell,row in
  70. cell.textLabel?.font = setting_content_textFont
  71. cell.textLabel?.textColor = setting_content_textColor
  72. }
  73. self.loadCurrentPersonInfo()
  74. }
  75. private func loadCurrentPersonInfo(){
  76. self.showLoading(title: "加载中...")
  77. self.viewModel.loadMyInfo().then { (person) in
  78. DispatchQueue.main.async {
  79. self.person = person
  80. self.loadAvatar()
  81. }
  82. }.catch { (error) in
  83. DispatchQueue.main.async {
  84. self.showError(title: "\(error)\n个人信息载入出错!")
  85. }
  86. }
  87. }
  88. private func loadAvatar() {
  89. let avatarUrlString = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":self.person?.id as AnyObject])
  90. let avatarUrl = URL(string: avatarUrlString!)
  91. self.myIconView.bounds = CGRect(x: 0, y: 0, width: 34, height: 34)
  92. self.myIconView.hnk_setImageFromURL(avatarUrl!, placeholder: UIImage(named: "personDefaultIcon"), format: nil, failure: { (error) in
  93. DDLogError("加载头像异常, \(String(describing: error))")
  94. self.setupForm(nil)
  95. }) { (image) in
  96. self.setupForm(image)
  97. }
  98. }
  99. private func setupForm(_ avatarImage: UIImage?) {
  100. self.title = self.person?.name
  101. if avatarImage == nil {
  102. DDLogError("没有头像数据!!!!!")
  103. }
  104. form +++ Section()
  105. <<< ImageRow("myAvatar"){ row in
  106. row.title = "头像"
  107. row.sourceTypes = [.PhotoLibrary,.Camera]
  108. row.clearAction = .no
  109. row.value = avatarImage
  110. }.onChange({ (row:ImageRow) in
  111. if let image = row.value {
  112. self.viewModel.updateMyIcon(icon: image)
  113. .then({ (result) in
  114. DDLogInfo("上次头像成功!,result:\(result)")
  115. self.notifyReloadAvatar()
  116. }).catch({ (error) in
  117. DDLogInfo("上传头像失败,\(error)")
  118. self.showError(title: "上传头像失败,\(error)")
  119. })
  120. }else {
  121. row.value = UIImage(named: "personDefaultIcon")
  122. }
  123. })
  124. <<< LabelRow(){
  125. $0.title = "工号"
  126. $0.value = person?.employee
  127. }
  128. <<< TextRow(){
  129. $0.title = "名字"
  130. $0.value = person?.name
  131. }.onChange({ (row) in
  132. self.person?.name = row.value
  133. self.updateFlag = true
  134. })
  135. <<< ActionSheetRow<String>(){
  136. $0.title = "性别"
  137. $0.selectorTitle = "请选择性别"
  138. $0.options = ["男","女"]
  139. $0.cancelTitle = "取消"
  140. $0.value = person?.genderType == "f" ? "女":"男"
  141. }.onChange({ (row:ActionSheetRow<String>) in
  142. if row.value == "男" {
  143. self.person?.genderType = "m"
  144. }else if row.value == "女"{
  145. self.person?.genderType = "f"
  146. }
  147. self.updateFlag = true
  148. })
  149. +++ Section()
  150. <<< EmailRow(){
  151. $0.title = "Email"
  152. $0.value = person?.mail
  153. }.onChange({ (row) in
  154. self.person?.mail = row.value
  155. self.updateFlag = true
  156. })
  157. <<< PhoneRow(){
  158. $0.title = "手机"
  159. $0.value = person?.mobile
  160. }.onChange({ (row) in
  161. self.person?.mobile = row.value
  162. self.updateFlag = true
  163. })
  164. <<< TextRow(){
  165. $0.title = "微信"
  166. $0.value = person?.weixin
  167. }.onChange({ (row) in
  168. self.person?.weixin = row.value
  169. self.updateFlag = true
  170. })
  171. <<< TextRow(){
  172. $0.title = "QQ"
  173. $0.value = person?.qq
  174. }.onChange({ (row) in
  175. self.person?.qq = row.value
  176. self.updateFlag = true
  177. })
  178. +++ Section()
  179. <<< ButtonRow() {
  180. $0.title = "退出登录"
  181. }.onCellSelection({ (cell, row) in
  182. let alertController = UIAlertController(title: "退出登录", message: "确定要退出系统吗?", preferredStyle: .actionSheet)
  183. let okAction = UIAlertAction(title: "退出", style: .destructive, handler: { _ in
  184. self.logout()
  185. })
  186. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  187. alertController.addAction(okAction)
  188. alertController.addAction(cancelAction)
  189. self.present(alertController, animated: true, completion: nil)
  190. }).cellSetup({ (cell:ButtonCellOf<String>, buttonRow) in
  191. cell.textLabel?.font = UIFont(name: "PingFangSC-Regular", size: 14.0)
  192. cell.textLabel?.theme_textColor = ThemeColorPicker(keyPath: "Base.base_color")
  193. })
  194. self.hideLoading()
  195. }
  196. func logout() {
  197. O2AuthSDK.shared.logout { (result, msg) in
  198. DDLogInfo("O2 登出 \(result), msg:\(msg ?? "")")
  199. }
  200. O2JPushManager.shared.O2JPushUnBind()
  201. // if O2IsConnect2Collect == false {
  202. // //解除绑定 设备号 内网直连版本
  203. //
  204. // }
  205. self.forwardDestVC("login", "loginVC")
  206. }
  207. //提交更新
  208. @objc func submitPersonUpdateAction(sender:UIBarButtonItem){
  209. if updateFlag == true {
  210. self.viewModel.updateMyInfo(person: self.person!).then { (result) in
  211. DDLogInfo("更新个人信息成功,\(result)")
  212. self.showSuccess(title: "更新成功!")
  213. }.catch { (error) in
  214. DDLogError("更新个人信息失败,\(error)")
  215. self.showError(title: "更新个人信息失败,\(error)")
  216. }
  217. }
  218. }
  219. override func didReceiveMemoryWarning() {
  220. super.didReceiveMemoryWarning()
  221. // Dispose of any resources that can be recreated.
  222. }
  223. /// 通知更新头像了
  224. @objc private func notifyReloadAvatar() {
  225. NotificationCenter.default.post(name: Notification.Name("reloadMyIcon"), object: nil)
  226. }
  227. }