ApplyCardExtensionViewController.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // ApplyCardExtensionViewController.swift
  3. // xingchuangke
  4. //
  5. // Created by Virgil on 2019/7/16.
  6. // Copyright © 2019 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class ApplyCardExtensionViewController: BaseViewController {
  10. @IBOutlet weak var imgView: UIImageView!
  11. /// 0办卡 1:积分
  12. var type = 0
  13. var wkWebView: WKWebView!
  14. var wkBridge: WKWebViewJavascriptBridge!
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. let config = WKWebViewConfiguration.init()
  18. wkWebView = WKWebView.init(frame: self.view.bounds, configuration: config)
  19. self.view.addSubview(wkWebView)
  20. // let htmlPath = "\(RequestURL.hostMainH5)tuiguang.html?type=\(type)"
  21. let htmlPath = "http://106.xingchuangke.net:8889/H5/tuiguang.html?type=0"
  22. wkWebView.loadDataForUrl(url: htmlPath)
  23. wkBridge = WKWebViewJavascriptBridge(for: wkWebView)
  24. wkBridge.setWebViewDelegate(self)
  25. // 接收js调用方法
  26. wkBridge.registerHandler("btnTest") { [weak self](data, _) in
  27. let dic = data as? NSDictionary
  28. if dic != nil {
  29. if dic!.getString(key: "funName") == "saveImage" {
  30. let param = CommonJSON.jsonToDic(json: dic!.getString(key: "param"))
  31. if param != nil {
  32. let index = param!.getInt(key: "index")
  33. if self!.arrData.count > index {
  34. let imgUrl = self!.getString(current: index, key: "image")
  35. SVProgressHUD.show()
  36. self!.imgView.sd_setImage(with: URL(string: imgUrl), completed: { (_, error, _, _) in
  37. if error == nil {
  38. self!.saveImage()
  39. } else {
  40. SVProgressHUD.dismiss()
  41. }
  42. })
  43. }
  44. }
  45. }
  46. }
  47. }
  48. WKWebViewJavascriptBridge.enableLogging()
  49. loadData()
  50. }
  51. func btnRightClick() {
  52. wkBridge.callHandler("getSelectIndex", data: nil) {[weak self] (content) in
  53. let param = content as? NSDictionary
  54. if param != nil {
  55. print(param!)
  56. let index = param!.getInt(key: "index")
  57. if self!.arrData.count > index {
  58. let imgUrl = self!.getString(current: index, key: "image")
  59. SVProgressHUD.show()
  60. self!.imgView.sd_setImage(with: URL(string: imgUrl), completed: { (img, error, _, url) in
  61. SVProgressHUD.dismiss()
  62. if error == nil {
  63. if self!.type == 0 {
  64. let urlStr = "http://app.xingchuangke.net/jsp/creditBank/bank_list.jsp?userId=\(CommonValue.getUserId())"
  65. CommonUMengShare.showSharedMenuCus(viewController: self!, title: "免费办理信用卡,分享即可拿奖励", desc: "办卡有奖励、手续简、下卡快、额度高", icon: UIImage(named: "icon")!, url: urlStr, img: self!.imgView.image!)
  66. } else {
  67. CommonUMengShare.showSharedMenuCus(viewController: self!, title: "立即注册,开启您的财富之旅", desc: "POS机办理,信用卡申请,积分换钱......", icon: UIImage(named: "icon")!, url: "\(RequestURL.userRecCode)?recCode=\(CommonValue.getUserRecCode())", img: self!.imgView.image!)
  68. }
  69. }
  70. })
  71. }
  72. }
  73. }
  74. }
  75. func saveImage() {
  76. if imgView.image != nil {
  77. let image = imgView.image!
  78. var urlStr = ""
  79. if type == 0 {
  80. urlStr = "http://app.xingchuangke.net/jsp/creditBank/bank_list.jsp?userId=\(CommonValue.getUserId())"
  81. } else {
  82. urlStr = "\(RequestURL.userRecCode)?recCode=\(CommonValue.getUserRecCode())"
  83. }
  84. let image2 = CommonBarCodeTools.setupQRCodeImage(urlStr, image: UIImage(named: "icon"))
  85. let img = drawImages(imageArray: [image, image2])
  86. UIImageWriteToSavedPhotosAlbum(img, self, #selector(saveImageResult), nil)
  87. } else {
  88. SVProgressHUD.dismiss()
  89. }
  90. }
  91. func drawImages(imageArray: [UIImage]) -> UIImage {
  92. // 1.1.图片的宽度
  93. let width: CGFloat = imageArray[0].size.width
  94. // 1.2.图片的高度
  95. let height: CGFloat = imageArray[0].size.height
  96. // 1.4.开始绘制图片的大小
  97. UIGraphicsBeginImageContext(CGSize(width: width, height: height))
  98. imageArray[0].draw(at: CGPoint(x: 0, y: 0))
  99. let codeSize = imageArray[0].size.width / 5
  100. imageArray[1].draw(in: CGRect(x: (imageArray[0].size.width - codeSize) / 2, y: imageArray[0].size.height - codeSize - 15, width: codeSize, height: codeSize))
  101. // // 1.5.设置一个变量用来获取UIImage的Y值
  102. // var imageY: CGFloat = 0
  103. // // 1.6.遍历图片的数组
  104. // for image in imageArray {
  105. // // 1.6.1.开始绘画图片
  106. // image.draw(at: CGPoint(x: 0, y: imageY))
  107. // // 1.6.2.自增每张图片的Y轴
  108. // imageY += image.size.height
  109. //
  110. // }
  111. // 1.7.获取已经绘制好的图片
  112. let drawImage = UIGraphicsGetImageFromCurrentImageContext()!
  113. // 1.8.结束绘制图片
  114. UIGraphicsEndImageContext()
  115. // 1.9.返回已经绘制的图片
  116. return drawImage
  117. }
  118. @objc func saveImageResult(image: UIImage, didFinishSavingWithError error: NSError?, contentInfo: AnyObject) {
  119. SVProgressHUD.dismiss()
  120. if error != nil {
  121. SVProgressHUD.showSuccess(withStatus: "保存失败")
  122. } else {
  123. SVProgressHUD.showSuccess(withStatus: "保存成功")
  124. }
  125. }
  126. // MARK: =============加载数据===============
  127. func loadData() {
  128. let url = RequestURL.extensionImageList
  129. let params = NSMutableDictionary()
  130. params.setValue(type, forKey: "type")
  131. loadDataList(url: url, params: params, tag: 1001)
  132. }
  133. override func returnData(tag: Int) {
  134. if tag == 1001 {
  135. }
  136. }
  137. override func returnError(tag: Int, type: String) {
  138. }
  139. override func viewWillAppear(_ animated: Bool) {
  140. super.viewWillAppear(animated)
  141. self.tabBarController?.title = self.title
  142. }
  143. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  144. super.init(nibName: "ApplyCardExtensionViewController", bundle: nil)
  145. }
  146. required init?(coder aDecoder: NSCoder) {
  147. fatalError("init(coder:) has not been implemented")
  148. }
  149. }