SPersonViewController.swift 9.8 KB

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