InformationViewController.swift 17.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
//
//  InformationViewController.swift
//  ParentAssistant
//
//  Created by 葛建军 on 2018/3/5.
//  Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//

import UIKit
class InformationViewController: UIViewController,UISearchBarDelegate {
    @IBOutlet weak var collectView: UICollectionView!
    var leftLabel:UILabel!
    var data:ChannelDemol!
    var topADs:[ADSModel]=[]
    var isFirstviewController:Bool=true
    var channel:Int=0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configTheme()

        if isFirstviewController{
            self.navigationItem.title = ""
            addLeftBarBtn()
        }else{
            
        }
        let searchView = UISearchBar(frame: CGRect(x: 0, y: 7, width: getScreenWidth()-40, height: 30))
        searchView.delegate = self
        searchView.placeholder = "请输入搜索内容"
        searchView.showsCancelButton = false
        self.navigationItem.titleView = searchView
        self.navigationController?.navigationBar.tintColor=UIColor.white
    }
    
    func getData(){
        let topADsQueue = DispatchQueue(label: "topADs")
        topADsQueue.async {
            //position 广告位置0-首次广告位1-开屏广告位2-资讯首页3-频道首页4-频道内容5-文章内容页
            var position:Int = 2
            if !self.isFirstviewController {
                position = 3
            }
            HTTPServer.shared.getAds(["position":position as AnyObject,"areaName":AccountManager.shared.address as AnyObject], completionHandler: { (str, error) in//getPercentEncodingString(str: AccountManager.shared.address)
                httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                    if json["status"] == 1 && error == nil{
                        self.topADs.removeAll()
                        for item in json.contentData().arrayValue{
                            self.topADs.append(ADSModel(j: item))
                        }
                        self.collectView.reloadData()
                    }
                }, failHandler: { (error) in
                    
                })
            })
        }
        topADsQueue.async {
            let dic = self.getListDic()
            HTTPServer.shared.getChannelInfo(dic, completionHandler: { (str, error) in
                httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                    if json["status"] == 1 && error == nil{
                        self.data = ChannelDemol(j: json.contentData())
                        self.collectView.reloadData()
                    }
                }, failHandler: { (error) in
                    
                })
            })
        }
    }
    
    func getList(key:String?) {
        if key == nil{
            return
        }
        var dic = getListDic()
        dic.updateValue(key as AnyObject, forKey: "keyword")
        
        HTTPServer.shared.getInformationTopic(dic, completionHandler: { (str, error) in
            httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                if json["status"] == 1 && error == nil{
                    self.data.channelContent.removeAll()
                    for item in json.contentData()["channelList"].arrayValue{
                        self.data.channelContent.append(ChannelContentDemol(j: item))
                    }
                    self.collectView.reloadData()
                }
            }, failHandler: { (error) in
                
            })
        })
    }
    
    //获得请求参数
    func getListDic()->[String:AnyObject]{
        var dic = [ "areaName": AccountManager.shared.address as AnyObject,"pageIndex": 1  as AnyObject,"pageSize": 20 as AnyObject]
        var toFirstPage:Int = 1
        var ch:Int = 0
        if !isFirstviewController{
            toFirstPage = 0
            ch = channel
        }
        dic.updateValue(ch as AnyObject, forKey: "channel")
        dic.updateValue(toFirstPage as AnyObject, forKey: "toFirstPage")
        return dic
    }

    //左上交城市选择按钮添加
    func addLeftBarBtn(){
        let leftbtn = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
        leftLabel = UILabel(frame: CGRect(x: 0, y: 10, width: 60, height: 20))
        leftLabel.text = AccountManager.shared.address
        leftLabel.textColor = UIColor.white
        leftLabel.sizeToFit()
        let image = UIImageView(frame: CGRect(x: leftLabel.frame.maxX+4, y: 15, width: 16, height: 9))
        image.image = #imageLiteral(resourceName: "evaluation_icon_pushDown")
        leftbtn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(EvaluationViewController.getAddreess(tap:))))
        leftbtn.addSubview(leftLabel)
        leftbtn.addSubview(image)
        leftbtn.frame.size = CGSize(width: image.frame.maxX+4, height: 40)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftbtn)
    }
    
    //地址选择
    @objc func getAddreess(tap:UITapGestureRecognizer){
        tap.view?.isUserInteractionEnabled = false
        LZCityPickerController.showPicker(in: self
            , select: { (address, province, city, area, isSelect) in
                if isSelect{
                    tap.view?.isUserInteractionEnabled = true
                    self.selectCity(city: area!)
                }
        }) {
            tap.view?.isUserInteractionEnabled = true
        }
    }
    
    //完成选择城市
    func selectCity(city: String) {
        AccountManager.shared.address = city
        addLeftBarBtn()
        getData()
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = true
    }
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchData(searchBar)
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchData(searchBar)
    }
    
    //搜索资讯
    func searchData(_ searchBar: UISearchBar){
        getList(key: searchBar.text)
        searchBar.resignFirstResponder()
        searchBar.showsCancelButton = false
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getData()
        if isFirstviewController{
            addLeftBarBtn()
        }else{
            
        }

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //点击资讯进入详情
    func pushToInfoView(index:Int){
        let vcs = Story.instantiateViewControllerWithIdentifier("JSViewController", storyName: "JS") as! JSViewController
        if data.channelContent[index].url != "" {
            vcs.webURL = data.channelContent[index].url
        }else{
            vcs.webURL = (HOSTWAP+"/InformationDetail.aspx?id="+"\(data.channelContent[index].id)"+"&areaname="+AccountManager.shared.address).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        }
        self.navigationController?.present(vcs.wrapWithNavigationController(), animated: true, completion: nil)
    }
    
}

