//
|
// WelfareVC.swift
|
// WanPai
|
//
|
// Created by 杨锴 on 2023/6/8.
|
//
|
|
import UIKit
|
import JQTools
|
import QMUIKit
|
|
let UpdateWelfare_Noti = Notification.Name.init("UpdateWelfare_Noti")
|
|
class WelfareVC: BaseVC {
|
|
@IBOutlet weak var img_userProfile: UIImageView!
|
@IBOutlet weak var label_username: UILabel!
|
@IBOutlet weak var btn_vip: QMUIButton!
|
@IBOutlet weak var label_coin: UILabel!
|
@IBOutlet weak var label_score: UILabel!
|
@IBOutlet weak var scrollView: UIScrollView!
|
@IBOutlet weak var coinCollectionView: UICollectionView!
|
@IBOutlet weak var btn_coupon: UIButton!
|
@IBOutlet weak var btn_shopping: UIButton!
|
// @IBOutlet weak var btn_shoppping: UIButton!
|
// @IBOutlet weak var btn_weekly: UIButton!
|
// @IBOutlet weak var btn_todayFree: UIButton!
|
|
private var timerOffsetX:Double = 0
|
private let cellW = 144.0
|
private var benefitHomeModel:BenefitHomeModel?
|
private var updateProdDate:Date?
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
getData()
|
}
|
|
override func viewDidAppear(_ animated: Bool) {
|
super.viewDidAppear(animated)
|
if let date = updateProdDate{
|
//防止反复切换,15秒
|
if Date().timeIntervalSince1970 - date.timeIntervalSince1970 > 15{
|
getData()
|
}
|
}
|
}
|
|
override func setUI() {
|
scrollView.contentInsetAdjustmentBehavior = .never
|
coinCollectionView.delegate = self
|
coinCollectionView.dataSource = self
|
coinCollectionView.register(UINib(nibName: "WelfareCoinCCell", bundle: nil), forCellWithReuseIdentifier: "_WelfareCoinCCell")
|
}
|
|
override func setRx() {
|
NotificationCenter.default.rx.notification(UpdateWelfare_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] noti in
|
self?.getData()
|
}).disposed(by: disposeBag)
|
}
|
|
private func getData(){
|
Services.benefitHome().subscribe(onNext: {[weak self] data in
|
if let model = data.data{
|
self?.benefitHomeModel = model
|
self?.img_userProfile.sd_setImage(with: URL(string: model.userHeadImg))
|
self?.label_username.text = model.userName.isEmpty ? "未命名":model.userName
|
self?.btn_vip.isHidden = model.isMember != "年度会员"
|
self?.label_coin.text = model.wpCoin.currencyNotPrefix()
|
self?.label_score.text = "\(model.userIntegral)"
|
self?.coinCollectionView.reloadData()
|
|
if let coupon = model.image?.myConpons{
|
self?.btn_coupon.sd_setImage(with: URL(string: coupon), for: .normal, placeholderImage: nil,context: nil)
|
}
|
|
if let onlineShop = model.image?.onlineShop{
|
self?.btn_shopping.sd_setImage(with: URL(string: onlineShop), for: .normal, placeholderImage: nil,context: nil)
|
}
|
|
|
if let weeksBenefit = model.image?.weeksBenefit{
|
// self?.btn_weekly.sd_setImage(with: URL(string: weeksBenefit), for: .normal, placeholderImage: nil, context: nil)
|
}
|
|
if let todayFree = model.image?.todayFree{
|
// self?.btn_todayFree.sd_setImage(with: URL(string: todayFree), for: .normal, placeholderImage: nil, context: nil)
|
}
|
}
|
}) { error in
|
|
}.disposed(by: disposeBag)
|
}
|
|
@IBAction func userProfileAction(_ sender: UIButton) {
|
let vc = ProfileVC()
|
push(vc: vc)
|
}
|
|
@IBAction func billAction(_ sender: UIButton) {
|
let vc = WelfareBillListVC()
|
push(vc: vc)
|
}
|
|
@IBAction func shoppingAction(_ sender: UIButton) {
|
CommonAlertView.show(title: "提示", content: "即将打开京东,是否继续?") { status in
|
if status{
|
|
if UIApplication.shared.canOpenURL(URL(string: "openApp.jdMobile://")!){
|
let url = "openApp.jdMobile://virtual?params={\"category\":\"jump\",\"des\":\"jshopMain\",\"shopId\":\"15995015\",\"sourceType\":\"APP\",\"sourceValue\":\"1000\",\"landPageId\":\"iOS\"}"
|
UIApplication.shared.open(URL(string: url)!)
|
}else{
|
let url = "https://shop.m.jd.com/shop/home?shopId=15995015"
|
UIApplication.shared.open(URL(string: url)!)
|
}
|
|
|
}
|
}
|
}
|
|
@IBAction func couponsAction(_ sender: UIButton) {
|
let vc = WelfareCouponsListVC()
|
push(vc: vc)
|
}
|
|
@IBAction func rechargeAction(_ sender: UIButton) {
|
let vc = RechargeRecordVC(coin: benefitHomeModel?.wpCoin ?? 0,subtype: .coin)
|
push(vc: vc)
|
}
|
|
@IBAction func welfareWeeklyAction(_ sender: UIButton) {
|
let vc = WelfareWeeklyListVC()
|
push(vc: vc)
|
}
|
|
@IBAction func freeTodayAction(_ sender: UIButton) {
|
if let storeStr = UserDefaults.standard.object(forKey: "CurrentStore") as? String{
|
if let deserModel = HomeStoreModel.deserialize(from: storeStr){
|
Services.queryStoreFreeBenefit(id: deserModel.storeId).subscribe(onNext: {[weak self] data in
|
if let m = data.data{
|
let vc = WelfareFreeVC(m)
|
self?.push(vc: vc)
|
}
|
}).disposed(by: disposeBag)
|
}else{
|
alert(msg: "门店获取失败")
|
}
|
}else{
|
alert(msg: "门店获取失败")
|
}
|
}
|
|
// @IBAction func storeAction(_ sender: Any) {
|
// let vc = CoinStoreCenterVC()
|
// vc.benefitHomeModel = benefitHomeModel
|
// push(vc: vc)
|
// }
|
|
override var preferredStatusBarStyle: UIStatusBarStyle{
|
return .lightContent
|
}
|
}
|
|
extension WelfareVC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
let model = benefitHomeModel!.commodities[indexPath.row]
|
let vc = WelfareRedeemGoodsDetailVC(commodityId: model.commodityId, goodsType: model.goodsType)
|
push(vc: vc)
|
}
|
}
|
|
extension WelfareVC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
let resultCount = min(5,benefitHomeModel?.commodities.count ?? 0)
|
return resultCount
|
}
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let model = benefitHomeModel!.commodities[indexPath.row]
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_WelfareCoinCCell", for: indexPath) as! WelfareCoinCCell
|
cell.label_name.text = model.commodityName
|
|
switch model.redemptionMethod {
|
case 1: //积分
|
cell.label_price.text = "\(model.integral)积分"
|
case 2: //现金+积分
|
cell.label_price.text = "\(model.integral)积分+\(model.commodityPrice.currency())"
|
case 3://现金
|
cell.label_price.text = "\(model.commodityPrice.currency())"
|
default:
|
cell.label_price.text = ""
|
}
|
|
|
cell.img_cover.sd_setImage(with: URL(string: model.commodityImg), placeholderImage: nil)
|
return cell
|
}
|
}
|
|
extension WelfareVC:UICollectionViewDelegateFlowLayout{
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
return 0
|
}
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
return 0
|
}
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
|
return CGSize(width: cellW, height: 178)
|
}
|
}
|