QRCodeResultViewController.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /// 扫码结果是否会议签到、扫码登录
  22. static func checkResultIsO2(result: String) -> (Bool, String) {
  23. let url = NSURL(string: result)
  24. //会议签到功能
  25. var isMeetingCheck = false
  26. let allU = url?.absoluteString
  27. if allU != nil && allU!.contains("/checkin") && allU!.contains("x_meeting_assemble_control") {
  28. isMeetingCheck = true
  29. }
  30. let query = url?.query
  31. let querys = query?.split("&")
  32. var meta = ""
  33. querys?.forEach { (e) in
  34. let name = e.split("=")[0]
  35. if name == "meta" {
  36. meta = e.split("=")[1]
  37. }
  38. }
  39. return (isMeetingCheck, meta)
  40. }
  41. @IBOutlet weak var loginStackView: UIStackView!
  42. @IBOutlet weak var loginImage: UIImageView!
  43. @IBOutlet weak var loginBtn: UIButton!
  44. @IBOutlet weak var resultLabel: O2CanCopyUILabel!
  45. //扫码结果
  46. var scanResult: String?
  47. //登录url
  48. private var loginURL: String?
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. if let result = scanResult {
  52. self.showLoading()
  53. //开始解析结果
  54. //todo 判断url还是其他
  55. self.resolveResult(result: result)
  56. }else {
  57. self.title = "扫码结果"
  58. self.resultLabel.isHidden = false
  59. self.resultLabel.text = "扫码结果为空"
  60. }
  61. }
  62. @IBAction func tap2Login(_ sender: UIButton) {
  63. //点击登陆
  64. if let login = self.loginURL {
  65. self.showLoading()
  66. let account = O2AuthSDK.shared.myInfo()
  67. let tokenName = O2AuthSDK.shared.tokenName()
  68. AF.request(login, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: [tokenName:(account?.token)!]).responseJSON(completionHandler: { (response) in
  69. switch response.result {
  70. case .success(let val):
  71. DispatchQueue.main.async {
  72. DDLogDebug(String(describing:val))
  73. self.hideLoading()
  74. let alertController = UIAlertController(title: "扫描结果", message: "PC端登录成功", preferredStyle: .alert)
  75. let okAction = UIAlertAction(title: "确定", style: .default) {
  76. action in
  77. self.popVC()
  78. }
  79. alertController.addAction(okAction)
  80. self.presentVC(alertController)
  81. }
  82. case .failure(let err):
  83. DispatchQueue.main.async {
  84. self.hideLoading()
  85. DDLogError(err.localizedDescription)
  86. let alertController = UIAlertController(title: "扫描结果", message: "PC端登录失败", preferredStyle: .alert)
  87. let okAction = UIAlertAction(title: "确定", style: .destructive) {
  88. action in
  89. self.popVC()
  90. }
  91. alertController.addAction(okAction)
  92. self.presentVC(alertController)
  93. }
  94. }
  95. })
  96. }
  97. }
  98. ///解析扫码结果
  99. private func resolveResult(result: String) {
  100. let url = NSURL(string: result)
  101. //会议签到功能
  102. var isMeetingCheck = false
  103. let allU = url?.absoluteString
  104. if allU != nil && allU!.contains("/checkin") && allU!.contains("x_meeting_assemble_control") {
  105. isMeetingCheck = true
  106. }
  107. if(isMeetingCheck) {//会议签到
  108. self.meetingCheck(url: allU!)
  109. }else {
  110. self.hideLoading()
  111. self.title = "扫码登录"
  112. let query = url?.query
  113. let querys = query?.split("&")
  114. var meta = ""
  115. querys?.forEach { (e) in
  116. let name = e.split("=")[0]
  117. if name == "meta" {
  118. meta = e.split("=")[1]
  119. }
  120. }
  121. if meta != "" {//登录O2OA
  122. self.loginURL = AppDelegate.o2Collect.generateURLWithAppContextKey(LoginContext.loginContextKey, query: LoginContext.scanCodeAuthActionQuery, parameter: ["##meta##":meta as AnyObject])
  123. self.loginStackView.isHidden = false
  124. self.loginBtn.isHidden = false
  125. }else {//其他扫描结果
  126. self.resultLabel.isHidden = false
  127. self.resultLabel.text = result
  128. }
  129. }
  130. }
  131. //会议签到
  132. private func meetingCheck(url: String) {
  133. self.title = "会议签到"
  134. let account = O2AuthSDK.shared.myInfo()
  135. let tokenName = O2AuthSDK.shared.tokenName()
  136. AF.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: [tokenName:(account?.token)!]).responseJSON(completionHandler: {(response) in
  137. switch response.result {
  138. case .success(let val):
  139. DispatchQueue.main.async {
  140. self.hideLoading()
  141. DDLogDebug(String(describing:val))
  142. let alertController = UIAlertController(title: "提示", message: "签到成功", preferredStyle: .alert)
  143. let okAction = UIAlertAction(title: "确定", style: .default) {
  144. action in
  145. self.popVC()
  146. }
  147. alertController.addAction(okAction)
  148. self.presentVC(alertController)
  149. }
  150. case .failure(let err):
  151. DispatchQueue.main.async {
  152. self.hideLoading()
  153. DDLogError(err.localizedDescription)
  154. let alertController = UIAlertController(title: "提示", message: "签到失败", preferredStyle: .alert)
  155. let okAction = UIAlertAction(title: "确定", style: .destructive) {
  156. action in
  157. self.popVC()
  158. }
  159. alertController.addAction(okAction)
  160. self.presentVC(alertController)
  161. }
  162. }
  163. })
  164. }
  165. }