AccountInputController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // AccountInputController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/22.
  6. //
  7. import Cocoa
  8. class AccountInputController: NSViewController {
  9. @IBOutlet weak var leftBox: NSBox!
  10. @IBOutlet weak var rightBox: NSBox!
  11. @IBOutlet weak var leftBackgroundIv: NSImageView!
  12. @IBOutlet weak var leftTitleLabel: NSTextField!
  13. @IBOutlet weak var leftListBox: NSBox!
  14. var glayer: CAGradientLayer?
  15. var rightListView: AccountRightListView?
  16. private var signInView_: AccountSignInView?
  17. private var loginView_: AccountLoginView?
  18. var rightDatas: [String] = [] {
  19. didSet {
  20. self.rightListView?.rightDatas = self.rightDatas
  21. }
  22. }
  23. var itemClick: KMCommonClickBlock?
  24. convenience init() {
  25. self.init(nibName: "AccountInputController", bundle: MainBundle)
  26. }
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. // Do view setup here.
  30. self.initSubviews()
  31. self.initDefaultValue()
  32. }
  33. func initDefaultValue() {
  34. self.leftBackgroundIv.image = NSImage(named: "KMImageNameAccountLeftBackgroundIcon")
  35. self.leftTitleLabel.stringValue = NSLocalizedString("Log in for a 7-Day Free Trial", comment: "")
  36. self.leftTitleLabel.font = .systemFont(ofSize: 20)
  37. self.leftTitleLabel.textColor = KMAppearance.themeColor()
  38. let gradientLayer = CAGradientLayer()
  39. let startColor = NSColor(red: 245/255.0, green: 73/255.0, blue: 212/255.0, alpha: 1.0)
  40. let endColor = NSColor(red: 125/255.0, green: 96/255.0, blue: 255/255.0, alpha: 1.0)
  41. gradientLayer.colors = [startColor, endColor]
  42. gradientLayer.startPoint = CGPoint(x: 0.5, y: 1)
  43. gradientLayer.endPoint = CGPoint(x: 0.5, y: 0)
  44. //// gradientLayer.locations = [0, 1]
  45. // self.leftTitleLabel.wantsLayer = true
  46. gradientLayer.frame = self.leftListBox.bounds
  47. //// gradientLayer.mask = self.leftTitleLabel.layer
  48. //// self.leftTitleLabel.layer?.frame = gradientLayer.bounds
  49. // self.glayer = gradientLayer
  50. //
  51. // self.leftBox.contentView?.wantsLayer = true
  52. //// self.leftBox.contentView?.layer?.insertSublayer(gradientLayer, at: 0)
  53. // self.leftBox.contentView?.layer?.addSublayer(gradientLayer)
  54. // self.leftTitleLabel.layer?.backgroundColor = NSColor.red.cgColor
  55. // self.leftTitleLabel.layer?.addSublayer(gradientLayer)
  56. self.leftListBox.wantsLayer = true
  57. self.leftListBox.layer?.addSublayer(gradientLayer)
  58. // 假设你已经有一个NSTextField实例叫textField
  59. // NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 50)];
  60. // 创建CAGradientLayer实例
  61. // CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  62. // gradientLayer.frame = textField.bounds; // 确保渐变层的大小和位置与textField相匹配
  63. // gradientLayer.colors = @[(id)[NSColor redColor].CGColor, (id)[NSColor blueColor].CGColor]; // 设置渐变色
  64. // 将textField的layer的背景色设置为Clear Color
  65. // [textField.layer setBackgroundColor: [NSColor clearColor].CGColor];
  66. // 添加渐变层作为子层
  67. // [textField.layer addSublayer:gradientLayer];
  68. // 如果需要,你可以继续自定义gradientLayer的其他属性,例如startPoint和endPoint来改变渐变方向
  69. self.leftListBox.borderWidth = 0
  70. self.leftListBox.fillColor = .clear
  71. self.leftBox.borderWidth = 0
  72. self.rightBox.borderWidth = 0
  73. }
  74. func initSubviews() {
  75. let rightListView = AccountRightListView.createFromNib()
  76. self.leftListBox.contentView = rightListView
  77. self.rightListView = rightListView
  78. rightListView?.rightDatas = self.rightDatas
  79. }
  80. func goToSignIn() {
  81. self._showSignInView()
  82. }
  83. func gotoLogin() {
  84. self._showLoginView()
  85. }
  86. private func _showSignInView() {
  87. let view = AccountSignInView.createFromNib()
  88. self.rightBox.contentView = view
  89. self.signInView_ = view
  90. view?.itemClick = { [weak self] idx, params in
  91. if idx == 1 { //切换登录
  92. self?._showLoginView()
  93. } else if idx == 2 { // 注册
  94. let state = self?._isConnectionAvailable() ?? false
  95. if !state {
  96. self?._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  97. return
  98. }
  99. guard let email = params.first as? String, email.isEmpty == false else {
  100. let string = NSLocalizedString("Please enter an email address.", comment: "")
  101. self?.signInView_?.inputView.showAccountError(string)
  102. return
  103. }
  104. if email.count > 50 {
  105. let string = NSLocalizedString("Email address must be no longer than 50 characters.", comment: "")
  106. self?.signInView_?.inputView.showAccountError(string)
  107. return
  108. }
  109. let valid = self?._validateEmail(email: email) ?? false
  110. if valid == false {
  111. let string = NSLocalizedString("Please enter a valid email address.", comment: "")
  112. self?.signInView_?.inputView.showAccountError(string)
  113. return
  114. }
  115. guard let pString = params.last as? String, pString.isEmpty == false else {
  116. let string = NSLocalizedString("Please enter a password.", comment: "")
  117. self?.signInView_?.inputView.showPasswordError(string)
  118. return
  119. }
  120. if pString.count < 6 {
  121. let string = NSLocalizedString("Password length must be 6-16 characters.", comment: "")
  122. self?.signInView_?.inputView.showPasswordError(string)
  123. return
  124. }
  125. for char in pString {
  126. let str = "\(char)"
  127. if kPwdInputStrings.contains(str) == false {
  128. let err = NSLocalizedString("Password only contains alphabets, numbers and special characters.", comment: "")
  129. self?.signInView_?.inputView.showPasswordError(err)
  130. return
  131. }
  132. }
  133. let uuid = GetHardwareUUID()
  134. let params = ["email" : email, "password" : KMHTTP.md5(pString), "device_code" : uuid]
  135. self?.signInView_?.kStartloading()
  136. KMHTTP.OEM_POST(urlString: kURLAPI_oemRegister, parameter: params, headers: nil) { success , dataModel, err in
  137. let model = AccountInfoModel(dict: dataModel?.data as? [String : Any] ?? [:])
  138. self?.signInView_?.kEndLoading()
  139. if success == false {
  140. if dataModel?.code == 406 {
  141. let msg = dataModel?.msg ?? "The email address has been registered."
  142. let string = NSLocalizedString(msg, comment: "")
  143. self?.signInView_?.inputView.showAccountError(string)
  144. } else {
  145. let code = dataModel?.code ?? 0
  146. if code == 423 || code == 412 || code == 1000 || code == 407 {
  147. let msg = dataModel?.msg ?? ""
  148. let string = NSLocalizedString(msg, comment: "")
  149. self?.signInView_?.inputView.showAccountError(string)
  150. }
  151. }
  152. return
  153. }
  154. self?.itemClick?(1, model)
  155. }
  156. }
  157. }
  158. }
  159. private func _showLoginView() {
  160. let view = AccountLoginView.createFromNib()
  161. self.rightBox.contentView = view
  162. self.loginView_ = view
  163. if let data = KMDataManager.ud_string(forKey: kAccountEmailKey) {
  164. view?.inputView.accountTextFiled.stringValue = data
  165. }
  166. view?.itemClick = { [weak self] idx, params in
  167. if idx == 1 { //切换注册
  168. self?._showSignInView()
  169. } else if idx == 2 { // 登陆
  170. let state = self?._isConnectionAvailable() ?? false
  171. if !state {
  172. self?._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  173. return
  174. }
  175. guard let email = params.first as? String, email.isEmpty == false else {
  176. let string = NSLocalizedString("Please enter an email address.", comment: "")
  177. self?.loginView_?.inputView.showAccountError(string)
  178. return
  179. }
  180. if email.count > 50 {
  181. let string = NSLocalizedString("Email address must be no longer than 50 characters.", comment: "")
  182. self?.loginView_?.inputView.showAccountError(string)
  183. return
  184. }
  185. let valid = self?._validateEmail(email: email) ?? false
  186. if valid == false {
  187. let string = NSLocalizedString("Please enter a valid email address.", comment: "")
  188. self?.loginView_?.inputView.showAccountError(string)
  189. return
  190. }
  191. guard let pString = params.last as? String, pString.isEmpty == false else {
  192. let string = NSLocalizedString("Please enter a password.", comment: "")
  193. self?.loginView_?.inputView.showPasswordError(string)
  194. return
  195. }
  196. if pString.count < 6 {
  197. let string = NSLocalizedString("Password length must be 6-16 characters.", comment: "")
  198. self?.loginView_?.inputView.showPasswordError(string)
  199. return
  200. }
  201. let uuid = GetHardwareUUID()
  202. let params = ["email" : email, "password" : KMHTTP.md5(pString), "device_code" : uuid]
  203. self?.loginView_?.kStartloading()
  204. KMHTTP.OEM_POST(urlString: kURLAPI_oemLogin, parameter: params, headers: nil) { success , dataModel, err in
  205. let model = AccountInfoModel(dict: dataModel?.data as? [String : Any] ?? [:])
  206. self?.loginView_?.kEndLoading()
  207. if success == false {
  208. if dataModel?.code == 410 {
  209. // let msg = dataModel?.msg ?? "Please enter a password."
  210. let msg = "The account doesn't exist."
  211. let string = NSLocalizedString(msg, comment: "")
  212. self?.loginView_?.inputView.showAccountError(string)
  213. } else if dataModel?.code == 409 {
  214. let msg = dataModel?.msg ?? "Please enter the correct password."
  215. let string = NSLocalizedString(msg, comment: "")
  216. self?.loginView_?.inputView.showPasswordError(string)
  217. } else {
  218. let code = dataModel?.code ?? 0
  219. if code == 422 || code == 415 || code == 414 || code == 1001 || code == 407 || code == 405 || code == 401 {
  220. let msg = dataModel?.msg ?? ""
  221. let string = NSLocalizedString(msg, comment: "")
  222. self?.loginView_?.inputView.showPasswordError(string)
  223. }
  224. }
  225. return
  226. }
  227. self?.itemClick?(1, model)
  228. }
  229. } else if idx == 3 { // 忘记密码
  230. KMTools.openURL(urlString: kResetpasswordUrlString)
  231. }
  232. }
  233. }
  234. private func _validateEmail(email: String) -> Bool {
  235. // let ss = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  236. let regex = "^[a-z0-9A-Z+-\\._]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)*\\.)+[a-zA-Z]{2,}$"
  237. // let regex = "^[a-z0-9A-Z+-]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)*)+[a-zA-Z]{2,}$"
  238. let pred = NSPredicate(format: "SELF MATCHES %@", regex)
  239. return pred.evaluate(with: email)
  240. }
  241. private func _isConnectionAvailable() -> Bool {
  242. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  243. return false
  244. }
  245. return true
  246. }
  247. private func _showHud(msg: String) {
  248. // if let data = self.view {
  249. // _ = CustomAlertView(message: msg, from: data, with: .black)
  250. CustomAlertView.alertView(message: msg, fromView: self.view, withStyle: .black)
  251. // }
  252. }
  253. }