CreateOrderViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // CreateOrderViewController.swift
  2. // CommonFrame
  3. //
  4. // Created by Virgil on 2019/4/18.
  5. // Copyright © 2019 Virgil. All rights reserved.
  6. //
  7. import UIKit
  8. class CreateOrderViewController: BaseViewController {
  9. @IBOutlet weak var view1Height: NSLayoutConstraint!
  10. @IBOutlet weak var btnWXPay: Virgil_ListMenuSelectButton!
  11. @IBOutlet weak var btnAlipay: Virgil_ListMenuSelectButton!
  12. @IBOutlet weak var viewPay: UIView!
  13. @IBOutlet weak var lblCount: UILabel!
  14. @IBOutlet weak var lblMoney2: UILabel!
  15. @IBOutlet weak var imgShow: UIImageView!
  16. @IBOutlet weak var lblMoney: UILabel!
  17. @IBOutlet weak var lblTitle: UILabel!
  18. @IBOutlet weak var btnAddress: UIButton!
  19. @IBOutlet weak var lblPhone: UILabel!
  20. @IBOutlet weak var lblAddress: UILabel!
  21. @IBOutlet weak var lblName: UILabel!
  22. @IBOutlet weak var view3: UIView!
  23. @IBOutlet weak var view2: UIView!
  24. @IBOutlet weak var view1: UIView!
  25. @IBOutlet weak var viewExpress: UIView!
  26. @IBOutlet weak var btnExpress2: UIButton!
  27. @IBOutlet weak var btnExpress1: UIButton!
  28. @IBOutlet weak var btnExpressBtn3: UIButton!
  29. var id = ""
  30. var num = 1
  31. ///0:创建订单支付 2:订单详情支付
  32. var fromType = 0
  33. ///0:顺丰到付 1:自提
  34. var expressType = 0
  35. ///0:支付宝 1:微信
  36. var payType = 0
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. initNavLeftBackButton()
  40. self.title = "确认订单"
  41. view1.setCornerRadius(size: 8)
  42. view2.setCornerRadius(size: 8)
  43. if expressType == 1 {
  44. view1Height.constant = 0
  45. }
  46. btnExpress1.setCornerRadius(size: 3)
  47. btnExpress2.setCornerRadius(size: 3)
  48. btnExpressBtn3.setCornerRadius(size: 3)
  49. viewExpress.setCornerRadius(size: 8)
  50. btnExpress1.setTitleColor(.white, for: .normal)
  51. btnExpress2.setTitleColor(.white, for: .normal)
  52. btnExpressBtn3.setTitleColor(.white, for: .normal)
  53. }
  54. func initData() {
  55. lblTitle.text = getString(key: "name")
  56. let strPrice = "¥\(getDoubleValue(key: "price"))"
  57. 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: 10), UIFont.systemFont(ofSize: 15)])
  58. let strPrice2 = "¥\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))"
  59. lblMoney2.attributedText = CommonViewUntils.getAttributedStringForFont(str: strPrice2, rangs: [NSRange.init(location: 0, length: 1), NSRange.init(location: 1, length: strPrice2.length() - 1)], fonts: [UIFont.systemFont(ofSize: 10), UIFont.systemFont(ofSize: 15)])
  60. imgShow.loadImage(imgUrl: getString(key: "image"), defaultImage: "")
  61. lblCount.text = "\(num)件"
  62. }
  63. @IBAction func btnMenuClick(_ sender: AnyObject) {
  64. switch (sender as! UIButton).tag {
  65. case 1001: // 选择地址
  66. // let vc = SelectedAddressViewController()
  67. // vc.dicAddress.setDic(dic: dicAddress)
  68. // if(lblName.text! != "")
  69. // {
  70. // vc.dicData.setValue(name, forKey: "name")
  71. // vc.dicData.setValue(phone, forKey: "phone")
  72. // vc.dicData.setValue(address1, forKey: "address1")
  73. // vc.dicData.setValue(address2, forKey: "address2")
  74. // }
  75. // toViewController(viewController: vc)
  76. let vc = AddressListViewController()
  77. toViewController(viewController: vc)
  78. break
  79. case 1002: // 提交订单
  80. if name == "" && expressType == 0 {
  81. SVProgressHUD.showError(withStatus: "请选择收货地址")
  82. return
  83. }
  84. if name == "" && expressType == 2 {
  85. SVProgressHUD.showError(withStatus: "请选择收货地址")
  86. return
  87. }
  88. submitData()
  89. break
  90. case 1003: // 微信
  91. payType = 1
  92. btnWXPay.isSelected = true
  93. btnAlipay.isSelected = false
  94. break
  95. case 1004: // 支付宝
  96. payType = 0
  97. btnWXPay.isSelected = false
  98. btnAlipay.isSelected = true
  99. break
  100. case 1005: //
  101. break
  102. case 1006: //快递寄付
  103. self.expressType = 0
  104. // if(name == "" && expressType == 0)
  105. // {
  106. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  107. // return;
  108. // }
  109. // if(name == "" && expressType == 2)
  110. // {
  111. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  112. // return;
  113. // }
  114. btnExpress1.isSelected = true
  115. btnExpress2.isSelected = false
  116. btnExpressBtn3.isSelected = false
  117. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  118. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  119. btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  120. view1Height.constant = 90
  121. getFreight()
  122. break
  123. case 1007: // 顺丰到付
  124. self.expressType = 2
  125. // if(name == "" && expressType == 0)
  126. // {
  127. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  128. // return;
  129. // }
  130. // if(name == "" && expressType == 2)
  131. // {
  132. // SVProgressHUD.showError(withStatus: "请选择收货地址")
  133. // return;
  134. // }
  135. btnExpress1.isSelected = false
  136. btnExpress2.isSelected = true
  137. btnExpressBtn3.isSelected = false
  138. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  139. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  140. btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  141. view1Height.constant = 90
  142. let strPrice2 = "¥\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))"
  143. lblMoney2.text = strPrice2
  144. self.freight = 0.0
  145. break
  146. case 1008: // 上门自提
  147. self.expressType = 1
  148. btnExpress1.isSelected = false
  149. btnExpress2.isSelected = true
  150. btnExpressBtn3.isSelected = false
  151. btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  152. btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
  153. btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
  154. view1Height.constant = 0
  155. let strPrice2 = "¥\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))"
  156. lblMoney2.text = strPrice2
  157. self.freight = 0.0
  158. break
  159. default:
  160. break
  161. }
  162. }
  163. // MARK: =============加载数据===============
  164. func submitData() {
  165. let url = RequestURL.addProductOrderMainFast
  166. let params = NSMutableDictionary()
  167. params.setValue(id, forKey: "productId")
  168. params.setValue(num, forKey: "num")
  169. params.setValue(CommonValue.getUserId(), forKey: "accountId")
  170. params.setValue(expressType, forKey: "deliveryType")
  171. params.setValue(self.getDoubleValue(key: "price"), forKey: "price")
  172. params.setValue("\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))", forKey: "total")
  173. params.setValue(self.freight, forKey: "freight")
  174. if expressType == 0 || expressType == 2 {
  175. if lblName.text!.length() > 4 {
  176. params.setValue(lblName.text!.substringFrom(index: 4), forKey: "linkname")
  177. } else {
  178. params.setValue("", forKey: "linkname")
  179. }
  180. if lblAddress.text!.length() > 5 {
  181. params.setValue(lblAddress.text!.substringFrom(index: 5), forKey: "linkaddress")
  182. } else {
  183. params.setValue("", forKey: "linkaddress")
  184. }
  185. if lblPhone.text!.length() > 5 {
  186. params.setValue(lblPhone.text!.substringFrom(index: 5), forKey: "linkphone")
  187. } else {
  188. params.setValue("", forKey: "linkphone")
  189. }
  190. } else {
  191. params.setValue("", forKey: "linkname")
  192. params.setValue("", forKey: "linkaddress")
  193. params.setValue("", forKey: "linkphone")
  194. }
  195. print(params)
  196. let http = AFHTTPSessionManager()
  197. http.post(url, parameters: params, progress: { (_) in
  198. }, success: { (operation, json) in
  199. print(json as Any)
  200. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  201. if success == 200 {
  202. let dic = (json as! NSDictionary).object(forKey: "data") as! NSDictionary
  203. let vc = PayShoppingMallViewController()
  204. vc.orderId = dic.getString(key: "code")
  205. if self.expressType == 0 {
  206. vc.money = "\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)+self.freight))"
  207. } else {
  208. vc.money = "\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)))"
  209. }
  210. self.toViewController(viewController: vc)
  211. }
  212. }) { (_, _) in
  213. }
  214. // submitData(url: url, params: params, tag: 1001)
  215. }
  216. var dicAddress = NSDictionary()
  217. func loadData() {
  218. let params = NSMutableDictionary()
  219. params.setValue(CommonValue.getUserId(), forKey: "userId")
  220. params.setValue(1, forKey: "currentPage")
  221. params.setValue(1, forKey: "showCount")
  222. let http = AFHTTPSessionManager()
  223. http.get(RequestURL.getUserAddressList, parameters: params, progress: { (_) in
  224. }, success: { (operation, json) in
  225. print(json as Any)
  226. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  227. if success == 200 {
  228. let dic = (json as! NSDictionary).object(forKey: "data") as! NSDictionary
  229. let addressArray = dic["addressList"]as!NSArray
  230. if addressArray.count>0 {
  231. let defaultAddressDic = addressArray[0] as! NSDictionary
  232. self.dicAddress = defaultAddressDic
  233. if defaultAddressDic["is_default"]as!String == "1"{
  234. self.btnAddress.setTitle("", for: .normal)
  235. self.lblName.text = "收货人:\(defaultAddressDic["link_name"]as!String)"
  236. self.name = defaultAddressDic["link_name"]as!String
  237. self.lblPhone.text = "联系方式:\(defaultAddressDic["link_phone"]as!String)"
  238. self.lblAddress.text = "收货地址:\(defaultAddressDic["province_name"]as!String)\(defaultAddressDic["city_name"]as!String)\(defaultAddressDic["area_name"]as!String)\(defaultAddressDic["address"]as!String)"
  239. self.getFreight()
  240. } else {
  241. self.btnAddress.setTitle("请选择地址", for: .normal)
  242. self.lblName.text = ""
  243. self.lblPhone.text = ""
  244. self.lblAddress.text = ""
  245. self.name = ""
  246. }
  247. } else {
  248. self.btnAddress.setTitle("请选择地址", for: .normal)
  249. self.lblName.text = ""
  250. self.lblPhone.text = ""
  251. self.lblAddress.text = ""
  252. self.name = ""
  253. }
  254. }
  255. }) { (_, _) in
  256. }
  257. }
  258. override func returnData(tag: Int) {
  259. if tag == 1001 {
  260. } else if tag == 1002 {
  261. if dicAddress.getString(key: "link_name") != "" {
  262. btnAddress.setTitle("", for: .normal)
  263. name = dicAddress.getString(key: "link_name")
  264. phone = dicAddress.getString(key: "link_phone")
  265. address1 = "\(dicAddress.getString(key: "province_name"))\(dicAddress.getString(key: "city_name"))\(dicAddress.getString(key: "area_name"))"
  266. address2 = dicAddress.getString(key: "address")
  267. lblName.text = "收货人:\(name)"
  268. lblPhone.text = "联系方式:\(phone)"
  269. lblAddress.text = "收货地址:\(address1) \(address2)"
  270. }
  271. }
  272. }
  273. override func returnError(tag: Int, type: String) {
  274. }
  275. var freight = 0.0
  276. func getFreight() {
  277. print(dicData)
  278. if self.expressType == 0 {
  279. } else {
  280. return
  281. }
  282. if name == ""{
  283. return
  284. }
  285. let params = NSMutableDictionary()
  286. let weight = (dicData["weight"]as!Double)*(Double(num))
  287. print(weight)
  288. params.setValue(dicAddress["province_name"], forKey: "province")
  289. params.setValue(dicAddress["city_name"], forKey: "city")
  290. params.setValue(weight, forKey: "weight")
  291. print(params)
  292. let http = AFHTTPSessionManager()
  293. http.post(RequestURL.getFreight, parameters: params, progress: { (_) in
  294. }, success: { (operation, json) in
  295. print(json as Any)
  296. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  297. if success == 200 {
  298. let Freight = (json as! NSDictionary).object(forKey: "data") as! Double
  299. self.freight = Freight
  300. let strPrice2 = "¥\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)+Freight))含运费:\(Freight)元"
  301. let str4 = "¥\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)+Freight))"
  302. self.lblMoney2.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)])
  303. }
  304. }) { (_, _) in
  305. }
  306. }
  307. var name = ""
  308. var phone = ""
  309. var address1 = ""
  310. var address2 = ""
  311. override func viewWillAppear(_ animated: Bool) {
  312. super.viewWillAppear(animated)
  313. initData()
  314. loadData()
  315. }
  316. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  317. super.init(nibName: "CreateOrderViewController", bundle: nil)
  318. }
  319. required init?(coder aDecoder: NSCoder) {
  320. fatalError("init(coder:) has not been implemented")
  321. }
  322. }