| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- //
- // PaymentOrderViewController.swift
- // MTP2_iOS
- //
- // Created by 曹晓亮 on 2020/12/10.
- // Copyright © 2020 Muchinfo. All rights reserved.
- //
- import UIKit
- import SwiftyAttributes
- import NVActivityIndicatorView
- import WHToast
- /// 填写订单视图容器控制类
- class PaymentOrderViewController: BaseViewController {
-
- // MARK: - 属性列表
- /// 提交订单
- @IBOutlet weak var payment: UIButton!
- /// 订单金额
- @IBOutlet weak var orderAmount: UILabel!
- /// 合计
- @IBOutlet weak var totalAmount: UILabel!
- /// 商品金额
- @IBOutlet weak var priceAmount: UILabel!
- /// 运费
- @IBOutlet weak var freight: UILabel!
- /// 商品价格
- @IBOutlet weak var orderPrice: UILabel!
- /// 商品名称
- @IBOutlet weak var goodsName: UILabel!
- /// 收货人姓名
- @IBOutlet weak var userName: UILabel!
- /// 收货人电话号码
- @IBOutlet weak var mobile: UILabel!
- /// 收货人地址信息
- @IBOutlet weak var address: UILabel!
- /// 供应商名称
- @IBOutlet weak var vendorName: UILabel!
- /// 支付方式
- @IBOutlet weak var payWay: UILabel!
- /// 商品图片
- @IBOutlet weak var goodsImage: UIImageView!
- /// 最大可购买数量
- @IBOutlet weak var maxQty: UILabel!
- /// 已选择优惠券
- @IBOutlet weak var hasCoupon: UILabel!
- /// 优惠券
- @IBOutlet weak var coupon: UIButton!
- /// 商品输入框
- @IBOutlet weak var qtyField: IBNumberEditText! {
- didSet {
- qtyField.valueType = 1
- qtyField.delegate = self
- }
- }
- /// 余额
- @IBOutlet weak var blance: UILabel!
-
- /// 商品信息
- override var takeInfo: Any? {
- didSet {}
- }
- /// 用户地址信息
- var userAddress: MoUserAddress? {
- didSet {
- /// 显示用户地址信息
- self.showReciveUserInfo()
- }
- }
- /// 优惠券
- var couponHold: MoMyCouponHold? {
- didSet {
- /// 异常
- if let obj = couponHold,
- let goods = takeInfo as? MoGoodsInfo {
- /// 已选择优惠券
- hasCoupon.attributedText = "已选优惠券".withTextColor(UIColorFromHex(rgbValue: 0x76c2dd))
- /// 现金券
- if obj.couponcategroy == 1 {
- coupon.setTitle("-\(goods.currencysign)\(obj.couponvalue.toString(reserve: 2))", for: .normal)
- } else if obj.couponcategroy == 2 {
- /// 折扣券
- /// 数量*金额*合约单位*折扣
- coupon.setTitle("-\(goods.currencysign)\((Double(qtyField.currentIntNumbers)*goods.orderprice*goods.agreeUnit*(1-obj.couponvalue)).toString(reserve: 2))", for: .normal)
- } else {
- /// 只能用于单个
- /// 数量*金额*合约单位*折扣
- coupon.setTitle("-\(goods.currencysign)\((goods.orderprice*goods.agreeUnit*(1-obj.couponvalue)).toString(reserve: 2))", for: .normal)
- }
- } else {
- coupon.setTitle("未使用", for: .normal)
- /// 已选择优惠券
- hasCoupon.attributedText = "".withTextColor(UIColorFromHex(rgbValue: 0x76c2dd))
- }
- /// 计算
- self.calculate()
- }
- }
- /// 选中的优惠券
- var rowNum: Int?
-
- // MARK: - 生命周期
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
-
- /// loding.......
- self.addLoadingView()
- /// 请求用户地址信息
- self.requsetQueryReciveUserInfo()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- /// 隐藏导航栏
- self.navigationController?.setNavigationBarHidden(false, animated: true)
- }
-
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
-
- /// 异常
- guard let goods = takeInfo as? MoGoodsInfo else { return }
- /// 商城商品明细
- self.requestQueryHsbyMarketGoodsDetail(goods.orderid)
- }
-
- // MARK: - 数据显示
- /// 商品信息显示
- fileprivate func showGoodsInfo(_ goods: MoGoodsInfo?) {
- /// 数据异常
- guard let obj = goods,
- let accountManager = MTP2BusinessCore.shared.accountManager,
- let taAccountInfo = accountManager.getCurrentTAAccountInfo() else { return }
-
- /// 商品名称
- goodsName.text = obj.goodsname
- /// 供应商名称
- vendorName.text = obj.vendorname
- /// 商品价格
- orderPrice.attributedText = (obj.currencysign.withFont(.font_12)+obj.orderprice.toString(reserve: obj.decimalplace).withFont(.font_16)).withTextColor(UIColorFromHex(rgbValue: 0x76c2dd))
- /// 输入步进值
- qtyField.incrementIntNum = obj.lotsize != 0 ? obj.lotsize : 1
- qtyField.currentIntNumbers = obj.lotsize != 0 ? obj.lotsize : 1
- qtyField.minIntNum = obj.lotsize != 0 ? obj.lotsize : 1
- /// 商品图片
- guard let url = StringUtils.getImageUrl(obj.picurls) else { return }
- goodsImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
-
- /// 查询资金信息
- accountManager.loginQueryReq(queryItem: "1,2") { (isComplete, error) in
- DispatchQueue.main.async {
- /// 期末余额
- let blance = taAccountInfo.currentBalance-(taAccountInfo.freezeMargin+taAccountInfo.otherFreezeMargin+taAccountInfo.freezeCharge)
- /// 最大可用数量
- self.qtyField.maxIntNum = obj.buymaxqty == 0 ? (obj.orderqty-obj.tradeqty) : min((obj.orderqty-obj.tradeqty), obj.buymaxqty)
- /// 最大可用数量
- self.maxQty.text = "最大可购买数量:\(self.qtyField.maxIntNum)"
- /// 余额
- self.blance.text = "可用余额:\(blance.toString(reserve: 2))"
- /// 请求优惠券
- self.requestQueryMyCoupons(Double(self.qtyField.currentIntNumbers)*obj.orderprice*obj.agreeUnit)
- }
- }
- }
-
- /// 显示收货人信息
- fileprivate func showReciveUserInfo() {
- /// 数据异常
- guard let userAddress = userAddress else { return }
- /// 收货人姓名
- userName.text = userAddress.receivername
- /// 收货人电话号码
- mobile.text = String(userAddress.phonenum)
- /// 收货人地址信息
- address.text = userAddress.provincename + (userAddress.cityname == "null" ? "" : userAddress.cityname) + (userAddress.districtname == "null" ? "" : userAddress.districtname) + userAddress.address
- }
-
- // MARK: - 交互相关
- /// 按钮点击交互相关
- /// - Parameter sender: sender
- @IBAction fileprivate func onButtonPressed(_ sender: UIControl) {
- switch sender {
- case payment: /// 付款下单
- doDelistingTrade()
- case coupon: /// 优惠券选择
- /// 异常
- guard let obj = takeInfo as? MoGoodsInfo else {
- return
- }
- let chooseCoupon = viewController(name: "Coupon", identifier: "ChooseCoupon") as! ChooseCouponViewController
- chooseCoupon.usedBlock = { (_ obj: MoMyCouponHold?, _ rowNum: Int?) in
- /// 优惠券
- self.couponHold = rowNum == nil ? nil : obj
- self.rowNum = rowNum
- }
- chooseCoupon.modalTransitionStyle = .crossDissolve
- chooseCoupon.modalPresentationStyle = .overFullScreen
- chooseCoupon.amount = Double(self.qtyField.currentIntNumbers)*obj.orderprice*obj.agreeUnit
- chooseCoupon.coupon = self.couponHold
- self.present(chooseCoupon, animated: true, completion: {
- chooseCoupon.takeInfo = obj
- })
- default:
- break
- }
- }
-
- /// onTapGestureRecognizer
- /// - Parameter sender: sender
- @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) {
- /// 地址切换
- let addressController = viewController(name: "Settings", identifier: "PickUpAddress") as! ReceiveGoodsInfoViewController
- addressController.delegate = self
- self.navigationController?.pushViewController(addressController, animated: true)
- }
-
- /// 计算相关
- fileprivate func calculate() {
- /// 数据异常
- guard let goods = takeInfo as? MoGoodsInfo,
- let accountManager = MTP2BusinessCore.shared.accountManager,
- let taAccountInfo = accountManager.getCurrentTAAccountInfo() else { return }
- /// 期末余额
- let blance = taAccountInfo.currentBalance-(taAccountInfo.freezeMargin+taAccountInfo.otherFreezeMargin+taAccountInfo.freezeCharge)
- /// 商品金额
- let amount = goods.goodsprice*goods.agreeUnit*Double(qtyField.currentIntNumbers)
-
- var tlAmount = 0.0
- /// 有优惠券
- if let obj = self.couponHold {
- if obj.couponcategroy == 1 {
- tlAmount = (amount - obj.couponvalue) > 0.0 ? (amount - obj.couponvalue) : 0.0
- } else if obj.couponcategroy == 2 {
- tlAmount = amount*obj.couponvalue
- /// 优惠金额 数量*金额*合约单位*折扣
- coupon.setTitle("-\(goods.currencysign)\((Double(qtyField.currentIntNumbers)*goods.goodsprice*goods.agreeUnit*(1-obj.couponvalue)).toString(reserve: 2))", for: .normal)
- } else {
- tlAmount = amount - (goods.goodsprice*goods.agreeUnit*(1-obj.couponvalue))
- }
- } else {
- tlAmount = amount
- }
- payWay.text = tlAmount <= blance ? "余额支付" : "线上支付"
- /// 商品金额
- priceAmount.attributedText = (goods.currencysign.withFont(.font_12)+amount.toString(reserve: goods.decimalplace).withFont(.font_16)).withTextColor(UIColorFromHex(rgbValue: 0x999999))
- /// 合计金额
- totalAmount.attributedText = "合计:\(goods.currencysign)".withFont(.font_12).withTextColor(UIColorFromHex(rgbValue: 0x999999))+tlAmount.toString(reserve: 2).withFont(.font_16).withTextColor(UIColorFromHex(rgbValue: 0xdf5958))
- /// 订单金额
- orderAmount.attributedText = "合计:\(goods.currencysign)".withFont(.font_12).withTextColor(UIColorFromHex(rgbValue: 0x999999))+tlAmount.toString(reserve: 2).withFont(.font_16).withTextColor(UIColorFromHex(rgbValue: 0xdf5958))
- }
-
- // MARK: - 接口请求
- /// 获取用户地址信息
- func requsetQueryReciveUserInfo() {
- /// 数据异常
- guard let deliveryManager = MTP2BusinessCore.shared.deliveryManager else { return }
- /// startAnimating
- _anim?.startAnimating()
- /// 请求用户地址信息
- deliveryManager.queryUserAddressInfo { (isSuccess, errorInfo, results) in
- DispatchQueue.main.async {
- self._anim?.stopAnimating()
- if isSuccess,
- let infos = results {
- /// 默认显示地址
- if let info = infos.first(where: {$0.isDefault}) {
- self.userAddress = info
- } else {
- self.userAddress = infos.first
- }
- }
- }
- }
- }
-
- /// 服务端查询商城商品详情请求方法
- /// - Parameter orderID: orderID
- fileprivate func requestQueryHsbyMarketGoodsDetail(_ orderID: String) {
- /// 异常
- guard let goodsManager = MTP2BusinessCore.shared.goodsManager else { return }
- /// startAnimating
- _anim?.startAnimating()
- /// 发送请求
- goodsManager.requestQueryHsbyMarketGoodsDetail(orderID, callback: { (isComplete, error, detail) in
- DispatchQueue.main.async {
- /// stopAnimating
- self._anim?.stopAnimating()
-
- if isComplete {
- /// 显示商品信息
- self.showGoodsInfo(detail)
- /// 计算
- self.calculate()
- } else {
- WHToast.showMessage("数据加载失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
- }
- }
- })
- }
-
- /// 查询我的优惠券
- fileprivate func requestQueryMyCoupons(_ amount: Double) {
- /// 异常
- guard let orderManager = MTP2BusinessCore.shared.orderManager,
- let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
-
- /// startAnimating
- _anim?.startAnimating()
- /// 查询数据
- orderManager.requestQueryMyCouponHolds(moGoodsInfo.goodsid,
- amount,
- moGoodsInfo.sellUserID,
- "1,2") { (isComplete, error, objs) in
- DispatchQueue.main.async {
- /// stopAnimating
- self._anim?.stopAnimating()
- /// 查询成功
- if isComplete {
- if objs?.count != 0 { self.couponHold = objs?.first }
- self.rowNum = 0
- } else {
- /// showError
- WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
- }
- }
- }
- }
-
- /// 预售下单请求
- fileprivate func doDelistingTrade() {
- /// 数据异常
- guard let tradeManager = MTP2BusinessCore.shared.tradeManager,
- let obj = takeInfo as? MoGoodsInfo,
- check(),
- let accountManager = MTP2BusinessCore.shared.accountManager,
- let taAccountInfo = accountManager.getCurrentTAAccountInfo() else { return }
- /// 构建交易参数
- let tradeParam = MoTradeParam()
- /// 买卖方向
- tradeParam.buyOrSell = .ORDERDIRECTION_BUY
- /// 数量
- tradeParam.orderQty = Double(qtyField.currentIntNumbers)
- /// 点选成交
- tradeParam.listingSelectType = .LISTINGSELECTTYPE_DELISTING
- /// 有效类型
- tradeParam.validType = .VALIDTYPE_DR
- /// 下单类型
- tradeParam.buildType = .BUILDTYPE_OPEN
- /// 时间有效类型
- tradeParam.timevalidType = .VALIDTYPE_YZ
- /// 市场编号
- tradeParam.marketID = obj.marketid
- /// 商品ID
- tradeParam.goodsID = obj.goodsid
- /// 关联单号
- tradeParam.relatedID = UInt64(obj.orderid) ?? 0
- /// 优惠券
- if let id = couponHold?.coupontypeid { tradeParam.couponTypeID = UInt64(id)}
- /// 优惠券
- if let id = userAddress?.autoid { tradeParam.receiveInfoID = UInt64(id)}
-
- /// 开启Loading
- NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "订单生成中......", type: .ballClipRotate, color: UIColor.loding(), backgroundColor: .clear), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION)
-
- tradeManager.tradeCommision(tradeParam: tradeParam) { [weak self] (isSuccess, error, orderNo) in
- DispatchQueue.main.async {
- /// dismisss loding.....
- NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION)
- if isSuccess {
- /// 期末余额
- let blance = taAccountInfo.currentBalance-(taAccountInfo.freezeMargin+taAccountInfo.otherFreezeMargin+taAccountInfo.freezeCharge)
- var tlAmount = 0.0
- let amount = obj.orderprice*obj.agreeUnit*Double(self?.qtyField.currentIntNumbers ?? 0)
- /// 有优惠券
- if let hold = self?.couponHold {
- if hold.couponcategroy == 1 { /// 现金券
- tlAmount = (amount - hold.couponvalue) > 0.0 ? (amount - hold.couponvalue) : 0.0
- } else if hold.couponcategroy == 2 { /// 折扣券
- tlAmount = amount*hold.couponvalue
- } else { /// 单张折扣券
- tlAmount = amount - (obj.orderprice*obj.agreeUnit*hold.couponvalue)
- }
- } else {
- tlAmount = amount
- }
-
- /// 如果可用金额不够
- if tlAmount > blance {
- WHToast.showSuccess(withMessage: "订单生成成功", duration: 1.5, finishHandler: {
- /// 异常
- guard let orderManager = MTP2BusinessCore.shared.orderManager else {
- return
- }
- /// 查询查询单据数据
- orderManager.requestQueryMyPayOrders(orderNo, nil) { (isComplete, error, payOrders) in
- DispatchQueue.main.async {
- if isComplete,
- payOrders?.count != 0 {
- /// 异常
- guard let paymentController = self?.viewController(name: "Order", identifier: "Payment") as? PaymentViewController else { return }
- /// 待付款单据
- paymentController.myPayOrder = payOrders?.first
- /// 页面跳转
- self?.navigationController?.pushViewController(paymentController, animated: true)
- } else {
- WHToast.showError(withMessage: "单据获取失败,请稍候重试!", duration: 1.5, finishHandler: {})
- }
- }
- }
- })
- } else{
- guard let viewController = UIStoryboard(name: "Order", bundle: nil).instantiateViewController(withIdentifier: "Packet") as? BaseViewController else { return }
- self?.navigationController?.pushViewController(viewController, animated: true)
- viewController.selectedIndex = 0
- }
- } else {
- WHToast.showError(withMessage: "订单生成失败,\(ErrorUtils.getDesc(errorCode: error?.retCode ?? -99999) ?? "未知错误")", duration: 1.5, finishHandler: {})
- }
- }
- }
- }
-
- /// 合规性校验
- /// - Returns: 是否合规
- fileprivate func check() -> Bool {
-
- /// 请先选择收获地址
- guard let _ = userAddress else {
- WHToast.showError(withMessage: "请先选择收获地址!", duration: 1.5, finishHandler: {})
- return false
- }
-
- /// 请输入委托数量
- if qtyField.currentIntNumbers == 0 {
- WHToast.showError(withMessage: "请输入购买数量", duration: 1.5, finishHandler: {})
- return false
- }
-
- /// 委托数量不能大于
- if qtyField.currentIntNumbers > qtyField.maxIntNum {
- WHToast.showError(withMessage: "购买数量应小于等于\(qtyField.maxIntNum)", duration: 1.5, finishHandler: {})
- return false
- }
- return true
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destination.
- // Pass the selected object to the new view controller.
- }
- */
- }
- // MARK: - IBNumberEditTextDelegate
- extension PaymentOrderViewController: IBNumberEditTextDelegate {
-
- func numberInputViewDidEndEditing(_ numberInputView: IBNumberEditText) {
- /// 重新获取优惠券
- if numberInputView == qtyField,
- let obj = takeInfo as? MoGoodsInfo {
- /// 请求优惠券
- requestQueryMyCoupons(Double(qtyField.currentIntNumbers)*obj.goodsprice*obj.agreeUnit)
- }
- }
-
- func numberInputView(_ numberInputView: IBNumberEditText, _ buttonType: NumberInputViewButtonType) {
- /// 重新获取优惠券
- if numberInputView == qtyField,
- let obj = takeInfo as? MoGoodsInfo {
- /// 请求优惠券
- requestQueryMyCoupons(Double(qtyField.currentIntNumbers)*obj.goodsprice*obj.agreeUnit)
- }
- /// 计算
- calculate()
- }
-
- func IBNumberEditText(textField: UITextField) {
- /// 计算
- calculate()
- }
- }
- // MARK: - ReceiveGoodsInfoViewControllerDelegate
- extension PaymentOrderViewController: ReceiveGoodsInfoViewControllerDelegate {
-
- /// receiveGoodsInfoViewController
- /// - Parameter sender: sender
- func receiveGoodsInfoViewController(_ sender: ReceiveGoodsInfoViewController) {}
-
- /// receiveGoodsInfoViewController
- /// - Parameters:
- /// - sender: sender
- /// - model: model
- func receiveGoodsInfoViewController(_ sender: ReceiveGoodsInfoViewController, userAddressInfo model: MoUserAddress?) {
- self.userAddress = model
- }
- }
|