ApplyerViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // ApplyerViewController.swift
  3. // xingchuangke
  4. //
  5. // Created by Virgil on 2019/3/28.
  6. // Copyright © 2019 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class ApplyerViewController: BaseViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  10. @IBOutlet weak var btnMenu1: UIButton!
  11. @IBOutlet weak var btnMenu2: UIButton!
  12. @IBOutlet weak var btnMenu3: UIButton!
  13. @IBOutlet weak var btnMenu4: UIButton!
  14. @IBOutlet weak var viewMenu1: UIView!
  15. @IBOutlet weak var viewMenu2: UIView!
  16. @IBOutlet weak var viewMenu3: UIView!
  17. @IBOutlet weak var viewMenu4: UIView!
  18. @IBOutlet weak var img1: UIImageView!
  19. @IBOutlet weak var img2: UIImageView!
  20. @IBOutlet weak var img3: UIImageView!
  21. @IBOutlet weak var img4: UIImageView!
  22. @IBOutlet weak var viewContent: UIView!
  23. @IBOutlet weak var btnFinish: UIButton!
  24. @IBOutlet weak var txtCard: UITextField!
  25. @IBOutlet weak var txtName: UITextField!
  26. var arrImages = ["", "", "", ""]
  27. var arrImagesLocal = ["", "", "", ""]
  28. var shopOpen: ShopOpenEntity!
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. self.title = "申请人信息"
  32. initNavLeftBackButton()
  33. CommonViewUntils.setViewAverage(arrView: [viewMenu1, viewMenu2])
  34. CommonViewUntils.setViewAverage(arrView: [viewMenu3, viewMenu4])
  35. loadDefaultData()
  36. }
  37. func loadDefaultData() {
  38. txtName.text = shopOpen.name
  39. txtCard.text = shopOpen.card
  40. if shopOpen.cardFront != "" {
  41. arrImages[0] = shopOpen.cardFront
  42. arrImages[1] = shopOpen.cardReverse
  43. arrImages[2] = shopOpen.cardHoldFront
  44. arrImages[3] = shopOpen.otherImage
  45. arrImagesLocal[0] = shopOpen.applicantCardFrontLocal
  46. arrImagesLocal[1] = shopOpen.applicantCardReverseLocal
  47. arrImagesLocal[2] = shopOpen.applicantCardHoldFrontLocal
  48. shopOpen.name = txtName.text!
  49. shopOpen.card = txtCard.text!
  50. img1.loadImage(imgUrl: shopOpen.cardFront, defaultImage: "")
  51. img2.loadImage(imgUrl: shopOpen.cardReverse, defaultImage: "")
  52. img3.loadImage(imgUrl: shopOpen.cardHoldFront, defaultImage: "")
  53. if shopOpen.otherImage != "" {
  54. img4.loadImage(imgUrl: shopOpen.otherImage, defaultImage: "")
  55. btnMenu4.isSelected = true
  56. }
  57. btnMenu1.isSelected = true
  58. btnMenu2.isSelected = true
  59. btnMenu3.isSelected = true
  60. }
  61. }
  62. override func KeyboardHidden(gestureRecognizer: UIGestureRecognizer) {
  63. txtCard.resignFirstResponder()
  64. txtName.resignFirstResponder()
  65. }
  66. @IBAction func btnMenuClick(_ sender: AnyObject) {
  67. switch (sender as! UIButton).tag {
  68. case 1001: // 身份证正面
  69. uploadImage(type: 0)
  70. break
  71. case 1002: // 身份证背面
  72. uploadImage(type: 1)
  73. break
  74. case 1003: // 手持身份证证件照
  75. uploadImage(type: 2)
  76. break
  77. case 1004: // 辅助证明材料
  78. uploadImage(type: 3)
  79. break
  80. case 1005: // 完成
  81. if !btnMenu1.isSelected {
  82. SVProgressHUD.showError(withStatus: "请上传身份证正面")
  83. return
  84. }
  85. if !btnMenu2.isSelected {
  86. SVProgressHUD.showError(withStatus: "请上传身份证背面")
  87. return
  88. }
  89. if !btnMenu3.isSelected {
  90. SVProgressHUD.showError(withStatus: "请上传手持身份证照")
  91. return
  92. }
  93. if txtName.text!.length() < 1 {
  94. SVProgressHUD.showError(withStatus: "请输入姓名")
  95. return
  96. }
  97. if txtCard.text!.length() < 1 {
  98. SVProgressHUD.showError(withStatus: "请输入身份证号")
  99. return
  100. }
  101. shopOpen.cardFront = arrImages[0]
  102. shopOpen.cardReverse = arrImages[1]
  103. shopOpen.cardHoldFront = arrImages[2]
  104. shopOpen.otherImage = arrImages[3]
  105. shopOpen.applicantCardFrontLocal = arrImagesLocal[0]
  106. shopOpen.applicantCardReverseLocal = arrImagesLocal[1]
  107. shopOpen.applicantCardHoldFrontLocal = arrImagesLocal[2]
  108. shopOpen.name = txtName.text!
  109. shopOpen.card = txtCard.text!
  110. handleBack()
  111. break
  112. case 1006: //
  113. break
  114. default:
  115. break
  116. }
  117. }
  118. // MARK: ============选择头像============
  119. var isUpload = false
  120. var uploadImageType = 0
  121. func uploadImage(type: Int) {
  122. if isUpload {
  123. return
  124. }
  125. CommonUntils.AlertView(controller: self, title: nil, message: nil, buttons: ["拍照上传", "从相册上传"], style: .actionSheet) {[weak self] (index, _) in
  126. self!.isUpload = true
  127. self!.uploadImageType = type
  128. let ipc = UIImagePickerController()
  129. if index == 0 {
  130. ipc.sourceType = .camera
  131. } else {
  132. ipc.sourceType = .photoLibrary
  133. }
  134. ipc.delegate = self!
  135. if type == 0 || type == 1 {
  136. ipc.allowsEditing = true
  137. } else {
  138. ipc.allowsEditing = false
  139. }
  140. self!.present(ipc, animated: true, completion: nil)
  141. }
  142. }
  143. var imgTemp: UIImage!
  144. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
  145. self.dismiss(animated: true) {[weak self] in
  146. var imgTemp: UIImage? = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
  147. if picker.allowsEditing {
  148. imgTemp = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
  149. }
  150. if imgTemp != nil {
  151. self!.UploadFile(img: imgTemp!)
  152. } else {
  153. self!.isUpload = false
  154. SVProgressHUD.showError(withStatus: "图片选择失败,请重试")
  155. }
  156. }
  157. }
  158. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  159. self.isUpload = false
  160. self.dismiss(animated: true) {
  161. }
  162. }
  163. func UploadFile(img: UIImage) {
  164. MBProgressHUD.showAdded(to: self.view, animated: true)
  165. let uploadFile = QiNiuUploadFile()
  166. uploadFile.uploadFile(img: img) {[weak self] (type, str) in
  167. if type == 1 {
  168. let userLoginUrl = RequestURL.qiNiuImageUrl + str
  169. self!.arrImages[self!.uploadImageType] = userLoginUrl
  170. self!.uploadWeb(img: img)
  171. } else {
  172. self!.isUpload = false
  173. MBProgressHUD.hide(for: self!.view, animated: true)
  174. SVProgressHUD.showError(withStatus: "上传失败")
  175. }
  176. }
  177. }
  178. func uploadWeb(img: UIImage) {
  179. if self.uploadImageType == 3 {
  180. MBProgressHUD.hide(for: self.view, animated: true)
  181. self.isUpload = false
  182. self.btnMenu4.isSelected = true
  183. self.img4.loadImage(imgUrl: self.arrImages[self.uploadImageType], defaultImage: "")
  184. return
  185. }
  186. let manage = CommonValue.getHttp()
  187. let param = NSMutableDictionary()
  188. var url = RequestURL.uploadFile
  189. if self.uploadImageType == 0 {
  190. url = RequestURL.uploadMerchantFile
  191. param.setValue("2", forKey: "type")
  192. } else if self.uploadImageType == 1 {
  193. param.setValue("3", forKey: "type")
  194. } else if self.uploadImageType == 2 {
  195. param.setValue("7", forKey: "type")
  196. }
  197. manage.requestSerializer.timeoutInterval = 60.0
  198. manage.post(url, parameters: param, constructingBodyWith: { (formDate) in
  199. formDate.appendPart(withFileData: img.jpegData(compressionQuality: 0.4)!, name: "fileInput", fileName: "fileInput", mimeType: "multipart/form-data")
  200. }, progress: nil, success: {[weak self] (op, json1) in
  201. self!.isUpload = false
  202. let json = json1 as! NSDictionary
  203. print(json)
  204. let succeed = json.object(forKey: "succeed") as! String
  205. if succeed == "000" {
  206. if self!.uploadImageType == 0 {
  207. let status = (json["dataInfo"] as! NSDictionary).getInt(key: "statusCode")
  208. if status == 200 {
  209. self!.img1.loadImage(imgUrl: self!.arrImages[self!.uploadImageType], defaultImage: "")
  210. self!.btnMenu1.isSelected = true
  211. self!.arrImagesLocal[self!.uploadImageType] = (json["dataInfo"] as! NSDictionary).getString(key: "url")
  212. let haike = (json["dataInfo"] as! NSDictionary)["haike"] as! NSDictionary
  213. let hkResult = haike["result"] as! NSDictionary
  214. let hkResultData = hkResult["data"] as! NSDictionary
  215. self!.txtName.text = hkResultData.getString(key: "name")
  216. self!.txtCard.text = hkResultData.getString(key: "idNumber")
  217. } else {
  218. SVProgressHUD.showError(withStatus: "身份证识别失败,请重新上传!")
  219. }
  220. } else if self!.uploadImageType == 1 {
  221. self!.btnMenu2.isSelected = true
  222. self!.img2.loadImage(imgUrl: self!.arrImages[self!.uploadImageType], defaultImage: "")
  223. self!.arrImagesLocal[self!.uploadImageType] = (json["dataInfo"] as! NSDictionary).getString(key: "url")
  224. } else if self!.uploadImageType == 2 {
  225. self!.btnMenu3.isSelected = true
  226. self!.img3.loadImage(imgUrl: self!.arrImages[self!.uploadImageType], defaultImage: "")
  227. self!.arrImagesLocal[self!.uploadImageType] = (json["dataInfo"] as! NSDictionary).getString(key: "url")
  228. }
  229. } else {
  230. SVProgressHUD.showInfo(withStatus: "上传失败,请重新上传")
  231. }
  232. MBProgressHUD.hide(for: self!.view, animated: true)
  233. }) {[weak self] (_, _) in
  234. self!.isUpload = false
  235. SVProgressHUD.showInfo(withStatus: "上传失败,请重新上传")
  236. MBProgressHUD.hide(for: self!.view, animated: true)
  237. }
  238. }
  239. // MARK: 身份证识别
  240. ///上传图片到海科识别
  241. func uploadHaiKe(img: UIImage) {
  242. var test: [UInt8] = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
  243. let key = "abcdefghabcde05h"
  244. let data = img.jpegData(compressionQuality: 1)!
  245. let base64 = data.base64EncodedString(options: .endLineWithLineFeed)
  246. let dic = NSMutableDictionary()
  247. dic.setValue(base64, forKey: "file")
  248. dic.setValue("2", forKey: "type")
  249. let dicPara = NSMutableDictionary()
  250. dicPara.setValue(dic, forKey: "data")
  251. let jsonStr = CommonJSON.dicToJson(dic: dicPara).replacingOccurrences(of: "\n", with: "")
  252. // print(jsonStr);
  253. let tools = CryptorTools()
  254. tools.loadPublicKey(withFilePath: Bundle.main.path(forResource: "uen", ofType: "cer")!)
  255. let keyNew = RSAEncryptor.encryptString(key, publicKeyWithContentsOfFile: Bundle.main.path(forResource: "uen", ofType: "cer")!)
  256. ///Data 方式
  257. var signKey = keyNew!.data(using: .utf8)! //tools.rsaEncryptStringData(key)
  258. let dataContent1 = jsonStr.data(using: .utf8)! as NSData
  259. let dataContent = dataContent1.aes128EncryptedData(withKey: keyNew, iv: "mjVyGyIPWPClJJnuHtdIiA==")!
  260. // dataContent.aes
  261. //let dataContent2 = CryptorTools.aesEncryptString(jsonStr, keyData: signKey, iv: CommonBase64Tools.base64DecodingToData(encodedString: "mjVyGyIPWPClJJnuHtdIiA=="))
  262. signKey.append(dataContent)
  263. let base = signKey.base64EncodedData()
  264. var re = URLRequest(url: URL(string: "http://hkrt.uenpay.com/api/uhf/mercManage/uploadMerchantFile")!)
  265. re.httpMethod = "POST"
  266. re.setValue("utf-8", forHTTPHeaderField: "Accept-Charset")
  267. re.setValue("application/x-json", forHTTPHeaderField: "Content-Type")
  268. re.setValue("\(base.count)", forHTTPHeaderField: "Content-Length")
  269. re.httpBody = base
  270. let manage = AFURLSessionManager()
  271. manage.responseSerializer = AFHTTPResponseSerializer()
  272. manage.uploadTask(with: re, from: base, progress: { (_) in
  273. //print(progress);
  274. }) {[weak self] (_, any, error) in
  275. if error != nil {
  276. print(error)
  277. } else {
  278. print("=====")
  279. if any != nil {
  280. let data = any as! Data
  281. let re = self!.getResultString(str: String(data: data, encoding: .utf8)!)
  282. print(re)
  283. }
  284. }
  285. }.resume()
  286. }
  287. func getResultString(str: String) -> String {
  288. let tools = CryptorTools()
  289. tools.loadPublicKey(withFilePath: Bundle.main.path(forResource: "uen", ofType: "cer")!)
  290. let bstr11 = CommonBase64Tools.base64DecodingToData(encodedString: str) //base64Decoding(encodedString: str);
  291. let data1 = bstr11.subdata(in: Range(NSRange(location: 0, length: 256))!)
  292. let data2 = bstr11.subdata(in: Range(NSRange(location: 256, length: bstr11.count - 256))!)
  293. let key = tools.rsaDecryptData(data1)
  294. let keyData = key.subdata(in: Range(NSRange(location: key.count - 16, length: 16))!)
  295. let data = CryptorTools.aesDecryptData(data2, keyData: keyData, iv: CommonBase64Tools.base64DecodingToData(encodedString: "mjVyGyIPWPClJJnuHtdIiA=="))
  296. let result = String(data: data, encoding: .utf8)!
  297. return result
  298. }
  299. override func keyboardWasShown(noti: NSNotification) {
  300. if !CommonUntils.isIphonex() {
  301. viewContent.marginTop(top: 250)
  302. }
  303. }
  304. override func keyboardWasHidden(noti: NSNotification) {
  305. viewContent.marginTop(top: 316)
  306. }
  307. override func viewWillAppear(_ animated: Bool) {
  308. super.viewWillAppear(animated)
  309. }
  310. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  311. super.init(nibName: "ApplyerViewController", bundle: nil)
  312. }
  313. required init?(coder aDecoder: NSCoder) {
  314. fatalError("init(coder:) has not been implemented")
  315. }
  316. }