CreatShopOrderViewController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // CreatShopOrderViewController.swift
  3. // xingchuangke
  4. //
  5. // Created by machaochao on 2020/10/13.
  6. // Copyright © 2020 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class CreatShopOrderViewController: BaseViewController, UIScrollViewDelegate {
  10. @IBOutlet weak var viewExpress: UIView!
  11. @IBOutlet weak var btnExpress2: UIButton!
  12. @IBOutlet weak var btnExpress1: UIButton!
  13. @IBOutlet weak var btnExpressBtn3: UIButton!
  14. @IBOutlet weak var view1: UIView!
  15. @IBOutlet weak var btnAddress: UIButton!
  16. @IBOutlet weak var lblPhone: UILabel!
  17. @IBOutlet weak var lblAddress: UILabel!
  18. @IBOutlet weak var lblName: UILabel!
  19. let scrollView = UIScrollView.init()
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. initNavLeftBackButton()
  23. self.title = "确认订单"
  24. loadAddressList()
  25. btnExpress1.setCornerRadius(size: 3)
  26. btnExpress2.setCornerRadius(size: 3)
  27. btnExpressBtn3.setCornerRadius(size: 3)
  28. viewExpress.setCornerRadius(size: 8)
  29. btnExpress1.setTitleColor(.white, for: .normal)
  30. btnExpress2.setTitleColor(.white, for: .normal)
  31. btnExpressBtn3.setTitleColor(.white, for: .normal)
  32. self.creatGoodsList()
  33. // Do any additional setup after loading the view.
  34. }
  35. var dicAddress = NSDictionary()
  36. var name = ""
  37. var phone = ""
  38. var address1 = ""
  39. var address2 = ""
  40. ///0:顺丰1到付 2:自提
  41. var expressType = 0
  42. @IBAction func btnMenuClick(_ sender: AnyObject) {
  43. switch (sender as! UIButton).tag {
  44. case 1001: // 选择地址
  45. let vc = AddressListViewController()
  46. toViewController(viewController: vc)
  47. break
  48. case 1002: // 提交订单
  49. break
  50. case 1005: //
  51. break
  52. case 1006: //快递寄付
  53. self.expressType = 0
  54. // if(name == "" && expressType == 0)
  55. // {
  56. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  57. // return;
  58. // }
  59. // if(name == "" && expressType == 2)
  60. // {
  61. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  62. // return;
  63. // }
  64. btnExpress1.isSelected = true
  65. btnExpress2.isSelected = false
  66. btnExpressBtn3.isSelected = false
  67. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  68. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  69. btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  70. self.view1.isHidden = false
  71. self.view1.setSizeHeight(height: 90.0)
  72. self.scrollView.mj_y = view1.bottom()+10
  73. self.scrollView.setSizeHeight(height: kScreenHeight-self.view1.bottom()-20-45-navheight)
  74. self.getFreight()
  75. break
  76. case 1007: // 顺丰到付
  77. self.expressType = 2
  78. // if(name == "" && expressType == 0)
  79. // {
  80. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  81. // return;
  82. // }
  83. // if(name == "" && expressType == 2)
  84. // {
  85. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  86. // return;
  87. // }
  88. btnExpress1.isSelected = false
  89. btnExpress2.isSelected = true
  90. btnExpressBtn3.isSelected = false
  91. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  92. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  93. btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  94. self.view1.isHidden = false
  95. self.view1.setSizeHeight(height: 90.0)
  96. self.scrollView.mj_y = view1.bottom()+10
  97. self.scrollView.setSizeHeight(height: kScreenHeight-self.view1.bottom()-20-45-navheight)
  98. let strPrice2 = "¥\(String.init(format: "%.2f", self.allPrice))"
  99. self.hejiLable.text = strPrice2
  100. self.freight = 0.0
  101. break
  102. case 1008: // 上门自提
  103. self.expressType = 1
  104. btnExpress1.isSelected = false
  105. btnExpress2.isSelected = true
  106. btnExpressBtn3.isSelected = false
  107. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  108. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  109. btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  110. self.view1.setSizeHeight(height: 0.0)
  111. self.view1.isHidden = true
  112. self.scrollView.mj_y = viewExpress.bottom()+10
  113. self.scrollView.setSizeHeight(height: kScreenHeight-self.viewExpress.bottom()-20-45-navheight)
  114. let strPrice2 = "¥\(String.init(format: "%.2f", self.allPrice))"
  115. self.hejiLable.text = strPrice2
  116. self.freight = 0.0
  117. break
  118. default:
  119. break
  120. }
  121. }
  122. var goodsArray = NSArray.init()
  123. let hejiLable = UILabel.init()
  124. var allPrice: Float = 0
  125. var num: Int = 0
  126. var idstr = ""
  127. func creatGoodsList() {
  128. scrollView.delegate = self //设置代理
  129. let height = kScreenHeight-self.view1.bottom()-10-45-10-navheight
  130. scrollView.frame = CGRect(x: 0, y: self.view1.bottom()+10, width: kScreenWidth, height:height)//设置scrollview的大小
  131. scrollView.backgroundColor = self.view.backgroundColor//设置背景颜色
  132. scrollView.isPagingEnabled = true //是否支持分页
  133. scrollView.bounces = false //是否支持回弹效果
  134. scrollView.showsVerticalScrollIndicator = false //垂直滑动线隐藏
  135. scrollView.showsHorizontalScrollIndicator = false //水平滑动线隐藏
  136. //scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10) //设置内边距
  137. scrollView.contentSize = CGSize(width: kScreenWidth, height: CGFloat(goodsArray.count*100))
  138. self.view.addSubview(scrollView)
  139. for i in 0..<goodsArray.count {
  140. let goodsModel = goodsArray[i]as! YCOrderGoodsModel
  141. let viewb = UIView.init()
  142. viewb.frame = CGRect(x: 5, y: 10+100*i, width: Int(SCREEN_WIDTH-10), height: 90)
  143. viewb.backgroundColor = .white
  144. scrollView.addSubview(viewb)
  145. let imageView = UIImageView.init()
  146. imageView.frame = CGRect(x: 16, y: 16, width: 74, height: 74)
  147. imageView.sd_setImage(with: NSURL.init(string: goodsModel.goodsIconUrl!) as URL?)
  148. viewb.addSubview(imageView)
  149. let label = UILabel.init()
  150. label.frame = CGRect(x: 90, y: 0, width: Int(kScreenWidth-90), height: 30)
  151. label.textColor = .black
  152. label.text = goodsModel.goodsName
  153. viewb.addSubview(label)
  154. let labelT = UILabel.init()
  155. labelT.frame = CGRect(x: 90, y: 30, width: Int(kScreenWidth-90), height: 30)
  156. labelT.textColor = .gray
  157. labelT.text = "\(goodsModel.goodsCount!)件"
  158. viewb.addSubview(labelT)
  159. let labelTT = UILabel.init()
  160. labelTT.frame = CGRect(x: 90, y: 60, width: Int(kScreenWidth-90), height: 30)
  161. labelTT.textColor = .black
  162. labelTT.textColor = .red
  163. let price: String = goodsModel.goodsPrice!
  164. let priceNum: Float = Float(price)!
  165. let ccount: Int = goodsModel.goodsCount!
  166. self.num+=ccount
  167. self.allPrice += priceNum*Float(ccount)
  168. labelTT.text = "¥\(priceNum*Float(ccount))"
  169. viewb.addSubview(labelTT)
  170. if i == 0 {
  171. self.idstr = goodsModel.productId!
  172. } else {
  173. self.idstr = "\(self.idstr),\(goodsModel.productId!)"
  174. }
  175. }
  176. let bottomView = UIView.init()
  177. bottomView.frame = CGRect(x: 0, y: SCREEN_HEIGHT-45-navheight, width: SCREEN_WIDTH, height: 45)
  178. bottomView.backgroundColor = .white
  179. self.view.addSubview(bottomView)
  180. let label12 = UILabel.init()
  181. label12.frame = CGRect(x: 0, y: 0, width: 50, height: 45)
  182. label12.text = "合计:"
  183. bottomView.addSubview(label12)
  184. hejiLable.frame = CGRect(x: 50, y: 0, width: 150, height: 45)
  185. hejiLable.text = "\(self.allPrice)元"
  186. hejiLable.textColor = .red
  187. bottomView.addSubview(hejiLable)
  188. let jiesuanBtn = UIButton.init()
  189. jiesuanBtn.frame = CGRect(x: SCREEN_WIDTH-100, y: 0, width: 100, height: 45)
  190. jiesuanBtn.backgroundColor = .red
  191. jiesuanBtn.setTitle("提交订单", for: .normal)
  192. jiesuanBtn.addTarget(self, action: #selector(shopCarSettl), for: .touchUpInside)
  193. bottomView.addSubview(jiesuanBtn)
  194. }
  195. @objc func shopCarSettl() {
  196. if name == "" && expressType == 0 {
  197. SVProgressHUD.showError(withStatus: "请选择收货地址")
  198. return
  199. }
  200. if name == "" && expressType == 2 {
  201. SVProgressHUD.showError(withStatus: "请选择收货地址")
  202. return
  203. }
  204. let url = RequestURL.addProductOrderMain
  205. let params = NSMutableDictionary()
  206. params.setValue(self.idstr, forKey: "productIds")
  207. // params.setValue(num, forKey: "num")
  208. params.setValue(expressType, forKey: "deliveryType")
  209. params.setValue(CommonValue.getUserId(), forKey: "accountId")
  210. params.setValue(self.allPrice, forKey: "total")
  211. params.setValue(self.freight, forKey: "freight")
  212. if expressType == 0 || expressType == 2 {
  213. if lblName.text!.length() > 4 {
  214. params.setValue(lblName.text!.substringFrom(index: 4), forKey: "linkname")
  215. } else {
  216. params.setValue("", forKey: "linkname")
  217. }
  218. if lblAddress.text!.length() > 5 {
  219. params.setValue(lblAddress.text!.substringFrom(index: 5), forKey: "linkaddress")
  220. } else {
  221. params.setValue("", forKey: "linkaddress")
  222. }
  223. if lblPhone.text!.length() > 5 {
  224. params.setValue(lblPhone.text!.substringFrom(index: 5), forKey: "linkphone")
  225. } else {
  226. params.setValue("", forKey: "linkphone")
  227. }
  228. } else {
  229. params.setValue("", forKey: "linkname")
  230. params.setValue("", forKey: "linkaddress")
  231. params.setValue("", forKey: "linkphone")
  232. }
  233. print(params)
  234. let http = AFHTTPSessionManager()
  235. http.post(url, parameters: params, progress: { (_) in
  236. }, success: { (operation, json) in
  237. print(json as Any)
  238. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  239. if success == 200 {
  240. let orderId = (json as! NSDictionary).object(forKey: "data") as! String
  241. let vc = PayShoppingMallViewController()
  242. vc.orderId = orderId
  243. if self.expressType == 0 {
  244. vc.money = "\(String.init(format: "%.2f", Float(self.freight)+self.allPrice))"
  245. } else {
  246. vc.money = "\(String.init(format: "%.2f", self.allPrice))"
  247. }
  248. self.toViewController(viewController: vc)
  249. }
  250. }) { (_, _) in
  251. }
  252. // submitData(url: url, params: params, tag: 1001)
  253. }
  254. var freight = 0.0
  255. func getFreight() {
  256. print(dicData)
  257. if name == ""{
  258. return
  259. }
  260. let params = NSMutableDictionary()
  261. params.setValue(dicAddress["province_name"], forKey: "province")
  262. params.setValue(dicAddress["city_name"], forKey: "city")
  263. params.setValue(self.weight, forKey: "weight")
  264. let http = AFHTTPSessionManager()
  265. http.post(RequestURL.getFreight, parameters: params, progress: { (_) in
  266. }, success: { (operation, json) in
  267. print(json as Any)
  268. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  269. if success == 200 {
  270. let Freight = (json as! NSDictionary).object(forKey: "data") as! Float
  271. self.freight = Double(Freight)
  272. let strPrice2 = "¥\(String.init(format: "%.2f", self.allPrice+Freight))含运费:\(Freight)元"
  273. let str4 = "¥\(String.init(format: "%.2f", self.allPrice+Freight))"
  274. self.hejiLable.attributedText = CommonViewUntils.getAttributedStringForFont(str: strPrice2, rangs: [NSRange.init(location: str4.length(), length: strPrice2.length()-str4.length()), NSRange.init(location: str4.length(), length: strPrice2.length() - str4.length())], fonts: [UIFont.systemFont(ofSize: 10), UIFont.systemFont(ofSize: 10)])
  275. }
  276. }) { (_, _) in
  277. }
  278. }
  279. var weight: Double = 0.0
  280. func getWeight() {
  281. if self.expressType == 0 {
  282. } else {
  283. return
  284. }
  285. let url = RequestURL.getGoPayProductList
  286. let params = NSMutableDictionary()
  287. params.setValue(self.idstr, forKey: "DATA_IDS")
  288. params.setValue(CommonValue.getUserId(), forKey: "userId")
  289. let http = AFHTTPSessionManager()
  290. http.get(url, parameters: params, progress: { (_) in
  291. }, success: { (operation, json) in
  292. print(json as Any)
  293. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  294. if success == 200 {
  295. let dic = (json as! NSDictionary).object(forKey: "data") as! NSDictionary
  296. self.weight = dic["sum"]as!Double
  297. self.getFreight()
  298. }
  299. }) { (_, _) in
  300. }
  301. }
  302. /*
  303. // MARK: - Navigation
  304. // In a storyboard-based application, you will often want to do a little preparation before navigation
  305. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  306. // Get the new view controller using segue.destination.
  307. // Pass the selected object to the new view controller.
  308. }
  309. */
  310. override func viewWillAppear(_ animated: Bool) {
  311. super.viewWillAppear(animated)
  312. self.loadAddressList()
  313. let strPrice2 = "¥\(String.init(format: "%.2f", self.allPrice))"
  314. self.hejiLable.text = strPrice2
  315. self.getWeight()
  316. }
  317. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  318. super.init(nibName: "CreatShopOrderViewController", bundle: nil)
  319. }
  320. required init?(coder aDecoder: NSCoder) {
  321. fatalError("init(coder:) has not been implemented")
  322. }
  323. func loadAddressList() {
  324. let params = NSMutableDictionary()
  325. params.setValue(CommonValue.getUserId(), forKey: "userId")
  326. params.setValue(1, forKey: "currentPage")
  327. params.setValue(1, forKey: "showCount")
  328. let http = AFHTTPSessionManager()
  329. http.get(RequestURL.getUserAddressList, parameters: params, progress: { (_) in
  330. }, success: { (operation, json) in
  331. print(json as Any)
  332. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  333. if success == 200 {
  334. let dic = (json as! NSDictionary).object(forKey: "data") as! NSDictionary
  335. let addressArray = dic["addressList"]as!NSArray
  336. if addressArray.count>0 {
  337. let defaultAddressDic = addressArray[0] as! NSDictionary
  338. self.dicAddress = defaultAddressDic
  339. if defaultAddressDic["is_default"]as!String == "1"{
  340. self.btnAddress.setTitle("", for: .normal)
  341. self.lblName.text = "收货人:\(defaultAddressDic["link_name"]as!String)"
  342. self.name = defaultAddressDic["link_name"]as!String
  343. self.lblPhone.text = "联系方式:\(defaultAddressDic["link_phone"]as!String)"
  344. self.lblAddress.text = "收货地址:\(defaultAddressDic["province_name"]as!String)\(defaultAddressDic["city_name"]as!String)\(defaultAddressDic["area_name"]as!String)\(defaultAddressDic["address"]as!String)"
  345. } else {
  346. self.btnAddress.setTitle("请选择地址", for: .normal)
  347. self.lblName.text = ""
  348. self.lblPhone.text = ""
  349. self.lblAddress.text = ""
  350. self.name = ""
  351. }
  352. } else {
  353. self.btnAddress.setTitle("请选择地址", for: .normal)
  354. self.lblName.text = ""
  355. self.lblPhone.text = ""
  356. self.lblAddress.text = ""
  357. self.name = ""
  358. }
  359. }
  360. }) { (_, _) in
  361. }
  362. }
  363. }