//
|
// HomeVC.swift
|
// WanPai
|
//
|
// Created by 杨锴 on 2023/6/8.
|
//
|
|
import UIKit
|
import JQTools
|
import RxSwift
|
import RxCocoa
|
import SDWebImage
|
|
//http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
|
//http://vjs.zencdn.net/v/oceans.mp4
|
//https://media.w3.org/2010/05/sintel/trailer.mp4
|
|
let ChooseHomeStore_Noti = Notification.Name.init("ChooseHomeStore_Noti")
|
|
class HomeVC: BaseVC{
|
@IBOutlet weak var collectionView: BaseCollectionView!
|
@IBOutlet weak var label_vipInfo: UILabel!
|
@IBOutlet weak var view_banner: CommonBannerView!
|
@IBOutlet weak var btn_localTap: TapBtn!
|
private var layout:WaterFallFlowLayout!
|
@IBOutlet weak var label_store: UILabel!
|
private var items = Array<Any>()
|
private var storeModel:HomeStoreModel?
|
private var storeId:Int?
|
private var currentLocal:CLLocation?
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
startLocation()
|
|
Services.startCourseHome().subscribe(onNext: {[weak self] data in
|
if let model = data.data{
|
if model.isThere == 1{
|
let courseInfoVC = CourseInfoVC()
|
courseInfoVC.startClouseHomeModel = model
|
let clouseNav = BaseNav(rootViewController: courseInfoVC)
|
clouseNav.tabBarItem = UITabBarItem(title: "玩湃运动营", image: UIImage(named: "tabbar_course"), selectedImage: UIImage(named: "tabbar_course_s")?.withRenderingMode(.alwaysOriginal))
|
self?.tabBarController?.viewControllers![1] = clouseNav
|
}else {
|
if let vc = (self?.tabBarController?.viewControllers?[1] as? BaseNav)?.topViewController as? CourseVC{
|
vc.startClouseHomeModel = model
|
}
|
}
|
}
|
}).disposed(by: disposeBag)
|
|
Services.bannerList(position: .homeTop).subscribe(onNext: {[weak self] data in
|
if let models = data.data{
|
let imgs = models.compactMap({$0.img})
|
self?.view_banner.setImages(images: imgs, type: .URL) { index in
|
jumpPage(name: models[index].name, page: models[index].page, type: models[index].type, id: models[index].turnId)
|
}
|
}
|
}) { error in
|
|
}.disposed(by: disposeBag)
|
|
}
|
|
override func setUI() {
|
view.backgroundColor = UIColor(hexStr: "EEF0F3")
|
|
layout = WaterFallFlowLayout()
|
layout.cols = 2
|
layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34)
|
layout.delegate = self
|
collectionView.collectionViewLayout = layout
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.backgroundColor = .white
|
collectionView.register(UINib(nibName: "HomeCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeCCell")
|
// collectionView.jq_setEmptyView("无数据", image: UIImage(named: "icon_LOGO")?.jq_imageWithTintColor(color: .gray), foregroundColor: .gray, clouse: nil)
|
}
|
|
override func setRx() {
|
NotificationCenter.default.rx.notification(ChooseHomeStore_Noti).take(until: self.rx.deallocated).subscribe(onNext: { [weak self] noti in
|
guard let weakSelf = self else { return }
|
weakSelf.chooseStoresAction(weakSelf.btn_localTap)
|
}).disposed(by: disposeBag)
|
}
|
|
private func getStoreInfo(){
|
Services.homeInfo().subscribe(onNext: {[weak self] data in
|
if let m = data.data{
|
var text = "欢迎来到 快乐运动俱乐部"
|
if m.storeId != 0{
|
self?.label_store.text = m.name.isEmpty ? "门店获取失败":m.name
|
self?.storeModel = m
|
self?.storeId = m.storeId
|
if m.isVip == 0{text.append("\n加入我们 成为会员")}
|
self?.label_vipInfo.text = text
|
UserDefaults.standard.set(m.toJSONString(), forKey: "CurrentStore")
|
UserDefaults.standard.synchronize()
|
self?.getStoreItemList()
|
}
|
}
|
}) { [weak self] error in
|
self?.label_store.text = "门店获取失败"
|
}.disposed(by: disposeBag)
|
}
|
|
private func getStoreItemList(){
|
|
showHUD()
|
items.removeAll()
|
let group = DispatchGroup()
|
let queue = DispatchQueue(label: "imgRadio")
|
queue.async(group: group){[weak self] () in
|
guard let weakSelf = self else { return }
|
group.enter()
|
Services.homeStoreConfig(storeId: weakSelf.storeId!).subscribe(onNext: {[weak self] data in
|
self?.items.append(contentsOf: (data.data ?? []).filter({$0.isOpen == 1}))
|
Services.homeStoreConfig2(storeId: weakSelf.storeId!).subscribe(onNext: {[weak self] data in
|
self?.items.append(contentsOf: (data.data ?? []))
|
group.leave()
|
}) { error in
|
group.leave()
|
}.disposed(by: weakSelf.disposeBag)
|
}) { error in
|
group.leave()
|
}.disposed(by: weakSelf.disposeBag)
|
}
|
|
group.notify(queue: .main){[weak self] () in
|
guard let weakSelf = self else { return }
|
var i = 0
|
showHUD()
|
let w = (weakSelf.view.frame.width - 78.0) / 2.0
|
for (_,model) in weakSelf.items.enumerated(){
|
if let m = model as? HomeStoreConfigModel{
|
SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
|
|
} completed: {[weak self] image, data, error, status in
|
if let img = image{
|
m.cellHeight = w / (img.size.width / img.size.height)
|
}else{
|
m.cellHeight = w / (m.type.defaultImg.size.width / m.type.defaultImg.size.height)
|
}
|
i += 1
|
if i == weakSelf.items.count{
|
self?.updateUI()
|
}
|
}
|
}
|
|
if let m = model as? HomeStoreTopBannerModel{
|
SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
|
|
} completed: {[weak self] image, data, error, status in
|
if let img = image{
|
m.cellHeight = w / (img.size.width / img.size.height)
|
i += 1
|
if i == weakSelf.items.count{
|
self?.updateUI()
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
|
private func updateUI(){
|
//重置Layout,不然不更新
|
layout = WaterFallFlowLayout()
|
layout.itemCount = items.count
|
layout.delegate = self
|
layout.cols = 2
|
layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34)
|
collectionView.collectionViewLayout = self.layout
|
collectionView.reloadData()
|
hiddenHUD()
|
}
|
|
private func startLocation(){
|
locationTool.startLocation { [weak self] local in
|
guard self!.currentLocal != nil else {
|
self!.currentLocal = local
|
self!.getStoreInfo()
|
locationTool.stopLocation()
|
return
|
}
|
|
} errorClouse: { [weak self] error in
|
alertError(msg: "定位获取失败")
|
self?.label_store.text = "定位获取失败"
|
}
|
}
|
|
@IBAction func scanAction(_ sender: UIButton) {
|
let codeVC = CommonScanQRCodeVC { str, status in
|
|
}
|
push(vc: codeVC)
|
}
|
|
|
@IBAction func joinMemberAction(_ sender: UIButton) {
|
let joinMemberVC = JoinMemberIntroduceVC()
|
push(vc: joinMemberVC)
|
}
|
|
|
@IBAction func chooseStoresAction(_ sender: TapBtn) {
|
StoresChooseView.show { [weak self] storeId,storeName in
|
guard let weakSelf = self else { return }
|
weakSelf.storeId = storeId
|
weakSelf.label_store.text = storeName
|
weakSelf.storeModel = HomeStoreModel(isVip: 0, storeId: storeId, name: storeName, lon: 0, lat: 0)
|
weakSelf.items.removeAll()
|
weakSelf.getStoreItemList()
|
|
UserDefaults.standard.set(weakSelf.storeModel!.toJSONString(), forKey: "CurrentStore")
|
UserDefaults.standard.synchronize()
|
|
}
|
}
|
|
override var preferredStatusBarStyle: UIStatusBarStyle{
|
return .lightContent
|
}
|
}
|
|
extension HomeVC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
var simpleModel:NormalSimpleModel?
|
if storeModel != nil{
|
simpleModel = NormalSimpleModel(id: storeModel!.storeId, name: storeModel!.name)
|
}
|
|
let item = items[indexPath.row]
|
if let m = item as? HomeStoreConfigModel{
|
switch m.type{
|
case .course:
|
let vc = CourseListVC(selectStore: simpleModel)
|
push(vc: vc)
|
case .booking:
|
let vc = YardListVC(selectStore: simpleModel)
|
push(vc: vc)
|
case .activity:
|
let vc = ActivityListVC()
|
push(vc: vc)
|
case .video:
|
let vc = CourseOnlineListVC(position: 2)
|
vc.title = "看视频得奖励"
|
push(vc: vc)
|
case .coin:
|
let vc = CourseOnlineListVC(position: 1)
|
vc.title = "线上课得积分"
|
push(vc: vc)
|
case .ticket:
|
let vc = CoinStoreCenterVC(selectStore: simpleModel)
|
push(vc: vc)
|
|
case .wisdomCourt:
|
guard storeId != 0 else {alert(msg: "请先选择门店");return}
|
let vc = GamesVC()
|
push(vc: vc)
|
case .welfare:
|
if storeId != 0{
|
Services.queryStoreFreeBenefit(id: storeId!).subscribe(onNext: {[weak self] data in
|
if let m = data.data{
|
let vc = WelfareFreeVC(m)
|
self?.push(vc: vc)
|
}
|
}).disposed(by: disposeBag)
|
}else{
|
alertError(msg: "当前门店获取失败")
|
}
|
case .none:break
|
}
|
}
|
|
if let m = item as? HomeStoreTopBannerModel{
|
jumpPage(name: m.name, page: m.page, type: m.type,id: m.turnId)
|
}
|
}
|
|
}
|
|
extension HomeVC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeCCell", for: indexPath) as! HomeCCell
|
let item = items[indexPath.row]
|
|
if let m = item as? HomeStoreConfigModel{
|
cell.img.sd_setImage(with: URL(string: m.backgroundImage), placeholderImage: m.type.defaultImg, options: .lowPriority)
|
}
|
|
if let m = item as? HomeStoreTopBannerModel{
|
cell.img.sd_setImage(with: URL(string: m.backgroundImage), placeholderImage: nil, options: .lowPriority)
|
}
|
cell.img.cornerRadius = 10
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return items.count
|
}
|
|
}
|
|
extension HomeVC: WaterFallLayoutDelegate{
|
func waterFlowLayout(_ waterFlowLayout: WaterFallFlowLayout, itemHeight indexPath: IndexPath) -> CGFloat {
|
let item = items[indexPath.row]
|
|
if let m = item as? HomeStoreConfigModel{
|
return m.cellHeight
|
}
|
|
if let m = item as? HomeStoreTopBannerModel{
|
return m.cellHeight
|
}
|
|
return 0
|
}
|
}
|