// // AddressListViewController.swift // xingchuangke // // Created by machaochao on 2020/10/15. // Copyright © 2020 Virgil. All rights reserved. // import UIKit class AddressListViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate { //var tableView: UITableView! @IBOutlet weak var tableView: UITableView! @IBOutlet weak var addBtn: UIButton! override func viewDidLoad() { super.viewDidLoad() initNavLeftBackButton() self.title = "我的地址" tableView.register(UINib(nibName: "MyAddressTableViewCell", bundle: nil), forCellReuseIdentifier: "MyAddressTableViewCell") tableView.setSizeHeight(height: kScreenHeight-navheight-100.0) tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .none tableView.showsVerticalScrollIndicator = false tableView.estimatedRowHeight = 120 tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in self!.currentPage = 1 self!.loadData() }) tableView.mj_header?.lastUpdatedTimeKey = "MyAddressTableViewCell" tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {[weak self] in self!.currentPage += 1 self!.loadData() }) tableView.mj_header?.beginRefreshing() addBtn.layer.cornerRadius = 15 addBtn.layer.masksToBounds = true // Do any additional setup after loading the view. } var dataArray = NSArray.init() func loadData() { let params = NSMutableDictionary() params.setValue(CommonValue.getUserId(), forKey: "userId") params.setValue(self.currentPage, forKey: "currentPage") params.setValue(10, 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 self.dataArray = dic["addressList"]as!NSArray self.tableView.reloadData() } self.tableView.mj_header?.endRefreshing() self.tableView.mj_footer?.endRefreshing() }) { (_, _) in } } // MARK: TABLEVLEW 实现 func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.dataArray.count // return 10; } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 120 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyAddressTableViewCell", for: indexPath as IndexPath) as! MyAddressTableViewCell //cell.lblTitle.text = getString(indexPath.row, key: "Name") let dic = self.dataArray[indexPath.row]as!NSDictionary cell.setBtn.addTarget(self, action: #selector(self.setMoren(_:)), for: .touchUpInside) cell.setBtn.tag = indexPath.row cell.changeBtn.addTarget(self, action: #selector(self.changeAd(_:)), for: .touchUpInside) cell.changeBtn.tag = indexPath.row cell.delectBtn.addTarget(self, action: #selector(self.delectAd(_:)), for: .touchUpInside) cell.delectBtn.tag = indexPath.row cell.loadDataWith(dic: dic) return cell } @IBAction func addBtnTouch(_ sender: Any) { let vc = SelectedAddressViewController() toViewController(viewController: vc) } @objc func setMoren(_ sender: UIButton) { let dic = self.dataArray[sender.tag]as!NSDictionary let params = NSMutableDictionary() params.setValue(CommonValue.getUserId(), forKey: "userId") params.setValue(dic["address"]as!String, forKey: "address") if dic.getString(key: "is_default") == "1"{ params.setValue(0, forKey: "isDefault") } else { params.setValue(1, forKey: "isDefault") } params.setValue(dic["province_id"]as!String, forKey: "provinceId") params.setValue(dic["province_name"]as!String, forKey: "provinceName") params.setValue(dic["city_id"]as!String, forKey: "cityId") params.setValue(dic["city_name"]as!String, forKey: "cityName") params.setValue(dic["area_id"]as!String, forKey: "areaId") params.setValue(dic["area_name"]as!String, forKey: "areaName") params.setValue(dic["link_name"]as!String, forKey: "linkName") params.setValue(dic["link_phone"]as!String, forKey: "linkPhone") params.setValue(dic["id"]as!String, forKey: "id") let http = AFHTTPSessionManager() http.post(RequestURL.updateUserAddress, 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 // self.loadData() } }) { (_, _) in } } @objc func changeAd(_ sender: UIButton) { let dic = self.dataArray[sender.tag]as!NSDictionary let vc = SelectedAddressViewController() vc.dicAddress.setDic(dic: dic) vc.dicData.setValue(dic["link_name"], forKey: "name") vc.dicData.setValue(dic["link_phone"], forKey: "phone") vc.dicData.setValue("\(dic["province_name"]as!String)\(dic["city_name"]as!String)\(dic["area_name"]as!String))", forKey: "address1") vc.dicData.setValue(dic["address"], forKey: "address2") toViewController(viewController: vc) } @objc func delectAd(_ sender: UIButton) { let dic = self.dataArray[sender.tag]as!NSDictionary if dic["is_default"]as!String == "1"{ if self.dataArray.count == 1 { } else { let alert = UIAlertController.init(title: "请先取消默认地址再删除!", message: "", preferredStyle: .alert) let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (_) in } let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil) alert.addAction(confirmaction) alert.addAction(cancleAction) self.present(alert, animated: true, completion: nil) } } let alert = UIAlertController.init(title: "您确定要删除这个地址吗?", message: "", preferredStyle: .alert) let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (_) in let dic = self.dataArray[sender.tag]as!NSDictionary self.delectAddress(adid: dic["id"]as!String) } let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil) alert.addAction(confirmaction) alert.addAction(cancleAction) self.present(alert, animated: true, completion: nil) } func delectAddress(adid: String) { let params = NSMutableDictionary() params.setValue(CommonValue.getUserId(), forKey: "userId") params.setValue(adid, forKey: "DATA_IDS") let http = AFHTTPSessionManager() http.post(RequestURL.deleteUserAddress, 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 self.loadData() } }) { (_, _) in } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // let vc = OrderDetailViewController(); // //let dicProduct = (self.arrData[indexPath.row] as! NSDictionary)["product"] as! NSDictionary; // vc.orderId = self.arrData.getString(index: indexPath.row, key: "id") // //vc.dicData.setDictionary((self.arrData[indexPath.row] as! NSDictionary) as! [AnyHashable : Any]) // toViewController(viewController: vc); } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.currentPage = 1 self.loadData() } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: "AddressListViewController", bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }