ContactCompanyDeptController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // ContactCompanyDeptController.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 ContactCompanyDeptController: UITableViewController {
  16. var superCompany:Company? {
  17. didSet{
  18. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKey, query: ContactContext.subCompanyByNameQuery, parameter: ["##name##":(superCompany?.name)! as AnyObject])
  19. myCompanyURL = url
  20. }
  21. }
  22. var compContacts:[CellViewModel] = []
  23. var myCompanyURL:String?
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. self.title = superCompany?.name
  27. self.tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadCompData(_:)))
  28. self.tableView.separatorStyle = .none
  29. self.loadCompData(nil)
  30. }
  31. // MARK: - Table view data source
  32. override func numberOfSections(in tableView: UITableView) -> Int {
  33. // #warning Incomplete implementation, return the number of sections
  34. return 1
  35. }
  36. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  37. return self.compContacts.count
  38. }
  39. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  40. let cell = tableView.dequeueReusableCell(withIdentifier: "compContactCell", for: indexPath) as! ContactItemCell
  41. cell.cellViewModel = self.compContacts[(indexPath as NSIndexPath).row]
  42. return cell
  43. }
  44. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  45. let viewModel = self.compContacts[(indexPath as NSIndexPath).row]
  46. switch viewModel.dataType {
  47. case .company(let c):
  48. self.superCompany = c as? Company
  49. self.loadCompData(nil)
  50. case .depart(let d):
  51. self.performSegue(withIdentifier: "sDeptPersonSegue", sender:d)
  52. default:
  53. DDLogDebug(viewModel.name!)
  54. }
  55. }
  56. /// Cell 圆角背景计算
  57. override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  58. //圆率
  59. let cornerRadius:CGFloat = 10.0
  60. //大小
  61. let bounds:CGRect = cell.bounds
  62. //行数
  63. let numberOfRows = tableView.numberOfRows(inSection: indexPath.section)
  64. //绘制曲线
  65. var bezierPath: UIBezierPath? = nil
  66. if (indexPath.row == 0 && numberOfRows == 1) {
  67. //一个为一组时,四个角都为圆角
  68. bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
  69. } else if (indexPath.row == 0) {
  70. //为组的第一行时,左上、右上角为圆角
  71. bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
  72. } else if (indexPath.row == numberOfRows - 1) {
  73. //为组的最后一行,左下、右下角为圆角
  74. bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
  75. } else {
  76. //中间的都为矩形
  77. bezierPath = UIBezierPath(rect: bounds)
  78. }
  79. //cell的背景色透明
  80. cell.backgroundColor = .clear
  81. //新建一个图层
  82. let layer = CAShapeLayer()
  83. //图层边框路径
  84. layer.path = bezierPath?.cgPath
  85. //图层填充色,也就是cell的底色
  86. layer.fillColor = UIColor.white.cgColor
  87. //图层边框线条颜色
  88. /*
  89. 如果self.tableView.style = UITableViewStyleGrouped时,每一组的首尾都会有一根分割线,目前我还没找到去掉每组首尾分割线,保留cell分割线的办法。
  90. 所以这里取巧,用带颜色的图层边框替代分割线。
  91. 这里为了美观,最好设为和tableView的底色一致。
  92. 设为透明,好像不起作用。
  93. */
  94. layer.strokeColor = UIColor.white.cgColor
  95. //将图层添加到cell的图层中,并插到最底层
  96. cell.layer.insertSublayer(layer, at: 0)
  97. }
  98. // MARK: - Navigation
  99. // In a storyboard-based application, you will often want to do a little preparation before navigation
  100. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  101. if segue.identifier == "sDeptPersonSegue" {
  102. let destVC = segue.destination as! ContactDeptPersonController
  103. destVC.superOrgUnit = sender as? OrgUnit
  104. }
  105. }
  106. @objc func loadCompData(_ obj:AnyObject?){
  107. self.showLoading()
  108. AF.request(self.myCompanyURL!).responseJSON {
  109. response in
  110. //debugPrint(response)
  111. switch response.result {
  112. case .success(let val):
  113. self.compContacts.removeAll()
  114. let compnayList = JSON(val)["data"]["companyList"]
  115. let companys = Mapper<Company>().mapArray(JSONString:compnayList.description)
  116. for comp in companys!{
  117. let vm = CellViewModel(name: comp.name,sourceObject: comp)
  118. self.compContacts.append(vm)
  119. }
  120. let departmentList = JSON(val)["data"]["departmentList"]
  121. let departmets = Mapper<Department>().mapArray(JSONString:departmentList.description)
  122. for dept in departmets!{
  123. let vm = CellViewModel(name: dept.name,sourceObject: dept)
  124. self.compContacts.append(vm)
  125. }
  126. // self.showSuccess(title: "加载完成")
  127. case .failure(let err):
  128. DDLogError(err.localizedDescription)
  129. self.showError(title: L10n.errorWithMsg(err.localizedDescription))
  130. }
  131. if self.tableView.mj_header.isRefreshing() {
  132. self.tableView.mj_header.endRefreshing()
  133. }
  134. self.tableView.reloadData()
  135. }}
  136. }