CNLiveDigitalResumePreViewMineJianJieController.swift 11.7 KB
//
//  CNLiveDigitalResumePreViewMineJianJieController.swift
//  metaCode
//
//  Created by zx on 2024/8/13.
//  数字简历-自我介绍页面

import Foundation
import SnapKit
import QMUIKit

class CNLiveDigitalResumePreViewMineJianJieController:CNLiveBaseViewController, QMUITextViewDelegate{
    
    var introduce = ""
    lazy var bgImgView :UIImageView = {
        let iconImgView = UIImageView()
        iconImgView.image = UIImage(named: "mine_jianli_introduceBg")
        return iconImgView
    }()
    lazy var yuanmaLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = HexColor("#333333")
        titleLabel.text = "自我介绍"
        titleLabel.font = BoldFont_24
        titleLabel.adjustsFontSizeToFitWidth=true
        return titleLabel
    }()
    
    lazy var bottomDescLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = HexColor("#B4B4B4")
        titleLabel.text = "请确保你本人的信息真实有效\n 上传虚假信息将对你的信用产生负面影响"
        titleLabel.font = Font_13
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 2
        return titleLabel
    }()
    lazy var yuanmaDescLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = HexColor("#333333")
        titleLabel.text = "Introduce"
        titleLabel.font = Font_16
        return titleLabel
    }()
    lazy var addWorkBtn: UIButton = {
        let  nextBtn = UIButton(type: .custom)
        nextBtn.setTitle("提交", for: .normal)
        nextBtn.setTitleColor(.white, for: .normal)
        nextBtn.backgroundColor = HexColor("#1961FE")
        nextBtn.layer.cornerRadius = 20
        nextBtn.clipsToBounds = true
        nextBtn.addTarget(self, action: #selector(addWorkBtnAction), for: .touchUpInside)
        return nextBtn
    }()
    lazy var textBgView :UIView = {
        let bottomNavView = UIView()
        bottomNavView.backgroundColor = .white
        bottomNavView.layer.masksToBounds = true
        bottomNavView.layer.cornerRadius = 12
        return bottomNavView
    }()

    lazy var textView: QMUITextView = {
        let textView = QMUITextView.init(frame: .zero)
        textView.backgroundColor = HexColor("#FFFFFF")
        textView.font = Font_15
        textView.textColor = HexColor("#333333")
        textView.placeholderColor = HexColor("#999999")
        textView.placeholderMargins = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        textView.placeholder = "请输入自我介绍"
//        textView.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        textView.textAlignment = .left
        textView.delegate = self
        textView.returnKeyType = .done
        return textView
    }()
    lazy var huoJiangNumberLabel : UILabel = {
        let numberLabel = UILabel.init()
        numberLabel.text = "0/500"
        numberLabel.textColor = .black
        numberLabel.font = Font_15
        numberLabel.textAlignment = .right
        return numberLabel
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarHidden = false
        titleName = "自我介绍"
        self.initUI()
        if self.introduce.count > 0{
            self.textView.text = self.introduce
            self.huoJiangNumberLabel.text = "\(self.introduce.count)/500"
        }
    }
    
    deinit {}
    func initUI(){
        self.view.backgroundColor = HexColor("#F9F9F9")
        
        self.view.addSubview(self.bgImgView)
        self.bgImgView.snp.makeConstraints { make in
            make.top.equalTo(KNavigationHeight)
            make.left.right.equalToSuperview()
            make.height.equalTo(KSreenWidth*457/375)
        }
        self.bgImgView.addSubview(self.yuanmaLabel)
        self.yuanmaLabel.snp.makeConstraints { make in
            make.top.equalTo(59)
            make.left.equalTo(30)
        }
        self.bgImgView.addSubview(self.yuanmaDescLabel)
        self.yuanmaDescLabel.snp.makeConstraints { make in
            make.top.equalTo(self.yuanmaLabel.snp.bottom).offset(5)
            make.left.equalTo(30)
        }
        
        self.view.addSubview(self.bottomDescLabel)
        self.bottomDescLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview().offset(-5)
            make.height.equalTo(40)
        }

        self.view.addSubview(self.addWorkBtn)
        self.addWorkBtn.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalTo(self.bottomDescLabel.snp.top).offset(-15)
            make.height.equalTo(40)
        }
        
        self.view.addSubview(self.textBgView)
        self.textBgView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalTo(self.addWorkBtn.snp.top).offset(-15)
            make.top.equalTo(self.yuanmaDescLabel.snp.bottom).offset(42)
        }

        self.textBgView.addSubview(self.textView)
        self.textView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(-41)
            make.top.equalTo(20)
        }
        self.textBgView.addSubview(self.huoJiangNumberLabel)
        self.huoJiangNumberLabel.snp.makeConstraints { make in
            make.width.equalTo(200)
            make.bottom.equalToSuperview().offset(-21)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(21)
        }

        
    }
    

    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
