123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- //
- // OrderDetailViewController.swift
- // ADHTuanCan
- //
- // Created by 敖德亨 on 2023/11/6.
- //
- import UIKit
- import RxSwift
- import RxCocoa
- import StripePaymentSheet
- class OrderDetailViewController: ADHBaseVC {
- @IBOutlet weak var topCont: NSLayoutConstraint!
- @IBOutlet weak var tableView: UITableView!
-
- /// 订单ID
- var orderId : NSNumber?{
- didSet{
- self.viewModel.getOrderDetailModel.id = orderId
- }
- }
-
- /// 购物车ID
- var shopcartId : NSNumber?{
- didSet{
- self.viewModel.getOrderDetailModel.id = shopcartId
- }
- }
-
- let viewModel : OrderViewModel = OrderViewModel.init()
- let diposableBag = DisposeBag()
-
- let headView = Bundle.main.loadNibNamed("OderDetailHeadView", owner: nil, options: nil)?.last as? OderDetailHeadView
- let footView = Bundle.main.loadNibNamed("OrderDetailFootView", owner: nil, options: nil)?.last as? OrderDetailFootView
-
- /// 当前选中支付方式
- 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
- ///呼叫客服
- let callView = Bundle.main.loadNibNamed("phoneCallView", owner: nil, options: nil)?.last as? phoneCallView
-
- ///修改地址
- let addrView = Bundle.main.loadNibNamed("selectAddressView", owner: nil, options: nil)?.last as? selectAddressView
-
- var section : [[ShopMealMsgDetailModel]?]?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // self.view.backgroundColor = kTBackgroundColor
- self.navBar = MCNavBarView.init(title: LanguagesUtil.createTextBy(Ctext: "订单详情", Etext: "Order detail"), subTitle: nil)
- self.topCont.constant = kNavAndStatuHeight
- // Do any additional setup after loading the view.
- self.tableView.register(withType: OrderReadyCell.self)
- self.tableView.contentInsetAdjustmentBehavior = .never
- self.tableView.tableHeaderView = self.headView
- self.tableView.tableFooterView = self.footView
- // self.tableView.backgroundColor = UIColor.clear
- self.headView?.isHidden = true
- self.footView?.isHidden = true
-
- ///type = 1去支付 2修改地址 3再来一单 4确认收货 5去评价 6联系商家 7取消订单
- self.headView?.headBtnBlock = {[unowned self] type in
- if type == 1{
- self.userPayTypeView?.priceLab.text = "\(self.viewModel.orderOutDataModel?.price ?? "")"
- self.getPayMent()
- }
- if type == 2{
- self.addrView?.show()
- }
- if type == 3{
-
- }
- if type == 4{
- self.confirmReceipt()
- }
- if type == 5{
- let vc = AddEvaluateViewController()
- vc.orderId = self.orderId
- self.navigationController?.pushViewController(vc, animated: true)
- }
- if type == 6{
- self.callView?.showWithPhone(phoneNum: "123")
- }
- if type == 7{
- self.cancelOrder()
- }
- }
-
- //去支付
- self.userPayTypeView?.payTypeBlock = {[weak self] model in
- self?.payMentModel = model
- self?.getIsStripePay()
- }
- //密码支付
- self.pswPayView?.pswPayBlock = {[weak self] str in
- self?.pswPayAction(psw: str)
- }
-
- //选择地址保存
- self.addrView?.saveBlock = {[unowned self] model in
- self.editOrderAddress(addModel: model)
- }
- //去添加地址
- self.addrView?.addAddressBlock = {[weak self] in
- let vc = AddressEditorVC()
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- //去修改地址 shopcartId绑定购物车的地址
- self.addrView?.editAddressBlock = {[unowned self] model in
- let vc = AddressEditorVC()
- vc.configDefualData(model: model)
- self.navigationController?.pushViewController(vc, animated: true)
- }
-
-
- self.getData()
- }
- func getData(){
- self.hud.showLoading()
- self.viewModel.signalGetOrderDetail()?.on(success: { dd in
- self.hud.hidden()
- self.headView?.isHidden = false
- self.footView?.isHidden = false
- self.headView?.configModel(model: self.viewModel.orderOutDataModel!)
- self.footView?.configModel(model: self.viewModel.orderOutDataModel!)
-
- self.section = []
-
- if let _ = self.viewModel.orderOutDataModel?.timeVoList{
- for item in self.viewModel.orderOutDataModel!.timeVoList! {
- var list : [ShopMealMsgDetailModel]? = []
- let DetailModel : OrderDetailModel = item
- if let _ = DetailModel.orderInsideMsgs{
- for item1 in DetailModel.orderInsideMsgs!{
- let insideMsgsModel = item1
- let zzwdata : [ShopMealMsgDetailModel] = insideMsgsModel.orderMealMsgVos!
- for i in 0..<zzwdata.count{
- let model : ShopMealMsgDetailModel = zzwdata[i]
- if i == 0{
- model.isFistLine = true
- }
- list?.append(model)
- }
- }
- }
- self.section?.append(list)
- }
- }
-
- self.tableView.reloadData()
- }, error: { error in
- self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
- })
- }
-
- //MARK: 取消订单
- func cancelOrder(){
- self.hud.showLoading()
- self.viewModel.cancelOrderModel.id = self.orderId
- self.viewModel.signalCancelOrder()?.on(success: { dd in
- self.hud.hidden()
- self.getData()
- 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 editOrderAddress(addModel : AddressModel){
- self.hud.showLoading()
- self.viewModel.editOrderAddressModel.orderId = self.orderId
- self.viewModel.editOrderAddressModel.addressId = addModel.id
- self.viewModel.signalEditOrderAddress()?.on(success: { dd in
- self.hud.hidden()
- self.getData()
- 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 confirmReceipt(){
- self.hud.showLoading()
- self.viewModel.confirmReceiptModel.id = self.orderId
- self.viewModel.signalConfirmReceipt()?.on(success: { dd in
- self.hud.hidden()
- self.getData()
- 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 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.orderId
- 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.orderId
- self.viewModel.pswPayModel.payPassword = psw
- self.viewModel.signalPSWPay()?.on(success: { dd in
- NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
- NotificationCenter.default.post(name: NSNotification.Name("刷新个人中心"), object: self, userInfo:nil)
- self.hud.hidden()
- self.getData()
-
- }, 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.orderId
- 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:
- self.getData()
- print("Your order is confirmed")
- NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
- case .canceled:
- print("Canceled!")
- case .failed(let error):
- print("Payment failed: \(error)")
- }
- }
- }
- }
- extension OrderDetailViewController : UITableViewDelegate,UITableViewDataSource{
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- let modelList = self.section![section]
- return modelList?.count ?? 0
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withType: OrderReadyCell.self)
-
- let modelList = self.section![indexPath.section]
- let model : ShopMealMsgDetailModel = modelList![indexPath.row]
- cell.configModel(model: model, isfirstLine: model.isFistLine)
- return cell
- }
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return self.section?.count ?? 0
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
-
- let dayModel : OrderDetailModel = (self.viewModel.orderOutDataModel?.timeVoList![section])!
- let sectionView = Bundle.main.loadNibNamed("OrderDetailSectionView", owner: nil, options: nil)?.last as? OrderDetailSectionView
- sectionView?.configModel(model: dayModel)
- return sectionView
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 50
- }
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.01
- }
- }
|