OderDetailHeadView.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. //
  2. // OderDetailHeadView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/11/4.
  6. //
  7. import UIKit
  8. class OderDetailHeadView: UIView {
  9. @IBOutlet weak var btnView: UIView!
  10. /// 时间提示View
  11. @IBOutlet weak var timeView: UIView!
  12. /// 介绍状态View
  13. @IBOutlet weak var detailView: UIView!
  14. /// 配送中View
  15. @IBOutlet weak var deliveringView: UIView!
  16. /// 介绍状态
  17. @IBOutlet weak var tipLab: UILabel!
  18. /// 二级介绍状态
  19. @IBOutlet weak var secTipLab: UILabel!
  20. /// 订单取消TypeLab
  21. @IBOutlet weak var cancelLab: UILabel!
  22. /// 配送
  23. @IBOutlet weak var deliveryView: UIView!
  24. /// 超时取消
  25. @IBOutlet weak var timeOutView: UIView!
  26. /// 去支付BtnV
  27. @IBOutlet weak var payBtnView: UIView!
  28. /// 地址BtnV
  29. @IBOutlet weak var addressBtnView: UIView!
  30. /// 再次购买BtnV
  31. @IBOutlet weak var buyAgainBtnView: UIView!
  32. /// 确认收货BtnV
  33. @IBOutlet weak var receivingBtnView: UIView!
  34. /// 去评价BtnV
  35. @IBOutlet weak var evaluateBtnView: UIView!
  36. /// 联系商家BtnV
  37. @IBOutlet weak var callBtnView: UIView!
  38. /// 取消订单BtnV
  39. @IBOutlet weak var cancelBtnView: UIView!
  40. /// 订单状态
  41. @IBOutlet weak var typeLab: UILabel!
  42. /// 下单时间
  43. @IBOutlet weak var makeOrdrtTime: UILabel!
  44. /// 配送时间
  45. @IBOutlet weak var deliveryTme: UILabel!
  46. /// 地址
  47. @IBOutlet weak var addressLab: UILabel!
  48. /// 邮编
  49. @IBOutlet weak var postCodeLab: UILabel!
  50. /// 电话
  51. @IBOutlet weak var phoneLab: UILabel!
  52. /// 收货人
  53. @IBOutlet weak var nameLab: UILabel!
  54. /// 1去支付 2修改地址 3再来一单 4确认收货 5去评价 6联系商家 7取消订单
  55. var headBtnBlock : ((Int)->Void)?
  56. var dataModel : OrderOutDetailModel?
  57. //进入后台时存储当前时间
  58. var becomeDeathTime: Date!
  59. //进入前台时存储当前时间
  60. var becomeActiveTime: Date!
  61. /// 计时器
  62. var codeTimer: DispatchSourceTimer!
  63. var time = 60
  64. var minutes = 0
  65. var second = 0
  66. override func awakeFromNib() {
  67. super.awakeFromNib()
  68. }
  69. func configModel(model : OrderOutDetailModel){
  70. self.dataModel = model
  71. self.makeOrdrtTime.text = "\(model.createTime ?? "")"
  72. self.deliveryTme.text = "\(model.deliveryTime ?? "")"
  73. self.addressLab.text = "\(model.detailAddress ?? "")"
  74. self.addressLab.sizeToFit()
  75. self.postCodeLab.text = "\(model.postcode ?? "")"
  76. self.phoneLab.text = "\(model.phoneNum ?? "")"
  77. self.nameLab.text = "\(model.receiving ?? "")"
  78. self.addressLab.sizeToFit()
  79. self.timeView.isHidden = true
  80. self.detailView.isHidden = true
  81. self.deliveryView.isHidden = true
  82. self.timeOutView.isHidden = true
  83. self.deliveringView.isHidden = true
  84. self.setBtnView()
  85. self.minutes = model.minutes ?? 0
  86. self.second = model.seconds ?? 0
  87. self.time = self.minutes * 60 + self.second
  88. if self.time > 0{
  89. self.codeCountdown(isCodeTimer: true)
  90. }
  91. }
  92. func setBtnView(){
  93. // ///改收货地址按钮,为0隐藏,为1显示
  94. // var alterAddress : String?
  95. // ///取消订单按钮,为0隐藏,为1显示
  96. // var cancel : String?
  97. // ///联系商家按钮,为0隐藏,为1显示
  98. // var contact : String?
  99. // ///确认收货按钮,为0隐藏,为1显示
  100. // var toConfirm : String?
  101. // /// 去支付按钮,为0隐藏,为1显示
  102. // var toPay : String?
  103. // ///去评价按钮,为0隐藏,为1显示
  104. // var toReview : String?
  105. self.buyAgainBtnView.isHidden = true
  106. var isShowBtn : Bool = false
  107. if self.dataModel?.alterAddress === 1{
  108. self.addressBtnView.isHidden = false
  109. isShowBtn = true
  110. }else{
  111. self.addressBtnView.isHidden = true
  112. }
  113. if self.dataModel?.cancel === 1{
  114. self.cancelBtnView.isHidden = false
  115. isShowBtn = true
  116. }else{
  117. self.cancelBtnView.isHidden = true
  118. }
  119. if self.dataModel?.toConfirm === 1{
  120. self.receivingBtnView.isHidden = false
  121. isShowBtn = true
  122. }else{
  123. self.receivingBtnView.isHidden = true
  124. }
  125. if self.dataModel?.toPay === 1{
  126. self.payBtnView.isHidden = false
  127. isShowBtn = true
  128. }else{
  129. self.payBtnView.isHidden = true
  130. }
  131. if self.dataModel?.toReview === 1{
  132. self.evaluateBtnView.isHidden = false
  133. isShowBtn = true
  134. }else{
  135. self.evaluateBtnView.isHidden = true
  136. }
  137. if self.dataModel?.contact === 1{
  138. self.callBtnView.isHidden = false
  139. isShowBtn = true
  140. }else{
  141. self.callBtnView.isHidden = true
  142. }
  143. let labHeight : CGFloat = (self.addressLab.frame.size.height)
  144. var btnViewHeight : CGFloat = 0
  145. if isShowBtn{
  146. self.btnView.isHidden = false
  147. btnViewHeight = 100
  148. }else{
  149. self.btnView.isHidden = true
  150. btnViewHeight = 0
  151. }
  152. if self.addressLab.text.isNotEmptyStr{
  153. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height:135 + labHeight + 30 + 100 + btnViewHeight + 100))
  154. }else{
  155. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height:155 + 30 + 100 + 100 + 100 + btnViewHeight))
  156. }
  157. let status : String = "\(self.dataModel!.orderStatus ?? "")"
  158. if status === 1{
  159. self.timeView.isHidden = false
  160. self.detailView.isHidden = false
  161. self.deliveryView.isHidden = true
  162. self.timeOutView.isHidden = true
  163. self.deliveringView.isHidden = true
  164. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "待支付", Etext: "Wait for payment")
  165. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "待支付,请立即支付", Etext: "Please pay as soon as possible")
  166. if self.addressLab.text.isNotEmptyStr{
  167. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeView.height + self.detailView.height + 135 + labHeight + btnViewHeight + 70))
  168. }else{
  169. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeView.height + self.detailView.height + 155 + btnViewHeight + 70))
  170. }
  171. }
  172. if status === 2{
  173. //待接单
  174. self.timeView.isHidden = true
  175. self.detailView.isHidden = false
  176. self.deliveryView.isHidden = true
  177. self.timeOutView.isHidden = true
  178. self.deliveringView.isHidden = true
  179. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "待商家接单", Etext: "Waiting fo accept the order")
  180. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "已成功下单,感谢您对KINA SENG的信任", Etext: "Order has been successfully placed, thank you for your trust in KINA SENG")
  181. self.secTipLab.text = ""
  182. if self.addressLab.text.isNotEmptyStr{
  183. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 135 + labHeight + btnViewHeight + 70))
  184. }else{
  185. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 155 + btnViewHeight + 70))
  186. }
  187. }
  188. if status === 3{
  189. //待配送
  190. self.timeView.isHidden = true
  191. self.detailView.isHidden = true
  192. self.deliveryView.isHidden = false
  193. self.timeOutView.isHidden = true
  194. self.deliveringView.isHidden = true
  195. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "订单待配送", Etext: "Waiting for delivery")
  196. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "", Etext: "")
  197. if self.addressLab.text.isNotEmptyStr{
  198. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.deliveryView.height + 135 + labHeight + btnViewHeight + 70))
  199. }else{
  200. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.deliveryView.height + 155 + btnViewHeight + 70))
  201. }
  202. }
  203. if status === 4{
  204. //已完成未评价
  205. self.timeView.isHidden = true
  206. self.detailView.isHidden = false
  207. self.deliveryView.isHidden = true
  208. self.timeOutView.isHidden = true
  209. self.deliveringView.isHidden = true
  210. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "订单已完成", Etext: "Order completed")
  211. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "感谢您对KINA SENG的信任,欢迎再次光临", Etext: "Thank you for your trust in KINA SENG and welcome to visit us again")
  212. self.secTipLab.text = ""
  213. if self.addressLab.text.isNotEmptyStr{
  214. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 135 + labHeight + btnViewHeight + 70))
  215. }else{
  216. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 155 + btnViewHeight + 70))
  217. }
  218. }
  219. if status === 5{
  220. //已取消
  221. self.timeView.isHidden = true
  222. self.detailView.isHidden = true
  223. self.deliveryView.isHidden = true
  224. self.timeOutView.isHidden = false //超时
  225. self.deliveringView.isHidden = true
  226. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "订单已取消", Etext: "Order cancelled")
  227. self.cancelLab.text = LanguagesUtil.createTextBy(Ctext: "该订单已经取消,期待您的下次光临", Etext: "The order has been cancelled and we look forward to your next visit")
  228. if self.addressLab.text.isNotEmptyStr{
  229. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeOutView.height + 135 + labHeight + btnViewHeight + 70))
  230. }else{
  231. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeOutView.height + 155 + btnViewHeight + 70))
  232. }
  233. }
  234. if status === 6{
  235. //退款中
  236. self.timeView.isHidden = true
  237. self.detailView.isHidden = true
  238. self.deliveryView.isHidden = true
  239. self.timeOutView.isHidden = false
  240. self.deliveringView.isHidden = true
  241. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "订单退款中", Etext: "Order refund process")
  242. if self.addressLab.text.isNotEmptyStr{
  243. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeOutView.height + 135 + labHeight + btnViewHeight + 70))
  244. }else{
  245. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeOutView.height + 155 + btnViewHeight + 70))
  246. }
  247. }
  248. if status === 7{
  249. //已退款
  250. self.timeView.isHidden = true
  251. self.detailView.isHidden = true
  252. self.deliveryView.isHidden = true
  253. self.timeOutView.isHidden = false
  254. self.deliveringView.isHidden = true
  255. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "订单已退款", Etext: "Order refunded")
  256. if self.addressLab.text.isNotEmptyStr{
  257. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeOutView.height + 135 + labHeight + btnViewHeight + 70))
  258. }else{
  259. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.timeOutView.height + 155 + btnViewHeight + 70))
  260. }
  261. }
  262. if status === 8{
  263. //完成已评价
  264. self.timeView.isHidden = true
  265. self.detailView.isHidden = false
  266. self.deliveryView.isHidden = true
  267. self.timeOutView.isHidden = true
  268. self.deliveringView.isHidden = true
  269. self.secTipLab.text = ""
  270. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "订单已完成", Etext: "Order completed")
  271. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "感谢您对KINA SENG的信任,欢迎再次光临", Etext: "Thank you for your trust in KINA SENG and welcome to visit us again")
  272. if self.addressLab.text.isNotEmptyStr{
  273. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 135 + labHeight + btnViewHeight + 70))
  274. }else{
  275. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 155 + btnViewHeight + 70))
  276. }
  277. }
  278. if status === 9{
  279. //已支付定金
  280. self.timeView.isHidden = true
  281. self.detailView.isHidden = false
  282. self.deliveryView.isHidden = true
  283. self.timeOutView.isHidden = true
  284. self.deliveringView.isHidden = true
  285. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "已支付定金", Etext: "Deposit paid")
  286. if self.dataModel?.isComplete === 1{
  287. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "定金尾款已结清", Etext: "The balance of the deposit has been settled")
  288. }else{
  289. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "请尽快支付剩余尾款:\(self.dataModel?.residual ?? "")", Etext: "Please pay the balance as soon as possible : \(self.dataModel?.residual ?? "")")
  290. }
  291. if self.addressLab.text.isNotEmptyStr{
  292. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 135 + labHeight + btnViewHeight + 70))
  293. }else{
  294. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 155 + btnViewHeight + 70))
  295. }
  296. }
  297. if status === 10{
  298. //延迟付款
  299. self.timeView.isHidden = true
  300. self.detailView.isHidden = false
  301. self.deliveryView.isHidden = true
  302. self.timeOutView.isHidden = true
  303. self.deliveringView.isHidden = true
  304. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "延迟付款", Etext: "Delayed payment")
  305. self.tipLab.text = LanguagesUtil.createTextBy(Ctext: "待支付,请立即支付", Etext: "Please pay as soon as possible")
  306. if self.addressLab.text.isNotEmptyStr{
  307. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 135 + labHeight + btnViewHeight + 70))
  308. }else{
  309. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 155 + btnViewHeight + 70))
  310. }
  311. }
  312. if status === 11{
  313. //延迟付款
  314. self.timeView.isHidden = true
  315. self.detailView.isHidden = true
  316. self.deliveryView.isHidden = true
  317. self.timeOutView.isHidden = true
  318. self.deliveringView.isHidden = false
  319. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "配送中", Etext: "On delivery")
  320. if self.addressLab.text.isNotEmptyStr{
  321. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 135 + labHeight + btnViewHeight + 70))
  322. }else{
  323. self.setFrameSize(CGSize.init(width: kSCREEN_WIDTH, height: 60 + self.detailView.height + 155 + btnViewHeight + 70))
  324. }
  325. }
  326. }
  327. /// 去支付
  328. /// - Parameter sender: <#sender description#>
  329. @IBAction func goToPayAction(_ sender: UIButton) {
  330. if self.headBtnBlock != nil{
  331. self.headBtnBlock!(1)
  332. }
  333. }
  334. /// 修改地址
  335. /// - Parameter sender: <#sender description#>
  336. @IBAction func addressAction(_ sender: UIButton) {
  337. if self.headBtnBlock != nil{
  338. self.headBtnBlock!(2)
  339. }
  340. }
  341. /// 再来一单
  342. /// - Parameter sender: <#sender description#>
  343. @IBAction func oneMoreAgain(_ sender: UIButton) {
  344. if self.headBtnBlock != nil{
  345. self.headBtnBlock!(3)
  346. }
  347. }
  348. /// 确认收货
  349. /// - Parameter sender: <#sender description#>
  350. @IBAction func confirmReceipt(_ sender: UIButton) {
  351. if self.headBtnBlock != nil{
  352. self.headBtnBlock!(4)
  353. }
  354. }
  355. /// 去评价
  356. /// - Parameter sender: <#sender description#>
  357. @IBAction func goToEvaluate(_ sender: UIButton) {
  358. if self.headBtnBlock != nil{
  359. self.headBtnBlock!(5)
  360. }
  361. }
  362. /// 联系商家
  363. /// - Parameter sender: <#sender description#>
  364. @IBAction func phoneCall(_ sender: UIButton) {
  365. if self.headBtnBlock != nil{
  366. self.headBtnBlock!(6)
  367. }
  368. }
  369. /// 取消订单
  370. /// - Parameter sender: <#sender description#>
  371. @IBAction func cancelOrderAction(_ sender: UIButton) {
  372. if self.headBtnBlock != nil{
  373. self.headBtnBlock!(7)
  374. }
  375. }
  376. func addNotification(isAdd:Bool) {
  377. if isAdd {
  378. //注册进入前台的通知
  379. NotificationCenter.default.addObserver(self, selector: #selector(becomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
  380. //注册进入后台的通知
  381. NotificationCenter.default.addObserver(self, selector: #selector(becomeDeath), name: UIApplication.willResignActiveNotification, object: nil)
  382. } else {
  383. //移除通知
  384. NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
  385. NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
  386. }
  387. }
  388. @objc func becomeActive() {
  389. //存储进入前台时的时间
  390. becomeActiveTime = Date()
  391. if becomeDeathTime != nil {
  392. //计算出退出后台到进入前台的时间差
  393. let difference:Int = Int(becomeActiveTime.timeIntervalSince(becomeDeathTime))
  394. //DispatchSourceTimer退出后台时是暂停的,返回前台时,需要减去时间差
  395. time = time - difference
  396. }
  397. }
  398. @objc func becomeDeath() {
  399. //存储进入后台时的时间
  400. becomeDeathTime = Date()
  401. }
  402. //计时器**和注销的方法
  403. func codeCountdown(isCodeTimer:Bool) {
  404. if isCodeTimer {
  405. //添加监控退出到后台以及进入前台的通知
  406. addNotification(isAdd: true)
  407. //子线程创建计时器
  408. codeTimer = DispatchSource.makeTimerSource(flags: .init(rawValue: 0), queue: DispatchQueue.global())
  409. //dealine:开始执行时间 repeating:重复时间
  410. codeTimer.schedule(deadline: .now(), repeating: .milliseconds(1000))
  411. //添加计时器的事件
  412. codeTimer.setEventHandler { [self] in
  413. time = time - 1
  414. if time < 0 {
  415. //注销计时器
  416. codeTimer.cancel()
  417. //主线程更新UI
  418. DispatchQueue.main.async {
  419. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "剩余时间:", Etext: "Time remaining:")
  420. time = 0
  421. //刷新详情
  422. if self.headBtnBlock != nil{
  423. self.headBtnBlock!(100)
  424. }
  425. NotificationCenter.default.post(name: NSNotification.Name("刷新订单列表"), object: self, userInfo:nil)
  426. //移除通知
  427. addNotification(isAdd: false)
  428. }
  429. return
  430. }
  431. DispatchQueue.main.async {
  432. //分钟转换
  433. let min = self.time/60
  434. var minStr = "00"
  435. if min > 9{
  436. minStr = "\(min)"
  437. }else{
  438. minStr = "0\(min)"
  439. }
  440. //秒转换
  441. let sec = self.time % 60
  442. var secStr = "00"
  443. if sec > 9{
  444. secStr = "\(sec)"
  445. }else{
  446. secStr = "0\(sec)"
  447. }
  448. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "剩余时间:\(minStr):\(secStr)", Etext: "Time remaining:\(minStr):\(secStr)")
  449. }
  450. }
  451. //计时器**
  452. codeTimer.activate()
  453. }else{
  454. if codeTimer != nil {
  455. codeTimer.cancel()
  456. addNotification(isAdd: false)
  457. time = 0
  458. self.typeLab.text = LanguagesUtil.createTextBy(Ctext: "待支付", Etext: "Wait for payment")
  459. }
  460. }
  461. }
  462. }