QiNiuUploadFile.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // QiNiuUploadFile.swift
  3. // sdjy
  4. //
  5. // Created by Virgil on 2017/4/19.
  6. // Copyright © 2017年 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class QiNiuUploadFile: NSObject {
  10. var uploadFileName = ""
  11. var backFun: RebackFunction?
  12. var tryCount = 0
  13. ///type:1成功 0失败
  14. func uploadFile(img: UIImage, backFunction:@escaping RebackFunction) {
  15. uploadFileName = ""
  16. let fileData = img.jpegData(compressionQuality: 0.4)! //UIImageJPEGRepresentation(img, 0.6)!
  17. tryCount = 0
  18. self.backFun = backFunction
  19. if qiNiuUploadImageToken == "" {
  20. getToken(fileData: fileData)
  21. } else {
  22. uploadFileGo(fileData: fileData)
  23. }
  24. }
  25. ///type:1成功 0失败
  26. func uploadFile(data: Data, backFunction:@escaping RebackFunction) {
  27. uploadFileName = ""
  28. tryCount = 0
  29. self.backFun = backFunction
  30. if qiNiuUploadImageToken == "" {
  31. getToken(fileData: data)
  32. } else {
  33. uploadFileGo(fileData: data)
  34. }
  35. }
  36. ///type:1成功 0失败
  37. func uploadFile(data: Data, fileName: String, backFunction:@escaping RebackFunction) {
  38. uploadFileName = fileName
  39. tryCount = 0
  40. self.backFun = backFunction
  41. if qiNiuUploadImageToken == "" {
  42. getToken(fileData: data)
  43. } else {
  44. uploadFileGo(fileData: data)
  45. }
  46. }
  47. private func getToken(fileData: Data) {
  48. let http = AFHTTPSessionManager()
  49. http.requestSerializer.setValue(CommonValue.getUserId(), forHTTPHeaderField: "userId")
  50. http.requestSerializer.setValue("", forHTTPHeaderField: "phoneType")
  51. http.requestSerializer.setValue(CommonUntils.getSystemVersion(), forHTTPHeaderField: "apkVersion")
  52. let params = NSMutableDictionary()
  53. let url = RequestURL.getQNToken
  54. SVProgressHUD.show()
  55. http.get(url, parameters: params, progress: nil, success: { (_, json1) -> Void in
  56. let json = json1 as! NSDictionary
  57. print(json)
  58. let succeed = json.object(forKey: "succeed") as! String
  59. if succeed == "000" {
  60. SVProgressHUD.dismiss()
  61. qiNiuUploadImageToken = json.object(forKey: "dataInfo") as! String
  62. self.uploadFileGo(fileData: fileData)
  63. } else {
  64. SVProgressHUD.dismiss()
  65. self.backFun!(0, "")
  66. }
  67. }, failure: { (_, _) -> Void in
  68. SVProgressHUD.dismiss()
  69. self.backFun!(0, "")
  70. })
  71. }
  72. private func uploadFileGo(fileData: Data) {
  73. let upManager = QNUploadManager()!
  74. if uploadFileName != "" {
  75. upManager.put(fileData, key: uploadFileName, token: qiNiuUploadImageToken, complete: { (info, _, resp) in
  76. let inf = info as! QNResponseInfo
  77. print("七牛上传返回的代码\(inf.statusCode)")
  78. if inf.statusCode == 200 {
  79. self.backFun!(1, (resp as! NSDictionary)["key"] as! String)
  80. } else {
  81. if self.tryCount == 0 {
  82. self.getToken(fileData: fileData)
  83. } else {
  84. self.backFun!(0, "")
  85. }
  86. }
  87. }, option: nil)
  88. } else {
  89. upManager.put(fileData, key: nil, token: qiNiuUploadImageToken, complete: { (info, _, resp) in
  90. let inf = info as! QNResponseInfo
  91. print("七牛上传返回的代码\(inf.statusCode)")
  92. if inf.statusCode == 200 {
  93. self.backFun!(1, (resp as! NSDictionary)["key"] as! String)
  94. } else {
  95. if self.tryCount == 0 {
  96. self.getToken(fileData: fileData)
  97. } else {
  98. self.backFun!(0, "")
  99. }
  100. }
  101. }, option: nil)
  102. }
  103. }
  104. }