ShoppingMallDetailViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // ShoppingMallDetailViewController.swift
  3. // CommonFrame
  4. //
  5. // Created by Virgil on 2019/4/18.
  6. // Copyright © 2019 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class ShoppingMallDetailViewController: BaseViewController, WKNavigationDelegate {
  10. var id = ""
  11. @IBOutlet weak var viewPayBottom: UIView!
  12. @IBOutlet weak var viewPay: UIView!
  13. @IBOutlet weak var lblCount: UITextField!
  14. @IBOutlet weak var viewCount: UIView!
  15. @IBOutlet weak var btnPay: UIButton!
  16. @IBOutlet weak var btnCus: UIButton!
  17. @IBOutlet weak var shopCarBtn: UIButton!
  18. @IBOutlet weak var addShopCarBtn: UIButton!
  19. @IBOutlet weak var viewBottom: UIView!
  20. @IBOutlet weak var lblDesc: UILabel!
  21. @IBOutlet weak var lblMoney: UILabel!
  22. @IBOutlet weak var lblName: UILabel!
  23. @IBOutlet weak var webBgView: UIView!
  24. @IBOutlet weak var viewContent: UIView!
  25. @IBOutlet weak var scrollView: UIScrollView!
  26. @IBOutlet weak var imgShow: UIImageView!
  27. @IBOutlet weak var viewExpress: UIView!
  28. @IBOutlet weak var btnExpress2: UIButton!
  29. @IBOutlet weak var btnExpress1: UIButton!
  30. var wkWebView: WKWebView!
  31. var maxNum = -1
  32. ///0:返回上一级 1:跳到商城
  33. var backType = 0
  34. var isCar = 0
  35. var count = 1
  36. let numLabel = UILabel.init()
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. initNavLeftBackButton()
  40. self.title = "商品详情"
  41. imgShow.setSize(width: ScreenWidth, height: ScreenWidth)//
  42. viewContent.marginTop(top: 0, view: imgShow)
  43. viewExpress.marginTop(top: 8, view: viewContent)
  44. webBgView.marginTop(top: 6, view: viewExpress)
  45. scrollView.contentSize.height = webBgView.bottom()
  46. let config = WKWebViewConfiguration.init()
  47. wkWebView = WKWebView.init(frame: webBgView.bounds, configuration: config)
  48. wkWebView.navigationDelegate = self
  49. webBgView.addSubview(wkWebView)
  50. loadData()
  51. viewCount.setCornerRadius(size: 4)
  52. lblCount.text = "\(count)"
  53. scrollView.setSizeHeight(height: viewHeight - viewBottom.height())
  54. viewBottom.marginTop(top: viewHeight - viewBottom.height())
  55. viewPay.setSizeHeight(height: viewHeight)
  56. viewPayBottom.marginTop(top: viewHeight)
  57. btnExpress1.setCornerRadius(size: 3)
  58. btnExpress2.setCornerRadius(size: 3)
  59. numLabel.frame = CGRect(x: shopCarBtn.mj_x+35, y: 7, width: 15, height: 15)
  60. // numLabel.center = shopCarBtn.center
  61. numLabel.backgroundColor = .red
  62. numLabel.font = UIFont.systemFont(ofSize: 10)
  63. numLabel.textColor = .white
  64. numLabel.layer.cornerRadius = 7.5
  65. numLabel.textAlignment = .center
  66. numLabel.layer.masksToBounds = true
  67. numLabel.isHidden = true
  68. viewBottom.addSubview(numLabel)
  69. loadNum()
  70. }
  71. //btnAddTarget(view: self.view, selector: #selector(self.btnMenuClick))
  72. @IBAction func btnMenuClick(_ sender: AnyObject) {
  73. switch (sender as! UIButton).tag {
  74. case 1000://商城
  75. // if backType == 0 {
  76. // handleBack()
  77. // } else {
  78. // toViewController(viewController: ShoppingMallViewController())
  79. // }
  80. appDelegate.navController.popToRootViewController(animated: true)
  81. appDelegate.mainTabbar.selectedIndex = 1
  82. break
  83. case 1001: // 客服
  84. let qySource = QYSource()
  85. qySource.title = "客服"
  86. qySource.urlString = ""
  87. let sessionQY = QYSDK.shared().sessionViewController()
  88. if sessionQY != nil {
  89. sessionQY!.sessionTitle = "在线客服"// CommonValue.getNikeName()
  90. sessionQY!.source = qySource
  91. sessionQY!.hidesBottomBarWhenPushed = true
  92. appDelegate.navController.pushViewController(sessionQY!, animated: true)
  93. initCusServiceBackButton(viewController: sessionQY!)
  94. }
  95. break
  96. case 1002: // 立即购买
  97. isCar = 0
  98. showBottomMenu()
  99. break
  100. case 1003: // 加
  101. if maxNum != -1 && count >= maxNum {
  102. SVProgressHUD.showError(withStatus: "此商品最多可以购买\(maxNum)个")
  103. return
  104. }
  105. count += 1
  106. lblCount.text = "\(count)"
  107. break
  108. case 1004: // 减
  109. if count > 1 {
  110. count -= 1
  111. lblCount.text = "\(count)"
  112. }
  113. break
  114. case 1005: // 关闭下边菜单
  115. hiddenBottomMenu()
  116. break
  117. case 1006: // 确定购买
  118. if isCar == 0 {
  119. hiddenBottomMenu()
  120. let vc = CreateOrderViewController()
  121. vc.id = getString(key: "id")
  122. vc.num = count
  123. vc.expressType = 0
  124. vc.dicData.setDictionary(self.dicData as! [AnyHashable: Any])
  125. toViewController(viewController: vc)
  126. } else if isCar == 1 {
  127. self.addToShopCar()
  128. hiddenBottomMenu()
  129. }
  130. break
  131. case 1007: // 顺丰到付
  132. btnExpress1.isSelected = true
  133. btnExpress2.isSelected = false
  134. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  135. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  136. break
  137. case 1008: // 上门自提
  138. btnExpress1.isSelected = false
  139. btnExpress2.isSelected = true
  140. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  141. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  142. break
  143. case 1009: // 购物车
  144. toViewController(viewController: OrderController())
  145. break
  146. case 1010: // 加入购物车
  147. isCar = 1
  148. showBottomMenu()
  149. break
  150. default:
  151. break
  152. }
  153. }
  154. // MARK: =============加载数据===============
  155. func loadData() {
  156. let url = RequestURL.productDetail
  157. let params = NSMutableDictionary()
  158. params.setValue(id, forKey: "id")
  159. loadDataInfo(url: url, params: params, tag: 1001)
  160. }
  161. override func returnData(tag: Int) {
  162. if tag == 1001 {
  163. maxNum = getIntValue(key: "max_num")
  164. lblDesc.text = "数量:\(getIntValue(key: "num")) | 销量:\(getIntValue(key: "sales_num"))"
  165. self.title = getString(key: "name")
  166. lblName.text = getString(key: "name")
  167. // lblDesc.text = "数量:100 | 销量:26"//getString(key: "brief")
  168. let strPrice = "¥\(getDoubleValue(key: "price"))"
  169. 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)])
  170. imgShow.loadImage(imgUrl: getString(key: "image"), defaultImage: "")
  171. let urlNew = CommonURL.formatURL(getString(key: "webUrl"))
  172. if urlNew != nil {
  173. wkWebView.load(NSURLRequest(url: NSURL(string: urlNew!)! as URL, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 15) as URLRequest)
  174. }
  175. }
  176. }
  177. func showBottomMenu() {
  178. viewPayBottom.marginTop(top: viewHeight)
  179. viewPay.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0)
  180. viewPay.isHidden = false
  181. UIView.animate(withDuration: 0.3) {
  182. self.viewPay.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0.6)
  183. self.viewPayBottom.marginTop(top: self.viewPay.height() - self.viewPayBottom.height())
  184. }
  185. }
  186. func hiddenBottomMenu() {
  187. UIView.animate(withDuration: 0.3, animations: {
  188. self.viewPayBottom.marginTop(top: self.viewPay.height())
  189. self.viewPay.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0)
  190. }) { (_) in
  191. self.viewPay.isHidden = true
  192. }
  193. }
  194. override func returnError(tag: Int, type: String) {
  195. }
  196. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  197. //获取内容实际高度(像素)@"document.getElementById(\"content\").offsetHeight;"
  198. var webHeight = 0.0
  199. webView.evaluateJavaScript("document.body.scrollHeight") {[unowned self] (result, _) in
  200. if let tempHeight: Double = result as? Double {
  201. webHeight = tempHeight
  202. }
  203. DispatchQueue.main.async {[unowned self] in
  204. self.webBgView.setSizeHeight(height: CGFloat(webHeight))
  205. self.wkWebView.setSizeHeight(height: CGFloat(webHeight))
  206. self.scrollView.contentSize.height = self.webBgView.bottom()
  207. }
  208. }
  209. }
  210. override func viewWillAppear(_ animated: Bool) {
  211. super.viewWillAppear(animated)
  212. loadNum()
  213. }
  214. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  215. super.init(nibName: "ShoppingMallDetailViewController", bundle: nil)
  216. }
  217. required init?(coder aDecoder: NSCoder) {
  218. fatalError("init(coder:) has not been implemented")
  219. }
  220. func addToShopCar() {
  221. let params = NSMutableDictionary()
  222. let tojson = NSMutableDictionary()
  223. tojson.setValue(CommonValue.getUserId(), forKey: "accountId")
  224. tojson.setValue(getString(key: "id"), forKey: "productId")
  225. tojson.setValue(count, forKey: "num")
  226. tojson.setValue("12345", forKey: "shopId")
  227. tojson.setValue(getDoubleValue(key: "price"), forKey: "price")
  228. params.setValue(self.dicValueString(tojson as! [String: Any]), forKey: "toJson")
  229. // loadDataList(url: url, params: params, tag: 1001, array: arrType)
  230. print(params)
  231. let http = AFHTTPSessionManager()
  232. http.post(RequestURL.addProductOrder, parameters: params, progress: { (_) in
  233. }, success: { (operation, json) in
  234. print(json as Any)
  235. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  236. if success == 200 {
  237. SVProgressHUD.showSuccess(withStatus: "添加成功")
  238. self.loadNum()
  239. }
  240. }) { (_, _) in
  241. }
  242. }
  243. func dicValueString(_ dic: [String: Any]) -> String? {
  244. let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
  245. var str = String(data: data!, encoding: String.Encoding.utf8)
  246. str = str!.replacingOccurrences(of: "\"", with: "'")
  247. print(str!)
  248. str = "[\(str!)]"
  249. // str = ""
  250. return str
  251. }
  252. func loadNum() {
  253. let params = NSMutableDictionary()
  254. params.setValue(CommonValue.getUserId(), forKey: "userId")
  255. let http = AFHTTPSessionManager()
  256. http.get(RequestURL.getProductOrderList, parameters: params, progress: { (_) in
  257. }, success: { (operation, json) in
  258. print(json as Any)
  259. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  260. if success == 200 {
  261. let dataArr: NSArray = (json as! NSDictionary).object(forKey: "data") as! NSArray
  262. if dataArr.count > 0 {
  263. self.numLabel.isHidden = false
  264. self.numLabel.text = "\(dataArr.count)"
  265. } else {
  266. self.numLabel.isHidden = true
  267. }
  268. }
  269. }) { (_, _) in
  270. }
  271. }
  272. }