【Swift】接入阿里雲一鍵登入(原始碼,可以直接貼走)
阿新 • • 發佈:2022-01-06
//MAKR: 一鍵登入 func toLoginAction() { let model = createLoginModel() TXCommonHandler.sharedInstance().getLoginToken(withTimeout: 3.0, controller: self, model: model) { resultDic in if let dict = resultDic as? Dictionary<String, Any> { var resultCode = ""if let code = dict["resultCode"] as? String { resultCode = code } if resultCode == PNSCodeLoginControllerPresentSuccess { print("授權頁拉起成功回撥:\(dict)") } else if resultCode == PNSCodeLoginControllerClickCancel { print("使用者取消一鍵登入:\(dict)") TXCommonHandler.sharedInstance().cancelLoginVC(animated: true, complete: nil) } else if resultCode == PNSCodeLoginControllerClickLoginBtn { print("使用者點選一鍵登入:\(dict)") var isChecked : Int= 0 if let checked = dict["isChecked"] as? Int { isChecked = checked } if isChecked == 0 { HBAlertUtils.hb_showFailMessage("請先同意相關開發協議", in: UIApplication.topViewController()?.view) return } } else if resultCode == PNSCodeLoginControllerClickCheckBoxBtn { print("使用者點選是否同意協議:\(dict)") } else if resultCode == PNSCodeLoginControllerClickChangeBtn { print("使用者點選切換其他登入") TXCommonHandler.sharedInstance().cancelLoginVC(animated: true) { let telLogin = LoginTelphoneController() self.navigationController!.pushViewController(telLogin, animated: true) } } else if resultCode == PNSCodeSuccess { print("獲取LoginToken成功回撥:\(dict)") print("接下來可以拿著Token去服務端換取手機號,有了手機號就可以登入,SDK提供服務到此結束") var tokenText = "" if let token = dict["token"] as? String { tokenText = token } TXCommonHandler.sharedInstance().cancelLoginVC(animated: true) { // self.userLoginByAli(token: tokenText) } } else { print("獲取LoginToken或拉起授權頁失敗回撥:\(dict)") let telLogin = LoginTelphoneController() self.navigationController!.pushViewController(telLogin, animated: true) } } } }
//MARK: 建立一鍵登入model func createLoginModel() -> TXCustomModel { let model = TXCustomModel() model.supportedInterfaceOrientations = .portrait model.navColor = UIColor.white model.navTitle = NSAttributedString(string: "") model.navBackImage = UIImage.init(named: "icn_dismiss_24")! model.hideNavBackItem = false model.logoImage = UIImage.init(named: "login_app_logo")! model.numberColor = UIColor.init(hexString: "#333333") model.numberFont = UIFont.init(name: PF_Regular, size: Font_24)! model.sloganIsHidden = true //一鍵登入 var imgArr = [UIImage]() imgArr.append(UIImage.init(named: "login_ali_bg")!) imgArr.append(UIImage.init(named: "login_ali_bg")!) imgArr.append(UIImage.init(named: "login_ali_bg")!) model.loginBtnBgImgs = imgArr let loginTitle = "本機號碼一鍵登入" let loginAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font:UIFont.init(name: PF_Regular, size: Font_17)] let loginAttri = NSAttributedString(string: loginTitle, attributes: loginAttribute as [NSAttributedString.Key : Any]) model.loginBtnText = loginAttri model.changeBtnIsHidden = false let otherLoginTitle = "切換其他手機號" let otherLoginAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#F9A51C"),NSAttributedString.Key.font:UIFont.init(name: PF_Regular, size: Font_15)] let otherLoginAttri = NSAttributedString(string: otherLoginTitle, attributes: otherLoginAttribute as [NSAttributedString.Key : Any]) model.changeBtnTitle = otherLoginAttri var checkArr = [UIImage]() checkArr.append(UIImage.init(named: "icn_agree_normal")!) checkArr.append(UIImage.init(named: "icn_agree_selected")!) model.checkBoxImages = checkArr model.checkBoxIsChecked = false model.checkBoxIsHidden = false model.checkBoxImageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2) model.checkBoxWH = 24 model.privacyOne = ["《使用協議》","http://******"] model.privacyTwo = ["《隱私協議》","http://******"] model.privacyConectTexts = ["和","和"] model.privacyOperatorPreText = "《" model.privacyOperatorSufText = "》" model.privacyPreText = "已同意" model.privacyColors = [UIColor.init(hexString: "#757575"),UIColor.init(hexString: "#F4A30E")] model.privacyFont = UIFont.init(name: PF_Regular, size: Font_16)! model.privacyNavBackImage = UIImage.init(named: "icn_back_black_24")! //logo的位置 model.logoFrameBlock = { (screenSize, superViewSize,frame) -> CGRect in return CGRect(x:kScreen_Width / 2 - 80, y: 21, width: 160, height: 136) } //手機號碼的位置 model.numberFrameBlock = { (screenSize, superViewSize,frame) -> CGRect in return CGRect(x: kScreen_Width / 2 - 72, y: 165, width: 144, height: 32) } model.privacyFrameBlock = {(screenSize, superViewSize,frame) -> CGRect in return CGRect(x: kScreen_Width / 2 - 148, y: KDeviceX ? 500 : 381, width: 296, height: 48) } //一鍵登入 model.loginBtnFrameBlock = {(screenSize, superViewSize,frame) -> CGRect in return CGRect(x: kScreen_Width / 2 - 148, y: KDeviceX ? 558 : 439, width: 296, height: 48) } //其他登入 model.changeBtnFrameBlock = {(screenSize, superViewSize,frame) -> CGRect in return CGRect(x: kScreen_Width / 2 - 75, y: KDeviceX ? 645 : 526, width: 150, height: 38) } return model }