SelectedAddressViewController.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // SelectedAddressViewController.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 SelectedAddressViewController: BaseViewController, UITextViewDelegate {
  10. @IBOutlet weak var txtAddress1: UITextField!
  11. @IBOutlet weak var txtPhone: UITextField!
  12. @IBOutlet weak var txtName: UITextField!
  13. @IBOutlet weak var btnConfrim: UIButton!
  14. @IBOutlet weak var txtContent: UITextView!
  15. @IBOutlet weak var view3: UIView!
  16. @IBOutlet weak var view2: UIView!
  17. @IBOutlet weak var view1: UIView!
  18. @IBOutlet weak var switchBtn: UISwitch!
  19. let dicAddress = NSMutableDictionary()
  20. var isDefault = 1
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. initNavLeftBackButton()
  24. self.title = "设置地址"
  25. view1.setLayer(borderWidth: 1.0, color: 0xf7f7f9)
  26. view2.setLayer(borderWidth: 1.0, color: 0xf7f7f9)
  27. view3.setLayer(borderWidth: 1.0, color: 0xf7f7f9)
  28. txtContent.setLayer(borderWidth: 1.0, color: 0xf7f7f9)
  29. view1.setCornerRadius(size: 6)
  30. view2.setCornerRadius(size: 6)
  31. view3.setCornerRadius(size: 6)
  32. txtContent.setCornerRadius(size: 8)
  33. lblAlert = UILabel(frame: CGRect(x: 15, y: 5, width: 180, height: 20))
  34. lblAlert.text = "输入详细地址"
  35. lblAlert.isEnabled = false
  36. lblAlert.backgroundColor = UIColor.clear
  37. lblAlert.font = UIFont.systemFont(ofSize: 12)
  38. txtContent.addSubview(lblAlert)
  39. txtContent.delegate = self
  40. txtName.setContentMarginLeft(leftWidth: 10)
  41. // txtContent.setmal
  42. btnConfrim.setCornerRadius(size: btnConfrim.height() / 2)
  43. }
  44. @IBAction func switchChanged(_ sender: Any) {
  45. if self.switchBtn.isOn == true {
  46. isDefault = 1
  47. } else {
  48. isDefault = 0
  49. }
  50. }
  51. override func KeyboardHidden(gestureRecognizer: UIGestureRecognizer) {
  52. txtContent.resignFirstResponder()
  53. txtName.resignFirstResponder()
  54. txtPhone.resignFirstResponder()
  55. txtAddress1.resignFirstResponder()
  56. }
  57. var lblAlert: UILabel!
  58. func textViewDidChange(_ textView: UITextView) {
  59. if textView.text.length() == 0 {
  60. lblAlert.text = "输入详细地址"
  61. } else {
  62. lblAlert.text = ""
  63. }
  64. }
  65. var address1 = ""
  66. var address2 = ""
  67. var address3 = ""
  68. var addressid1 = ""
  69. var addressid2 = ""
  70. var addressid3 = ""
  71. var viewSelectCity: Virgil_SelectCity!
  72. @IBAction func btnMenuClick(_ sender: AnyObject) {
  73. switch (sender as! UIButton).tag {
  74. case 1001: // 选择地址
  75. let addressList = CommonUntils.getPlistArrayForName(fileName: "AddressNew")
  76. if viewSelectCity == nil {
  77. viewSelectCity = Virgil_SelectCity(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: viewHeight))
  78. viewSelectCity.modelType = 2
  79. viewSelectCity.initView(arrAddress: addressList) {[weak self] (dic) in
  80. self!.txtAddress1.text = self!.getString(key: "address", dic: dic)
  81. self!.address1 = dic.getString(key: "addressName1")
  82. self!.address2 = dic.getString(key: "addressName2")
  83. self!.address3 = dic.getString(key: "addressName3")
  84. self!.addressid1 = dic.getString(key: "addressId1")
  85. self!.addressid2 = dic.getString(key: "addressId2")
  86. self!.addressid3 = dic.getString(key: "addressId3")
  87. }
  88. self.view.addSubview(viewSelectCity)
  89. }
  90. viewSelectCity.showPickerView()
  91. break
  92. case 1002: // 确定
  93. if txtName.text!.length() < 1 {
  94. SVProgressHUD.showError(withStatus: txtName.placeholder!)
  95. return
  96. }
  97. if txtPhone.text!.length() < 1 {
  98. SVProgressHUD.showError(withStatus: txtPhone.placeholder!)
  99. return
  100. }
  101. if txtAddress1.text!.length() < 1 {
  102. SVProgressHUD.showError(withStatus: txtAddress1.placeholder!)
  103. return
  104. }
  105. if txtContent.text!.length() < 1 {
  106. SVProgressHUD.showError(withStatus: "请输入详细地址")
  107. return
  108. }
  109. // mainViewControllerIsGo = false;
  110. // mainDicViewControllerIsGoValue.removeAllObjects();
  111. // mainDicViewControllerIsGoValue.setValue(txtName.text!, forKey: "name")
  112. // mainDicViewControllerIsGoValue.setValue(txtPhone.text!, forKey: "phone")
  113. // mainDicViewControllerIsGoValue.setValue(txtAddress1.text!, forKey: "address1")
  114. // mainDicViewControllerIsGoValue.setValue(txtContent.text!, forKey: "address2")
  115. submitData()
  116. break
  117. case 1003: //
  118. break
  119. case 1004: //
  120. break
  121. case 1005: //
  122. break
  123. case 1006: //
  124. break
  125. default:
  126. break
  127. }
  128. }
  129. // MARK: =============加载数据===============
  130. func submitData() {
  131. let params = NSMutableDictionary()
  132. if dicAddress.getString(key: "id") != "" {
  133. params.setValue(dicAddress.getString(key: "id"), forKey: "id")
  134. }
  135. params.setValue(CommonValue.getUserId(), forKey: "userId")
  136. params.setValue(address1, forKey: "provinceName")
  137. params.setValue(address2, forKey: "cityName")
  138. params.setValue(address3, forKey: "areaName")
  139. params.setValue(addressid1, forKey: "provinceId")
  140. params.setValue(addressid2, forKey: "cityId")
  141. params.setValue(addressid3, forKey: "areaId")
  142. params.setValue(txtName.text!, forKey: "linkName")
  143. params.setValue(txtContent.text!, forKey: "address")
  144. params.setValue(txtPhone.text!, forKey: "linkPhone")
  145. params.setValue(isDefault, forKey: "isDefault")
  146. let http = AFHTTPSessionManager()
  147. http.post(RequestURL.addUserAddress, parameters: params, progress: { (_) in
  148. }, success: { (operation, json) in
  149. print(json as Any)
  150. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  151. if success == 200 {
  152. // let dic = (json as! NSDictionary).object(forKey: "data") as! NSDictionary
  153. //
  154. SVProgressHUD.showSuccess(withStatus: "提交成功!")
  155. }
  156. }) { (_, _) in
  157. }
  158. // submitData(url: url, params: params, tag: 1001)
  159. }
  160. override func returnData(tag: Int) {
  161. if tag == 1001 {
  162. handleBack()
  163. }
  164. }
  165. override func returnError(tag: Int, type: String) {
  166. }
  167. override func viewWillAppear(_ animated: Bool) {
  168. super.viewWillAppear(animated)
  169. if dicData.getString(key: "name") != "" {
  170. txtName.text = dicAddress.getString(key: "link_name")
  171. txtPhone.text = dicAddress.getString(key: "link_phone")
  172. txtAddress1.text = "\(dicAddress.getString(key: "province_name"))\(dicAddress.getString(key: "city_name"))\(dicAddress.getString(key: "area_name"))"
  173. txtContent.text = dicAddress.getString(key: "address")
  174. lblAlert.text = ""
  175. self.address1 = dicAddress.getString(key: "province_name")
  176. self.address2 = dicAddress.getString(key: "city_name")
  177. self.address3 = dicAddress.getString(key: "area_name")
  178. self.isDefault = dicAddress.getInt(key: "is_default")
  179. if dicAddress.getString(key: "is_default") == "1"{
  180. self.switchBtn.isOn = true
  181. } else {
  182. self.switchBtn.isOn = false
  183. }
  184. }
  185. }
  186. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  187. super.init(nibName: "SelectedAddressViewController", bundle: nil)
  188. }
  189. required init?(coder aDecoder: NSCoder) {
  190. fatalError("init(coder:) has not been implemented")
  191. }
  192. }