// // MainTabBarController.swift // MTP2_iOS // // Created by Muchinfo on 2020/12/28. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import WHToast import NVActivityIndicatorView import DeviceKit /// normal var normal: [NSAttributedString.Key : Any] { return [NSAttributedString.Key.font: Device.current.isFaceIDCapable ? UIFont.font_15 : UIFont.font_13, NSAttributedString.Key.foregroundColor: UIColor.fromHex(rgbValue: 0x8F8AA6)] } /// selected var selected: [NSAttributedString.Key : Any] { return [NSAttributedString.Key.font: Device.current.isFaceIDCapable ? UIFont.font_15 : UIFont.font_13, NSAttributedString.Key.foregroundColor: UIColor.fromHex(rgbValue: 0x2A80B9)] } /// 主菜单项控制视图容器类 class MainTabBarController: UITabBarController { // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// white UITabBar.appearance().barTintColor = .white /// 设置通用样式 UITabBarItem.appearance().setTitleTextAttributes(normal, for: .normal) UITabBarItem.appearance().setTitleTextAttributes(selected, for: .selected) /// 视图容器初始化 initControllers() /// 数据初始化 requestData() } /// 视图容器初始化 fileprivate func initControllers() { /// 视图 var controllers: [UIViewController] = [] /// 企业风管 if MTP2BusinessCore.shared.address?.oem == "qyfg" { controllers.append(contentsOf: initFxglViewControllers()) } /// 千海金 if MTP2BusinessCore.shared.address?.oem == "qhj" { controllers.append(contentsOf: initQhjViewControllers()) } /// 云融大宗 if MTP2BusinessCore.shared.address?.oem == "yrdz" { controllers.append(contentsOf: initHnstViewControllers()) } /// 天津麦顿 if MTP2BusinessCore.shared.address?.oem == "tjmd" { controllers.append(contentsOf: initTjmdViewControllers()) } /// 廣陈优品 if MTP2BusinessCore.shared.address?.oem == "xhcp" { controllers.append(contentsOf: initXhcpViewControllers()) } /// 设置视图 self.viewControllers = controllers } /************************************************企业风管***********************************************/ /// 初始化企业风管视图容器控制类数据 /// - Returns: [UIViewController] fileprivate func initFxglViewControllers() -> [UIViewController] { /// 视图 var viewControllers: [UIViewController] = [] /// 首页 let h = viewController("Main", "HomeNav") h.tabBarItem = getTabBarItem("首页", "tab_home", "tab_home_selected") viewControllers.append(h) /// 该账户没权限 guard let commonManager = MTP2BusinessCore.shared.commonManager else { return viewControllers } /// 现货 判断其是否有现货权限 if commonManager.containMenu(key: "client_spots") { let s = viewController("Spots", "SpotsNav") s.tabBarItem = getTabBarItem("现货", "tab_caigou", "tab_caigou_selected") viewControllers.append(s) } /// 期货 判断其是否有期货权限 if commonManager.containMenu(key: "client_futures") { let f = viewController("Futures", "FuturesNav") f.tabBarItem = getTabBarItem("期货", "tab_qihuo", "tab_qihuo_selected") viewControllers.append(f) } /// 敞口 判断其是否有敞口权限 if commonManager.containMenu(key: "client_exposure") { let e = viewController("Exposure", "ExposureNav") e.tabBarItem = getTabBarItem("敞口", "tab_changkou", "tab_changkou_selected") viewControllers.append(e) } /// 报表 判断其是否有报表权限 if commonManager.containMenu(key: "client_report") { let r = viewController("Report", "ReportNav") r.tabBarItem = getTabBarItem("报表", "tab_xiaoshou", "tab_xiaoshou_selected") viewControllers.append(r) } return viewControllers } /************************************************天津麦顿***********************************************/ /// 初始化天津麦顿视图容器控制类数据 /// - Returns: [UIViewController] fileprivate func initTjmdViewControllers() -> [UIViewController] { /// 视图 var viewControllers: [UIViewController] = [] /// 首页 let h = viewController("TjmdHome", "TjmdHomeNav") h.tabBarItem = getTabBarItem("首页", "tab_home", "tab_home_selected") viewControllers.append(h) /// 该账户没权限 guard let yrdzManager = MTP2BusinessCore.shared.yrdzManager, let commonManager = MTP2BusinessCore.shared.commonManager else { return viewControllers } /// 市场属性 var sections: [MoMarketSection] = [] /// 如果多于三个 则取前三个 if yrdzManager.sections.count > 3 { sections = Array(yrdzManager.sections[0...2]) } else { sections = yrdzManager.sections } /// MD Fuck TMD for (index, obj) in sections.enumerated() { /// 参考行情 if let _ = obj.mlist.first(where: {$0.trademode == .TRADEMODE_QHJ_CKHQ}) { let q = self.viewController("QHJQuote", "BJBQuote") as! BJBQuoteViewController q.title = "参考行情" q.isQhj = false let nav = BaseNavigationController(rootViewController: q) nav.tabBarItem = getTabBarItem(obj.marketsectionname, "hnst_tab_hyjy", "hnst_tab_hyjy_selected") viewControllers.append(nav) } else { let nav = viewController("YrdzQuote", "TjmdQuoteNav") as! BaseNavigationController switch index { case 0: nav.tabBarItem = getTabBarItem(obj.marketsectionname, "hnst_tab_cdmy", "hnst_tab_cdmy_selected") default: nav.tabBarItem = getTabBarItem(obj.marketsectionname, "hnst_tab_cdys", "hnst_tab_cdys_selected") } (nav.viewControllers[0] as? TjmdQuoteViewController)?.takeInfo = obj viewControllers.append(nav) } } /// 云融大宗-我的 判断其是否有云融大宗-我的权限 if commonManager.containMenu(key: "client_mobile_hnstmine") { let m = viewController("YrdzMine", "YrdzMineNav") m.tabBarItem = getTabBarItem("我的", "hnst_tab_mine", "hnst_tab_mine_selected") viewControllers.append(m) } return viewControllers } /************************************************千海金***********************************************/ /// 初始化千海金视图容器控制类数据 /// - Returns: [UIViewController] fileprivate func initQhjViewControllers() -> [UIViewController] { /// 视图 var viewControllers: [UIViewController] = [] /// 首页 let h = viewController("Main", "HomeNav") h.tabBarItem = getTabBarItem("首页", "tab_home", "tab_home_selected") viewControllers.append(h) /// 该账户没权限 guard let commonManager = MTP2BusinessCore.shared.commonManager else { return viewControllers } /// 千海金-铂金宝 判断其是否有千海金-铂金宝 if commonManager.containMenu(key: "client_qhjbjb") { let b = viewController("QHJQuote", "BJBNav") b.tabBarItem = getTabBarItem("铂金宝", "qhj_tab_bjb", "qhj_tab_bjb_selected") viewControllers.append(b) } /// 千海金-饰品回收 判断其是否有千海金-饰品回收 if commonManager.containMenu(key: "client_qhjsphg") { let s = viewController("QHJQuote", "SPHGNav") s.tabBarItem = getTabBarItem("饰品回收", "qhj_tab_sphg", "qhj_tab_sphg_selected") viewControllers.append(s) } /// 千海金-我的 判断其是否有千海金-我的权限 if commonManager.containMenu(key: "client_qhjmine") { let m = viewController("QHJMine", "QhjMine") m.tabBarItem = getTabBarItem("我的", "qhj_tab_mine", "qhj_tab_mine_selected") viewControllers.append(m) } return viewControllers } /************************************************云融大宗***********************************************/ /// 初始化云融大宗视图容器控制类数据 /// - Returns: [UIViewController] fileprivate func initHnstViewControllers() -> [UIViewController] { /// 视图 var viewControllers: [UIViewController] = [] /// 首页 let h = viewController("YrdzHome", "YrdzHomeNav") h.tabBarItem = getTabBarItem("首页", "tab_home", "tab_home_selected") viewControllers.append(h) /// 该账户没权限 guard let commonManager = MTP2BusinessCore.shared.commonManager else { return viewControllers } /// 云融大宗-仓单贸易 判断其是否云融大宗-仓单贸易权限 if commonManager.containMenu(key: "client_mobile_hnstcdmy") { let c = viewController("YrdzQuote", "WarehouseNav") as! BaseNavigationController (c.viewControllers[0] as? WarehouseQuoteViewController)?.isCdmy = 1 c.tabBarItem = getTabBarItem("仓单贸易", "hnst_tab_cdmy", "hnst_tab_cdmy_selected") viewControllers.append(c) } /// 云融大宗-仓单预售 判断其是否有顺通-仓单预售权限 if commonManager.containMenu(key: "client_mobile_hnstcdys") { let c = viewController("YrdzQuote", "WarehouseNav") as! BaseNavigationController (c.viewControllers[0] as? WarehouseQuoteViewController)?.isCdmy = 0 c.tabBarItem = getTabBarItem("仓单预售", "hnst_tab_cdys", "hnst_tab_cdys_selected") viewControllers.append(c) } /// 云融大宗-合约交易 判断其是否有顺通-订单交易权限 if commonManager.containMenu(key: "client_mobile_hnsthyjy") { let h = viewController("YrdzQuote", "WarehouseNav") as! BaseNavigationController (h.viewControllers[0] as? WarehouseQuoteViewController)?.isCdmy = 2 h.tabBarItem = getTabBarItem("订单交易", "hnst_tab_hyjy", "hnst_tab_hyjy_selected") viewControllers.append(h) } /// 云融大宗-我的 判断其是否有云融大宗-我的权限 if commonManager.containMenu(key: "client_mobile_hnstmine") { let m = viewController("YrdzMine", "YrdzMineNav") m.tabBarItem = getTabBarItem("我的", "hnst_tab_mine", "hnst_tab_mine_selected") viewControllers.append(m) } return viewControllers } /************************************************廣陈优品***********************************************/ /// 初始化廣陈优品视图容器控制类数据 /// - Returns: [UIViewController] fileprivate func initXhcpViewControllers() -> [UIViewController] { /// 视图 var viewControllers: [UIViewController] = [] /// 首页 let h = viewController("xhcp", "XHCPHomeNav") h.tabBarItem = getTabBarItem("首页", "tab_home", "tab_home_selected") viewControllers.append(h) /// 该账户没权限 guard let yrdzManager = MTP2BusinessCore.shared.yrdzManager, let commonManager = MTP2BusinessCore.shared.commonManager else { return viewControllers } /// 市场属性 var sections: [MoMarketSection] = [] /// 如果多于三个 则取前三个 if yrdzManager.sections.count > 3 { sections = Array(yrdzManager.sections[0...2]) } else { sections = yrdzManager.sections } /// MD Fuck TMD for (index, obj) in sections.enumerated() { /// 参考行情 if let _ = obj.mlist.first(where: {$0.trademode == .TRADEMODE_CDYS}) { let s = self.viewController("xhcp", "XHCPPreSaleNav") as! BaseNavigationController s.tabBarItem = getTabBarItem(obj.marketsectionname, "hnst_tab_hyjy", "hnst_tab_hyjy_selected") viewControllers.append(s) } else { /// 系统参数 093 现货商品是否有详 let system = MTP2BusinessCore.shared.accountManager?.loginQuery?.systemParams.first(where: {$0.paramcode == "093"}) if system?.paramvalue == "1" { let l = viewController("xhcp", "XHCPListingQuoteNav") as! BaseNavigationController l.tabBarItem = getTabBarItem(obj.marketsectionname, "hnst_tab_cdys", "hnst_tab_cdys_selected") (l.viewControllers[0] as? XHCPListingQuoteViewController)?.takeInfo = obj viewControllers.append(l) } else { let nav = viewController("YrdzQuote", "TjmdQuoteNav") as! BaseNavigationController switch index { case 0: nav.tabBarItem = getTabBarItem(obj.marketsectionname, "hnst_tab_cdmy", "hnst_tab_cdmy_selected") default: nav.tabBarItem = getTabBarItem(obj.marketsectionname, "hnst_tab_cdys", "hnst_tab_cdys_selected") } (nav.viewControllers[0] as? TjmdQuoteViewController)?.takeInfo = obj viewControllers.append(nav) } } } /// 云融大宗-我的 判断其是否有云融大宗-我的权限 if commonManager.containMenu(key: "client_mobile_hnstmine") { let m = viewController("YrdzMine", "YrdzMineNav") m.tabBarItem = getTabBarItem("我的", "hnst_tab_mine", "hnst_tab_mine_selected") viewControllers.append(m) } return viewControllers } /// 获取 /// - Parameters: /// - title: title /// - image: image /// - selectedImage: selectedImage /// - Returns: UITabBarItem fileprivate func getTabBarItem(_ title: String, _ image: String, _ selectedImage: String) -> UITabBarItem { return UITabBarItem(title: title, image: UIImage.getImage(name: image), selectedImage: UIImage.getImage(name: selectedImage)) } // MARK: - 接口请求 /// 数据初始化 private func requestData() { /// 异常 guard let oem = MTP2BusinessCore.shared.address?.oem, let quoteManger = MTP2BusinessCore.shared.quoteManager, let orderManager = MTP2BusinessCore.shared.orderManager, let spotManager = MTP2BusinessCore.shared.spotManager, let goodsManager = MTP2BusinessCore.shared.goodsManager, let qhjManager = MTP2BusinessCore.shared.qhjManager, let quoteSubscriptManager = MTP2BusinessCore.shared.quoteSubscriptManager, let commonManager = MTP2BusinessCore.shared.commonManager, let yrdzManager = MTP2BusinessCore.shared.yrdzManager else { return } /// 链接行情 let group = DispatchGroup() /// startAnimating NVActivityIndicatorPresenter.sharedInstance.startAnimating(self.animData("数据更新中...")) /// 千海金 需要查询的接口 if oem.lowercased() == "qhj" { group.enter() /// 查询千海金定投单据数据信息 qhjManager.queryQhjRStrategy(status: "1,2") { strategy, _, _ in dPrint("queryQhjContract 查询千海金定投单据数据信息\(strategy ? "成功" : "失败")") group.leave() } group.enter() /// 查询千海金-融资单据数据信息 qhjManager.queryQhjContract(scfcontractid: nil) { contract, _, _ in dPrint("queryQhjContract 查询千海金-融资单据数据信息\(contract ? "成功" : "失败")") group.leave() } } group.enter() quoteManger.requestQuoteLink(callback: { (isSuccess, _) in dPrint("[\(#function)] \(isSuccess ? "行情链路链接成功" : "行情链路链接失败")") group.leave() }) group.enter() /// 查询省市区信息 commonManager.requestGetDivisions { (isSuccess, _, _) in dPrint("requestGetDivisions 查询省市区信息\(isSuccess ? "成功" : "失败")") group.leave() } /// 查询商品交割关系表 group.enter() yrdzManager.queryDeliveryRelation(goodsid: nil, deliverygoodsid: nil, marketid: nil) { isSuccess, _, _ in dPrint("[\(#function)] 查询商品交割关系表\(isSuccess ? "成功" : "失败")") group.leave() } /// 查询市场交易参数配置 group.enter() yrdzManager.queryWrMarketTradeConfig(marketid: nil) { isSuccess, _, _ in dPrint("[\(#function)] 查询市场交易参数配置\(isSuccess ? "成功" : "失败")") group.leave() } group.enter() /// 查询商品扩展信息 qhjManager.queryGoodsEx(nil) { isSuccess, _, _ in dPrint("[\(#function)] 查询商品扩展信息\(isSuccess ? "成功" : "失败")") group.leave() } group.enter() /// 持仓汇总查询(合约市场) orderManager.queryTradePosition(tradeMode: nil) { isSuccess, _, objs in dPrint("[\(#function)] 持仓汇总查询(合约市场)\(isSuccess ? "成功" : "失败")") group.leave() } group.enter() /// 委托单数据查询(合约市场) orderManager.queryTradeOrderDetail(tradeMode: nil, orderStatus: nil, orderID: nil) { isSuccess, _, _ in dPrint("[\(#function)] 委托单数据查询(合约市场)\(isSuccess ? "成功" : "失败")") group.leave() } /// 头寸查询 group.enter() orderManager.queryErmcpTradePositions(marketID: nil) { (isSuccess, _, _) in dPrint("[\(#function)] \(isSuccess ? "头寸查询成功" : "头寸查询失败")") group.leave() } group.enter() orderManager.queryErmcpTradeDetails(goodsid: nil, buyOrSell: nil, orderID: nil) { (isSuccess, _, _) in dPrint("[\(#function)] \(isSuccess ? "今日成交查询成功" : "今日成交查询失败")") group.leave() } group.enter() /// 查询现货商品数据信息 spotManager.queryDeliveryGoods(excludecfg: nil) { (isSuccess, _, _) in dPrint("queryDeliveryGoods 查询现货商品数据信息\(isSuccess ? "成功" : "失败")") group.leave() } /// 查询用户藏品数据 group.enter() goodsManager.queryUserFavoriteGoodses { (isSuccess, _, _) in dPrint("queryUserFavoriteGoodses 查询用户藏品数据\(isSuccess ? "成功" : "失败")") group.leave() } /// 获取主力合约数据 group.enter() goodsManager.getErmcpGoodsSortByPosition(sortIndex: "1") { (isSuccess, _, _) in dPrint("getErmcpGoodsSortByPosition 获取主力合约数据\(isSuccess ? "成功" : "失败")") group.leave() } /// 掉期商品 let marketid = MTP2BusinessCore.getMarketId(tradeMode: .TRADEMODE_TJMD) group.enter() /// 查询商品掉期报价数据 yrdzManager.queryQuoteGoodsList(goodsid: nil, goodsgroupid: nil, marketids: "\(marketid)") { isSuccess, er, objs in dPrint("getErmcpGoodsSortByPosition查询商品掉期报价数据\(isSuccess ? "成功" : "失败")") group.leave() } /// 查询系统状态 group.enter() goodsManager.queryMarketRun(marketId: 0, callback: { (isSuccess, _, _) in dPrint("[\(#function)] 查询系统状态\(isSuccess ? "成功" : "失败")") group.leave() }) group.notify(queue: .main) { /// stopAnimating NVActivityIndicatorPresenter.sharedInstance.stopAnimating() var goodsCodes = Set() /// 如果头寸 不为空 需要去订阅行情 if let ps = orderManager.positionDetails, ps.count != 0 { ps.forEach { obj in goodsCodes.insert(obj.goodscode) } } /// 如果头寸 不为空 需要去订阅行情 if let ps = orderManager.qhjTradePositions, ps.count != 0 { ps.forEach { obj in /// 掉期商品 if obj.trademode == .TRADEMODE_TJMD, let goodsList = yrdzManager.goodsList.first(where: {$0.goodsid == obj.goodsid}) { goodsCodes.insert(goodsList.refgoodscode) } else { goodsCodes.insert(obj.goodscode) } } } quoteSubscriptManager.updateQuoteSubcriptGoods(uuid: UUID().uuidString, goodsCode: goodsCodes) } } }