// // NewCustomerInfoViewController.swift // MTP2_iOS // // Created by Muchinfo on 2021/1/2. // Copyright © 2021 Muchinfo. All rights reserved. // import UIKit import WHToast import IBAnimatable import ActionSheetPicker_3_0 import ZLPhotoBrowser import JXPhotoBrowser import SDWebImage import NVActivityIndicatorView /// 新增客户资料视图容器控制类 class NewCustomerInfoViewController: BaseViewController { // MARK: - 属性列表 /// 客户类型 @IBOutlet weak var customerType: UIButton! { didSet { customerType.setTitle("企业", for: .normal) customerType.setTitleColor(.hex333(), for: .normal) } } /// 企业名称视图 @IBOutlet weak var enterpriseView: UIView! /// 企业名称 @IBOutlet weak var enterpriseName: AnimatableTextField! /// 企业简称视图 @IBOutlet weak var nickNameView: UIView! /// 企业简称 @IBOutlet weak var nickName: UITextField! /// 证件类型视图 @IBOutlet weak var certificateView: UIView! /// 证件类型 @IBOutlet weak var certificateType: UIButton! /// 证件号码视图 @IBOutlet weak var certificateNoView: UIView! /// 证件号码 @IBOutlet weak var certificateNo: AnimatableTextField! /// 法定代表人视图 @IBOutlet weak var legalView: UIView! /// 法定代表人 @IBOutlet weak var legalName: UITextField! /// 纳税人识别号视图 @IBOutlet weak var taxpayerView: UIView! /// 纳税人识别号 @IBOutlet weak var taxpayerNo: UITextField! /// 联系人视图 @IBOutlet weak var contractView: UIView! /// 联系人 @IBOutlet weak var contractName: UITextField! /// 营业执照视图 @IBOutlet weak var licenseView: UIView! /// 上传 @IBOutlet weak var upload: AnimatableButton! /// 查看附件 @IBOutlet weak var checkFile: UIButton! /// 手机号码视图 @IBOutlet weak var mobilePhoneView: UIView! /// 手机号码 @IBOutlet weak var mobilePhone: AnimatableTextField! /// 联系电话视图 @IBOutlet weak var telphoneView: UIView! /// 联系电话 @IBOutlet weak var telphone: AnimatableTextField! /// 通讯地址 @IBOutlet weak var address: UIButton! /// 详细地址 @IBOutlet weak var detailAddress: AnimatableTextView! /// 备注 @IBOutlet weak var remark: AnimatableTextField! /// 保存草稿 @IBOutlet weak var saveDrafts: UIButton! /// 提交审核 @IBOutlet weak var audit: UIButton! /// 姓名视图 @IBOutlet weak var personalView: UIView! /// 姓名 @IBOutlet weak var personalName: UITextField! /// 邮箱视图 @IBOutlet weak var emailView: UIView! /// 邮箱 @IBOutlet weak var email: UITextField! /// 个人手机号码视图 @IBOutlet weak var personalPhoneView: UIView! /// 个人手机号码 @IBOutlet weak var personalPhone: UITextField! /// 身份证号码视图 @IBOutlet weak var identifyView: UIView! /// 身份证号码 @IBOutlet weak var identifyNo: UITextField! /// 身份证证件照正面视图 @IBOutlet weak var identifyFrontView: UIView! /// 上传 @IBOutlet weak var frontUpload: AnimatableButton! /// 查看附件 @IBOutlet weak var frontFile: UIButton! /// 身份证证件照背面正面视图 @IBOutlet weak var identifyBackgroundView: UIView! /// 上传 @IBOutlet weak var backgroundUpload: AnimatableButton! /// 查看附件 @IBOutlet weak var backgroundFile: UIButton! /// 地址标签 @IBOutlet weak var addressTitle: UILabel! /// 所属机构 @IBOutlet weak var area: UIButton! /// 所属机构视图 @IBOutlet weak var areaView: UIView! { didSet { areaView.isHidden = !(MTP2BusinessCore.shared.address?.oem.lowercased() == "qhj") } } /// 登录账号 @IBOutlet weak var loginno: UITextField! /// 登录账号视图 @IBOutlet weak var loginnoView: UIView! { didSet { loginnoView.isHidden = !(MTP2BusinessCore.shared.address?.oem.lowercased() == "qhj") } } /// 登录密码 @IBOutlet weak var password: UITextField! /// 登录密码视图 @IBOutlet weak var passwordView: UIView! { didSet { passwordView.isHidden = !(MTP2BusinessCore.shared.address?.oem.lowercased() == "qhj") } } /// 证件类型 var cardType: Int? { didSet { /// 异常 guard let type = cardType else { return } certificateType.setTitle(CardType(rawValue: type)?.description, for: .normal) certificateType.setTitleColor(.hex333(), for: .normal) } } /// 客户类型 var accountType: AccountType = .enterprise { didSet { customerType.setTitle(accountType == .enterprise ? "企业" : "个人", for: .normal) customerType.setTitleColor(.hex333(), for: .normal) /// 地址标签 addressTitle.text = accountType == .enterprise ? "通讯地址" : "常住地址" /// 企业名称视图 enterpriseView.isHidden = accountType != .enterprise /// 企业简称视图 nickNameView.isHidden = accountType != .enterprise /// 证件类型视图 certificateView.isHidden = accountType != .enterprise /// 证件号码视图 certificateNoView.isHidden = accountType != .enterprise /// 证件号码 certificateNo.isHidden = accountType != .enterprise /// 法定代表人视图 legalView.isHidden = accountType != .enterprise /// 纳税人识别号视图 taxpayerView.isHidden = accountType != .enterprise /// 联系人视图 contractView.isHidden = accountType != .enterprise /// 营业执照视图 licenseView.isHidden = accountType != .enterprise /// 手机号码视图 mobilePhoneView.isHidden = accountType != .enterprise /// 联系电话视图 telphoneView.isHidden = accountType != .enterprise /// 姓名视图 personalView.isHidden = accountType == .enterprise /// 邮箱视图 emailView.isHidden = accountType == .enterprise /// 个人手机号码视图 personalPhoneView.isHidden = accountType == .enterprise /// 身份证号码视图 identifyView.isHidden = accountType == .enterprise /// 身份证证件照正面视图 identifyFrontView.isHidden = accountType == .enterprise /// 身份证证件照背面正面视图 identifyBackgroundView.isHidden = accountType == .enterprise } } /// 证件类型 var cards: [MoEnum] { /// 目前只显示机构类型执照 return (try! DatabaseHelper.getEnumInfo(enumdiccode: "certificatetype"))?.filter({ (obj) -> Bool in return obj.enumitemname == 2 || obj.enumitemname == 4 || obj.enumitemname == 18 || obj.enumitemname == 21 }) ?? [] } /// 证件执照 var file: UIImage? { didSet { /// 异常 guard let image = file else { return } /// 文件上传 self.uploadFile(image) } } /// 身份证正反面照路径 var bgfile: UIImage? { didSet { /// 异常 guard let image = file else { return } /// 文件上传 self.uploadFile(image, false) } } /// 文件上传路径 var filePath: String? /// 身份证正反面照路径 var bgfilePath: String? /// 国家 var country: MoDivisions? /// 省 var province: MoDivisions? /// 市 var city: MoDivisions? /// 区 var district: MoDivisions? /// 操作 var operatorType: OperatorType? { didSet { self.navigationItem.title = (operatorType?.description ?? "")+"客户资料" } } /// 地址 var addressStr: String? { didSet { self.address.setTitle(addressStr, for: .normal) self.address.setTitleColor(.hex333(), for: .normal) } } /// 所属机构列表信息 var arealists: [MoQhjAreaList] = [] /// 选中的所属机构 var moQhjArea: MoQhjAreaList? { didSet { /// 异常 guard let obj = moQhjArea else { return } area.setTitle(obj.accountname, for: .normal) area.setTitleColor(.hex333(), for: .normal) } } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// loding..... addLoadingView() /// 千海金才需要查询 if MTP2BusinessCore.shared.address?.oem.lowercased() == "qhj" { /// 查询所属机构信息 queryQhjAreaLists() } } override func loadView() { super.loadView() /// 输入框 moveView(views: [enterpriseName, nickName, certificateNo, legalName, taxpayerNo, contractName, mobilePhone, telphone, detailAddress, remark]) /// 异常 guard let obj = takeInfo as? MoUserInfo else { return } /// 企业名称 self.enterpriseName.text = obj.customername /// 客户类型 self.accountType = obj.userinfotype /// 手机号码 self.mobilePhone.text = obj.mobile /// 联系电话 self.telphone.text = obj.telphone /// 证件号码 self.certificateNo.text = obj.cardnum /// 证件类型 if obj.cardtype != -1 { self.cardType = Int(obj.cardtype) } /// 备注信息 self.remark.text = obj.remark /// 地址信息 self.detailAddress.text = obj.address /// 联系人 self.contractName.text = obj.contactname /// 纳税人识别号 self.taxpayerNo.text = obj.taxpayernum /// 法定代表人 self.legalName.text = obj.legalpersonname /// 企业简称 self.nickName.text = obj.nickname /// 个人姓名 self.personalName.text = obj.customername /// 手机号码 self.personalPhone.text = obj.mobile /// 身份证号码 self.identifyNo.text = obj.cardnum /// 邮箱 self.email.text = obj.email.isBlank() /// 查询省市信息 queryProvincesAndCitys() /// 异常 if let file = (takeInfo as? MoUserInfo)?.cardfrontphotourl, file != "", let dnUrl = URL(string: file.getUrlString()) { self.filePath = file /// 图片 SDWebImageDownloader.shared.downloadImage(with: dnUrl, completed: { (image, imageData, error, _) in DispatchQueue.main.async { self.file = image } }) } /// 异常 if let file = (takeInfo as? MoUserInfo)?.cardbackphotourl, file != "", let dnUrl = URL(string: file.getUrlString()) { self.bgfilePath = file /// 图片 SDWebImageDownloader.shared.downloadImage(with: dnUrl, completed: { (image, imageData, error, _) in DispatchQueue.main.async { self.bgfile = image } }) } } // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: UIControl) { switch sender { case area: /// 所属机构 /// 异常 guard arealists.count != 0 else { WHToast.showMessage("没有所属机构信息", duration: ToastTimer, finishHandler: {}) return } ActionSheetStringPicker.show(withTitle: "请选择所属机构", rows: arealists.map({ obj in return obj.accountname }), initialSelection: 0, doneBlock: { picker, value, index in self.moQhjArea = self.arealists[value] }, cancel: { picker in return }, origin: sender) case customerType: /// 客户类型 ActionSheetStringPicker.show(withTitle: "请选择客户类型", rows: ["个人", "企业"], initialSelection: 0, doneBlock: { picker, value, index in self.accountType = AccountType(rawValue: value+1)! }, cancel: { picker in return }, origin: sender) case certificateType: /// 证件类型 ActionSheetStringPicker.show(withTitle: "请选择证件类型", rows: cards.map({ (obj) -> String in return obj.enumdicname }), initialSelection: 0, doneBlock: { picker, value, index in self.cardType = self.cards[value].enumitemname }, cancel: { picker in return }, origin: sender) case address: /// 地址选择 self.view.endEditing(true) /// 弹出地址选择视图 let selectAreaPopupView = Bundle.main.loadNibNamed("SelectArea", owner: nil, options: nil)?.first as! SelectAreaPopupView selectAreaPopupView.delegate = self selectAreaPopupView.show() case upload, frontUpload, backgroundUpload: /// 文件上传 let ps = ZLPhotoPreviewSheet() ps.selectImageBlock = { [weak self] (images, assets, isOriginal) in if sender == self?.backgroundUpload { self?.bgfile = images.first } else { self?.file = images.first } } ps.showPhotoLibrary(sender: self) case checkFile, frontFile, backgroundFile: /// 查看文件 /// 异常 guard let image = (sender == backgroundFile ? bgfile : file) else { WHToast.showMessage("请选择上传文件", duration: ToastTimer, finishHandler: {}) return } /// 实例化 let bro = JXPhotoBrowser() /// 浏览过程中实时获取数据总量 bro.numberOfItems = { return 1 } /// 刷新Cell数据。本闭包将在Cell完成位置布局后调用。 bro.reloadCellAtIndex = { context in let cell = context.cell as? JXPhotoBrowserImageCell cell?.imageView.image = image } /// 可指定打开时定位到哪一页 bro.pageIndex = 1 /// 展示 bro.show() case saveDrafts: /// 保存草稿 ((takeInfo as? MoUserInfo) != nil) ? doAddUserInfoApply(operatetype: 2, userstate: 1) : doAddUserInfoApply(operatetype: 1, userstate: 1) case audit: /// 提交审核 ((takeInfo as? MoUserInfo) != nil) ? doAddUserInfoApply(operatetype: 2, userstate: 2) : doAddUserInfoApply(operatetype: 1, userstate: 2) default: break } } // MARK: - 接口请求 /// 查询对应的省市区 func queryProvincesAndCitys() { /// 异常 guard let obj = takeInfo as? MoUserInfo else { return } /// 对应的省 province = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.provinceid}) /// 对应的市 city = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.cityid}) /// 对应的区 district = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.districtid}) /// 地址信息 addressStr = "\(province?.divisionname ?? "") \(city?.divisionname ?? "") \(district?.divisionname ?? "")" } /// 查询千海金-所属机构信息 fileprivate func queryQhjAreaLists() { /// 异常 guard let qhjManager = MTP2BusinessCore.shared.qhjManager else { return } /// startAnimating _anim?.startAnimating() /// 发送查询请求 qhjManager.queryParentAreaList { isSuccess, er, objs in DispatchQueue.main.async { ///stopAnimating self._anim?.stopAnimating() /// 查询失败 if !isSuccess { WHToast.showMessage("所属机构数据获取失败,原因:\(ErrorUtils.desc(er?.retCode))", duration: ToastTimer, finishHandler: {}) return } /// 数据赋值 self.arealists = objs ?? [] } } } /// 文件上传 /// - Parameter image: image fileprivate func uploadFile(_ image: UIImage, _ isFront: Bool = true) { /// 异常 guard let commonManager = MTP2BusinessCore.shared.commonManager else { return } /// 请选择上传文件 guard let ig = file else { WHToast.showMessage("请选择上传文件", duration: ToastTimer, finishHandler: {}) return } /// startAnimating NVActivityIndicatorPresenter.sharedInstance.startAnimating(animData("文件上传中...")) /// 发送请求 commonManager.uploadImageData(image: ig) { (isSuccess, path, name, er) in DispatchQueue.main.async { /// stopAnimating NVActivityIndicatorPresenter.sharedInstance.stopAnimating() /// 查询失败 if !isSuccess { WHToast.showMessage("上传失败,原因:\(ErrorUtils.desc(er?.retCode))", duration: ToastTimer, finishHandler: {}) return } /// 上传成功 WHToast.showSuccess(withMessage: "上传成功", duration: ToastTimer, finishHandler: { if isFront { self.filePath = path } else { self.bgfilePath = path } }) } } } /// 客户资料操作申请 /// - Parameters: /// - operatetype: 操作类型-1:新增 2:修改 /// - userstate: 开户状态 1:保存草稿 2:提交审核 fileprivate func doAddUserInfoApply(operatetype: Int, userstate: Int) { /// 异常 guard let spotManager = MTP2BusinessCore.shared.spotManager, let oem = MTP2BusinessCore.shared.address?.oem, check() else { return } /// 企业名称 let name = accountType == .enterprise ? enterpriseName.text : personalName.text /// 证件类型 let type = accountType == .enterprise ? cardType : 0 /// 证件号码 let num = accountType == .enterprise ? certificateNo.text : identifyNo.text /// 手机号码 let mobile = accountType == .enterprise ? mobilePhone.text : personalPhone.text /// 法定代表人 let ledge = accountType == .enterprise ? legalName.text : nil /// 纳税人识别号 let taxpayer = accountType == .enterprise ? taxpayerNo.text : nil /// 联系人 let contract = accountType == .enterprise ? contractName.text : nil /// 昵称 let nick = accountType == .enterprise ? nickName.text : name /// 联系电话 let tel = accountType == .enterprise ? telphone.text : nil /// 证件执照路径 let url = filePath /// 地址信息 let add = detailAddress.text /// 备注信息 let rek = remark.text /// 市 let cityid = city?.autoid /// 区 let districtid = district?.autoid /// 省 let provinceid = province?.autoid /// 邮箱 let em = accountType == .personal ? email.text : nil /// 背面照 let bgurl = accountType == .personal ? self.bgfilePath : nil /// 登录密码 let loginpwd: String? = oem == "qhj" ? password.text : nil /// 登录账号 let loginno: String? = oem == "qhj" ? loginno.text : nil /// 所属机构id let areaid: Int? = oem == "qhj" ? moQhjArea?.userid : Int(MTP2BusinessCore.getUserID()) /// 用户ID(修改时必填) let userid: Int? = (takeInfo as? MoUserInfo) != nil ? Int((takeInfo as? MoUserInfo)!.userid) : nil /// startAnimating NVActivityIndicatorPresenter.sharedInstance.startAnimating(animData("请求中...")) /// 发送请求 spotManager.requestCustomerInfoOperate(operatetype: operatetype, userid: userid, areaid: areaid, logincode: loginno, loginpwd: loginpwd, userinfotype: self.accountType.rawValue, username: name, nickname: nick, cardtype: type, cardnum: num, cardfrontphotourl: url, cardbackphotourl: bgurl, email: em, referral: nil, legalpersonname: ledge, taxpayernum: taxpayer, proxystatementurl: url, contactname: contract, mobilephone: mobile, telphone: tel, provinceid: provinceid, cityid: cityid, districtid: districtid, ipaddress: add, remark: rek, userstate: userstate) { isSuccess, rsp, er in DispatchQueue.main.async { /// stopAnimating NVActivityIndicatorPresenter.sharedInstance.stopAnimating() /// 请求失败 if !isSuccess { WHToast.showMessage("请求失败,原因:\(rsp?.retDesc ?? "未知错误")", duration: ToastTimer, finishHandler: {}) return } /// 请求成功 WHToast.showSuccess(withMessage: "请求成功", duration: ToastTimer, finishHandler: { /// 执行回调 if let bk = self.popBlock { bk(userstate == 1 ? OperatorType.saveDrafts : OperatorType.add) } /// 返回上层视图 self.navigationController?.popViewController(animated: true) }) } } } // MARK: - 合规性校验 /// 合规性校验 /// - Returns: Bool fileprivate func check() -> Bool { /// 千海金需要这些功能 if MTP2BusinessCore.shared.address?.oem.lowercased() == "qhj" { /// 请选择所属机构 guard let _ = moQhjArea else { WHToast.showMessage("请选择所属机构", duration: ToastTimer, finishHandler: {}) return false } /// 请输入登录账号 guard let no = loginno.text, no.count != 0 else { WHToast.showMessage("请输入登录账号", duration: ToastTimer, finishHandler: {}) return false } /// 请输入登录密码 guard let pwd = password.text, pwd.count != 0 else { WHToast.showMessage("请输入登录密码", duration: ToastTimer, finishHandler: {}) return false } } /// 企业 if accountType == .enterprise { /// 请输入企业名称 guard let name = enterpriseName.text, name.count != 0 else { WHToast.showMessage("请输入企业名称", duration: ToastTimer, finishHandler: {}) return false } /// 请输入企业简称! guard let nick = nickName.text, nick.count != 0 else { WHToast.showMessage("请输入企业简称", duration: ToastTimer, finishHandler: {}) return false } /// 请输入正确的手机号码 guard let mo = mobilePhone.text, mo.count != 0 else { WHToast.showMessage("请输入手机号码", duration: ToastTimer, finishHandler: {}) return false } /// 请输入正确的手机号码 guard RegularUtils.checkPhoneNumber(with: mo) else { WHToast.showMessage("请输入正确的手机号码", duration: ToastTimer, finishHandler: {}) return false } } else { /// 请输入姓名 guard let name = personalName.text, name.count != 0 else { WHToast.showMessage("请输入姓名", duration: ToastTimer, finishHandler: {}) return false } /// 请输入正确的身份证号码 if let no = identifyNo.text, no != "", RegularUtils.isValidIDCard(no) { WHToast.showMessage("请输入正确的身份证号码", duration: ToastTimer, finishHandler: {}) return false } /// 请输入手机号码 guard let phone = personalPhone.text, phone.count != 0 else { WHToast.showMessage("请输入手机号码", duration: ToastTimer, finishHandler: {}) return false } /// 请输入正确的手机号码 guard RegularUtils.checkPhoneNumber(with: phone) else { WHToast.showMessage("请输入正确的手机号码", duration: ToastTimer, finishHandler: {}) return false } } return true } } // MARK: - SelectAreaPopupViewDelegate extension NewCustomerInfoViewController: SelectAreaPopupViewDelegate { /// selectAreaPopupView /// - Parameters: /// - sender: sender /// - country: country /// - province: province /// - city: city /// - district: district func selectAreaPopupView(_ sender: SelectAreaPopupView, country: MoDivisions, province: MoDivisions, city: MoDivisions?, district: MoDivisions?) { /// 隐藏视图 sender.hidePopupView() /// 地址拼接 self.country = country self.province = province self.city = city self.district = district /// 地址拼接 var areaAddress = province.divisionname /// 市 if let cityname = city?.divisionname, cityname != "null" { areaAddress.append(" \(cityname)") } /// 区 if let districtname = district?.divisionname, districtname != "null" { areaAddress.append(" \(districtname)") } self.addressStr = areaAddress } }