123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- //
- // OOListUnitViewModel.swift
- // o2app
- //
- // Created by 刘振兴 on 2017/11/21.
- // Copyright © 2017年 zone. All rights reserved.
- //
- import UIKit
- import Moya
- class OOListUnitViewModel:NSObject {
-
- private var unitDatas:[Int:[DataModel]] = [:]
-
- private let contactAPI = OOMoyaProvider<OOContactAPI>()
-
- typealias AddBlock = (_ msg:String?) -> Void
-
- var updateBlock:AddBlock?
-
- override init() {
- super.init()
- initSetup()
- }
-
- func initSetup() {
- unitDatas[0] = []
- }
-
- // MARK: - 读取数据
- func refreshData(_ unitFlag:String){
- initSetup()
- //读取下一级
- contactAPI.request(.listSubDirect(unitFlag)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOUnitModel]>>(result)
- if myResult.isResultSuccess() {
- myResult.model?.data?.forEach({ (uModel) in
- self.unitDatas[0]?.append(uModel)
- })
- }
- guard let block = self.updateBlock else {
- return
- }
- if myResult.isResultSuccess() {
- block(nil)
- }else{
- block(myResult.error.debugDescription)
- }
- }
- //读取本级下的所有人员
- contactAPI.request(.getUnit(unitFlag)) { (result) in
- let myResult = OOResult<BaseModelClass<OOUnitModel>>(result)
- if myResult.isResultSuccess() {
- let model = myResult.model?.data
- model?.woSubDirectIdentityList?.forEach({ (iModel) in
- self.unitDatas[0]?.append(iModel.woPerson!)
- })
- }
- guard let block = self.updateBlock else {
- return
- }
- if myResult.isResultSuccess() {
- block(nil)
- }else{
- block(myResult.error.debugDescription)
- }
- }
-
- }
-
- // MARK: - 获取icon
- func getIconOfPerson(_ person:OOPersonModel,compeletionBlock:@escaping (_ image:UIImage?,_ errMsg:String?) -> Void) {
- contactAPI.request(.iconByPerson(person.id!)) { (result) in
- switch result {
- case .success(let res):
- guard let image = UIImage(data: res.data) else {
- compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
- return
- }
- compeletionBlock(image,nil)
- break
- case .failure(let err):
- compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
- break
- }
- }
- }
-
- }
- extension OOListUnitViewModel{
- func numberOfSections() -> Int {
- return unitDatas.count
- }
-
- func numberOfRowsInSection(_ section: Int) -> Int {
- return unitDatas[section]!.count
- }
-
- func nodeForIndexPath(_ indexPath:IndexPath) -> DataModel? {
- return unitDatas[indexPath.section]?[indexPath.row]
- }
-
- func headerHeightOfSection(_ section:Int) -> CGFloat {
- return 40.0
- }
-
- func footerHeightOfSection(_ section:Int) -> CGFloat {
- return 10.0
- }
-
- func headerTypeOfSection(_ section:Int) -> UIView {
- return UIView()
- }
-
- }
|