QRCodeResultViewController.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // QRCodeResultViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/8/26.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import ObjectMapper
  13. import CocoaLumberjack
  14. class QRCodeResultViewController: UIViewController {
  15. ///打开扫码结果
  16. static func openQRResult(result: String, vc: UIViewController) {
  17. let resultVC = QRCodeResultViewController()
  18. resultVC.scanResult = result
  19. vc.navigationController?.pushViewController(resultVC, animated: false)
  20. }
  21. @IBOutlet weak var loginStackView: UIStackView!
  22. @IBOutlet weak var loginImage: UIImageView!
  23. @IBOutlet weak var loginBtn: UIButton!
  24. @IBOutlet weak var resultLabel: O2CanCopyUILabel!
  25. //扫码结果
  26. var scanResult: String?
  27. //登录url
  28. private var loginURL: String?
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. if let result = scanResult {
  32. self.showLoading()
  33. //开始解析结果
  34. //todo 判断url还是其他
  35. self.resolveResult(result: result)
  36. }else {
  37. self.title = "扫码结果"
  38. self.resultLabel.isHidden = false
  39. self.resultLabel.text = "扫码结果为空"
  40. }
  41. }
  42. @IBAction func tap2Login(_ sender: UIButton) {
  43. //点击登陆
  44. if let login = self.loginURL {
  45. self.showLoading()
  46. let account = O2AuthSDK.shared.myInfo()
  47. AF.request(login, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: ["x-token":(account?.token)!]).responseJSON(completionHandler: { (response) in
  48. switch response.result {
  49. case .success(let val):
  50. DispatchQueue.main.async {
  51. DDLogDebug(String(describing:val))
  52. self.hideLoading()
  53. let alertController = UIAlertController(title: "扫描结果", message: "PC端登录成功", preferredStyle: .alert)
  54. let okAction = UIAlertAction(title: "确定", style: .default) {
  55. action in
  56. self.popVC()
  57. }
  58. alertController.addAction(okAction)
  59. self.presentVC(alertController)
  60. }
  61. case .failure(let err):
  62. DispatchQueue.main.async {
  63. self.hideLoading()
  64. DDLogError(err.localizedDescription)
  65. let alertController = UIAlertController(title: "扫描结果", message: "PC端登录失败", preferredStyle: .alert)
  66. let okAction = UIAlertAction(title: "确定", style: .destructive) {
  67. action in
  68. self.popVC()
  69. }
  70. alertController.addAction(okAction)
  71. self.presentVC(alertController)
  72. }
  73. }
  74. })
  75. }
  76. }
  77. ///解析扫码结果
  78. private func resolveResult(result: String) {
  79. let url = NSURL(string: result)
  80. //会议签到功能
  81. var isMeetingCheck = false
  82. let allU = url?.absoluteString
  83. if allU != nil && allU!.contains("/checkin") && allU!.contains("x_meeting_assemble_control") {
  84. isMeetingCheck = true
  85. }
  86. if(isMeetingCheck) {//会议签到
  87. self.meetingCheck(url: allU!)
  88. }else {
  89. self.hideLoading()
  90. self.title = "扫码登录"
  91. let query = url?.query
  92. let querys = query?.split("&")
  93. var meta = ""
  94. querys?.forEach { (e) in
  95. let name = e.split("=")[0]
  96. if name == "meta" {
  97. meta = e.split("=")[1]
  98. }
  99. }
  100. if meta != "" {//登录O2OA
  101. self.loginURL = AppDelegate.o2Collect.generateURLWithAppContextKey(LoginContext.loginContextKey, query: LoginContext.scanCodeAuthActionQuery, parameter: ["##meta##":meta as AnyObject])
  102. self.loginStackView.isHidden = false
  103. self.loginBtn.isHidden = false
  104. }else {//其他扫描结果
  105. // let alertController = UIAlertController(title: "扫描结果", message: result, preferredStyle: .alert)
  106. // let okAction = UIAlertAction(title: "确定", style: .default) {
  107. // action in
  108. // self.popVC()
  109. // }
  110. // alertController.addAction(okAction)
  111. // self.presentVC(alertController)
  112. self.resultLabel.isHidden = false
  113. self.resultLabel.text = result
  114. }
  115. }
  116. }
  117. //会议签到
  118. private func meetingCheck(url: String) {
  119. self.title = "会议签到"
  120. let account = O2AuthSDK.shared.myInfo()
  121. AF.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: ["x-token":(account?.token)!]).responseJSON(completionHandler: {(response) in
  122. switch response.result {
  123. case .success(let val):
  124. DispatchQueue.main.async {
  125. self.hideLoading()
  126. DDLogDebug(String(describing:val))
  127. let alertController = UIAlertController(title: "提示", message: "签到成功", preferredStyle: .alert)
  128. let okAction = UIAlertAction(title: "确定", style: .default) {
  129. action in
  130. self.popVC()
  131. }
  132. alertController.addAction(okAction)
  133. self.presentVC(alertController)
  134. }
  135. case .failure(let err):
  136. DispatchQueue.main.async {
  137. self.hideLoading()
  138. DDLogError(err.localizedDescription)
  139. let alertController = UIAlertController(title: "提示", message: "签到失败", preferredStyle: .alert)
  140. let okAction = UIAlertAction(title: "确定", style: .destructive) {
  141. action in
  142. self.popVC()
  143. }
  144. alertController.addAction(okAction)
  145. self.presentVC(alertController)
  146. }
  147. }
  148. })
  149. }
  150. }