OrderDetailViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // OrderDetailViewController.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/11/6.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. import StripePaymentSheet
  11. class OrderDetailViewController: ADHBaseVC {
  12. @IBOutlet weak var topCont: NSLayoutConstraint!
  13. @IBOutlet weak var tableView: UITableView!
  14. /// 订单ID
  15. var orderId : NSNumber?{
  16. didSet{
  17. self.viewModel.getOrderDetailModel.id = orderId
  18. }
  19. }
  20. /// 购物车ID
  21. var shopcartId : NSNumber?{
  22. didSet{
  23. self.viewModel.getOrderDetailModel.id = shopcartId
  24. }
  25. }
  26. let viewModel : OrderViewModel = OrderViewModel.init()
  27. let diposableBag = DisposeBag()
  28. let headView = Bundle.main.loadNibNamed("OderDetailHeadView", owner: nil, options: nil)?.last as? OderDetailHeadView
  29. let footView = Bundle.main.loadNibNamed("OrderDetailFootView", owner: nil, options: nil)?.last as? OrderDetailFootView
  30. /// 当前选中支付方式
  31. var payMentModel : PayMentTypeModel?
  32. /// 支付配置参数 后台返回
  33. var clientSecret : String!
  34. /// 支付使用商户名
  35. var merchantDisplayName : String!
  36. /// StripeSDK 类
  37. var paymentSheet: PaymentSheet?
  38. ///支付方式
  39. let userPayTypeView = Bundle.main.loadNibNamed("UserPayMentSheet", owner: nil, options: nil)?.last as? UserPayMentSheet
  40. /// 密码支付View
  41. let pswPayView = Bundle.main.loadNibNamed("PswPayView", owner: nil, options: nil)?.last as? PswPayView
  42. ///呼叫客服
  43. let callView = Bundle.main.loadNibNamed("phoneCallView", owner: nil, options: nil)?.last as? phoneCallView
  44. ///修改地址
  45. let addrView = Bundle.main.loadNibNamed("selectAddressView", owner: nil, options: nil)?.last as? selectAddressView
  46. var section : [[ShopMealMsgDetailModel]?]?
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. // self.view.backgroundColor = kTBackgroundColor
  50. self.navBar = MCNavBarView.init(title: LanguagesUtil.createTextBy(Ctext: "订单详情", Etext: "Order detail"), subTitle: nil)
  51. self.topCont.constant = kNavAndStatuHeight
  52. // Do any additional setup after loading the view.
  53. self.tableView.register(withType: OrderReadyCell.self)
  54. self.tableView.contentInsetAdjustmentBehavior = .never
  55. self.tableView.tableHeaderView = self.headView
  56. self.tableView.tableFooterView = self.footView
  57. // self.tableView.backgroundColor = UIColor.clear
  58. self.headView?.isHidden = true
  59. self.footView?.isHidden = true
  60. ///type = 1去支付 2修改地址 3再来一单 4确认收货 5去评价 6联系商家 7取消订单
  61. self.headView?.headBtnBlock = {[unowned self] type in
  62. if type == 1{
  63. self.userPayTypeView?.priceLab.text = "\(self.viewModel.orderOutDataModel?.price ?? "")"
  64. self.getPayMent()
  65. }
  66. if type == 2{
  67. self.addrView?.show()
  68. }
  69. if type == 3{
  70. }
  71. if type == 4{
  72. self.confirmReceipt()
  73. }
  74. if type == 5{
  75. let vc = AddEvaluateViewController()
  76. vc.orderId = self.orderId
  77. self.navigationController?.pushViewController(vc, animated: true)
  78. }
  79. if type == 6{
  80. self.callView?.showWithPhone(phoneNum: "123")
  81. }
  82. if type == 7{
  83. self.cancelOrder()
  84. }
  85. }
  86. //去支付
  87. self.userPayTypeView?.payTypeBlock = {[weak self] model in
  88. self?.payMentModel = model
  89. self?.getIsStripePay()
  90. }
  91. //密码支付
  92. self.pswPayView?.pswPayBlock = {[weak self] str in
  93. self?.pswPayAction(psw: str)
  94. }
  95. //选择地址保存
  96. self.addrView?.saveBlock = {[unowned self] model in
  97. self.editOrderAddress(addModel: model)
  98. }
  99. //去添加地址
  100. self.addrView?.addAddressBlock = {[weak self] in
  101. let vc = AddressEditorVC()
  102. self?.navigationController?.pushViewController(vc, animated: true)
  103. }
  104. //去修改地址 shopcartId绑定购物车的地址
  105. self.addrView?.editAddressBlock = {[unowned self] model in
  106. let vc = AddressEditorVC()
  107. vc.configDefualData(model: model)
  108. self.navigationController?.pushViewController(vc, animated: true)
  109. }
  110. self.getData()
  111. }
  112. func getData(){
  113. self.hud.showLoading()
  114. self.viewModel.signalGetOrderDetail()?.on(success: { dd in
  115. self.hud.hidden()
  116. self.headView?.isHidden = false
  117. self.footView?.isHidden = false
  118. self.headView?.configModel(model: self.viewModel.orderOutDataModel!)
  119. self.footView?.configModel(model: self.viewModel.orderOutDataModel!)
  120. self.section = []
  121. if let _ = self.viewModel.orderOutDataModel?.timeVoList{
  122. for item in self.viewModel.orderOutDataModel!.timeVoList! {
  123. var list : [ShopMealMsgDetailModel]? = []
  124. let DetailModel : OrderDetailModel = item
  125. if let _ = DetailModel.orderInsideMsgs{
  126. for item1 in DetailModel.orderInsideMsgs!{
  127. let insideMsgsModel = item1
  128. let zzwdata : [ShopMealMsgDetailModel] = insideMsgsModel.orderMealMsgVos!
  129. for i in 0..<zzwdata.count{
  130. let model : ShopMealMsgDetailModel = zzwdata[i]
  131. if i == 0{
  132. model.isFistLine = true
  133. }
  134. list?.append(model)
  135. }
  136. }
  137. }
  138. self.section?.append(list)
  139. }
  140. }
  141. self.tableView.reloadData()
  142. }, error: { error in
  143. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  144. })
  145. }
  146. //MARK: 取消订单
  147. func cancelOrder(){
  148. self.hud.showLoading()
  149. self.viewModel.cancelOrderModel.id = self.orderId
  150. self.viewModel.signalCancelOrder()?.on(success: { dd in
  151. self.hud.hidden()
  152. self.getData()
  153. NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
  154. }, error: { error in
  155. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  156. })
  157. }
  158. //MARK: 修改订单地址
  159. func editOrderAddress(addModel : AddressModel){
  160. self.hud.showLoading()
  161. self.viewModel.editOrderAddressModel.orderId = self.orderId
  162. self.viewModel.editOrderAddressModel.addressId = addModel.id
  163. self.viewModel.signalEditOrderAddress()?.on(success: { dd in
  164. self.hud.hidden()
  165. self.getData()
  166. NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
  167. }, error: { error in
  168. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  169. })
  170. }
  171. //MARK: 修改订单地址
  172. func confirmReceipt(){
  173. self.hud.showLoading()
  174. self.viewModel.confirmReceiptModel.id = self.orderId
  175. self.viewModel.signalConfirmReceipt()?.on(success: { dd in
  176. self.hud.hidden()
  177. self.getData()
  178. NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
  179. }, error: { error in
  180. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  181. })
  182. }
  183. //MARK: 获取支付方式列表
  184. func getPayMent(){
  185. self.hud.showLoading()
  186. self.viewModel.signalGetPayMentType()?.on(success: { dd in
  187. self.userPayTypeView?.configDataSource(dataSource: self.viewModel.payMentList)
  188. self.userPayTypeView?.show()
  189. self.hud.hidden()
  190. }, error: { error in
  191. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  192. })
  193. }
  194. //MARK: 是否拉起支付 或者密码支付
  195. func getIsStripePay(){
  196. self.hud.showLoading()
  197. self.viewModel.getIsStripePayModel.orderId = self.orderId
  198. self.viewModel.getIsStripePayModel.payTypeId = self.payMentModel?.id
  199. self.viewModel.signalGetIsStripePayRequest()?.on(success: { dd in
  200. let model : IsStripePayModel = self.viewModel.isStripePayModel!
  201. if model.pullUp === 1{
  202. self.getPayConfiguration()
  203. }else{
  204. //密码支付
  205. self.pswPayView?.show()
  206. }
  207. self.hud.hidden()
  208. }, error: { error in
  209. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  210. })
  211. }
  212. //MARK: 密码支付
  213. func pswPayAction(psw : String!){
  214. self.hud.showLoading()
  215. self.viewModel.pswPayModel.orderId = self.orderId
  216. self.viewModel.pswPayModel.payPassword = psw
  217. self.viewModel.signalPSWPay()?.on(success: { dd in
  218. NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
  219. NotificationCenter.default.post(name: NSNotification.Name("刷新个人中心"), object: self, userInfo:nil)
  220. self.hud.hidden()
  221. self.getData()
  222. }, error: { error in
  223. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  224. })
  225. }
  226. //MARK: 获取支付配置数据
  227. func getPayConfiguration(){
  228. self.hud.showLoading()
  229. self.viewModel.getPayConfigurationModel.orderId = self.orderId
  230. self.viewModel.signalGetPayConfiguration()?.on(success: { dd in
  231. self.clientSecret = self.viewModel.payConfiguration?.clientSecret
  232. self.merchantDisplayName = self.viewModel.payConfiguration?.merchantDisplayName
  233. self.hud.hidden()
  234. self.configurationPaymentSheet()
  235. }, error: { error in
  236. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  237. })
  238. }
  239. // MARK: 拉起支付
  240. func configurationPaymentSheet(){
  241. NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil)
  242. let paymentIntentClientSecret = self.clientSecret
  243. let publishableKey = "pk_test_51O3DvzBqBnSGwj1K0ePpneI04LzHX5e9JC0BYqLXfnEqhJJgaSdtUM7ppv3YaK0vTdJ5mUTSLoG84zIILjfsyglz00Cgl4haJ3"
  244. STPAPIClient.shared.publishableKey = publishableKey
  245. // MARK: Create a PaymentSheet instance
  246. var configuration = PaymentSheet.Configuration()
  247. configuration.merchantDisplayName = self.merchantDisplayName
  248. // configuration.customer = .init(id: customerId, ephemeralKeySecret: customerEphemeralKeySecret)
  249. configuration.allowsDelayedPaymentMethods = true
  250. self.paymentSheet = PaymentSheet(paymentIntentClientSecret: paymentIntentClientSecret!, configuration: configuration)
  251. self.didTapCheckoutButton()
  252. }
  253. func didTapCheckoutButton() {
  254. paymentSheet?.present(from: self) { paymentResult in
  255. // MARK: Handle the payment result
  256. switch paymentResult {
  257. case .completed:
  258. self.getData()
  259. print("Your order is confirmed")
  260. NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
  261. case .canceled:
  262. print("Canceled!")
  263. case .failed(let error):
  264. print("Payment failed: \(error)")
  265. }
  266. }
  267. }
  268. }
  269. extension OrderDetailViewController : UITableViewDelegate,UITableViewDataSource{
  270. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  271. let modelList = self.section![section]
  272. return modelList?.count ?? 0
  273. }
  274. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  275. let cell = tableView.dequeueReusableCell(withType: OrderReadyCell.self)
  276. let modelList = self.section![indexPath.section]
  277. let model : ShopMealMsgDetailModel = modelList![indexPath.row]
  278. cell.configModel(model: model, isfirstLine: model.isFistLine)
  279. return cell
  280. }
  281. func numberOfSections(in tableView: UITableView) -> Int {
  282. return self.section?.count ?? 0
  283. }
  284. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  285. let dayModel : OrderDetailModel = (self.viewModel.orderOutDataModel?.timeVoList![section])!
  286. let sectionView = Bundle.main.loadNibNamed("OrderDetailSectionView", owner: nil, options: nil)?.last as? OrderDetailSectionView
  287. sectionView?.configModel(model: dayModel)
  288. return sectionView
  289. }
  290. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  291. return 50
  292. }
  293. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  294. return 0.01
  295. }
  296. }