//        if textView == self.textView {
//            if self.textView.text?.count ?? 0 > 500 {
//                showMessage(message: "最大限制500个字")
//                return false
//            }
//        }
        return self.textView.resignFirstResponder()
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.introduce = textView.text ?? ""
    }
    
    func textViewShouldReturn(_ textView: QMUITextView!) -> Bool {
        self.introduce = textView.text ?? ""
        self.textView.resignFirstResponder()
        return true
    }
    func textViewDidChange(_ textView: UITextView) {
        let maxCount = 500
        if textView.text.count > maxCount {
            let selectRang = textView.markedTextRange
            
            if let selectRang = selectRang {
                let position = textView.position(from: (selectRang.start), offset: 0)
                if position != nil {
                    return
                }
            }
            
            let textContent = textView.text ?? ""
            let textNum = textContent.count
            
            if textNum > maxCount {
                textView.text = string_prefix(index: maxCount, text: textContent)
            }
        }
        self.huoJiangNumberLabel.text = "\(textView.text.count)/500"
    }
    private func string_prefix(index: Int, text: String) -> String {
        if text.count <= index {
            return text
        } else {
            let index = text.index(text.startIndex, offsetBy: index)
            let str = text.prefix(upTo: index)
            return String(str)
        }
    }
    func saveJianJie(introduction:String){
        if var userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
            userDic.updateValue(introduction, forKey: "jianli_selfIntroduction")
            UserDefaults.standard.setValue(userDic, forKey: UserInfoManager.shared.userInfo.uid)
            UserDefaults.standard.synchronize()
            self.updateViate(key: "jianli_selfIntroduction", value: introduction)
        }else{
            let tempDic = NSMutableDictionary()
            UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
            UserDefaults.standard.synchronize()
        }
    }

    func updateViate(key:String,value:Any){
        if let userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
            var jianli_city = userDic["jianli_city"] as? String
            if key == "jianli_city"{
                jianli_city = (value as! String)
            }
            var jianli_vitaeId = userDic["jianli_vitaeId"] as? String
            if key == "jianli_vitaeId"{
                jianli_vitaeId = (value as! String)
            }
            var vsid = userDic["vsid"] as? String
            if key == "vsid"{
                vsid = (value as! String)
            }
            var jianli_profession = userDic["jianli_profession"] as? String ?? ""
            if key == "jianli_profession"{
                jianli_profession = value as! String
            }
            var jianli_professionId = userDic["jianli_professionId"] as? String
            if key == "jianli_professionId"{
                jianli_professionId = (value as! String)
            }
            var jianli_icon = userDic["jianli_icon"] as? String ?? ""
            if key == "jianli_icon"{
                jianli_icon = value as! String
            }
            var jianli_phone = userDic["jianli_phone"] as? String ?? ""
            if key == "jianli_phone"{
                jianli_phone = value as! String
            }
            var jianli_selfIntroduction = userDic["jianli_selfIntroduction"] as? String
            if key == "jianli_selfIntroduction"{
                jianli_selfIntroduction = (value as! String)
            }
            var jianli_workTime = userDic["jianli_workTime"] as? String
            if key == "jianli_workTime"{
                jianli_workTime = (value as! String)
            }
            var jianli_showAge = userDic["jianli_showAge"] as? Bool ?? false
            if key == "jianli_showAge"{
                jianli_showAge = value as! Bool
            }
            var jianli_showAddress = userDic["jianli_showAddress"] as? Bool ?? false
            if key == "jianli_showAddress"{
                jianli_showAddress = value as! Bool
            }
            var jianli_showAwards = userDic["jianli_showAwards"] as? Bool ?? false
            if key == "jianli_showAwards"{
                jianli_showAwards = value as! Bool
            }
            var jianli_showPhone = userDic["jianli_showPhone"] as? Bool ?? false
            if key == "jianli_showPhone"{
                jianli_showPhone = value as! Bool
            }
            var jianli_updateTime = userDic["jianli_updateTime"] as? String ?? ""
            if key == "jianli_updateTime"{
                jianli_updateTime = value as! String
            }

            CNLiveVitaeViewModel.saveVitae(city: jianli_city ?? "", icon: jianli_icon, isSave: false, id: jianli_vitaeId ?? "", phone: jianli_phone, profession: jianli_profession , professionId: jianli_professionId ?? "", selfIntroduction: jianli_selfIntroduction ?? "", showAddress: jianli_showAddress, showAge: jianli_showAge, showAwards: jianli_showAwards, showPhone:jianli_showPhone, updateTime: jianli_updateTime, vsid: vsid ?? "", workTime: jianli_workTime ?? ""){ result, respStatue in
                if respStatue == .success{
                    self.navigationController?.popViewController(animated: true)
                }
            }
        }else{
            let tempDic = NSMutableDictionary()
            UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
            UserDefaults.standard.synchronize()
        }


    }

    @objc func addWorkBtnAction(){
        //提交
//        if self.textView.text.count > 0{
            self.saveJianJie(introduction:self.textView.text)
//        }else{
//            showMessage(message: "请输入自我介绍")
//        }
    }
    
}