CNLiveMineViewController.swift 15.1 KB
//
//  CNLiveMineViewController.swift
//  metaCode
//
//  Created by open on 2023/6/6.
//

import Foundation
import CNLiveShareToolKit

class CNLiveMineViewController : CNLiveBaseViewController, UITableViewDelegate, UITableViewDataSource{
    
    let kCNLiveMineOneViewCellIdentifier = "kCNLiveMineOneViewCellIdentifier"
    let kCNLiveMineTwoViewCellIdentifier = "kCNLiveMineTwoViewCellIdentifier"
    let kMineBodyThreeViewCellIdentifier = "kMineBodyThreeViewCellIdentifier"
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView.init()
        tableView.separatorStyle = .none
        tableView.register(MineBodyOneListViewCell.self, forCellReuseIdentifier: kCNLiveMineOneViewCellIdentifier)
        tableView.register(MineBodyTwoListViewCell.self, forCellReuseIdentifier: kCNLiveMineTwoViewCellIdentifier)
        tableView.register(MineBodyThreeViewCell.self, forCellReuseIdentifier: kMineBodyThreeViewCellIdentifier)
        return tableView
    }()
    
    lazy var mainView: UIImageView = {
        let mainView = UIImageView.init()
        mainView.backgroundColor = .clear
        mainView.image = UIImage(named: "mine_header_icon_back")
        mainView.contentMode = .scaleAspectFill
        return mainView
    }()
    
    lazy var dataArray: NSMutableArray = {
        let dataArray = NSMutableArray()
        dataArray.add([""])
        let array01 : [Dictionary] = [["name":"我的身份","icon":"mine_header_icon_identity"]]
        dataArray.add(array01)
        let array02 : [Dictionary] = [["name":"我的数字简历","icon":"mine_jianli"]]
        dataArray.add(array02)
//        let array03 : [Dictionary] = [["name":"官方认证","icon":"mine_renzheng"]]
//        dataArray.add(array03)
        let array04 : [Dictionary] = [["name":"帮助与反馈","icon":"mine_list_icon_help_feedback"],
                                     ["name":"更多设置","icon":"mine_list_icon_set_more"]]
        dataArray.add(array04)
        
        let array05 : [Dictionary] = [["name":"订单列表","icon":"mine_list_icon_help_feedback"],
                                      ["name":"交易记录","icon":"mine_list_icon_help_feedback"],
                                      ["name":"新增优惠券","icon":"mine_list_icon_help_feedback"],
                                      ["name":"优惠券管理","icon":"mine_list_icon_help_feedback"],
                                      ["name":"抖音短视频","icon":"mine_list_icon_help_feedback"]]
//        dataArray.add(array05)
        
        return dataArray
    }()
    
    lazy var headerView: MineHeaderView = {
        let headerView = MineHeaderView.init(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: 80))
        return headerView
    }()
    
    var checkModel: MinePointCheckInModel!
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = HexColor("#f9f9f9")
        setNeedsStatusBarAppearanceUpdate()
        
        view.addSubview(mainView)
        mainView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(KSreenHeight * 0.4)
        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(StandAuthNotificationSuccessAction), name: Notification.Name.StandAuth.StandAuthNotificationSuccess, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(StandAuthNotificationSuccessAction), name: Notification.Name.StandAuth.StandAuthSortNotificationResult, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(loadDataWithPoint), name: Notification.Name.NetworkStatus.CNLiveNetworkStatusNotificationReachableViaWiFi, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(loadDataWithPoint), name: Notification.Name.NetworkStatus.CNLiveNetworkStatusNotificationReachableViaWWAN, object: nil)
        StandAuthNotificationSuccessAction()
    }

    
    @objc public func loadDataWithPoint() {
        CNLiveMineViewModel.requestWithCheckIn { [weak self] result, respStatue in
            if respStatue == .success {
                self?.checkModel = result as? MinePointCheckInModel
                self?.tableView.reloadData()
            } else {
                self?.checkModel = MinePointCheckInModel.init()
                self?.tableView.reloadData()
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        headerView.updateHeaderView()
        tableView.reloadData()
        loadDataWithPoint()
        
        delegate.window?.subviews.forEach({ view in
            if view.tag >= 20000{
                view.isHidden = false
            }
        })
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
      return .lightContent
    }
    
    let delegate = UIApplication.shared.delegate as! AppDelegate
    /// 未认证-inputType17时弹窗背景图
    lazy var alertBgImgView:UIImageView = {
        let alertBgImgView:UIImageView = UIImageView()
        alertBgImgView.tag = 20001
        alertBgImgView.isUserInteractionEnabled = true
        alertBgImgView.contentMode = .scaleAspectFit
        alertBgImgView.backgroundColor = .white
        return alertBgImgView
    }()
    lazy var agreeBtn: UIButton = {
        let  nextBtn = UIButton(type: .custom)
        nextBtn.width = 285
        nextBtn.height = 44
        nextBtn.setTitle("同意协议 领取一下", for: .normal)
        nextBtn.setTitleColor(.white, for: .normal)
        nextBtn.layer.cornerRadius = 22
        nextBtn.clipsToBounds = true
        nextBtn.addTarget(self, action: #selector(agreeBtnAction), for: .touchUpInside)
        nextBtn.backgroundColor = HexColor("#8E8BFB")
        // fillCode
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 0.1, green: 0.38, blue: 1, alpha: 1).cgColor, UIColor(red: 0.56, green: 0.55, blue: 0.98, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = nextBtn.bounds
        bgLayer1.startPoint = CGPoint(x: 0.5, y: 1)
        bgLayer1.endPoint = CGPoint(x: 0, y: 0)
        nextBtn.layer.addSublayer(bgLayer1)
        return nextBtn
    }()
    lazy var xieyiLabel: UILabel = {
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "查看《元码数字简历服务协议》")
        label.frame = CGRect(x: 0, y: 0, width: KSreenWidth, height: 20)
        let attr1: [NSAttributedString.Key : Any] = [.font: Font_14,.foregroundColor: HexColor("#20252A")!]
        attrString.addAttributes(attr1, range: NSRange(location: 0, length: 2))
        let attr: [NSAttributedString.Key : Any] = [.font: Font_14,.foregroundColor: HexColor("#1961FE")!]
        attrString.addAttributes(attr, range: NSRange(location: 2, length: attrString.length-2))
        label.attributedText = attrString
        label.textAlignment = .center
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(xieyiAction)))
        return label
    }()
    // MARK: - 弹出数字简历协议alert
    func showXieyiAlert(){
        delegate.window?.addSubview(self.alertBgImgView)
        alertBgImgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalToSuperview()
        }
        self.alertBgImgView.image = UIImage(named: "mine_jianli_xieyiBg")
        alertBgImgView.addSubview(self.agreeBtn)
        self.agreeBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-50)
            make.width.equalTo(285)
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
        }
        alertBgImgView.addSubview(self.xieyiLabel)
        self.xieyiLabel.snp.makeConstraints { make in
            make.bottom.equalTo(self.agreeBtn.snp.top).offset(-10)
            make.left.right.equalToSuperview()
            make.height.equalTo(20)
        }
    }
    @objc func xieyiAction(){
        //查看数字简历协议
        let webVC = CNWebViewController()
        webVC.url = URL(string: "https://wjj.ys1.cnliveimg.com/term/ymRlsbyhxxcjxy.html")
        self.navigationController?.pushViewController(webVC, animated: true)
        
        delegate.window?.subviews.forEach({ view in
            if view.tag >= 20000{
                view.isHidden = true
            }
        })
    }
    @objc func agreeBtnAction(){
        //同意协议按钮
        delegate.window?.subviews.forEach({ view in
            if view.tag >= 20000{
                view.removeFromSuperview()
            }
        })
        //本地存储已同意
        UserDefaults.standard.set(true,forKey: "is_agree_shuzijianlixieyi")

        //跳转数字简历页面
        let vc = CNLiveDigitalResumeController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    
    @objc func StandAuthNotificationSuccessAction() {
        CNLiveMineViewModel.requestsGETAuthInfoList(page: 1) { [self] result, respStatue in
            if respStatue == .success {
                tableView.reloadData()
            }
        }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: Notification.Name.StandAuth.StandAuthNotificationSuccess, object: nil)
        NotificationCenter.default.removeObserver(self, name: Notification.Name.StandAuth.StandAuthSortNotificationResult, object: nil)
    }
}

