Files
yingmeng-ios-switf/yinmeng-ios/Modules/Pay/UserPayViewController.swift
2024-03-04 14:14:33 +08:00

368 lines
15 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// UserPayViewController.swift
// yinmeng-ios
//
// Created by yinmeng on 2024/2/29.
//
import UIKit
import MBProgressHUD
class UserPayViewController: BaseViewController, HiddenNavigationBarProtocol{
var chooseModel:UserPayViewModel?
var orderId = ""
override func viewDidLoad() {
super.viewDidLoad()
setUILayout()
requestData()
NotificationCenter.default.addObserver(self, selector: #selector(receiveInitiatePaymentNot(not:)), name: NSNotification.Name(rawValue: "kInitiatePayment"), object: nil)
}
@objc func receiveInitiatePaymentNot(not:Notification) {
//
//
//
}
func requestData(){
RequestGet(path: "chargeprod/list", parma: ["channelType":"8"]) { data in
if let list = Deserialized<UserPayViewModel>.toArray(with: data) {
for (i,view) in self.viewList.enumerated() {
if i < list.count{
view.model = list[i]
self.chooseModel = list[0]
view.isHidden = false
}else{
view.isHidden = true
}
}
}
} fail: { code, data in
print(code)
}
RequestGet(path: "purse/query", parma: ["channelType":"8"]) { data in
if let model = Deserialized<UserPayViewModel>.toModel(with: data) {
self.pricView.text = model.diamonds
}
} fail: { code, data in
print(code)
}
}
@objc func clickPriceItemAction(tap:UITapGestureRecognizer){
for view in viewList {
if tap.view == view{
view.layer.borderWidth = 2
self.chooseModel = view.model
}else{
view.layer.borderWidth = 0
}
}
}
@objc func lookUpTap(){
let web = WebViewController(url: "/\(H5Utils.pay.rawValue)")
self.navigationController?.pushViewController(web, animated: true)
}
func setUILayout(){
view.addSubview(backgroundView)
view.addSubview(scrollView)
view.addSubview(backBtn)
scrollView.addSubview(headBgView)
headBgView.addSubview(iconView)
headBgView.addSubview(textVeiw)
headBgView.addSubview(pricView)
scrollView.addSubview(itemVeiw1)
scrollView.addSubview(itemVeiw2)
scrollView.addSubview(itemVeiw3)
scrollView.addSubview(itemVeiw4)
scrollView.addSubview(payAgreement)
scrollView.addSubview(publishBtn)
viewList.append(itemVeiw1)
viewList.append(itemVeiw2)
viewList.append(itemVeiw3)
viewList.append(itemVeiw4)
for (i,view) in viewList.enumerated() {
view.tag = i + 100
let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickPriceItemAction(tap:)))
view.addGestureRecognizer(tap)
}
let lookUpTap = UITapGestureRecognizer.init(target: self, action: #selector(lookUpTap))
payAgreement.addGestureRecognizer(lookUpTap)
scrollView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
backgroundView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
backBtn.snp.makeConstraints { make in
make.width.height.equalTo(30)
make.left.equalTo(16)
make.top.equalTo(StatusBarHeight + 12)
}
headBgView.snp.makeConstraints { make in
make.top.equalTo(self.backBtn.snp.bottom).offset(41)
make.width.equalTo(337)
make.centerX.equalTo(self.view)
make.height.equalTo(124)
}
iconView.snp.makeConstraints { make in
make.centerX.equalTo(self.headBgView).offset(-11)
make.width.height.equalTo(22)
make.top.equalTo(27)
}
textVeiw.snp.makeConstraints { make in
make.left.equalTo(self.iconView.snp.right).offset(6)
make.centerY.equalTo(self.iconView)
}
pricView.snp.makeConstraints { make in
make.top.equalTo(51)
make.centerX.equalTo(self.view)
}
itemVeiw1.snp.makeConstraints { make in
make.left.equalTo(self.headBgView.snp.left).offset(13)
make.width.equalTo(144)
make.height.equalTo(83)
make.top.equalTo(self.headBgView.snp.bottom).offset(19)
}
itemVeiw2.snp.makeConstraints { make in
make.left.equalTo(self.itemVeiw1.snp.right).offset(23)
make.width.equalTo(144)
make.height.equalTo(83)
make.top.equalTo(self.headBgView.snp.bottom).offset(19)
}
itemVeiw3.snp.makeConstraints { make in
make.left.equalTo(self.headBgView.snp.left).offset(13)
make.width.equalTo(144)
make.height.equalTo(83)
make.top.equalTo(self.itemVeiw1.snp.bottom).offset(20)
}
itemVeiw4.snp.makeConstraints { make in
make.left.equalTo(self.itemVeiw2)
make.width.equalTo(144)
make.height.equalTo(83)
make.top.equalTo(self.itemVeiw1.snp.bottom).offset(20)
}
payAgreement.snp.makeConstraints { make in
make.left.right.equalTo(self.view)
make.top.equalTo(self.itemVeiw4.snp.bottom).offset(40)
}
publishBtn.snp.makeConstraints { make in
make.width.equalTo(296)
make.height.equalTo(48)
make.centerX.equalTo(self.view)
make.top.equalTo(self.payAgreement.snp.bottom).offset(161)
}
publishBtn.superview?.layoutIfNeeded()
let y = publishBtn.y + publishBtn.frame.height
self.scrollView.contentSize = CGSize(width: ScreenWidth, height: y)
}
@objc func publishBtnAction(){
guard let chargeProdId = self.chooseModel?.chargeProdId else{
return
}
MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
let uid = "\(AuthManager.userUid)"
let ticket = AuthManager.ticket
let deviceInfo = UIDevice.current.identifierForVendor?.uuidString ?? ""
let clientIp = "0.0.0.0"
RequestPost(path: "storeKitV2/placeOrder", parma: ["chargeProdId":chargeProdId,"uid":uid,"ticket":ticket,"deviceInfo":deviceInfo]) { data in
if let _data = data as? [String:Any],let recordId = _data["recordId"] as? String,let appAccountToken = _data["appAccountToken"] as? String{
self.initiatePaymentAction(recordId: recordId, appAccountToken: appAccountToken)
}else{
HUDTool.show(with: "购买失败")
MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
}
} fail: { code, data in
HUDTool.show(with: data)
MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
}
}
deinit{
NotificationCenter.default.removeObserver(self)
}
func initiatePaymentAction(recordId:String,appAccountToken:String) {
self.orderId = recordId
if #available(iOS 15.0, *) {
UserPayViewManager.shared._ConditionBlock = {[weak self] (state , result) in
if state == .Pay || state == .Start || state == .VerifiedServer{
}else if state == .Unowned{
HUDTool.show(with: "出现未知错误,请重新尝试")
MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
}else{
HUDTool.show(with: "购买失败")
MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
}
if state == .VerifiedServer{
if let data = result,let transactionId = data["transactionId"]{
self?.paySuccess(transactionId:"\(transactionId)")
}else{
HUDTool.show(with: "购买失败")
MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
}
}
}
Task {
do {
try await UserPayViewManager.shared._demandCommodityThing(productId: self.chooseModel?.chargeProdId ?? "", uuid: appAccountToken)
}catch{
}
}
} else {
HUDTool.show(with: "充值失败。当前仅支持运行iOS15及以上系统的手机进行充值请升级系统版本后重试。")
}
}
func paySuccess(transactionId:String){
let uid = "\(AuthManager.userUid)"
let ticket = AuthManager.ticket
RequestPost(path: "storeKitV2/verifyOrder", parma: ["chargeRecordId":self.orderId,"chooseEnv":"true","uid":uid,"ticket":ticket,"transcationId":transactionId]) { data in
MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
if #available(iOS 15.0, *) {
Task {
try await UserPayViewManager.shared._verifyBusinessAccomplish(transaction: transactionId)
self.orderId = ""
RequestGet(path: "purse/query", parma: ["channelType":"8"]) { data in
if let model = Deserialized<UserPayViewModel>.toModel(with: data) {
self.pricView.text = model.diamonds
}
} fail: { code, data in
print(code)
}
}
}
} fail: { code, data in
if code == 1444{
Task {
if #available(iOS 15.0, *) {
try await UserPayViewManager.shared._verifyBusinessAccomplish(transaction: transactionId)
} else {
// Fallback on earlier versions
}
}
}
HUDTool.show(with: data)
MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
}
}
@objc func backBtnAction(){
self.navigationController?.popViewController(animated: true)
}
var scrollView:UIScrollView = {
let _scrollView = UIScrollView()
_scrollView.backgroundColor = .clear
return _scrollView
}()
var backBtn:UIButton = {
let _backBtn = UIButton()
_backBtn.setImage(UIImage(named: "yin_user_pay_arrow"), for: .normal)
_backBtn.addTarget(self, action: #selector(backBtnAction), for: .touchUpInside)
return _backBtn
}()
private lazy var backgroundView:UIImageView = {
let _backgroundView = UIImageView()
_backgroundView.image = UIImage(named: "yin_user_pay_bg")
_backgroundView.isUserInteractionEnabled = true
return _backgroundView
}()
var headBgView:UIImageView = {
let _headBgView = UIImageView()
_headBgView.image = UIImage(named: "yin_user_pay_head_bg")
return _headBgView
}()
var iconView:UIImageView = {
let _iconView = UIImageView()
_iconView.image = UIImage(named: "yin_user_pay_icon")
return _iconView
}()
var textVeiw:UILabel = {
let _textVeiw = UILabel()
_textVeiw.text = "余额"
_textVeiw.font = UIFont.systemFont(ofSize: 18, weight: .regular)
_textVeiw.textColor = .white
return _textVeiw
}()
var pricView: UILabel = {
let _pricView = UILabel()
_pricView.text = "0"
_pricView.textColor = .white
_pricView.font = UIFont.systemFont(ofSize: 34, weight: .semibold)
return _pricView
}()
var viewList:[UserPayViewItemView] = []
var itemVeiw1:UserPayViewItemView = {
let _itemVeiw1 = UserPayViewItemView.init(frame:.zero)
_itemVeiw1.layer.borderWidth = 2
return _itemVeiw1
}()
var itemVeiw2:UserPayViewItemView = {
let _itemVeiw2 = UserPayViewItemView.init(frame:.zero)
return _itemVeiw2
}()
var itemVeiw3:UserPayViewItemView = {
let _itemVeiw3 = UserPayViewItemView.init(frame:.zero)
return _itemVeiw3
}()
var itemVeiw4:UserPayViewItemView = {
let _itemVeiw4 = UserPayViewItemView.init(frame:.zero)
return _itemVeiw4
}()
var payAgreement:UILabel = {
let _payAgreement = UILabel()
let text = "已阅读并同意《用户充值协议》\n如有任何问题请咨询客服"
let attText = NSMutableAttributedString.init(string: text, attributes: [.foregroundColor:ThemeColor(hexStr: "#FFFFFF",alpha: 0.8),.font:UIFont.systemFont(ofSize: 13, weight: .regular)])
attText.addAttributes([.foregroundColor:ThemeColor(hexStr: "#5DA4F5"),.font:UIFont.systemFont(ofSize: 13, weight: .regular)], range: NSRange(location: 6, length: 8))
_payAgreement.textAlignment = .center
_payAgreement.attributedText = attText
_payAgreement.numberOfLines = 2
_payAgreement.isUserInteractionEnabled = true
return _payAgreement
}()
var publishBtn:UIButton = {
let _publishBtn = UIButton()
_publishBtn.setBackgroundImage(UIImage(named: "yin_hoem_voice_record_publish_btn"), for: .normal)
_publishBtn.setTitle("立即充值", for: .normal)
_publishBtn.setTitleColor(.white, for: .normal)
_publishBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
_publishBtn.addTarget(self, action: #selector(publishBtnAction), for: .touchUpInside)
return _publishBtn
}()
}