OrderManagerVC.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // OrderManagerVC.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/8.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. import StripePaymentSheet
  11. class OrderManagerVC: ADHBaseVC {
  12. @IBOutlet weak var navHeight: NSLayoutConstraint!
  13. @IBOutlet weak var leftBtn: UIButton!
  14. @IBOutlet weak var rightBtn: UIButton!
  15. //白团
  16. @IBAction func leftAction(_ sender: UIButton) {
  17. self.leftBtn.isSelected = true
  18. self.rightBtn.isSelected = false
  19. self.leftBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
  20. self.rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  21. self.shopType = .whiteOrder
  22. self.viewModel.shopType = self.shopType
  23. self.getOrderList(true)
  24. }
  25. //普团
  26. @IBAction func rightAction(_ sender: UIButton) {
  27. self.leftBtn.isSelected = false
  28. self.rightBtn.isSelected = true
  29. // self.leftBtn.titleLabel?.font = UIFont.init(name: "Medium", size: 14)
  30. // self.rightBtn.titleLabel?.font = UIFont.init(name: "Medium", size: 16)
  31. self.leftBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  32. self.rightBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
  33. self.shopType = .generalOrder
  34. self.viewModel.shopType = self.shopType
  35. self.getOrderList(true)
  36. }
  37. //预览
  38. @IBAction func checkAction(_ sender: UIButton) {
  39. }
  40. @IBOutlet weak var btnSView: UIView!
  41. @IBOutlet var sliderBtnList: [UIButton]!
  42. @IBOutlet weak var tableView: UITableView!
  43. var shopType = OrderGeneraType.whiteOrder
  44. var orderType = OrderTypeEnumProtocol.willPay
  45. let viewModel : OrderViewModel = OrderViewModel.init()
  46. let diposableBag = DisposeBag()
  47. ///当前选择订单
  48. var nowOrderListModel : OrderListModel?
  49. /// 当前选中支付方式
  50. var payMentModel : PayMentTypeModel?
  51. /// 支付配置参数 后台返回
  52. var clientSecret : String!
  53. /// 支付使用商户名
  54. var merchantDisplayName : String!
  55. /// StripeSDK 类
  56. var paymentSheet: PaymentSheet?
  57. ///支付方式
  58. let userPayTypeView = Bundle.main.loadNibNamed("UserPayMentSheet", owner: nil, options: nil)?.last as? UserPayMentSheet
  59. /// 密码支付View
  60. let pswPayView = Bundle.main.loadNibNamed("PswPayView", owner: nil, options: nil)?.last as? PswPayView
  61. override func viewWillAppear(_ animated: Bool) {
  62. super.viewWillAppear(true)
  63. UIScrollView.appearance().isPagingEnabled = false
  64. }
  65. override func viewDidLoad() {
  66. super.viewDidLoad()
  67. self.navHeight.constant = kNavAndStatuHeight
  68. self.view.backgroundColor = UIColor.color(hex: "#F6F6F8")
  69. _ = NotificationCenter.default.rx.notification(Notification.Name(rawValue: "刷新订单列表")).takeUntil(self.rx.deallocated).subscribe(onNext:{[weak self] (notification) in
  70. self?.getOrderList(true)
  71. })
  72. self.tableView.register(withType: OrderListCell.self)
  73. self.tableView.contentInsetAdjustmentBehavior = .never
  74. self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in
  75. self.getOrderList(true)
  76. });
  77. self.tableView.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {[unowned self] in
  78. self.getOrderList(false)
  79. });
  80. self.tableView.ly_emptyView = LYEmptyView.emptyActionView(with: UIImage.init(named: "暂无数据"), titleStr: "暂无数据", detailStr: "", btnTitleStr: "点击刷新", btnClick: {[weak self] in
  81. self?.getOrderList(true)
  82. })
  83. self.viewModel.shopType = self.shopType
  84. self.viewModel.orderType = self.orderType
  85. //去支付
  86. self.userPayTypeView?.payTypeBlock = {[weak self] model in
  87. self?.payMentModel = model
  88. self?.getIsStripePay()
  89. }
  90. //密码支付
  91. self.pswPayView?.pswPayBlock = {[weak self] str in
  92. self?.pswPayAction(psw: str)
  93. }
  94. self.setBtnsActions()
  95. self.getOrderList(true)
  96. }
  97. //MARK: 获取列表
  98. func getOrderList(_ isFirstPage: Bool){
  99. self.hud.showLoading()
  100. self.viewModel.signalGetOrderList(isFirstPage)?.on(success: { dd in
  101. self.hud.hidden()
  102. self.tableView.reloadData()
  103. self.tableView.mj_header.endRefreshing()
  104. self.tableView.mj_footer.endRefreshing()
  105. }, error: { error in
  106. self.tableView.mj_header.endRefreshing()
  107. self.tableView.mj_footer.endRefreshing()
  108. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  109. })
  110. }
  111. //MARK: 取消订单
  112. func cancelOrder(){
  113. self.hud.showLoading()
  114. self.viewModel.cancelOrderModel.id = self.nowOrderListModel?.id
  115. self.viewModel.signalCancelOrder()?.on(success: { dd in
  116. self.hud.hidden()
  117. self.getOrderList(true)
  118. }, error: { error in
  119. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  120. })
  121. }
  122. //MARK: 获取支付方式列表
  123. func getPayMent(){
  124. self.hud.showLoading()
  125. self.viewModel.signalGetPayMentType()?.on(success: { dd in
  126. self.userPayTypeView?.configDataSource(dataSource: self.viewModel.payMentList)
  127. self.userPayTypeView?.show()
  128. self.hud.hidden()
  129. }, error: { error in
  130. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  131. })
  132. }
  133. //MARK: 是否拉起支付 或者密码支付
  134. func getIsStripePay(){
  135. self.hud.showLoading()
  136. self.viewModel.getIsStripePayModel.orderId = self.nowOrderListModel?.id
  137. self.viewModel.getIsStripePayModel.payTypeId = self.payMentModel?.id
  138. self.viewModel.signalGetIsStripePayRequest()?.on(success: { dd in
  139. let model : IsStripePayModel = self.viewModel.isStripePayModel!
  140. if model.pullUp === 1{
  141. self.getPayConfiguration()
  142. }else{
  143. //密码支付
  144. self.pswPayView?.show()
  145. }
  146. self.hud.hidden()
  147. }, error: { error in
  148. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  149. })
  150. }
  151. //MARK: 密码支付
  152. func pswPayAction(psw : String!){
  153. self.hud.showLoading()
  154. self.viewModel.pswPayModel.orderId = self.nowOrderListModel?.id
  155. self.viewModel.pswPayModel.payPassword = psw
  156. self.viewModel.signalPSWPay()?.on(success: { dd in
  157. self.hud.hidden()
  158. self.getOrderList(true)
  159. NotificationCenter.default.post(name: NSNotification.Name("刷新个人中心"), object: self, userInfo:nil)
  160. }, error: { error in
  161. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  162. })
  163. }
  164. //MARK: 获取支付配置数据
  165. func getPayConfiguration(){
  166. self.hud.showLoading()
  167. self.viewModel.getPayConfigurationModel.orderId = self.nowOrderListModel?.id
  168. self.viewModel.signalGetPayConfiguration()?.on(success: { dd in
  169. self.clientSecret = self.viewModel.payConfiguration?.clientSecret
  170. self.merchantDisplayName = self.viewModel.payConfiguration?.merchantDisplayName
  171. self.hud.hidden()
  172. self.configurationPaymentSheet()
  173. }, error: { error in
  174. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  175. })
  176. }
  177. // MARK: 拉起支付
  178. func configurationPaymentSheet(){
  179. NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil)
  180. let paymentIntentClientSecret = self.clientSecret
  181. let publishableKey = "pk_test_51O3DvzBqBnSGwj1K0ePpneI04LzHX5e9JC0BYqLXfnEqhJJgaSdtUM7ppv3YaK0vTdJ5mUTSLoG84zIILjfsyglz00Cgl4haJ3"
  182. STPAPIClient.shared.publishableKey = publishableKey
  183. // MARK: Create a PaymentSheet instance
  184. var configuration = PaymentSheet.Configuration()
  185. configuration.merchantDisplayName = self.merchantDisplayName
  186. // configuration.customer = .init(id: customerId, ephemeralKeySecret: customerEphemeralKeySecret)
  187. configuration.allowsDelayedPaymentMethods = true
  188. self.paymentSheet = PaymentSheet(paymentIntentClientSecret: paymentIntentClientSecret!, configuration: configuration)
  189. self.didTapCheckoutButton()
  190. }
  191. func didTapCheckoutButton() {
  192. paymentSheet?.present(from: self) { paymentResult in
  193. // MARK: Handle the payment result
  194. switch paymentResult {
  195. case .completed:
  196. print("Your order is confirmed")
  197. case .canceled:
  198. print("Canceled!")
  199. case .failed(let error):
  200. print("Payment failed: \(error)")
  201. }
  202. }
  203. }
  204. /// UI交互设置 头部
  205. func setBtnsActions(){
  206. self.btnSView.addSubview(self.lineView)
  207. for i in 0..<self.sliderBtnList.count{
  208. let btn = self.sliderBtnList[i]
  209. if i == 0{
  210. self.lineView.isHidden = false
  211. self.lineView.frame = CGRect(x: btn.frame.maxX - kSCREEN_WIDTH/5/2-15, y: 42.67, width: 30, height: 2.5)
  212. }
  213. btn.tag = i
  214. btn.addTarget(self, action: #selector(sliderAction(sender: )), for: .touchUpInside)
  215. }
  216. }
  217. @objc func sliderAction(sender : UIButton){
  218. if sender.tag == 0{
  219. self.orderType = .willPay
  220. }
  221. if sender.tag == 1{
  222. self.orderType = .takeOrdering
  223. }
  224. if sender.tag == 2{
  225. self.orderType = .finish
  226. }
  227. if sender.tag == 3{
  228. self.orderType = .delivery
  229. }
  230. if sender.tag == 4{
  231. self.orderType = .all
  232. }
  233. self.viewModel.orderType = self.orderType
  234. UIView.animate(withDuration: 0.2) {
  235. self.lineView.frame = CGRect(x: sender.frame.maxX - kSCREEN_WIDTH/5/2-15, y: 42.67, width: 30, height: 2.5)
  236. self.lineView.layoutIfNeeded()
  237. }
  238. self.getOrderList(true)
  239. }
  240. //MARK: collectionView
  241. lazy var lineView : UIView = {
  242. let lineView = UIScrollView.init(frame: CGRect(x: 0, y: 42.67, width: 60, height: 2.5))
  243. lineView.backgroundColor = kThemeColor
  244. lineView.isHidden = true
  245. return lineView
  246. }()
  247. }
  248. extension OrderManagerVC : UITableViewDelegate,UITableViewDataSource{
  249. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  250. return self.viewModel.dataList?.count ?? 0
  251. }
  252. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  253. let cell = tableView.dequeueReusableCell(withType: OrderListCell.self)
  254. let model : OrderListModel = self.viewModel.dataList![indexPath.row]
  255. cell.configModel(model: model)
  256. cell.contentView.backgroundColor = UIColor.clear
  257. cell.callBackBlock = {[weak self] type in
  258. self?.nowOrderListModel = model
  259. if type == 1{
  260. self?.userPayTypeView?.priceLab.text = "\(self?.nowOrderListModel!.orderPrice ?? "")"
  261. self?.getPayMent()
  262. }
  263. if type == 2{
  264. self?.cancelOrder()
  265. }
  266. }
  267. return cell
  268. }
  269. func numberOfSections(in tableView: UITableView) -> Int {
  270. return 1
  271. }
  272. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  273. return 0.01
  274. }
  275. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  276. let model : OrderListModel = self.viewModel.dataList![indexPath.row]
  277. let vc = OrderDetailViewController()
  278. vc.orderId = model.id
  279. self.navigationController?.pushViewController(vc, animated: true)
  280. }
  281. }