123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- // CreateOrderViewController.swift
- // CommonFrame
- //
- // Created by Virgil on 2019/4/18.
- // Copyright © 2019 Virgil. All rights reserved.
- //
- import UIKit
- class CreateOrderViewController: BaseViewController {
- @IBOutlet weak var view1Height: NSLayoutConstraint!
- @IBOutlet weak var btnWXPay: Virgil_ListMenuSelectButton!
- @IBOutlet weak var btnAlipay: Virgil_ListMenuSelectButton!
- @IBOutlet weak var viewPay: UIView!
- @IBOutlet weak var lblCount: UILabel!
- @IBOutlet weak var lblMoney2: UILabel!
- @IBOutlet weak var imgShow: UIImageView!
- @IBOutlet weak var lblMoney: UILabel!
- @IBOutlet weak var lblTitle: UILabel!
- @IBOutlet weak var btnAddress: UIButton!
- @IBOutlet weak var lblPhone: UILabel!
- @IBOutlet weak var lblAddress: UILabel!
- @IBOutlet weak var lblName: UILabel!
- @IBOutlet weak var view3: UIView!
- @IBOutlet weak var view2: UIView!
- @IBOutlet weak var view1: UIView!
- @IBOutlet weak var viewExpress: UIView!
- @IBOutlet weak var btnExpress2: UIButton!
- @IBOutlet weak var btnExpress1: UIButton!
- @IBOutlet weak var btnExpressBtn3: UIButton!
- var id = ""
- var num = 1
- ///0:创建订单支付 2:订单详情支付
- var fromType = 0
- ///0:顺丰到付 1:自提
- var expressType = 0
- ///0:支付宝 1:微信
- var payType = 0
- override func viewDidLoad() {
- super.viewDidLoad()
- initNavLeftBackButton()
- self.title = "确认订单"
- view1.setCornerRadius(size: 8)
- view2.setCornerRadius(size: 8)
- if expressType == 1 {
- view1Height.constant = 0
- }
- btnExpress1.setCornerRadius(size: 3)
- btnExpress2.setCornerRadius(size: 3)
- btnExpressBtn3.setCornerRadius(size: 3)
- viewExpress.setCornerRadius(size: 8)
- btnExpress1.setTitleColor(.white, for: .normal)
- btnExpress2.setTitleColor(.white, for: .normal)
- btnExpressBtn3.setTitleColor(.white, for: .normal)
- }
- func initData() {
- lblTitle.text = getString(key: "name")
- 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: 10), UIFont.systemFont(ofSize: 15)])
- let strPrice2 = "¥\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))"
- 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)])
- imgShow.loadImage(imgUrl: getString(key: "image"), defaultImage: "")
- lblCount.text = "\(num)件"
- }
- @IBAction func btnMenuClick(_ sender: AnyObject) {
- switch (sender as! UIButton).tag {
- case 1001: // 选择地址
- // let vc = SelectedAddressViewController()
- // vc.dicAddress.setDic(dic: dicAddress)
- // if(lblName.text! != "")
- // {
- // vc.dicData.setValue(name, forKey: "name")
- // vc.dicData.setValue(phone, forKey: "phone")
- // vc.dicData.setValue(address1, forKey: "address1")
- // vc.dicData.setValue(address2, forKey: "address2")
- // }
- // toViewController(viewController: vc)
- let vc = AddressListViewController()
- toViewController(viewController: vc)
- break
- case 1002: // 提交订单
- if name == "" && expressType == 0 {
- SVProgressHUD.showError(withStatus: "请选择收货地址")
- return
- }
- if name == "" && expressType == 2 {
- SVProgressHUD.showError(withStatus: "请选择收货地址")
- return
- }
- submitData()
- break
- case 1003: // 微信
- payType = 1
- btnWXPay.isSelected = true
- btnAlipay.isSelected = false
- break
- case 1004: // 支付宝
- payType = 0
- btnWXPay.isSelected = false
- btnAlipay.isSelected = true
- break
- case 1005: //
- break
- case 1006: //快递寄付
- self.expressType = 0
- // if(name == "" && expressType == 0)
- // {
- // SVProgressHUD.showError(withStatus: "请选择收货地址")
- // return;
- // }
- // if(name == "" && expressType == 2)
- // {
- // SVProgressHUD.showError(withStatus: "请选择收货地址")
- // return;
- // }
- btnExpress1.isSelected = true
- btnExpress2.isSelected = false
- btnExpressBtn3.isSelected = false
- btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
- btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
- btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
- view1Height.constant = 90
- getFreight()
- break
- case 1007: // 顺丰到付
- self.expressType = 2
- // if(name == "" && expressType == 0)
- // {
- // SVProgressHUD.showError(withStatus: "请选择收货地址")
- // return;
- // }
- // if(name == "" && expressType == 2)
- // {
- // SVProgressHUD.showError(withStatus: "请选择收货地址")
- // return;
- // }
- btnExpress1.isSelected = false
- btnExpress2.isSelected = true
- btnExpressBtn3.isSelected = false
- btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
- btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
- btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
- view1Height.constant = 90
- let strPrice2 = "¥\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))"
- lblMoney2.text = strPrice2
- self.freight = 0.0
- break
- case 1008: // 上门自提
- self.expressType = 1
- btnExpress1.isSelected = false
- btnExpress2.isSelected = true
- btnExpressBtn3.isSelected = false
- btnExpress1.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
- btnExpress2.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xdddddd, alpha: 1.0)
- btnExpressBtn3.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x36a5fe, alpha: 1.0)
- view1Height.constant = 0
- let strPrice2 = "¥\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))"
- lblMoney2.text = strPrice2
- self.freight = 0.0
- break
- default:
- break
- }
- }
- // MARK: =============加载数据===============
- func submitData() {
- let url = RequestURL.addProductOrderMainFast
- let params = NSMutableDictionary()
- params.setValue(id, forKey: "productId")
- params.setValue(num, forKey: "num")
- params.setValue(CommonValue.getUserId(), forKey: "accountId")
- params.setValue(expressType, forKey: "deliveryType")
- params.setValue(self.getDoubleValue(key: "price"), forKey: "price")
- params.setValue("\(String.init(format: "%.2f", getDoubleValue(key: "price") * Double(num)))", forKey: "total")
- params.setValue(self.freight, forKey: "freight")
- if expressType == 0 || expressType == 2 {
- if lblName.text!.length() > 4 {
- params.setValue(lblName.text!.substringFrom(index: 4), forKey: "linkname")
- } else {
- params.setValue("", forKey: "linkname")
- }
- if lblAddress.text!.length() > 5 {
- params.setValue(lblAddress.text!.substringFrom(index: 5), forKey: "linkaddress")
- } else {
- params.setValue("", forKey: "linkaddress")
- }
- if lblPhone.text!.length() > 5 {
- params.setValue(lblPhone.text!.substringFrom(index: 5), forKey: "linkphone")
- } else {
- params.setValue("", forKey: "linkphone")
- }
- } else {
- params.setValue("", forKey: "linkname")
- params.setValue("", forKey: "linkaddress")
- params.setValue("", forKey: "linkphone")
- }
- print(params)
- let http = AFHTTPSessionManager()
- http.post(url, 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 dic = (json as! NSDictionary).object(forKey: "data") as! NSDictionary
- let vc = PayShoppingMallViewController()
- vc.orderId = dic.getString(key: "code")
- if self.expressType == 0 {
- vc.money = "\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)+self.freight))"
- } else {
- vc.money = "\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)))"
- }
- self.toViewController(viewController: vc)
- }
- }) { (_, _) in
- }
- // submitData(url: url, params: params, tag: 1001)
- }
- var dicAddress = NSDictionary()
- func loadData() {
- let params = NSMutableDictionary()
- params.setValue(CommonValue.getUserId(), forKey: "userId")
- params.setValue(1, forKey: "currentPage")
- params.setValue(1, forKey: "showCount")
- let http = AFHTTPSessionManager()
- http.get(RequestURL.getUserAddressList, 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 dic = (json as! NSDictionary).object(forKey: "data") as! NSDictionary
- let addressArray = dic["addressList"]as!NSArray
- if addressArray.count>0 {
- let defaultAddressDic = addressArray[0] as! NSDictionary
- self.dicAddress = defaultAddressDic
- if defaultAddressDic["is_default"]as!String == "1"{
- self.btnAddress.setTitle("", for: .normal)
- self.lblName.text = "收货人:\(defaultAddressDic["link_name"]as!String)"
- self.name = defaultAddressDic["link_name"]as!String
- self.lblPhone.text = "联系方式:\(defaultAddressDic["link_phone"]as!String)"
- self.lblAddress.text = "收货地址:\(defaultAddressDic["province_name"]as!String)\(defaultAddressDic["city_name"]as!String)\(defaultAddressDic["area_name"]as!String)\(defaultAddressDic["address"]as!String)"
- self.getFreight()
- } else {
- self.btnAddress.setTitle("请选择地址", for: .normal)
- self.lblName.text = ""
- self.lblPhone.text = ""
- self.lblAddress.text = ""
- self.name = ""
- }
- } else {
- self.btnAddress.setTitle("请选择地址", for: .normal)
- self.lblName.text = ""
- self.lblPhone.text = ""
- self.lblAddress.text = ""
- self.name = ""
- }
- }
- }) { (_, _) in
- }
- }
- override func returnData(tag: Int) {
- if tag == 1001 {
- } else if tag == 1002 {
- if dicAddress.getString(key: "link_name") != "" {
- btnAddress.setTitle("", for: .normal)
- name = dicAddress.getString(key: "link_name")
- phone = dicAddress.getString(key: "link_phone")
- address1 = "\(dicAddress.getString(key: "province_name"))\(dicAddress.getString(key: "city_name"))\(dicAddress.getString(key: "area_name"))"
- address2 = dicAddress.getString(key: "address")
- lblName.text = "收货人:\(name)"
- lblPhone.text = "联系方式:\(phone)"
- lblAddress.text = "收货地址:\(address1) \(address2)"
- }
- }
- }
- override func returnError(tag: Int, type: String) {
- }
- var freight = 0.0
- func getFreight() {
- print(dicData)
- if self.expressType == 0 {
- } else {
- return
- }
- if name == ""{
- return
- }
- let params = NSMutableDictionary()
- let weight = (dicData["weight"]as!Double)*(Double(num))
- print(weight)
- params.setValue(dicAddress["province_name"], forKey: "province")
- params.setValue(dicAddress["city_name"], forKey: "city")
- params.setValue(weight, forKey: "weight")
- print(params)
- let http = AFHTTPSessionManager()
- http.post(RequestURL.getFreight, 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 Freight = (json as! NSDictionary).object(forKey: "data") as! Double
- self.freight = Freight
- let strPrice2 = "¥\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)+Freight))含运费:\(Freight)元"
- let str4 = "¥\(String.init(format: "%.2f", self.getDoubleValue(key: "price") * Double(self.num)+Freight))"
- 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)])
- }
- }) { (_, _) in
- }
- }
- var name = ""
- var phone = ""
- var address1 = ""
- var address2 = ""
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- initData()
- loadData()
- }
- override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
- super.init(nibName: "CreateOrderViewController", bundle: nil)
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
|