extension CNLiveMineViewController {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (dataArray[section] as AnyObject).count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {//我的身份
            let cell = tableView.dequeueReusableCell(withIdentifier: kMineBodyThreeViewCellIdentifier) as! MineBodyThreeViewCell
            cell.checkModel = self.checkModel
            return cell
        }
        if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveMineOneViewCellIdentifier) as! MineBodyOneListViewCell
            cell.reloadWithData()
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveMineTwoViewCellIdentifier) as! MineBodyTwoListViewCell
        let array = dataArray[indexPath.section]
        let sub_dict = (array as! Array<Any>)[indexPath.row]
        cell.setupWithDataSource(show_type: .feedback,dict: sub_dict as! Dictionary<String, Any>)
        return cell

    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 100
        }
        if indexPath.section == 1 {
            return 120
        }
        return 60
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            self.navigationController?.pushViewController(CNLiveMineQuestViewController.init(checkModel: checkModel), animated: true)
        }
        if indexPath.section == 1 {
            //我的身份跳转 我的数字资产
            navigationViewController().pushViewController(CNLiveDigitalAssetsController(), animated: true)
        }
        if indexPath.section == 2 {
            //我的数字简历
            
            if UserInfoManager.shared.userInfo.userMsgUsers?.verifyUsers.count ?? 0 > 0{
                //实名认证过                
                let is_agree = UserDefaults.standard.bool(forKey: "is_agree_shuzijianlixieyi")
                if is_agree{
                    //跳转数字简历页面
                    let vc = CNLiveDigitalResumeController()
                    self.navigationController?.pushViewController(vc, animated: true)
                }else{
                    self.showXieyiAlert()
                }

            }else{
                //未实名认证->实名认证
                let vc = CNLiveRealNameAuthController()
                vc.isResume = true
                self.navigationController?.pushViewController(vc, animated: true)
            }
            
            
        }
