ContactDeptPersonController.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // ContactDeptPersonController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/7/14.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import CocoaLumberjack
  15. class ContactDeptPersonController: UITableViewController, UITextViewDelegate {
  16. var superOrgUnit : OrgUnit? {
  17. didSet {
  18. subUnitURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.subUnitByNameQuery, parameter: ["##name##":(superOrgUnit?.distinguishedName)! as AnyObject])
  19. subIdentityURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.subIdentityByNameQuery, parameter: ["##name##":(superOrgUnit?.unique)! as AnyObject])
  20. if self.headBars.count == 0 {
  21. self.headBars.append(superOrgUnit!)
  22. }else{
  23. var tag = true
  24. for (index,unit) in self.headBars.enumerated() {
  25. if unit.distinguishedName! == self.superOrgUnit!.distinguishedName! {
  26. tag = false
  27. let n = self.headBars.count - index
  28. if n>1 {
  29. self.headBars.removeLast(n-1)
  30. }
  31. break
  32. }
  33. }
  34. if tag {
  35. self.headBars.append(superOrgUnit!)
  36. }
  37. }
  38. }
  39. }
  40. var subUnitURL : String?
  41. var subIdentityURL : String?
  42. var contacts:[Int:[CellViewModel]] = [0:[],1:[],2:[]]
  43. var headBars:[OrgUnit] = []
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46. self.title = superOrgUnit?.name
  47. self.tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadMyDeptData(_:)))
  48. loadMyDeptData(nil)
  49. }
  50. override func viewWillAppear(_ animated: Bool) {
  51. super.viewWillAppear(animated)
  52. }
  53. // MARK: - Table view data source
  54. override func numberOfSections(in tableView: UITableView) -> Int {
  55. return self.contacts.count
  56. }
  57. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  58. return self.contacts[section]!.count
  59. }
  60. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  61. return 10
  62. }
  63. override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  64. return 3
  65. }
  66. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  67. var cellName = ""
  68. if indexPath.section == 0 {
  69. cellName = "addressBarCell"
  70. }else if indexPath.section == 1 {
  71. cellName = "addressUnitCell"
  72. }else {
  73. cellName = "addressPersonCell"
  74. }
  75. let cell = tableView.dequeueReusableCell(withIdentifier: cellName, for: indexPath) as! ContactItemCell
  76. let cellMod = self.contacts[indexPath.section]![indexPath.row]
  77. if !cellMod.openFlag {
  78. cell.accessoryType = UITableViewCell.AccessoryType.none
  79. } else {
  80. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  81. }
  82. cell.cellViewModel = cellMod
  83. if indexPath.section == 0 {
  84. cell.delegate = self
  85. }
  86. return cell
  87. }
  88. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  89. self.tableView.deselectRow(at: indexPath, animated: false)
  90. let viewModel = self.contacts[indexPath.section]![indexPath.row]
  91. switch viewModel.dataType {
  92. case .depart(let d):
  93. if viewModel.openFlag {
  94. self.superOrgUnit = d as? OrgUnit
  95. self.reloadView()
  96. }
  97. case .identity(let i):
  98. self.performSegue(withIdentifier: "showIdentityPersonSegueV2", sender: i)
  99. default:
  100. self.tableView.deselectRow(at: indexPath, animated: false)
  101. //DDLogDebug(viewModel.name!)
  102. }
  103. }
  104. func reloadView() {
  105. for i in 0..<self.contacts.count {
  106. self.contacts[i]?.removeAll()
  107. }
  108. self.tableView.reloadData()
  109. self.title = superOrgUnit?.name
  110. loadMyDeptData(nil)
  111. }
  112. func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
  113. if let scheme = URL.scheme {
  114. switch scheme {
  115. case "reloadto" :
  116. let distinguishedName = (URL.description as NSString).substring(from: 9)
  117. for unit in self.headBars {
  118. if distinguishedName == unit.distinguishedName! {
  119. self.superOrgUnit = unit
  120. self.reloadView()
  121. break;
  122. }
  123. }
  124. default:
  125. break
  126. }
  127. }
  128. return true
  129. }
  130. // MARK: - Navigation
  131. // In a storyboard-based application, you will often want to do a little preparation before navigation
  132. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  133. if segue.identifier == "showIdentityPersonSegueV2" {
  134. let destVC = segue.destination as! ContactPersonInfoV2ViewController
  135. destVC.identity = sender as? IdentityV2
  136. }
  137. }
  138. @objc func loadMyDeptData(_ sender:AnyObject?){
  139. let urls = [0:"111",1:subUnitURL,2:subIdentityURL]
  140. self.showLoading(title:"加载中...")
  141. var num = 0
  142. for (tag,url) in urls {
  143. num += 1
  144. if tag == 0 {
  145. self.contacts[tag]?.removeAll()
  146. if headBars.count > 0 {
  147. var barText: [OrgUnit] = []
  148. if headBars.count == 1 {
  149. barText.append(headBars[0])
  150. }else {
  151. barText.append(headBars[headBars.count-2])
  152. barText.append(headBars[headBars.count-1])
  153. }
  154. let head = HeadTitle(name: "bar", barText: barText)
  155. let vm = CellViewModel(name: "bar",sourceObject: head)
  156. self.contacts[tag]?.append(vm)
  157. }
  158. continue
  159. }
  160. AF.request(url!).responseJSON {
  161. response in
  162. self.contacts[tag]?.removeAll()
  163. switch response.result {
  164. case .success(let val):
  165. let objects = JSON(val)["data"]
  166. DDLogDebug(objects.description)
  167. self.contacts[tag]?.removeAll()
  168. switch tag {
  169. case 1:
  170. if let units = Mapper<OrgUnit>().mapArray(JSONString:objects.description) {
  171. for unit in units{
  172. let name = "\(unit.name!)(" + String(unit.subDirectIdentityCount+unit.subDirectUnitCount) + ")"
  173. let vm = CellViewModel(name: name,sourceObject: unit)
  174. self.contacts[tag]?.append(vm)
  175. }
  176. }
  177. case 2:
  178. if let identitys = Mapper<IdentityV2>().mapArray(JSONString:objects.description) {
  179. for identity in identitys{
  180. let vm = CellViewModel(name: identity.name,sourceObject: identity)
  181. self.contacts[tag]?.append(vm)
  182. }
  183. }
  184. default:
  185. break
  186. }
  187. case .failure(let err):
  188. DDLogError(err.localizedDescription)
  189. }
  190. if num == urls.count {
  191. DispatchQueue.main.async {
  192. self.hideLoading()
  193. if self.tableView.mj_header.isRefreshing() {
  194. self.tableView.mj_header.endRefreshing()
  195. }
  196. }
  197. }
  198. DispatchQueue.main.async {
  199. self.tableView.reloadData()
  200. }
  201. }
  202. }
  203. }
  204. }
  205. extension ContactDeptPersonController: ContactItemCellBreadcrumbClickDelegate {
  206. func breadcrumbTap(name: String, distinguished: String) {
  207. for unit in self.headBars {
  208. if distinguished == unit.distinguishedName! {
  209. self.superOrgUnit = unit
  210. self.reloadView()
  211. break;
  212. }
  213. }
  214. }
  215. }