123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- //
- // OrderManagerVC.swift
- // ADHTuanCan
- //
- // Created by 敖德亨 on 2023/10/8.
- //
- import UIKit
- import RxSwift
- import RxCocoa
- import StripePaymentSheet
- class OrderManagerVC: ADHBaseVC {
-
- @IBOutlet weak var navHeight: NSLayoutConstraint!
- @IBOutlet weak var leftBtn: UIButton!
-
- @IBOutlet weak var rightBtn: UIButton!
-
- //白团
- @IBAction func leftAction(_ sender: UIButton) {
- self.leftBtn.isSelected = true
- self.rightBtn.isSelected = false
- self.leftBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
- self.rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
- self.shopType = .whiteOrder
- self.viewModel.shopType = self.shopType
- self.getOrderList(true)
- }
- //普团
- @IBAction func rightAction(_ sender: UIButton) {
- self.leftBtn.isSelected = false
- self.rightBtn.isSelected = true
- // self.leftBtn.titleLabel?.font = UIFont.init(name: "Medium", size: 14)
- // self.rightBtn.titleLabel?.font = UIFont.init(name: "Medium", size: 16)
- self.leftBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
- self.rightBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
- self.shopType = .generalOrder
- self.viewModel.shopType = self.shopType
- self.getOrderList(true)
- }
-
- //预览
- @IBAction func checkAction(_ sender: UIButton) {
- }
-
- @IBOutlet weak var btnSView: UIView!
- @IBOutlet var sliderBtnList: [UIButton]!
-
- @IBOutlet weak var tableView: UITableView!
-
- var shopType = OrderGeneraType.whiteOrder
- var orderType = OrderTypeEnumProtocol.willPay
-
- let viewModel : OrderViewModel = OrderViewModel.init()
- let diposableBag = DisposeBag()
- ///当前选择订单
- var nowOrderListModel : OrderListModel?
-
- /// 当前选中支付方式
- var payMentModel : PayMentTypeModel?
-
- /// 支付配置参数 后台返回
- var clientSecret : String!
-
- /// 支付使用商户名
- var merchantDisplayName : String!
-
- /// StripeSDK 类
- var paymentSheet: PaymentSheet?
-
- ///支付方式
- let userPayTypeView = Bundle.main.loadNibNamed("UserPayMentSheet", owner: nil, options: nil)?.last as? UserPayMentSheet
-
- /// 密码支付View
- let pswPayView = Bundle.main.loadNibNamed("PswPayView", owner: nil, options: nil)?.last as? PswPayView
-
-
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(true)
- UIScrollView.appearance().isPagingEnabled = false
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- self.navHeight.constant = kNavAndStatuHeight
- self.view.backgroundColor = UIColor.color(hex: "#F6F6F8")
-
- _ = NotificationCenter.default.rx.notification(Notification.Name(rawValue: "刷新订单列表")).takeUntil(self.rx.deallocated).subscribe(onNext:{[weak self] (notification) in
- self?.getOrderList(true)
- })
-
-
- self.tableView.register(withType: OrderListCell.self)
- self.tableView.contentInsetAdjustmentBehavior = .never
- self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in
- self.getOrderList(true)
- });
- self.tableView.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {[unowned self] in
- self.getOrderList(false)
- });
- self.tableView.ly_emptyView = LYEmptyView.emptyActionView(with: UIImage.init(named: "暂无数据"), titleStr: "暂无数据", detailStr: "", btnTitleStr: "点击刷新", btnClick: {[weak self] in
- self?.getOrderList(true)
- })
-
- self.viewModel.shopType = self.shopType
- self.viewModel.orderType = self.orderType
-
- //去支付
- self.userPayTypeView?.payTypeBlock = {[weak self] model in
- self?.payMentModel = model
- self?.getIsStripePay()
- }
- //密码支付
- self.pswPayView?.pswPayBlock = {[weak self] str in
- self?.pswPayAction(psw: str)
- }
-
- self.setBtnsActions()
- self.getOrderList(true)
- }
-
-
- //MARK: 获取列表
- func getOrderList(_ isFirstPage: Bool){
- self.hud.showLoading()
-
- self.viewModel.signalGetOrderList(isFirstPage)?.on(success: { dd in
- self.hud.hidden()
- self.tableView.reloadData()
-
-
- self.tableView.mj_header.endRefreshing()
- self.tableView.mj_footer.endRefreshing()
- }, error: { error in
-
- self.tableView.mj_header.endRefreshing()
- self.tableView.mj_footer.endRefreshing()
- self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
- })
- }
-
- //MARK: 取消订单
- func cancelOrder(){
- self.hud.showLoading()
- self.viewModel.cancelOrderModel.id = self.nowOrderListModel?.id
- self.viewModel.signalCancelOrder()?.on(success: { dd in
- self.hud.hidden()
- self.getOrderList(true)
- }, error: { error in
- self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
- })
- }
-
- //MARK: 获取支付方式列表
- func getPayMent(){
- self.hud.showLoading()
- self.viewModel.signalGetPayMentType()?.on(success: { dd in
-
- self.userPayTypeView?.configDataSource(dataSource: self.viewModel.payMentList)
- self.userPayTypeView?.show()
-
- self.hud.hidden()
- }, error: { error in
- self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
- })
- }
-
- //MARK: 是否拉起支付 或者密码支付
- func getIsStripePay(){
- self.hud.showLoading()
- self.viewModel.getIsStripePayModel.orderId = self.nowOrderListModel?.id
- self.viewModel.getIsStripePayModel.payTypeId = self.payMentModel?.id
- self.viewModel.signalGetIsStripePayRequest()?.on(success: { dd in
- let model : IsStripePayModel = self.viewModel.isStripePayModel!
- if model.pullUp === 1{
- self.getPayConfiguration()
- }else{
- //密码支付
- self.pswPayView?.show()
- }
-
- self.hud.hidden()
- }, error: { error in
- self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
- })
- }
-
- //MARK: 密码支付
- func pswPayAction(psw : String!){
- self.hud.showLoading()
- self.viewModel.pswPayModel.orderId = self.nowOrderListModel?.id
- self.viewModel.pswPayModel.payPassword = psw
- self.viewModel.signalPSWPay()?.on(success: { dd in
-
- self.hud.hidden()
- self.getOrderList(true)
- NotificationCenter.default.post(name: NSNotification.Name("刷新个人中心"), object: self, userInfo:nil)
-
- }, error: { error in
- self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
- })
- }
-
- //MARK: 获取支付配置数据
- func getPayConfiguration(){
- self.hud.showLoading()
- self.viewModel.getPayConfigurationModel.orderId = self.nowOrderListModel?.id
- self.viewModel.signalGetPayConfiguration()?.on(success: { dd in
- self.clientSecret = self.viewModel.payConfiguration?.clientSecret
- self.merchantDisplayName = self.viewModel.payConfiguration?.merchantDisplayName
- self.hud.hidden()
- self.configurationPaymentSheet()
- }, error: { error in
- self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
- })
- }
-
- // MARK: 拉起支付
- func configurationPaymentSheet(){
-
- NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil)
- let paymentIntentClientSecret = self.clientSecret
- let publishableKey = "pk_test_51O3DvzBqBnSGwj1K0ePpneI04LzHX5e9JC0BYqLXfnEqhJJgaSdtUM7ppv3YaK0vTdJ5mUTSLoG84zIILjfsyglz00Cgl4haJ3"
-
- STPAPIClient.shared.publishableKey = publishableKey
- // MARK: Create a PaymentSheet instance
- var configuration = PaymentSheet.Configuration()
- configuration.merchantDisplayName = self.merchantDisplayName
- // configuration.customer = .init(id: customerId, ephemeralKeySecret: customerEphemeralKeySecret)
-
- configuration.allowsDelayedPaymentMethods = true
- self.paymentSheet = PaymentSheet(paymentIntentClientSecret: paymentIntentClientSecret!, configuration: configuration)
-
- self.didTapCheckoutButton()
- }
-
-
- func didTapCheckoutButton() {
- paymentSheet?.present(from: self) { paymentResult in
- // MARK: Handle the payment result
- switch paymentResult {
- case .completed:
- print("Your order is confirmed")
- case .canceled:
- print("Canceled!")
- case .failed(let error):
- print("Payment failed: \(error)")
- }
- }
- }
-
-
-
-
- /// UI交互设置 头部
- func setBtnsActions(){
- self.btnSView.addSubview(self.lineView)
- for i in 0..<self.sliderBtnList.count{
- let btn = self.sliderBtnList[i]
- if i == 0{
- self.lineView.isHidden = false
- self.lineView.frame = CGRect(x: btn.frame.maxX - kSCREEN_WIDTH/5/2-15, y: 42.67, width: 30, height: 2.5)
- }
- btn.tag = i
- btn.addTarget(self, action: #selector(sliderAction(sender: )), for: .touchUpInside)
- }
- }
-
- @objc func sliderAction(sender : UIButton){
- if sender.tag == 0{
- self.orderType = .willPay
- }
- if sender.tag == 1{
- self.orderType = .takeOrdering
- }
- if sender.tag == 2{
- self.orderType = .finish
- }
- if sender.tag == 3{
- self.orderType = .delivery
- }
- if sender.tag == 4{
- self.orderType = .all
- }
- self.viewModel.orderType = self.orderType
-
- UIView.animate(withDuration: 0.2) {
- self.lineView.frame = CGRect(x: sender.frame.maxX - kSCREEN_WIDTH/5/2-15, y: 42.67, width: 30, height: 2.5)
- self.lineView.layoutIfNeeded()
- }
- self.getOrderList(true)
- }
-
- //MARK: collectionView
- lazy var lineView : UIView = {
-
- let lineView = UIScrollView.init(frame: CGRect(x: 0, y: 42.67, width: 60, height: 2.5))
- lineView.backgroundColor = kThemeColor
- lineView.isHidden = true
- return lineView
-
- }()
- }
- extension OrderManagerVC : UITableViewDelegate,UITableViewDataSource{
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return self.viewModel.dataList?.count ?? 0
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withType: OrderListCell.self)
- let model : OrderListModel = self.viewModel.dataList![indexPath.row]
- cell.configModel(model: model)
- cell.contentView.backgroundColor = UIColor.clear
- cell.callBackBlock = {[weak self] type in
- self?.nowOrderListModel = model
- if type == 1{
- self?.userPayTypeView?.priceLab.text = "\(self?.nowOrderListModel!.orderPrice ?? "")"
- self?.getPayMent()
- }
-
- if type == 2{
- self?.cancelOrder()
- }
- }
- return cell
- }
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 0.01
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let model : OrderListModel = self.viewModel.dataList![indexPath.row]
- let vc = OrderDetailViewController()
- vc.orderId = model.id
- self.navigationController?.pushViewController(vc, animated: true)
- }
-
- }
|