123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- //
- // QiNiuUploadFile.swift
- // sdjy
- //
- // Created by Virgil on 2017/4/19.
- // Copyright © 2017年 Virgil. All rights reserved.
- //
- import UIKit
- class QiNiuUploadFile: NSObject {
- var uploadFileName = ""
- var backFun: RebackFunction?
- var tryCount = 0
- ///type:1成功 0失败
- func uploadFile(img: UIImage, backFunction:@escaping RebackFunction) {
- uploadFileName = ""
- let fileData = img.jpegData(compressionQuality: 0.4)! //UIImageJPEGRepresentation(img, 0.6)!
- tryCount = 0
- self.backFun = backFunction
- if qiNiuUploadImageToken == "" {
- getToken(fileData: fileData)
- } else {
- uploadFileGo(fileData: fileData)
- }
- }
- ///type:1成功 0失败
- func uploadFile(data: Data, backFunction:@escaping RebackFunction) {
- uploadFileName = ""
- tryCount = 0
- self.backFun = backFunction
- if qiNiuUploadImageToken == "" {
- getToken(fileData: data)
- } else {
- uploadFileGo(fileData: data)
- }
- }
- ///type:1成功 0失败
- func uploadFile(data: Data, fileName: String, backFunction:@escaping RebackFunction) {
- uploadFileName = fileName
- tryCount = 0
- self.backFun = backFunction
- if qiNiuUploadImageToken == "" {
- getToken(fileData: data)
- } else {
- uploadFileGo(fileData: data)
- }
- }
- private func getToken(fileData: Data) {
- let http = AFHTTPSessionManager()
- http.requestSerializer.setValue(CommonValue.getUserId(), forHTTPHeaderField: "userId")
- http.requestSerializer.setValue("", forHTTPHeaderField: "phoneType")
- http.requestSerializer.setValue(CommonUntils.getSystemVersion(), forHTTPHeaderField: "apkVersion")
- let params = NSMutableDictionary()
- let url = RequestURL.getQNToken
- SVProgressHUD.show()
- http.get(url, parameters: params, progress: nil, success: { (_, json1) -> Void in
- let json = json1 as! NSDictionary
- print(json)
- let succeed = json.object(forKey: "succeed") as! String
- if succeed == "000" {
- SVProgressHUD.dismiss()
- qiNiuUploadImageToken = json.object(forKey: "dataInfo") as! String
- self.uploadFileGo(fileData: fileData)
- } else {
- SVProgressHUD.dismiss()
- self.backFun!(0, "")
- }
- }, failure: { (_, _) -> Void in
- SVProgressHUD.dismiss()
- self.backFun!(0, "")
- })
- }
- private func uploadFileGo(fileData: Data) {
- let upManager = QNUploadManager()!
- if uploadFileName != "" {
- upManager.put(fileData, key: uploadFileName, token: qiNiuUploadImageToken, complete: { (info, _, resp) in
- let inf = info as! QNResponseInfo
- print("七牛上传返回的代码\(inf.statusCode)")
- if inf.statusCode == 200 {
- self.backFun!(1, (resp as! NSDictionary)["key"] as! String)
- } else {
- if self.tryCount == 0 {
- self.getToken(fileData: fileData)
- } else {
- self.backFun!(0, "")
- }
- }
- }, option: nil)
- } else {
- upManager.put(fileData, key: nil, token: qiNiuUploadImageToken, complete: { (info, _, resp) in
- let inf = info as! QNResponseInfo
- print("七牛上传返回的代码\(inf.statusCode)")
- if inf.statusCode == 200 {
- self.backFun!(1, (resp as! NSDictionary)["key"] as! String)
- } else {
- if self.tryCount == 0 {
- self.getToken(fileData: fileData)
- } else {
- self.backFun!(0, "")
- }
- }
- }, option: nil)
- }
- }
- }
|