123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- //
- // OrderController.swift
- // learnSwift
- //
- // Created by zyc on 2019/11/20.
- // Copyright © 2019 张言超. All rights reserved.
- //
- import UIKit
- class OrderController: BaseViewController {
- var myTableView = UITableView.init()
- /** 数据源 */
- var dataArr: NSMutableArray = NSMutableArray.init()
- /** 选择的数据 */
- var selectedArr: NSMutableArray = NSMutableArray.init()
- override func viewDidLoad() {
- super.viewDidLoad()
- initNavLeftBackButton()
- self.navigationItem.title = "购物车"
- self.customUI()
- self.initData()
- }
- var footView: YCOrderFootView?
- func customUI() {
- view.backgroundColor = UIColor.white
- self.myTableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-70), style: .grouped)
- self.myTableView.delegate = self
- self.myTableView.dataSource = self
- self.myTableView.separatorStyle = .none
- self.myTableView.register(YCOrderShopCartCell.self, forCellReuseIdentifier: "YCOrderShopCartCell")
- self.view.addSubview(self.myTableView)
- self.footView = YCOrderFootView.init(frame: CGRect(x: 0, y: kScreenHeight-navheight-70, width: kScreenWidth, height: 70))
- self.footView?.footDelegate = self
- self.footView?.accountBtn?.addTarget(self, action: #selector(accountBtnTouch), for: UIControl.Event.touchUpInside)
- self.view.addSubview(self.footView!)
- }
- @objc func accountBtnTouch() {
- self.addToShopCar()
- }
- @objc func delectGoods(_ sender: UIButton) {
- let alert = UIAlertController.init(title: "您确定要删除这个商品吗?", message: "", preferredStyle: .alert)
- let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (_) in
- let shopmodel: YCOrderShopModel = self.dataArr[0] as! YCOrderShopModel
- let arr: [YCOrderGoodsModel] = shopmodel.goodsArr!
- if arr.count > 0 {
- let goodmodel: YCOrderGoodsModel = arr[sender.tag]
- //删除选中的数组
- if goodmodel.goodsIsSelect {
- let xxtempArr: NSMutableArray = self.selectedArr[0] as! NSMutableArray
- if xxtempArr.contains(goodmodel) {
- xxtempArr.remove(goodmodel)
- }
- }
- //删除数据源
- var tempArr: [YCOrderGoodsModel] = NSMutableArray.init(array: arr) as! [YCOrderGoodsModel]
- let xxIndex: Int = tempArr.firstIndex(of: goodmodel)!
- var str = ""
- let ycmodel = tempArr[xxIndex]
- str = ycmodel.Id!
- self.delect(str)
- tempArr.remove(at: xxIndex)
- shopmodel.goodsArr = tempArr
- }
- //更新数据
- self.updateData()
- }
- let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
- alert.addAction(confirmaction)
- alert.addAction(cancleAction)
- self.present(alert, animated: true, completion: nil)
- }
- func delect(_ string: String) {
- let params = NSMutableDictionary()
- params.setValue(string, forKey: "DATA_IDS")
- let http = AFHTTPSessionManager()
- http.post(RequestURL.deleteProductOrder, 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 {
- SVProgressHUD.showSuccess(withStatus: "删除成功!")
- self.initData()
- }
- }) { (_, _) in
- }
- }
- func initData() {
- let params = NSMutableDictionary()
- params.setValue(CommonValue.getUserId(), forKey: "userId")
- let http = AFHTTPSessionManager()
- http.get(RequestURL.getProductOrderList, 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 {
- self.dataArr.removeAllObjects()
- self.selectedArr.removeAllObjects()
- let dataArr: NSArray = (json as! NSDictionary).object(forKey: "data") as! NSArray
- if dataArr.count>0 {
- let dict: NSDictionary = dataArr[0] as! NSDictionary
- let shopModel = YCOrderShopModel.init()
- shopModel.setValue(dataArr, forKey: "commoditys")
- shopModel.setValue(dict["shop_id"], forKey: "shopName")
- self.dataArr.add(shopModel)
- }
- self.myTableView.reloadData()
- //初始化选择的数组
- for _ in 0..<self.dataArr.count {
- let tempArr = NSMutableArray.init()
- self.selectedArr.add(tempArr)
- }
- }
- }) { (_, _) in
- }
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- self.tabBarController?.tabBar.backgroundColor = UIColor.white
- }
- }
- extension OrderController: UITableViewDelegate, UITableViewDataSource {
- func numberOfSections(in tableView: UITableView) -> Int {
- return self.dataArr.count
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- let shopModel: YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel
- return shopModel.goodsArr!.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell: YCOrderShopCartCell = tableView.dequeueReusableCell(withIdentifier: "YCOrderShopCartCell", for: indexPath) as! YCOrderShopCartCell
- cell.backgroundColor = UIColor.colorWithCustom(r: 242, g: 242, b: 242)
- let shopModel: YCOrderShopModel = self.dataArr[indexPath.section] as! YCOrderShopModel
- cell.xgoodModel = shopModel.goodsArr?[indexPath.row]
- cell.YCcellDelegate = self
- cell.delectBtn?.addTarget(self, action: #selector(delectGoods(_:)), for: UIControl.Event.touchUpInside)
- cell.delectBtn!.tag = indexPath.row
- return cell
- }
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 100.0
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: false)
- }
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- // let model:YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel
- // return model.goodsArr!.count > 0 ? 50 : 0
- return 0
- }
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- let model: YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel
- let headerview = YCOrderHeaderView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 50))
- headerview.shopModel = model
- headerview.index = section
- headerview.YCHeaderDelegate = self
- // return model.goodsArr!.count > 0 ? headerview : nil
- return nil
- }
- // func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
- // if editingStyle == .delete {
- // let alert = UIAlertController.init(title: "您确定要删除这个商品吗?", message: "", preferredStyle: .alert)
- // let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (UIAlertAction) in
- // let shopmodel:YCOrderShopModel = self.dataArr[indexPath.section] as! YCOrderShopModel
- // let arr:[YCOrderGoodsModel] = shopmodel.goodsArr!
- // if arr.count > 0{
- // let goodmodel:YCOrderGoodsModel = arr[indexPath.row]
- // //删除选中的数组
- // if goodmodel.goodsIsSelect {
- // let xxtempArr:NSMutableArray = self.selectedArr[indexPath.section] as! NSMutableArray
- // if xxtempArr.contains(goodmodel) {
- // xxtempArr.remove(goodmodel)
- // }
- // }
- // //删除数据源
- // var tempArr:[YCOrderGoodsModel] = NSMutableArray.init(array: arr) as! [YCOrderGoodsModel]
- // let xxIndex:Int = tempArr.firstIndex(of: goodmodel)!
- // tempArr.remove(at: xxIndex)
- // shopmodel.goodsArr = tempArr
- // }
- // //更新数据
- // self.updateData()
- // }
- // let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
- // alert.addAction(confirmaction)
- // alert.addAction(cancleAction)
- // self.present(alert, animated: true, completion: nil)
- // }
- // }
- // func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
- // return "删除"
- // }
- }
- // MARK: - - YCOrderShopCartCellDelegate
- extension OrderController: YCOrderShopCartCellDelegate {
- func shopCellDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) {
- print("点击cell,跳转商品详情")
- }
- func addBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) {
- self.disposeShopData(goodModel: goodModel, cell: cell, isAdd: true)
- }
- func minusBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) {
- self.disposeShopData(goodModel: goodModel, cell: cell, isAdd: false)
- }
- func leftSelBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell, isSel: Bool) {
- var shopModelIndex: Int?
- var goodModelIndex: Int?
- for shop_Index in 0..<self.dataArr.count {
- let shopmodel: YCOrderShopModel = self.dataArr[shop_Index] as! YCOrderShopModel
- let arr: [YCOrderGoodsModel] = shopmodel.goodsArr!
- if arr.count > 0 {
- for good_index in 0..<arr.count {
- let good_model: YCOrderGoodsModel = arr[good_index]
- if good_model == goodModel {
- good_model.goodsIsSelect = isSel
- shopModelIndex = self.dataArr.index(of: shopmodel)
- goodModelIndex = arr.firstIndex(of: good_model)
- let tempArr: NSMutableArray = self.selectedArr[shopModelIndex!] as! NSMutableArray
- if isSel {
- //装入数组
- if !tempArr.contains(goodModel) {
- tempArr.add(goodModel)
- if tempArr.count == shopmodel.goodsArr?.count {
- shopmodel.shopIsAllSelected = true
- } else {
- shopmodel.shopIsAllSelected = false
- }
- }
- } else {
- if tempArr.contains(goodModel) {
- tempArr.remove(goodModel)
- shopmodel.shopIsAllSelected = false
- }
- }
- }
- }
- }
- }
- self.updateData()
- }
- func disposeShopData(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell, isAdd: Bool) {
- var shopModelIndex: Int?
- var goodModelIndex: Int?
- for shop_Index in 0..<self.dataArr.count {
- let shopmodel: YCOrderShopModel = self.dataArr[shop_Index] as! YCOrderShopModel
- let arr: [YCOrderGoodsModel] = shopmodel.goodsArr!
- if arr.count > 0 {
- for good_index in 0..<arr.count {
- let good_model: YCOrderGoodsModel = arr[good_index]
- if good_model == goodModel {
- shopModelIndex = self.dataArr.index(of: shopmodel)
- goodModelIndex = arr.firstIndex(of: good_model)
- let tempArr: NSMutableArray = self.selectedArr[shopModelIndex!] as! NSMutableArray
- //是选中的那个
- var count: Int = good_model.goodsCount!
- if isAdd {
- if count == 99 {
- return
- }
- count += 1
- } else {
- if count == 1 {
- return
- }
- count -= 1
- }
- //更新选中数组中的个数
- if !tempArr.contains(good_model) {
- //如果没有加入的,加入选中数组
- goodModel.goodsIsSelect = true
- tempArr.add(goodModel)
- if tempArr.count == shopmodel.goodsArr?.count {
- shopmodel.shopIsAllSelected = true
- }
- }
- let xxindex: Int = tempArr.index(of: good_model)
- let good: YCOrderGoodsModel = tempArr[xxindex] as! YCOrderGoodsModel
- good.goodsCount = count
- }
- }
- }
- }
- self.updateData()
- }
- }
- // MARK: - - headerViewDelegate
- extension OrderController: YC_headerViewDelegate {
- func headerViewDidPress(index: NSInteger, shopmodel: YCOrderShopModel, isSel: Bool) {
- let selmodel: YCOrderShopModel = self.dataArr[index] as! YCOrderShopModel
- selmodel.shopIsAllSelected = isSel
- let arr: [YCOrderGoodsModel] = selmodel.goodsArr!
- for xindex in 0..<arr.count {
- let goodmodel: YCOrderGoodsModel = arr[xindex]
- goodmodel.goodsIsSelect = isSel
- }
- //存入选择数组
- let saveArr: NSMutableArray = self.selectedArr[index] as! NSMutableArray
- if isSel {
- saveArr.setArray(arr)
- } else {
- saveArr.removeAllObjects()
- }
- self.updateData()
- }
- }
- // MARK: - - FootViewDelegate
- extension OrderController: YC_FootViewDelegate {
- func selectedAllBtnDidPress(button: UIButton, isSel: Bool) {
- for xindex in 0..<self.dataArr.count {
- let shopmodel: YCOrderShopModel = self.dataArr[xindex] as! YCOrderShopModel
- let goodarr: NSArray = shopmodel.goodsArr! as NSArray
- if goodarr.count > 0 {
- for jindex in 0..<goodarr.count {
- let goodmodel: YCOrderGoodsModel = goodarr[jindex] as! YCOrderGoodsModel
- goodmodel.goodsIsSelect = isSel
- }
- }
- }
- //添加到选择数组
- if isSel {
- for kindex in 0..<self.dataArr.count {
- let tempArr: NSMutableArray = self.selectedArr[kindex] as! NSMutableArray
- let model: YCOrderShopModel = self.dataArr[kindex] as! YCOrderShopModel
- let arr: NSArray = model.goodsArr! as NSArray
- tempArr.setArray(arr as! [Any])
- }
- } else {
- for kindex in 0..<self.selectedArr.count {
- let tempArr: NSMutableArray = self.selectedArr[kindex] as! NSMutableArray
- tempArr.removeAllObjects()
- }
- }
- self.updateData()
- }
- //更新数据 --价格总计--tableviewUI变化
- func updateData() {
- self.updatePrice()
- self.myTableView.reloadData()
- }
- //更新价格
- func updatePrice() {
- //更新底部的价格
- self.footView?.updateArr(selectedArr: self.selectedArr, dataArr: self.dataArr)
- //判断每个店铺是否全选状态
- for lindex in 0..<self.dataArr.count {
- let model: YCOrderShopModel = self.dataArr[lindex] as! YCOrderShopModel
- let selectarr: NSMutableArray = self.selectedArr[lindex] as! NSMutableArray
- model.shopIsAllSelected = model.goodsArr?.count == selectarr.count ? true : false
- }
- }
- func addToShopCar() {
- let params = NSMutableDictionary()
- let array = self.selectedArr[0]as!NSArray
- if array.count == 0 {
- SVProgressHUD.showError(withStatus: "请选择商品!")
- return
- }
- var jsonStr = ""
- for i in 0..<array.count {
- let goodsModel = array[i]as! YCOrderGoodsModel
- let tojson = NSMutableDictionary()
- tojson.setValue(CommonValue.getUserId(), forKey: "accountId")
- tojson.setValue(goodsModel.productId, forKey: "productId")
- tojson.setValue(goodsModel.goodsCount, forKey: "num")
- tojson.setValue("12345", forKey: "shopId")
- tojson.setValue(goodsModel.goodsPrice, forKey: "price")
- if i == 0 {
- jsonStr = self.dicValueString(tojson as! [String: Any])!
- } else {
- jsonStr = "\(jsonStr),\(self.dicValueString(tojson as! [String: Any])!)"
- }
- }
- params.setValue("[\(jsonStr)]", forKey: "toJson")
- // loadDataList(url: url, params: params, tag: 1001, array: arrType)
- print(params)
- let http = AFHTTPSessionManager()
- http.post(RequestURL.addProductOrder, 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 vc = CreatShopOrderViewController()
- let array = self.selectedArr[0]as!NSArray
- vc.goodsArray = array
- self.toViewController(viewController: vc)
- }
- }) { (_, _) in
- }
- }
- func dicValueString(_ dic: [String: Any]) -> String? {
- let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
- var str = String(data: data!, encoding: String.Encoding.utf8)
- str = str!.replacingOccurrences(of: "\"", with: "'")
- print(str!)
- return str
- }
- }
|