SPersonViewController.swift 9.7 KB

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