extension InformationViewController:XRCarouselViewDelegate{
    func carouselView(_ carouselView: XRCarouselView!, clickImageAt index: Int) {
        pushToInfoView(index: index)
    }
}

//整体分三个部分1.轮播图,2频道栏,3列表
extension InformationViewController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            if data != nil && data.subchannel.count > 0{
                return 1
            }
            return 0
        default:
            return data != nil ? data.channelContent.count : 0
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        switch indexPath.section {
        case 0:
            return CGSize(width: getScreenWidth(), height: getScreenWidth()/375*200)
        case 1:
            return CGSize(width: getScreenWidth()-20, height: 92)
        default:
//            if indexPath.row == 0{
                return CGSize(width: getScreenWidth()-20, height: 92)
//            }
//            return CGSize(width: getScreenWidth()-20, height: 180)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 2{
            return CGSize(width: getScreenWidth()-20, height: 40)
        }else{
            return CGSize(width: getScreenWidth(), height: 10)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        collectionView.register(UINib(nibName: "ChannelCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "contentCell")
        switch indexPath.section {
        case 0:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topCell", for: indexPath) as! InformationViewTopCell
            if topADs.count > 0 {
                var arr:[String]=[]
                for item in topADs{
                    arr.append(HOSTImage+item.fileSrc)
                }
                cell.headerImageView.imageArray = arr
            }
            return cell
        case 1:
            
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "channelSelectCell", for: indexPath) as! ChannelSelectView
            cell.SubchannelList = data.subchannel
            cell.superNC = self.navigationController
            return cell
        default:
//            if indexPath.row == 0{
                let item = data.channelContent[indexPath.row]
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "contentCell", for: indexPath) as! ChannelCollectionViewCell
                cell.title.text = item.title
                cell.option.text = item.content
                cell.optionNum.text = "\(item.lookNum)"
                cell.shareNum.text = "\(item.forwardingNum)"
                cell.picture.sd_setImage(with: URL(string: (HOSTImage+item.image).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!), placeholderImage: UIImage(named: "navigationBar_backgrounImage"))
                return cell
//            }else{
//                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adiouPlayerCell", for: indexPath) as! AdiouPlayerCell
//                return cell
//            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch indexPath.section {
        case 1:
            let vc = Story.instantiateViewControllerWithIdentifier("InformationViewControllerVC", storyName: "Main") as! InformationViewController
            vc.channel = data.subchannel[indexPath.row].id
            vc.isFirstviewController = false
            self.navigationController?.pushViewController(vc, animated: true)
            break
        default:
            let vcs = Story.instantiateViewControllerWithIdentifier("JSViewController", storyName: "JS") as! JSViewController
            if data.channelContent[indexPath.row].url != "" {
                vcs.webURL = data.channelContent[indexPath.row].url
            }else{
                vcs.webURL = (HOSTWAP+"/InformationDetail.aspx?id="+"\(data.channelContent[indexPath.row].id)"+"&areaname="+AccountManager.shared.address).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            }
            self.navigationController?.present(vcs.wrapWithNavigationController(), animated: true, completion: nil)

//            pushToInfoView(index: indexPath.row)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableview:UICollectionReusableView?
        if kind == UICollectionElementKindSectionHeader{
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView", for: indexPath) as! InformationSectionHeader
            headerView.title.text = "咨讯话题"
            reusableview = headerView
        }
        if indexPath.section == 2{
            reusableview?.isHidden = false
        }else{
            reusableview?.isHidden = true
        }
        return reusableview!
    }
}


