// // ShoppingMallDetailViewController.swift // CommonFrame // // Created by Virgil on 2019/4/18. // Copyright © 2019 Virgil. All rights reserved. // import UIKit class ShoppingMallDetailViewController: BaseViewController, WKNavigationDelegate { var id = "" @IBOutlet weak var viewPayBottom: UIView! @IBOutlet weak var viewPay: UIView! @IBOutlet weak var lblCount: UITextField! @IBOutlet weak var viewCount: UIView! @IBOutlet weak var btnPay: UIButton! @IBOutlet weak var btnCus: UIButton! @IBOutlet weak var shopCarBtn: UIButton! @IBOutlet weak var addShopCarBtn: UIButton! @IBOutlet weak var viewBottom: UIView! @IBOutlet weak var lblDesc: UILabel! @IBOutlet weak var lblMoney: UILabel! @IBOutlet weak var lblName: UILabel! @IBOutlet weak var webBgView: UIView! @IBOutlet weak var viewContent: UIView! @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var imgShow: UIImageView! @IBOutlet weak var viewExpress: UIView! @IBOutlet weak var btnExpress2: UIButton! @IBOutlet weak var btnExpress1: UIButton! var wkWebView: WKWebView! var maxNum = -1 ///0:返回上一级 1:跳到商城 var backType = 0 var isCar = 0 var count = 1 let numLabel = UILabel.init() override func viewDidLoad() { super.viewDidLoad() initNavLeftBackButton() self.title = "商品详情" imgShow.setSize(width: ScreenWidth, height: ScreenWidth)// viewContent.marginTop(top: 0, view: imgShow) viewExpress.marginTop(top: 8, view: viewContent) webBgView.marginTop(top: 6, view: viewExpress) scrollView.contentSize.height = webBgView.bottom() let config = WKWebViewConfiguration.init() wkWebView = WKWebView.init(frame: webBgView.bounds, configuration: config) wkWebView.navigationDelegate = self webBgView.addSubview(wkWebView) loadData() viewCount.setCornerRadius(size: 4) lblCount.text = "\(count)" scrollView.setSizeHeight(height: viewHeight - viewBottom.height()) viewBottom.marginTop(top: viewHeight - viewBottom.height()) viewPay.setSizeHeight(height: viewHeight) viewPayBottom.marginTop(top: viewHeight) btnExpress1.setCornerRadius(size: 3) btnExpress2.setCornerRadius(size: 3) numLabel.frame = CGRect(x: shopCarBtn.mj_x+35, y: 7, width: 15, height: 15) // numLabel.center = shopCarBtn.center numLabel.backgroundColor = .red numLabel.font = UIFont.systemFont(ofSize: 10) numLabel.textColor = .white numLabel.layer.cornerRadius = 7.5 numLabel.textAlignment = .center numLabel.layer.masksToBounds = true numLabel.isHidden = true viewBottom.addSubview(numLabel) loadNum() } //btnAddTarget(view: self.view, selector: #selector(self.btnMenuClick)) @IBAction func btnMenuClick(_ sender: AnyObject) { switch (sender as! UIButton).tag { case 1000://商城 // if backType == 0 { // handleBack() // } else { // toViewController(viewController: ShoppingMallViewController()) // } appDelegate.navController.popToRootViewController(animated: true) appDelegate.mainTabbar.selectedIndex = 1 break case 1001: // 客服 let qySource = QYSource() qySource.title = "客服" qySource.urlString = "" let sessionQY = QYSDK.shared().sessionViewController() if sessionQY != nil { sessionQY!.sessionTitle = "在线客服"// CommonValue.getNikeName() sessionQY!.source = qySource sessionQY!.hidesBottomBarWhenPushed = true appDelegate.navController.pushViewController(sessionQY!, animated: true) initCusServiceBackButton(viewController: sessionQY!) } break case 1002: // 立即购买 isCar = 0 showBottomMenu() break case 1003: // 加 if maxNum != -1 && count >= maxNum { SVProgressHUD.showError(withStatus: "此商品最多可以购买\(maxNum)个") return } count += 1 lblCount.text = "\(count)" break case 1004: // 减 if count > 1 { count -= 1 lblCount.text = "\(count)" } break case 1005: // 关闭下边菜单 hiddenBottomMenu() break case 1006: // 确定购买 if isCar == 0 { hiddenBottomMenu() let vc = CreateOrderViewController() vc.id = getString(key: "id") vc.num = count vc.expressType = 0 vc.dicData.setDictionary(self.dicData as! [AnyHashable: Any]) toViewController(viewController: vc) } else if isCar == 1 { self.addToShopCar() hiddenBottomMenu() } break case 1007: // 顺丰到付 btnExpress1.isSelected = true btnExpress2.isSelected = false btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0) btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0) break case 1008: // 上门自提 btnExpress1.isSelected = false btnExpress2.isSelected = true btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0) btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0) break case 1009: // 购物车 toViewController(viewController: OrderController()) break case 1010: // 加入购物车 isCar = 1 showBottomMenu() break default: break } } // MARK: =============加载数据=============== func loadData() { let url = RequestURL.productDetail let params = NSMutableDictionary() params.setValue(id, forKey: "id") loadDataInfo(url: url, params: params, tag: 1001) } override func returnData(tag: Int) { if tag == 1001 { maxNum = getIntValue(key: "max_num") lblDesc.text = "数量:\(getIntValue(key: "num")) | 销量:\(getIntValue(key: "sales_num"))" self.title = getString(key: "name") lblName.text = getString(key: "name") // lblDesc.text = "数量:100 | 销量:26"//getString(key: "brief") let strPrice = "¥\(getDoubleValue(key: "price"))" lblMoney.attributedText = CommonViewUntils.getAttributedStringForFont(str: strPrice, rangs: [NSRange.init(location: 0, length: 1), NSRange.init(location: 1, length: strPrice.length() - 1)], fonts: [UIFont.systemFont(ofSize: 15), UIFont.systemFont(ofSize: 20)]) imgShow.loadImage(imgUrl: getString(key: "image"), defaultImage: "") let urlNew = CommonURL.formatURL(getString(key: "webUrl")) if urlNew != nil { wkWebView.load(NSURLRequest(url: NSURL(string: urlNew!)! as URL, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 15) as URLRequest) } } } func showBottomMenu() { viewPayBottom.marginTop(top: viewHeight) viewPay.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0) viewPay.isHidden = false UIView.animate(withDuration: 0.3) { self.viewPay.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0.6) self.viewPayBottom.marginTop(top: self.viewPay.height() - self.viewPayBottom.height()) } } func hiddenBottomMenu() { UIView.animate(withDuration: 0.3, animations: { self.viewPayBottom.marginTop(top: self.viewPay.height()) self.viewPay.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0) }) { (_) in self.viewPay.isHidden = true } } override func returnError(tag: Int, type: String) { } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { //获取内容实际高度(像素)@"document.getElementById(\"content\").offsetHeight;" var webHeight = 0.0 webView.evaluateJavaScript("document.body.scrollHeight") {[unowned self] (result, _) in if let tempHeight: Double = result as? Double { webHeight = tempHeight } DispatchQueue.main.async {[unowned self] in self.webBgView.setSizeHeight(height: CGFloat(webHeight)) self.wkWebView.setSizeHeight(height: CGFloat(webHeight)) self.scrollView.contentSize.height = self.webBgView.bottom() } } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) loadNum() } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: "ShoppingMallDetailViewController", bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func addToShopCar() { let params = NSMutableDictionary() let tojson = NSMutableDictionary() tojson.setValue(CommonValue.getUserId(), forKey: "accountId") tojson.setValue(getString(key: "id"), forKey: "productId") tojson.setValue(count, forKey: "num") tojson.setValue("12345", forKey: "shopId") tojson.setValue(getDoubleValue(key: "price"), forKey: "price") params.setValue(self.dicValueString(tojson as! [String: Any]), forKey: "toJson") // loadDataList(url: url, params: params, tag: 1001, array: arrType) print(params) let http = AFHTTPSessionManager() http.post(RequestURL.addProductOrder, parameters: params, progress: { (_) in }, success: { (operation, json) in print(json as Any) let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int if success == 200 { SVProgressHUD.showSuccess(withStatus: "添加成功") self.loadNum() } }) { (_, _) in } } func dicValueString(_ dic: [String: Any]) -> String? { let data = try? JSONSerialization.data(withJSONObject: dic, options: []) var str = String(data: data!, encoding: String.Encoding.utf8) str = str!.replacingOccurrences(of: "\"", with: "'") print(str!) str = "[\(str!)]" // str = "" return str } func loadNum() { let params = NSMutableDictionary() params.setValue(CommonValue.getUserId(), forKey: "userId") let http = AFHTTPSessionManager() http.get(RequestURL.getProductOrderList, parameters: params, progress: { (_) in }, success: { (operation, json) in print(json as Any) let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int if success == 200 { let dataArr: NSArray = (json as! NSDictionary).object(forKey: "data") as! NSArray if dataArr.count > 0 { self.numLabel.isHidden = false self.numLabel.text = "\(dataArr.count)" } else { self.numLabel.isHidden = true } } }) { (_, _) in } } }