ContactDeptPersonController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. self.tableView.separatorStyle = .none
  49. loadMyDeptData(nil)
  50. }
  51. override func viewWillAppear(_ animated: Bool) {
  52. super.viewWillAppear(animated)
  53. }
  54. // MARK: - Table view data source
  55. override func numberOfSections(in tableView: UITableView) -> Int {
  56. return self.contacts.count
  57. }
  58. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  59. return self.contacts[section]!.count
  60. }
  61. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  62. return 10
  63. }
  64. override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  65. return 3
  66. }
  67. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  68. var cellName = ""
  69. if indexPath.section == 0 {
  70. cellName = "addressBarCell"
  71. }else if indexPath.section == 1 {
  72. cellName = "addressUnitCell"
  73. }else {
  74. cellName = "addressPersonCell"
  75. }
  76. let cell = tableView.dequeueReusableCell(withIdentifier: cellName, for: indexPath) as! ContactItemCell
  77. let cellMod = self.contacts[indexPath.section]![indexPath.row]
  78. if !cellMod.openFlag {
  79. cell.accessoryType = UITableViewCell.AccessoryType.none
  80. } else {
  81. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  82. }
  83. cell.cellViewModel = cellMod
  84. if indexPath.section == 0 {
  85. cell.delegate = self
  86. }
  87. return cell
  88. }
  89. /// Cell 圆角背景计算
  90. override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  91. //圆率
  92. let cornerRadius:CGFloat = 10.0
  93. //大小
  94. let bounds:CGRect = cell.bounds
  95. //行数
  96. let numberOfRows = tableView.numberOfRows(inSection: indexPath.section)
  97. //绘制曲线
  98. var bezierPath: UIBezierPath? = nil
  99. if (indexPath.row == 0 && numberOfRows == 1) {
  100. //一个为一组时,四个角都为圆角
  101. bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
  102. } else if (indexPath.row == 0) {
  103. //为组的第一行时,左上、右上角为圆角
  104. bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
  105. } else if (indexPath.row == numberOfRows - 1) {
  106. //为组的最后一行,左下、右下角为圆角
  107. bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
  108. } else {
  109. //中间的都为矩形
  110. bezierPath = UIBezierPath(rect: bounds)
  111. }
  112. //cell的背景色透明
  113. cell.backgroundColor = .clear
  114. //新建一个图层
  115. let layer = CAShapeLayer()
  116. //图层边框路径
  117. layer.path = bezierPath?.cgPath
  118. //图层填充色,也就是cell的底色
  119. layer.fillColor = UIColor.white.cgColor
  120. //图层边框线条颜色
  121. /*
  122. 如果self.tableView.style = UITableViewStyleGrouped时,每一组的首尾都会有一根分割线,目前我还没找到去掉每组首尾分割线,保留cell分割线的办法。
  123. 所以这里取巧,用带颜色的图层边框替代分割线。
  124. 这里为了美观,最好设为和tableView的底色一致。
  125. 设为透明,好像不起作用。
  126. */
  127. layer.strokeColor = UIColor.white.cgColor
  128. //将图层添加到cell的图层中,并插到最底层
  129. cell.layer.insertSublayer(layer, at: 0)
  130. }
  131. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  132. self.tableView.deselectRow(at: indexPath, animated: false)
  133. let viewModel = self.contacts[indexPath.section]![indexPath.row]
  134. switch viewModel.dataType {
  135. case .depart(let d):
  136. if viewModel.openFlag {
  137. self.superOrgUnit = d as? OrgUnit
  138. self.reloadView()
  139. }
  140. case .identity(let i):
  141. self.performSegue(withIdentifier: "showIdentityPersonSegueV2", sender: i)
  142. default:
  143. self.tableView.deselectRow(at: indexPath, animated: false)
  144. //DDLogDebug(viewModel.name!)
  145. }
  146. }
  147. func reloadView() {
  148. for i in 0..<self.contacts.count {
  149. self.contacts[i]?.removeAll()
  150. }
  151. self.tableView.reloadData()
  152. self.title = superOrgUnit?.name
  153. loadMyDeptData(nil)
  154. }
  155. func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
  156. if let scheme = URL.scheme {
  157. switch scheme {
  158. case "reloadto" :
  159. let distinguishedName = (URL.description as NSString).substring(from: 9)
  160. for unit in self.headBars {
  161. if distinguishedName == unit.distinguishedName! {
  162. self.superOrgUnit = unit
  163. self.reloadView()
  164. break;
  165. }
  166. }
  167. default:
  168. break
  169. }
  170. }
  171. return true
  172. }
  173. // MARK: - Navigation
  174. // In a storyboard-based application, you will often want to do a little preparation before navigation
  175. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  176. if segue.identifier == "showIdentityPersonSegueV2" {
  177. let destVC = segue.destination as! ContactPersonInfoV2ViewController
  178. destVC.identity = sender as? IdentityV2
  179. }
  180. }
  181. @objc func loadMyDeptData(_ sender:AnyObject?){
  182. let urls = [0:"111",1:subUnitURL,2:subIdentityURL]
  183. self.showLoading()
  184. var num = 0
  185. for (tag,url) in urls {
  186. num += 1
  187. if tag == 0 {
  188. self.contacts[tag]?.removeAll()
  189. if headBars.count > 0 {
  190. var barText: [OrgUnit] = []
  191. if headBars.count == 1 {
  192. barText.append(headBars[0])
  193. }else {
  194. barText.append(headBars[headBars.count-2])
  195. barText.append(headBars[headBars.count-1])
  196. }
  197. let head = HeadTitle(name: "bar", barText: barText)
  198. let vm = CellViewModel(name: "bar",sourceObject: head)
  199. self.contacts[tag]?.append(vm)
  200. }
  201. continue
  202. }
  203. AF.request(url!).responseJSON {
  204. response in
  205. self.contacts[tag]?.removeAll()
  206. switch response.result {
  207. case .success(let val):
  208. let objects = JSON(val)["data"]
  209. DDLogDebug(objects.description)
  210. self.contacts[tag]?.removeAll()
  211. switch tag {
  212. case 1:
  213. if let units = Mapper<OrgUnit>().mapArray(JSONString:objects.description) {
  214. for unit in units{
  215. // 权限排除
  216. if !OrganizationPermissionManager.shared.isExcludeUnit(unit: unit.distinguishedName ?? "") {
  217. let name = "\(unit.name!)(" + String(unit.subDirectIdentityCount+unit.subDirectUnitCount) + ")"
  218. let vm = CellViewModel(name: name,sourceObject: unit)
  219. self.contacts[tag]?.append(vm)
  220. }
  221. }
  222. }
  223. case 2:
  224. if let identitys = Mapper<IdentityV2>().mapArray(JSONString:objects.description) {
  225. for identity in identitys{
  226. // 权限排除
  227. if !OrganizationPermissionManager.shared.isExcludePerson(person: identity.distinguishedName ?? "") {
  228. let vm = CellViewModel(name: identity.name,sourceObject: identity)
  229. self.contacts[tag]?.append(vm)
  230. }
  231. }
  232. }
  233. default:
  234. break
  235. }
  236. case .failure(let err):
  237. DDLogError(err.localizedDescription)
  238. }
  239. if num == urls.count {
  240. DispatchQueue.main.async {
  241. self.hideLoading()
  242. if self.tableView.mj_header.isRefreshing() {
  243. self.tableView.mj_header.endRefreshing()
  244. }
  245. }
  246. }
  247. DispatchQueue.main.async {
  248. self.tableView.reloadData()
  249. }
  250. }
  251. }
  252. }
  253. }
  254. extension ContactDeptPersonController: ContactItemCellBreadcrumbClickDelegate {
  255. func breadcrumbTap(name: String, distinguished: String) {
  256. for unit in self.headBars {
  257. if distinguished == unit.distinguishedName! {
  258. self.superOrgUnit = unit
  259. self.reloadView()
  260. break;
  261. }
  262. }
  263. }
  264. }