class InformationSectionHeader: UICollectionReusableView {
    @IBOutlet weak var title: UILabel!
    
}

//首页的轮播图所在的cell
class InformationViewTopCell: UICollectionViewCell {
    
    @IBOutlet weak var headerImageView: XRCarouselView!
    
}

//视频cell
class AdiouPlayerCell: UICollectionViewCell {
    
}

//频道栏
class ChannelSelectView: UICollectionViewCell {
    var superNC:UINavigationController!
    var SubchannelList:[SubchannelDemol]=[]
    @IBOutlet weak var channelSelectCollectionView: UICollectionView!
    override func awakeFromNib() {
        channelSelectCollectionView.register(UINib(nibName: "ChannelSelectViewCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "channelcell")
    }
}

extension ChannelSelectView:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return SubchannelList.count
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (getScreenWidth()-20)/4, height: 92)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "channelcell", for: indexPath) as! ChannelSelectViewCollectionViewCell
        cell.image.sd_setImage(with: URL(string: (HOSTImage+SubchannelList[indexPath.row].channelImage).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!), placeholderImage: UIImage(named: "evaluation_icon_highSchoolEnter"))
        cell.channelName.text = SubchannelList[indexPath.row].channelName
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = Story.instantiateViewControllerWithIdentifier("InformationViewControllerVC", storyName: "Main") as! InformationViewController
        vc.channel = SubchannelList[indexPath.row].id
        vc.isFirstviewController = false
        superNC.pushViewController(vc, animated: true)
    }
}

class ChannelDemol:NSObject{
    var id:Int=0 // 频道ID ,
    var channelName:String=""// 频道名称 ,
    var channelImage:String=""// 频道图片 ,
    var orderById:Int=0 // 频道排序 ,
    var subchannel:[SubchannelDemol] = []// 二级频道 ,
    var channelContent:[ChannelContentDemol] = []// 频道信息
    init(j:JSON){
        id = j["id"].intValue
        channelName = j["channelName"].stringValue
        channelName = j["channelName"].stringValue
        orderById = j["orderById"].intValue
        for item in j["subchannel"].arrayValue {
            subchannel.append(SubchannelDemol(j: item))
        }
        for item in j["channelContent"].arrayValue {
            channelContent.append(ChannelContentDemol(j: item))
        }
    }
}

class SubchannelDemol: NSObject {
    var id:Int=0 // 二级频道ID ,
    var channelName:String=""// 二级频道名称 ,
    var channelImage:String=""// 二级频道图片 ,
    var orderById:Int=0// 二级工具排序,
    var url:String=""// 二级工具链接
    init(j:JSON){
        id = j["id"].intValue
        channelName = j["channelName"].stringValue
        channelImage = j["channelImage"].stringValue
        orderById = j["orderById"].intValue
        url = j["url"].stringValue
    }

}

class ChannelContentDemol: NSObject {
    var id:Int=0// 资讯ID ,
    var image:String=""// 资讯标题图片 ,
    var title:String=""// 资讯标题 ,
    var url:String=""// 资讯链接 ,
    var content:String=""// 资讯内容 ,
    var forwardingNum:Int=0// 资讯转发数 ,
    var talkNum:Int=0 // 评论数 ,
    var lookNum:Int = 0 //浏览数
    var orderById:Int=0// 资讯排序
    var isAds:Int=0// 是否为广告 1是广告
    init(j:JSON){
        id = j["id"].intValue
        image = j["image"].stringValue
        title = j["title"].stringValue
        url = j["url"].stringValue
        content = j["content"].stringValue
        forwardingNum = j["forwardingNum"].intValue
        talkNum = j["talkNum"].intValue
        lookNum = j["lookNum"].intValue
        orderById = j["orderById"].intValue
        isAds = j["isAds"].intValue
    }

}

class ADSModel: NSObject {
    var id:Int=0// 广告ID ,
    var location:Int=0// 广告位 ,
    var describe:String=""// 广告内容 ,
    var fileSrc:String=""// 广告内容 ,
    var url:String=""// 广告跳转链接 ,
    var orderById:Int=0// 广告排序
    init(j:JSON){
        id = j["id"].intValue
        location = j["location"].intValue
        describe = j["describe"].stringValue
        fileSrc = j["fileSrc"].stringValue
        url = j["url"].stringValue
        orderById = j["orderById"].intValue
    }

}