优惠券swift版马甲包

YMDetailViewController.swift 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // YMDetailViewController.swift
  3. // MvpGoods
  4. //
  5. // Created by 小花 on 2019/4/15.
  6. // Copyright © 2019 MVP. All rights reserved.
  7. //
  8. import UIKit
  9. import ObjectMapper
  10. import WebKit
  11. class YMDetailViewController: YMBaseViewController {
  12. var WebViewObserKey = "WebViewObserKey"
  13. var tableView: UITableView?
  14. var tableViewHeader: UIView?
  15. var detailInfo: YMDetailInfoView?
  16. var shopInfo: YMDetailShopView?
  17. var goodModel: YMMainGoodModel!
  18. var detailModel: YMGoodDetailModel!
  19. var webView: WKWebView!
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. self.fd_prefersNavigationBarHidden = true
  23. self.view.backgroundColor = UIColor.white
  24. configSubViews()
  25. loadDetailData()
  26. }
  27. //初始化控件
  28. func configSubViews() {
  29. configTableView()
  30. configHeader()
  31. configWebView()
  32. let backBtn = UIButton(frame: CGRect(x: 15, y: 30, width: 40, height: 40))
  33. backBtn.setImage(UIImage(named: "back_detail"), for: .normal)
  34. backBtn.addTarget(self, action: #selector(backAction), for: .touchUpInside)
  35. self.view.addSubview(backBtn)
  36. self.view.addSubview(bottomView)
  37. }
  38. @objc func backAction() {
  39. self.navigationController?.popViewController(animated: true)
  40. }
  41. //加载详情数据
  42. func loadDetailData() {
  43. let url = BASE_URL + "/api/v2/goods/detail"
  44. let para = ["goods_id":self.goodModel.goods_id ?? 0,
  45. "coupon_price":self.goodModel.coupon_price ?? 0,
  46. "price":self.goodModel.price ?? "",
  47. "discount_price":self.goodModel.discount_price ?? "",
  48. "coupon_start_time":self.goodModel.coupon_start_time ?? "",
  49. "coupon_end_time":self.goodModel.coupon_end_time ?? ""] as [String : Any]
  50. NetWorkTool.postRequest(url: url, param: para, success: { (json) in
  51. let dict = json["rst"]!["data"].dictionaryObject
  52. self.detailModel = Mapper<YMGoodDetailModel>().map(JSON: dict!)
  53. print(json)
  54. self.setDataForHeader()
  55. }) { (error) in
  56. }
  57. }
  58. //给view赋值
  59. func setDataForHeader() {
  60. self.detailInfo?.detailModel = self.detailModel
  61. self.cycleView.setUrlsGroup(self.detailModel.small_img!)
  62. self.shopInfo?.detailModel = self.detailModel
  63. if self.detailModel.detail_pic!.count == 0 {
  64. let url = self.detailModel.detail_url!
  65. let request = URLRequest(url: URL(string: url)!)
  66. self.webView.load(request)
  67. self.webView.addObserver(self, forKeyPath: "scrollView.contentSize", options: .new, context: nil)
  68. }
  69. }
  70. func configTableView() {
  71. let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH), style: .plain)
  72. tableView.estimatedSectionHeaderHeight = 0;
  73. tableView.estimatedSectionFooterHeight = 0;
  74. tableView.sectionFooterHeight = 0;
  75. tableView.sectionHeaderHeight = 0;
  76. tableView.estimatedRowHeight = 0;
  77. tableView.delegate = self;
  78. tableView.dataSource = self;
  79. tableView.tableFooterView = UIView(frame: CGRect.zero)
  80. tableView.tableHeaderView = UIView(frame: CGRect.zero)
  81. tableView.backgroundColor = UIColor.white;
  82. tableView.showsVerticalScrollIndicator = false;
  83. tableView.separatorColor = UIColor.hexadecimalColor(hexadecimal: "0xe5e5e5");
  84. tableView.separatorStyle = .singleLine
  85. self.tableView = tableView
  86. self.view.addSubview(tableView)
  87. }
  88. func configWebView() {
  89. self.webView = WKWebView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 0))
  90. self.tableView?.tableFooterView = self.webView
  91. }
  92. //初始化头部
  93. func configHeader() {
  94. self.tableViewHeader = UIView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 0))
  95. self.tableViewHeader?.backgroundColor = UIColor.gloadGrayColor()
  96. self.tableViewHeader?.addSubview(cycleView)
  97. self.detailInfo = YMDetailInfoView(frame: CGRect(x: 0, y: cycleView.bottom, width: SCREENW, height: 125))
  98. self.detailInfo?.backgroundColor = UIColor.white
  99. self.tableViewHeader?.addSubview(self.detailInfo!)
  100. self.shopInfo = YMDetailShopView(frame: CGRect(x: 0, y: self.detailInfo!.bottom+5, width: SCREENW, height: 70))
  101. self.shopInfo?.backgroundColor = UIColor.white
  102. self.tableViewHeader!.addSubview(self.shopInfo!)
  103. self.tableViewHeader?.height = self.shopInfo!.bottom
  104. self.tableView?.tableHeaderView = self.tableViewHeader
  105. }
  106. @objc func getCoupleUrl() {
  107. let url = BASE_URL + "/api/v2/goods/getCouponShareUrl"
  108. let param: [String: Any] = ["goods_id":self.goodModel.goods_id!]
  109. NetWorkTool.postRequest(url: url, param: param, success: { (json) in
  110. let url = json["rst"]?["coupon_share_url"].string
  111. self.jumpAction(urlStr: url ?? "")
  112. }) { (error) in
  113. }
  114. }
  115. func jumpAction(urlStr: String){
  116. let page = AlibcTradePageFactory.page(urlStr)
  117. let taoKeParams: AlibcTradeTaokeParams = AlibcTradeTaokeParams()
  118. taoKeParams.pid = ALBC_PID
  119. let showParam: AlibcTradeShowParams = AlibcTradeShowParams()
  120. showParam.openType = AlibcOpenType.native
  121. AlibcTradeSDK.sharedInstance()?.tradeService()?.show(self, page: page, showParams: showParam, taoKeParams: taoKeParams, trackParam: nil, tradeProcessSuccessCallback: { (result) in
  122. }, tradeProcessFailedCallback: { (error) in
  123. })
  124. }
  125. lazy var cycleView: ZCycleView = {
  126. let cycleView = ZCycleView(frame: CGRect(x: 0, y: 0, width: SCREENW, height:SCREENW))
  127. cycleView.timeInterval = 3
  128. cycleView.pageControlIndictorImage = UIImage(named: "page_nor")
  129. cycleView.pageControlCurrentIndictorImage = UIImage(named: "page_sel")
  130. cycleView.pageControlHeight = 18
  131. cycleView.pageControlItemSize = CGSize(width: 8, height: 3)
  132. cycleView.pageControlAlignment = .center
  133. cycleView.itemSize = CGSize(width: SCREENW, height: SCREENW)
  134. cycleView.itemZoomScale = 1
  135. return cycleView
  136. }()
  137. lazy var bottomView: UIView = {
  138. let bottom = UIView(frame: CGRect(x: 0, y: SCREENH-bottomSafeHeight-50, width: SCREENW, height: 50))
  139. bottom.backgroundColor = UIColor.white
  140. let buy = UIButton(frame: CGRect(x: 15, y: 5, width: SCREENW-30, height: 40))
  141. buy.backgroundColor = UIColor.gloadRedColor()
  142. buy.layer.cornerRadius = 20
  143. buy.setTitle("领券购买", for: .normal)
  144. buy.setTitleColor(UIColor.white, for: .normal)
  145. buy.addTarget(self, action: #selector(getCoupleUrl), for: .touchUpInside)
  146. buy.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  147. bottom.addSubview(buy)
  148. return bottom
  149. }()
  150. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  151. if let newValue = change![NSKeyValueChangeKey.newKey]{
  152. let new = newValue as! CGSize
  153. self.webView.height = new.height
  154. self.tableView?.tableFooterView = self.webView
  155. }
  156. }
  157. deinit {
  158. if self.detailModel.detail_pic?.count == 0 {
  159. self.webView.removeObserver(self, forKeyPath: "scrollView.contentSize")
  160. }
  161. }
  162. func StringToFloat(str:String)->(CGFloat){
  163. let string = str
  164. var cgFloat:CGFloat = 0
  165. if let doubleValue = Double(string) {
  166. cgFloat = CGFloat(doubleValue)
  167. }
  168. return cgFloat
  169. }
  170. }
  171. extension YMDetailViewController: UITableViewDataSource, UITableViewDelegate {
  172. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  173. if self.detailModel == nil {
  174. return 0
  175. }
  176. if self.detailModel.detail_pic!.count > 0 {
  177. let pic = detailModel.detail_pic!
  178. return pic.count
  179. }else {
  180. return 0
  181. }
  182. }
  183. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  184. let cell = YMDetailImageCell.cellWithTableView(tableView: tableView) as! YMDetailImageCell
  185. cell.model = self.detailModel.detail_pic![indexPath.row]
  186. return cell
  187. }
  188. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  189. if self.detailModel.detail_pic!.count > 0 {
  190. let model = detailModel.detail_pic![indexPath.row]
  191. let picW = self.StringToFloat(str: model.width!)
  192. let picH = self.StringToFloat(str: model.height!)
  193. let height = picW/SCREENW * picH
  194. return height
  195. }else {
  196. return 0
  197. }
  198. }
  199. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  200. return 40
  201. }
  202. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  203. let header = UIView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 40))
  204. header.backgroundColor = UIColor.gloadGrayColor()
  205. let title = UILabel(frame: header.bounds)
  206. title.textColor = UIColor.gloadRedColor()
  207. title.font = UIFont.systemFont(ofSize: 14)
  208. title.textAlignment = .center
  209. title.text = "······ 商品详情 ······"
  210. header.addSubview(title)
  211. return header
  212. }
  213. }