//        if indexPath.section == 3 {
//            //官方认证--身份大库
//            let vc = CNLiveIdentityRepositoryController()
//            vc.channelId = ""
//            vc.type = "1"
//            navigationViewController().pushViewController(vc, animated: true)
//        }

        if indexPath.section == 3 && indexPath.row == 0 {
            
            self.navigationController?.pushViewController(CNLiveHelpFeedBackViewController.init(), animated: true)
        }
        if indexPath.section == 3 && indexPath.row == 1 {
            self.navigationController?.pushViewController(CNLiveTypeFeedBackViewController.init(show_type: .setting), animated: true)
        }
        
        if indexPath.section == 4 {
            if indexPath.row == 0 {
//                self.navigationController?.pushViewController(CNLiveShopOrderViewController.init(), animated: true)
//                StandSortManager.removeStandSort()
//                CNLiveShareImageManager.pushWeChatMiniProgrem(withMiniAppId: "gh_234d4ed615a3", path: "page/login_hint/login_hint", extMsg: "", extDic: [:])
            }
            if indexPath.row == 1 {
                self.navigationController?.pushViewController(CNLiveShopTransViewController.init(), animated: true)
            }
            if indexPath.row == 2 {
                self.navigationController?.pushViewController(CNLiveShopCouponViewController.init(), animated: true)
            }
            if indexPath.row == 3 {
                self.navigationController?.pushViewController(CNLiveShopCouponManagerViewController.init(), animated: true)
            }
            if indexPath.row == 4 {
                self.navigationController?.pushViewController(CNLiveShortVideoViewController.init(aid: "cms_ugc_10504077_1704334474587"), animated: true)
            }
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 150
        }
        return 0.001

    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            return headerView
        }
        return UIView.init()

    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        mainView.snp.updateConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(offsetY < 0 ? (KSreenHeight * 0.4) + abs(offsetY) : KSreenHeight * 0.4)
        }
    }
}