123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- //
- // YMDetailViewController.swift
- // MvpGoods
- //
- // Created by 小花 on 2019/4/15.
- // Copyright © 2019 MVP. All rights reserved.
- //
- import UIKit
- import ObjectMapper
- import WebKit
- class YMDetailViewController: YMBaseViewController {
- var WebViewObserKey = "WebViewObserKey"
-
- var tableView: UITableView?
- var tableViewHeader: UIView?
- var detailInfo: YMDetailInfoView?
- var shopInfo: YMDetailShopView?
-
- var goodModel: YMMainGoodModel!
- var detailModel: YMGoodDetailModel!
- var webView: WKWebView!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.fd_prefersNavigationBarHidden = true
- self.view.backgroundColor = UIColor.white
- configSubViews()
- loadDetailData()
- }
-
- //初始化控件
- func configSubViews() {
-
- configTableView()
- configHeader()
- configWebView()
- let backBtn = UIButton(frame: CGRect(x: 15, y: 30, width: 40, height: 40))
- backBtn.setImage(UIImage(named: "back_detail"), for: .normal)
- backBtn.addTarget(self, action: #selector(backAction), for: .touchUpInside)
- self.view.addSubview(backBtn)
- self.view.addSubview(bottomView)
- }
-
- @objc func backAction() {
- self.navigationController?.popViewController(animated: true)
- }
-
- //加载详情数据
- func loadDetailData() {
- let url = BASE_URL + "/api/v2/goods/detail"
- let para = ["goods_id":self.goodModel.goods_id ?? 0,
- "coupon_price":self.goodModel.coupon_price ?? 0,
- "price":self.goodModel.price ?? "",
- "discount_price":self.goodModel.discount_price ?? "",
- "coupon_start_time":self.goodModel.coupon_start_time ?? "",
- "coupon_end_time":self.goodModel.coupon_end_time ?? ""] as [String : Any]
-
- NetWorkTool.postRequest(url: url, param: para, success: { (json) in
- let dict = json["rst"]!["data"].dictionaryObject
- self.detailModel = Mapper<YMGoodDetailModel>().map(JSON: dict!)
- print(json)
- self.setDataForHeader()
- }) { (error) in
-
- }
- }
-
- //给view赋值
- func setDataForHeader() {
- self.detailInfo?.detailModel = self.detailModel
- self.cycleView.setUrlsGroup(self.detailModel.small_img!)
- self.shopInfo?.detailModel = self.detailModel
- if self.detailModel.detail_pic!.count == 0 {
- let url = self.detailModel.detail_url!
- let request = URLRequest(url: URL(string: url)!)
- self.webView.load(request)
- self.webView.addObserver(self, forKeyPath: "scrollView.contentSize", options: .new, context: nil)
- }
- }
-
-
- func configTableView() {
- let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH), style: .plain)
- tableView.estimatedSectionHeaderHeight = 0;
- tableView.estimatedSectionFooterHeight = 0;
- tableView.sectionFooterHeight = 0;
- tableView.sectionHeaderHeight = 0;
- tableView.estimatedRowHeight = 0;
- tableView.delegate = self;
- tableView.dataSource = self;
- tableView.tableFooterView = UIView(frame: CGRect.zero)
- tableView.tableHeaderView = UIView(frame: CGRect.zero)
- tableView.backgroundColor = UIColor.white;
- tableView.showsVerticalScrollIndicator = false;
- tableView.separatorColor = UIColor.hexadecimalColor(hexadecimal: "0xe5e5e5");
- tableView.separatorStyle = .singleLine
-
- self.tableView = tableView
- self.view.addSubview(tableView)
- }
-
- func configWebView() {
- self.webView = WKWebView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 0))
- self.tableView?.tableFooterView = self.webView
- }
-
- //初始化头部
- func configHeader() {
- self.tableViewHeader = UIView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 0))
- self.tableViewHeader?.backgroundColor = UIColor.gloadGrayColor()
- self.tableViewHeader?.addSubview(cycleView)
- self.detailInfo = YMDetailInfoView(frame: CGRect(x: 0, y: cycleView.bottom, width: SCREENW, height: 125))
- self.detailInfo?.backgroundColor = UIColor.white
- self.tableViewHeader?.addSubview(self.detailInfo!)
- self.shopInfo = YMDetailShopView(frame: CGRect(x: 0, y: self.detailInfo!.bottom+5, width: SCREENW, height: 70))
- self.shopInfo?.backgroundColor = UIColor.white
- self.tableViewHeader!.addSubview(self.shopInfo!)
- self.tableViewHeader?.height = self.shopInfo!.bottom
- self.tableView?.tableHeaderView = self.tableViewHeader
- }
-
- @objc func getCoupleUrl() {
- let url = BASE_URL + "/api/v2/goods/getCouponShareUrl"
- let param: [String: Any] = ["goods_id":self.goodModel.goods_id!]
- NetWorkTool.postRequest(url: url, param: param, success: { (json) in
- let url = json["rst"]?["coupon_share_url"].string
- self.jumpAction(urlStr: url ?? "")
- }) { (error) in
-
- }
- }
-
- func jumpAction(urlStr: String){
- let page = AlibcTradePageFactory.page(urlStr)
- let taoKeParams: AlibcTradeTaokeParams = AlibcTradeTaokeParams()
- taoKeParams.pid = ALBC_PID
- let showParam: AlibcTradeShowParams = AlibcTradeShowParams()
- showParam.openType = AlibcOpenType.native
-
- AlibcTradeSDK.sharedInstance()?.tradeService()?.show(self, page: page, showParams: showParam, taoKeParams: taoKeParams, trackParam: nil, tradeProcessSuccessCallback: { (result) in
-
- }, tradeProcessFailedCallback: { (error) in
-
- })
-
- }
-
- lazy var cycleView: ZCycleView = {
- let cycleView = ZCycleView(frame: CGRect(x: 0, y: 0, width: SCREENW, height:SCREENW))
- cycleView.timeInterval = 3
- cycleView.pageControlIndictorImage = UIImage(named: "page_nor")
- cycleView.pageControlCurrentIndictorImage = UIImage(named: "page_sel")
- cycleView.pageControlHeight = 18
- cycleView.pageControlItemSize = CGSize(width: 8, height: 3)
- cycleView.pageControlAlignment = .center
- cycleView.itemSize = CGSize(width: SCREENW, height: SCREENW)
- cycleView.itemZoomScale = 1
- return cycleView
- }()
-
- lazy var bottomView: UIView = {
- let bottom = UIView(frame: CGRect(x: 0, y: SCREENH-bottomSafeHeight-50, width: SCREENW, height: 50))
- bottom.backgroundColor = UIColor.white
- let buy = UIButton(frame: CGRect(x: 15, y: 5, width: SCREENW-30, height: 40))
- buy.backgroundColor = UIColor.gloadRedColor()
- buy.layer.cornerRadius = 20
- buy.setTitle("领券购买", for: .normal)
- buy.setTitleColor(UIColor.white, for: .normal)
- buy.addTarget(self, action: #selector(getCoupleUrl), for: .touchUpInside)
- buy.titleLabel?.font = UIFont.systemFont(ofSize: 15)
- bottom.addSubview(buy)
- return bottom
- }()
-
- override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
- if let newValue = change![NSKeyValueChangeKey.newKey]{
- let new = newValue as! CGSize
- self.webView.height = new.height
- self.tableView?.tableFooterView = self.webView
- }
- }
-
- deinit {
- if self.detailModel.detail_pic?.count == 0 {
- self.webView.removeObserver(self, forKeyPath: "scrollView.contentSize")
- }
-
- }
-
- func StringToFloat(str:String)->(CGFloat){
-
- let string = str
- var cgFloat:CGFloat = 0
- if let doubleValue = Double(string) {
- cgFloat = CGFloat(doubleValue)
- }
- return cgFloat
- }
-
- }
- extension YMDetailViewController: UITableViewDataSource, UITableViewDelegate {
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if self.detailModel == nil {
- return 0
- }
- if self.detailModel.detail_pic!.count > 0 {
- let pic = detailModel.detail_pic!
- return pic.count
- }else {
- return 0
- }
-
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = YMDetailImageCell.cellWithTableView(tableView: tableView) as! YMDetailImageCell
- cell.model = self.detailModel.detail_pic![indexPath.row]
- return cell
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- if self.detailModel.detail_pic!.count > 0 {
- let model = detailModel.detail_pic![indexPath.row]
- let picW = self.StringToFloat(str: model.width!)
- let picH = self.StringToFloat(str: model.height!)
- let height = picW/SCREENW * picH
- return height
- }else {
- return 0
- }
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 40
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- let header = UIView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 40))
- header.backgroundColor = UIColor.gloadGrayColor()
- let title = UILabel(frame: header.bounds)
- title.textColor = UIColor.gloadRedColor()
- title.font = UIFont.systemFont(ofSize: 14)
- title.textAlignment = .center
- title.text = "······ 商品详情 ······"
- header.addSubview(title)
- return header
- }
-
-
- }
|