// // AccountInputController.swift // PDF Reader Pro // // Created by User-Tangchao on 2024/10/22. // import Cocoa class AccountInputController: NSViewController { @IBOutlet weak var leftBox: NSBox! @IBOutlet weak var rightBox: NSBox! @IBOutlet weak var leftBackgroundIv: NSImageView! @IBOutlet weak var leftTitleLabel: NSTextField! @IBOutlet weak var leftListBox: NSBox! var glayer: CAGradientLayer? var rightListView: AccountRightListView? private var signInView_: AccountSignInView? private var loginView_: AccountLoginView? var rightDatas: [String] = [] { didSet { self.rightListView?.rightDatas = self.rightDatas } } var itemClick: KMCommonClickBlock? convenience init() { self.init(nibName: "AccountInputController", bundle: MainBundle) } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. self.initSubviews() self.initDefaultValue() } func initDefaultValue() { self.leftBackgroundIv.image = NSImage(named: "KMImageNameAccountLeftBackgroundIcon") self.leftTitleLabel.stringValue = NSLocalizedString("Log in for a 3-Day Free Trial", comment: "") self.leftTitleLabel.font = .systemFont(ofSize: 20) self.leftTitleLabel.textColor = KMAppearance.themeColor() let gradientLayer = CAGradientLayer() let startColor = NSColor(red: 245/255.0, green: 73/255.0, blue: 212/255.0, alpha: 1.0) let endColor = NSColor(red: 125/255.0, green: 96/255.0, blue: 255/255.0, alpha: 1.0) gradientLayer.colors = [startColor, endColor] gradientLayer.startPoint = CGPoint(x: 0.5, y: 1) gradientLayer.endPoint = CGPoint(x: 0.5, y: 0) //// gradientLayer.locations = [0, 1] // self.leftTitleLabel.wantsLayer = true gradientLayer.frame = self.leftListBox.bounds //// gradientLayer.mask = self.leftTitleLabel.layer //// self.leftTitleLabel.layer?.frame = gradientLayer.bounds // self.glayer = gradientLayer // // self.leftBox.contentView?.wantsLayer = true //// self.leftBox.contentView?.layer?.insertSublayer(gradientLayer, at: 0) // self.leftBox.contentView?.layer?.addSublayer(gradientLayer) // self.leftTitleLabel.layer?.backgroundColor = NSColor.red.cgColor // self.leftTitleLabel.layer?.addSublayer(gradientLayer) self.leftListBox.wantsLayer = true self.leftListBox.layer?.addSublayer(gradientLayer) // 假设你已经有一个NSTextField实例叫textField // NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 50)]; // 创建CAGradientLayer实例 // CAGradientLayer *gradientLayer = [CAGradientLayer layer]; // gradientLayer.frame = textField.bounds; // 确保渐变层的大小和位置与textField相匹配 // gradientLayer.colors = @[(id)[NSColor redColor].CGColor, (id)[NSColor blueColor].CGColor]; // 设置渐变色 // 将textField的layer的背景色设置为Clear Color // [textField.layer setBackgroundColor: [NSColor clearColor].CGColor]; // 添加渐变层作为子层 // [textField.layer addSublayer:gradientLayer]; // 如果需要,你可以继续自定义gradientLayer的其他属性,例如startPoint和endPoint来改变渐变方向 self.leftListBox.borderWidth = 0 self.leftListBox.fillColor = .clear self.leftBox.borderWidth = 0 self.rightBox.borderWidth = 0 } func initSubviews() { let rightListView = AccountRightListView.createFromNib() self.leftListBox.contentView = rightListView self.rightListView = rightListView rightListView?.rightDatas = self.rightDatas } func goToSignIn() { self._showSignInView() } func gotoLogin() { self._showLoginView() } private func _showSignInView() { let view = AccountSignInView.createFromNib() self.rightBox.contentView = view self.signInView_ = view view?.itemClick = { [weak self] idx, params in if idx == 1 { //切换登录 self?._showLoginView() } else if idx == 2 { // 注册 let state = self?._isConnectionAvailable() ?? false if !state { self?._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: "")) return } guard let email = params.first as? String, email.isEmpty == false else { let string = NSLocalizedString("Please enter an email address.", comment: "") self?.signInView_?.inputView.showAccountError(string) return } if email.count > 50 { let string = NSLocalizedString("Email address must be no longer than 50 characters.", comment: "") self?.signInView_?.inputView.showAccountError(string) return } let valid = self?._validateEmail(email: email) ?? false if valid == false { let string = NSLocalizedString("Please enter a valid email address.", comment: "") self?.signInView_?.inputView.showAccountError(string) return } guard let pString = params.last as? String, pString.isEmpty == false else { let string = NSLocalizedString("Please enter a password.", comment: "") self?.signInView_?.inputView.showPasswordError(string) return } if pString.count < 6 { let string = NSLocalizedString("Password length must be 6-16 characters.", comment: "") self?.signInView_?.inputView.showPasswordError(string) return } for char in pString { let str = "\(char)" if kPwdInputStrings.contains(str) == false { let err = NSLocalizedString("Password only contains alphabets, numbers and special characters.", comment: "") self?.signInView_?.inputView.showPasswordError(err) return } } let uuid = GetHardwareUUID() let params = ["email" : email, "password" : KMHTTP.md5(pString), "device_code" : uuid] self?.signInView_?.kStartloading() KMHTTP.OEM_POST(urlString: kURLAPI_oemRegister, parameter: params, headers: nil) { success , dataModel, err in let model = AccountInfoModel(dict: dataModel?.data as? [String : Any] ?? [:]) self?.signInView_?.kEndLoading() if success == false { if dataModel?.code == 406 { let msg = dataModel?.msg ?? "The email address has been registered." let string = NSLocalizedString(msg, comment: "") self?.signInView_?.inputView.showAccountError(string) } else { let code = dataModel?.code ?? 0 if code == 423 || code == 412 || code == 1000 || code == 407 { let msg = dataModel?.msg ?? "" let string = NSLocalizedString(msg, comment: "") self?.signInView_?.inputView.showAccountError(string) } } return } self?.itemClick?(1, model) } } } } private func _showLoginView() { let view = AccountLoginView.createFromNib() self.rightBox.contentView = view self.loginView_ = view if let data = KMDataManager.ud_string(forKey: kAccountEmailKey) { view?.inputView.accountTextFiled.stringValue = data } view?.itemClick = { [weak self] idx, params in if idx == 1 { //切换注册 self?._showSignInView() } else if idx == 2 { // 登陆 let state = self?._isConnectionAvailable() ?? false if !state { self?._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: "")) return } guard let email = params.first as? String, email.isEmpty == false else { let string = NSLocalizedString("Please enter an email address.", comment: "") self?.loginView_?.inputView.showAccountError(string) return } if email.count > 50 { let string = NSLocalizedString("Email address must be no longer than 50 characters.", comment: "") self?.loginView_?.inputView.showAccountError(string) return } let valid = self?._validateEmail(email: email) ?? false if valid == false { let string = NSLocalizedString("Please enter a valid email address.", comment: "") self?.loginView_?.inputView.showAccountError(string) return } guard let pString = params.last as? String, pString.isEmpty == false else { let string = NSLocalizedString("Please enter a password.", comment: "") self?.loginView_?.inputView.showPasswordError(string) return } if pString.count < 6 { let string = NSLocalizedString("Password length must be 6-16 characters.", comment: "") self?.loginView_?.inputView.showPasswordError(string) return } let uuid = GetHardwareUUID() let params = ["email" : email, "password" : KMHTTP.md5(pString), "device_code" : uuid] self?.loginView_?.kStartloading() KMHTTP.OEM_POST(urlString: kURLAPI_oemLogin, parameter: params, headers: nil) { success , dataModel, err in let model = AccountInfoModel(dict: dataModel?.data as? [String : Any] ?? [:]) self?.loginView_?.kEndLoading() if success == false { if dataModel?.code == 410 { // let msg = dataModel?.msg ?? "Please enter a password." let msg = "The account doesn't exist." let string = NSLocalizedString(msg, comment: "") self?.loginView_?.inputView.showAccountError(string) } else if dataModel?.code == 409 { let msg = dataModel?.msg ?? "Please enter the correct password." let string = NSLocalizedString(msg, comment: "") self?.loginView_?.inputView.showPasswordError(string) } else { let code = dataModel?.code ?? 0 if code == 422 || code == 415 || code == 414 || code == 1001 || code == 407 || code == 405 || code == 401 { let msg = dataModel?.msg ?? "" let string = NSLocalizedString(msg, comment: "") self?.loginView_?.inputView.showPasswordError(string) } } return } self?.itemClick?(1, model) } } else if idx == 3 { // 忘记密码 KMTools.openURL(urlString: kResetpasswordUrlString) } } } private func _validateEmail(email: String) -> Bool { // let ss = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" let regex = "^[a-z0-9A-Z+-\\._]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)*\\.)+[a-zA-Z]{2,}$" // let regex = "^[a-z0-9A-Z+-]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)*)+[a-zA-Z]{2,}$" let pred = NSPredicate(format: "SELF MATCHES %@", regex) return pred.evaluate(with: email) } private func _isConnectionAvailable() -> Bool { if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 { return false } return true } private func _showHud(msg: String) { // if let data = self.view { // _ = CustomAlertView(message: msg, from: data, with: .black) CustomAlertView.alertView(message: msg, fromView: self.view, withStyle: .black) // } } }