ContactCompanyDeptController.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.loadCompData(nil)
  29. }
  30. // MARK: - Table view data source
  31. override func numberOfSections(in tableView: UITableView) -> Int {
  32. // #warning Incomplete implementation, return the number of sections
  33. return 1
  34. }
  35. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  36. return self.compContacts.count
  37. }
  38. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  39. let cell = tableView.dequeueReusableCell(withIdentifier: "compContactCell", for: indexPath) as! ContactItemCell
  40. cell.cellViewModel = self.compContacts[(indexPath as NSIndexPath).row]
  41. return cell
  42. }
  43. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  44. let viewModel = self.compContacts[(indexPath as NSIndexPath).row]
  45. switch viewModel.dataType {
  46. case .company(let c):
  47. self.superCompany = c as? Company
  48. self.loadCompData(nil)
  49. case .depart(let d):
  50. self.performSegue(withIdentifier: "sDeptPersonSegue", sender:d)
  51. default:
  52. DDLogDebug(viewModel.name!)
  53. }
  54. }
  55. // MARK: - Navigation
  56. // In a storyboard-based application, you will often want to do a little preparation before navigation
  57. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  58. if segue.identifier == "sDeptPersonSegue" {
  59. let destVC = segue.destination as! ContactDeptPersonController
  60. destVC.superOrgUnit = sender as? OrgUnit
  61. }
  62. }
  63. @objc func loadCompData(_ obj:AnyObject?){
  64. self.showLoading(title: "加载中...")
  65. AF.request(self.myCompanyURL!).responseJSON {
  66. response in
  67. //debugPrint(response)
  68. switch response.result {
  69. case .success(let val):
  70. self.compContacts.removeAll()
  71. let compnayList = JSON(val)["data"]["companyList"]
  72. let companys = Mapper<Company>().mapArray(JSONString:compnayList.description)
  73. for comp in companys!{
  74. let vm = CellViewModel(name: comp.name,sourceObject: comp)
  75. self.compContacts.append(vm)
  76. }
  77. let departmentList = JSON(val)["data"]["departmentList"]
  78. let departmets = Mapper<Department>().mapArray(JSONString:departmentList.description)
  79. for dept in departmets!{
  80. let vm = CellViewModel(name: dept.name,sourceObject: dept)
  81. self.compContacts.append(vm)
  82. }
  83. self.showSuccess(title: "加载完成")
  84. case .failure(let err):
  85. DDLogError(err.localizedDescription)
  86. self.showError(title: "加载失败")
  87. }
  88. if self.tableView.mj_header.isRefreshing() {
  89. self.tableView.mj_header.endRefreshing()
  90. }
  91. self.tableView.reloadData()
  92. }}
  93. }