123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- //
- // ApplyerViewController.swift
- // xingchuangke
- //
- // Created by Virgil on 2019/3/28.
- // Copyright © 2019 Virgil. All rights reserved.
- //
- import UIKit
- class ApplyerViewController: BaseViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
- @IBOutlet weak var btnMenu1: UIButton!
- @IBOutlet weak var btnMenu2: UIButton!
- @IBOutlet weak var btnMenu3: UIButton!
- @IBOutlet weak var btnMenu4: UIButton!
- @IBOutlet weak var viewMenu1: UIView!
- @IBOutlet weak var viewMenu2: UIView!
- @IBOutlet weak var viewMenu3: UIView!
- @IBOutlet weak var viewMenu4: UIView!
- @IBOutlet weak var img1: UIImageView!
- @IBOutlet weak var img2: UIImageView!
- @IBOutlet weak var img3: UIImageView!
- @IBOutlet weak var img4: UIImageView!
- @IBOutlet weak var viewContent: UIView!
- @IBOutlet weak var btnFinish: UIButton!
- @IBOutlet weak var txtCard: UITextField!
- @IBOutlet weak var txtName: UITextField!
- var arrImages = ["", "", "", ""]
- var arrImagesLocal = ["", "", "", ""]
- var shopOpen: ShopOpenEntity!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.title = "申请人信息"
- initNavLeftBackButton()
- CommonViewUntils.setViewAverage(arrView: [viewMenu1, viewMenu2])
- CommonViewUntils.setViewAverage(arrView: [viewMenu3, viewMenu4])
- loadDefaultData()
- }
- func loadDefaultData() {
- txtName.text = shopOpen.name
- txtCard.text = shopOpen.card
- if shopOpen.cardFront != "" {
- arrImages[0] = shopOpen.cardFront
- arrImages[1] = shopOpen.cardReverse
- arrImages[2] = shopOpen.cardHoldFront
- arrImages[3] = shopOpen.otherImage
- arrImagesLocal[0] = shopOpen.applicantCardFrontLocal
- arrImagesLocal[1] = shopOpen.applicantCardReverseLocal
- arrImagesLocal[2] = shopOpen.applicantCardHoldFrontLocal
- shopOpen.name = txtName.text!
- shopOpen.card = txtCard.text!
- img1.loadImage(imgUrl: shopOpen.cardFront, defaultImage: "")
- img2.loadImage(imgUrl: shopOpen.cardReverse, defaultImage: "")
- img3.loadImage(imgUrl: shopOpen.cardHoldFront, defaultImage: "")
- if shopOpen.otherImage != "" {
- img4.loadImage(imgUrl: shopOpen.otherImage, defaultImage: "")
- btnMenu4.isSelected = true
- }
- btnMenu1.isSelected = true
- btnMenu2.isSelected = true
- btnMenu3.isSelected = true
- }
- }
- override func KeyboardHidden(gestureRecognizer: UIGestureRecognizer) {
- txtCard.resignFirstResponder()
- txtName.resignFirstResponder()
- }
- @IBAction func btnMenuClick(_ sender: AnyObject) {
- switch (sender as! UIButton).tag {
- case 1001: // 身份证正面
- uploadImage(type: 0)
- break
- case 1002: // 身份证背面
- uploadImage(type: 1)
- break
- case 1003: // 手持身份证证件照
- uploadImage(type: 2)
- break
- case 1004: // 辅助证明材料
- uploadImage(type: 3)
- break
- case 1005: // 完成
- if !btnMenu1.isSelected {
- SVProgressHUD.showError(withStatus: "请上传身份证正面")
- return
- }
- if !btnMenu2.isSelected {
- SVProgressHUD.showError(withStatus: "请上传身份证背面")
- return
- }
- if !btnMenu3.isSelected {
- SVProgressHUD.showError(withStatus: "请上传手持身份证照")
- return
- }
- if txtName.text!.length() < 1 {
- SVProgressHUD.showError(withStatus: "请输入姓名")
- return
- }
- if txtCard.text!.length() < 1 {
- SVProgressHUD.showError(withStatus: "请输入身份证号")
- return
- }
- shopOpen.cardFront = arrImages[0]
- shopOpen.cardReverse = arrImages[1]
- shopOpen.cardHoldFront = arrImages[2]
- shopOpen.otherImage = arrImages[3]
- shopOpen.applicantCardFrontLocal = arrImagesLocal[0]
- shopOpen.applicantCardReverseLocal = arrImagesLocal[1]
- shopOpen.applicantCardHoldFrontLocal = arrImagesLocal[2]
- shopOpen.name = txtName.text!
- shopOpen.card = txtCard.text!
- handleBack()
- break
- case 1006: //
- break
- default:
- break
- }
- }
- // MARK: ============选择头像============
- var isUpload = false
- var uploadImageType = 0
- func uploadImage(type: Int) {
- if isUpload {
- return
- }
- CommonUntils.AlertView(controller: self, title: nil, message: nil, buttons: ["拍照上传", "从相册上传"], style: .actionSheet) {[weak self] (index, _) in
- self!.isUpload = true
- self!.uploadImageType = type
- let ipc = UIImagePickerController()
- if index == 0 {
- ipc.sourceType = .camera
- } else {
- ipc.sourceType = .photoLibrary
- }
- ipc.delegate = self!
- if type == 0 || type == 1 {
- ipc.allowsEditing = true
- } else {
- ipc.allowsEditing = false
- }
- self!.present(ipc, animated: true, completion: nil)
- }
- }
- var imgTemp: UIImage!
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
- self.dismiss(animated: true) {[weak self] in
- var imgTemp: UIImage? = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
- if picker.allowsEditing {
- imgTemp = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
- }
- if imgTemp != nil {
- self!.UploadFile(img: imgTemp!)
- } else {
- self!.isUpload = false
- SVProgressHUD.showError(withStatus: "图片选择失败,请重试")
- }
- }
- }
- func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
- self.isUpload = false
- self.dismiss(animated: true) {
- }
- }
- func UploadFile(img: UIImage) {
- MBProgressHUD.showAdded(to: self.view, animated: true)
- let uploadFile = QiNiuUploadFile()
- uploadFile.uploadFile(img: img) {[weak self] (type, str) in
- if type == 1 {
- let userLoginUrl = RequestURL.qiNiuImageUrl + str
- self!.arrImages[self!.uploadImageType] = userLoginUrl
- self!.uploadWeb(img: img)
- } else {
- self!.isUpload = false
- MBProgressHUD.hide(for: self!.view, animated: true)
- SVProgressHUD.showError(withStatus: "上传失败")
- }
- }
- }
- func uploadWeb(img: UIImage) {
- if self.uploadImageType == 3 {
- MBProgressHUD.hide(for: self.view, animated: true)
- self.isUpload = false
- self.btnMenu4.isSelected = true
- self.img4.loadImage(imgUrl: self.arrImages[self.uploadImageType], defaultImage: "")
- return
- }
- let manage = CommonValue.getHttp()
- let param = NSMutableDictionary()
- var url = RequestURL.uploadFile
- if self.uploadImageType == 0 {
- url = RequestURL.uploadMerchantFile
- param.setValue("2", forKey: "type")
- } else if self.uploadImageType == 1 {
- param.setValue("3", forKey: "type")
- } else if self.uploadImageType == 2 {
- param.setValue("7", forKey: "type")
- }
- manage.requestSerializer.timeoutInterval = 60.0
- manage.post(url, parameters: param, constructingBodyWith: { (formDate) in
- formDate.appendPart(withFileData: img.jpegData(compressionQuality: 0.4)!, name: "fileInput", fileName: "fileInput", mimeType: "multipart/form-data")
- }, progress: nil, success: {[weak self] (op, json1) in
- self!.isUpload = false
- let json = json1 as! NSDictionary
- print(json)
- let succeed = json.object(forKey: "succeed") as! String
- if succeed == "000" {
- if self!.uploadImageType == 0 {
- let status = (json["dataInfo"] as! NSDictionary).getInt(key: "statusCode")
- if status == 200 {
- self!.img1.loadImage(imgUrl: self!.arrImages[self!.uploadImageType], defaultImage: "")
- self!.btnMenu1.isSelected = true
- self!.arrImagesLocal[self!.uploadImageType] = (json["dataInfo"] as! NSDictionary).getString(key: "url")
- let haike = (json["dataInfo"] as! NSDictionary)["haike"] as! NSDictionary
- let hkResult = haike["result"] as! NSDictionary
- let hkResultData = hkResult["data"] as! NSDictionary
- self!.txtName.text = hkResultData.getString(key: "name")
- self!.txtCard.text = hkResultData.getString(key: "idNumber")
- } else {
- SVProgressHUD.showError(withStatus: "身份证识别失败,请重新上传!")
- }
- } else if self!.uploadImageType == 1 {
- self!.btnMenu2.isSelected = true
- self!.img2.loadImage(imgUrl: self!.arrImages[self!.uploadImageType], defaultImage: "")
- self!.arrImagesLocal[self!.uploadImageType] = (json["dataInfo"] as! NSDictionary).getString(key: "url")
- } else if self!.uploadImageType == 2 {
- self!.btnMenu3.isSelected = true
- self!.img3.loadImage(imgUrl: self!.arrImages[self!.uploadImageType], defaultImage: "")
- self!.arrImagesLocal[self!.uploadImageType] = (json["dataInfo"] as! NSDictionary).getString(key: "url")
- }
- } else {
- SVProgressHUD.showInfo(withStatus: "上传失败,请重新上传")
- }
- MBProgressHUD.hide(for: self!.view, animated: true)
- }) {[weak self] (_, _) in
- self!.isUpload = false
- SVProgressHUD.showInfo(withStatus: "上传失败,请重新上传")
- MBProgressHUD.hide(for: self!.view, animated: true)
- }
- }
- // MARK: 身份证识别
- ///上传图片到海科识别
- func uploadHaiKe(img: UIImage) {
- var test: [UInt8] = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
- let key = "abcdefghabcde05h"
- let data = img.jpegData(compressionQuality: 1)!
- let base64 = data.base64EncodedString(options: .endLineWithLineFeed)
- let dic = NSMutableDictionary()
- dic.setValue(base64, forKey: "file")
- dic.setValue("2", forKey: "type")
- let dicPara = NSMutableDictionary()
- dicPara.setValue(dic, forKey: "data")
- let jsonStr = CommonJSON.dicToJson(dic: dicPara).replacingOccurrences(of: "\n", with: "")
- // print(jsonStr);
- let tools = CryptorTools()
- tools.loadPublicKey(withFilePath: Bundle.main.path(forResource: "uen", ofType: "cer")!)
- let keyNew = RSAEncryptor.encryptString(key, publicKeyWithContentsOfFile: Bundle.main.path(forResource: "uen", ofType: "cer")!)
- ///Data 方式
- var signKey = keyNew!.data(using: .utf8)! //tools.rsaEncryptStringData(key)
- let dataContent1 = jsonStr.data(using: .utf8)! as NSData
- let dataContent = dataContent1.aes128EncryptedData(withKey: keyNew, iv: "mjVyGyIPWPClJJnuHtdIiA==")!
- // dataContent.aes
- //let dataContent2 = CryptorTools.aesEncryptString(jsonStr, keyData: signKey, iv: CommonBase64Tools.base64DecodingToData(encodedString: "mjVyGyIPWPClJJnuHtdIiA=="))
- signKey.append(dataContent)
- let base = signKey.base64EncodedData()
- var re = URLRequest(url: URL(string: "http://hkrt.uenpay.com/api/uhf/mercManage/uploadMerchantFile")!)
- re.httpMethod = "POST"
- re.setValue("utf-8", forHTTPHeaderField: "Accept-Charset")
- re.setValue("application/x-json", forHTTPHeaderField: "Content-Type")
- re.setValue("\(base.count)", forHTTPHeaderField: "Content-Length")
- re.httpBody = base
- let manage = AFURLSessionManager()
- manage.responseSerializer = AFHTTPResponseSerializer()
- manage.uploadTask(with: re, from: base, progress: { (_) in
- //print(progress);
- }) {[weak self] (_, any, error) in
- if error != nil {
- print(error)
- } else {
- print("=====")
- if any != nil {
- let data = any as! Data
- let re = self!.getResultString(str: String(data: data, encoding: .utf8)!)
- print(re)
- }
- }
- }.resume()
- }
- func getResultString(str: String) -> String {
- let tools = CryptorTools()
- tools.loadPublicKey(withFilePath: Bundle.main.path(forResource: "uen", ofType: "cer")!)
- let bstr11 = CommonBase64Tools.base64DecodingToData(encodedString: str) //base64Decoding(encodedString: str);
- let data1 = bstr11.subdata(in: Range(NSRange(location: 0, length: 256))!)
- let data2 = bstr11.subdata(in: Range(NSRange(location: 256, length: bstr11.count - 256))!)
- let key = tools.rsaDecryptData(data1)
- let keyData = key.subdata(in: Range(NSRange(location: key.count - 16, length: 16))!)
- let data = CryptorTools.aesDecryptData(data2, keyData: keyData, iv: CommonBase64Tools.base64DecodingToData(encodedString: "mjVyGyIPWPClJJnuHtdIiA=="))
- let result = String(data: data, encoding: .utf8)!
- return result
- }
- override func keyboardWasShown(noti: NSNotification) {
- if !CommonUntils.isIphonex() {
- viewContent.marginTop(top: 250)
- }
- }
- override func keyboardWasHidden(noti: NSNotification) {
- viewContent.marginTop(top: 316)
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- }
- override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
- super.init(nibName: "ApplyerViewController", bundle: nil)
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
|