暂无描述

DetailPageViewModel.swift 8.9KB

    // // DetailPageViewModel.swift // PaiAi // // Created by zhengjianfei on 2017/1/4. // Copyright © 2017年 FFIB. All rights reserved. // import UIKit import ObjectMapper public final class DetailPageViewModel { public lazy var currentPhoto = PhotoModel() public lazy var thumbups = [ZanUserModel]() public lazy var comments = [CommentModel]() public lazy var thumbupsCount = 0 public lazy var commentsCount = 0 public lazy var hdPrice: Double = -0.01 public lazy var watermarkPrice: Double = -0.01 var detailPageApi = NetworkApi() // var tipDelegate : TipProtocol? = nil public init() {} public final func fetchThumbup(success: @escaping () -> Void) { let params = ["group_id": currentPhoto.group_id, "user_id": SharedUserInfo.userId, "photo_id": currentPhoto.photo_id] as [String: AnyObject] let request = ZanUserNetworkRequest(parameter: params) detailPageApi.post(request: request) {[weak self] (res) in guard let weakself = self else { return } weakself.thumbups = res weakself.thumbupsCount = res.count success() } } public final func fetchComment(success: @escaping () -> Void) { let params = ["group_id": currentPhoto.group_id, "user_id": SharedUserInfo.userId, "photo_id": currentPhoto.photo_id] as [String: AnyObject] let request = CommentNetworkRequest(parameter: params) detailPageApi.post(request: request) {[weak self] (res) in guard let weakself = self else { return } weakself.comments = res weakself.commentsCount = res.count success() } } public final func sendThumbup(success: @escaping () -> Void) { let params = ["group_id": currentPhoto.group_id, "user_id": SharedUserInfo.userId, "photo_id": currentPhoto.photo_id] as [String: AnyObject] let request = StatusNetworkRequest(param: params, path: .thumbupSubmit) detailPageApi.post(request: request) { (res) in if res.message == "Duplicate Thumb Up" { // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "照片已经点过赞了") } guard res.status == 200 else { // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "照片点赞失败") return } // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "照片点赞成功") success() } } public final func sendComment(content: String, success: @escaping () -> Void) { guard !content.isEmpty else { return } let params = ["group_id": currentPhoto.group_id, "user_id": SharedUserInfo.userId, "photo_id": currentPhoto.photo_id, "comment": content] as [String: AnyObject] let request = StatusNetworkRequest(param: params, path: .commentSubmit) detailPageApi.post(request: request) {(res) in guard res.status == 200 else { // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "评论失败") return } // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "评论成功") success() } } // MARK: pay public var orderId: String! = "" deinit { NotificationCenter.default.removeObserver(self) } } //wechat pay extension DetailPageViewModel { public final func handleResult(errorCode: Int, success: @escaping ((_ photoModel: PhotoModel) -> Void)) { func fetchOrderDetail() { detailPageApi.post(param: ["order_id": orderId, "user_id": SharedUserInfo.userId] as [String: AnyObject], url: .orderDetail) { (result) in guard let status = result["status"] as? Int, let data = result["data"] as? [String: AnyObject], let photoInfo = data["group_photo_info"] as? [String: AnyObject], status == 200 else { // FFToastView.hideLoadingToast() // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "") return } let photoModel = PhotoModel(map: Map(mappingType: .fromJSON, JSON: photoInfo)) self.currentPhoto.murl = photoModel.murl self.currentPhoto.rurl = photoModel.rurl success(photoModel) // FFToastView.hideLoadingToast() // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "") // PhotoLocalStorage.instance.updateLocalData(photoModel: photoModel) } } // Delay(3) { let orderQequest = StatusNetworkRequest(param: ["order_id": self.orderId as AnyObject], path: .orderQuery) self.detailPageApi.post(request: orderQequest, handler: { (res) in if res.status == 200 { fetchOrderDetail() } else { // FFToastView.hideLoadingToast() // FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "支付失败") } }) // } } public final func getHD(getPriceSuccess: ((_ isExist: Bool) -> Void)?) { if !currentPhoto.rurl.isEmpty { getPriceSuccess!(true) return } else { if hdPrice != -0.01 { payPrice(photoType: "origin") } else { getPrice(photoType: "origin", success: getPriceSuccess) } } } public final func getWatermark(getPriceSuccess: ((_ isExist: Bool) -> Void)?) { if !currentPhoto.murl.isEmpty { getPriceSuccess!(true) return } else { if watermarkPrice != -0.01 { payPrice(photoType: "nomark") } else { getPrice(photoType: "nomark", success: getPriceSuccess) } } } public final func payPrice(photoType: String) { //下单 let body = photoType == "origin" ? "获取高清照片" : "获取去水印照片" let price = photoType == "origin" ? hdPrice : watermarkPrice let params = ["user_id": SharedUserInfo.userId, "body": body, "total_fee": price, "trade_type": "APP", "group_id": currentPhoto.group_id, "photo_id": currentPhoto.photo_id, "photo_type": photoType] as [String: AnyObject] detailPageApi.post(param: params, url: .wxorderCreat) { (result) in // FFToastView.hideLoadingToast() guard let status = result["status"] as? Int, let items = result["data"] as? [String: AnyObject], status == 200 else { return } if let orderid = items["order_id"] as? String, let prepay_id = items["prepay_id"] as? String, let wxpay_params = items["wxpay_params"] as? [String: AnyObject], let appid = wxpay_params["appid"] as? String, let noncestr = wxpay_params["noncestr"] as? String, let package = wxpay_params["package"] as? String, let sign = wxpay_params["sign"] as? String, let partnerid = wxpay_params["partnerid"] as? String, let timestamp = wxpay_params["timestamp"] as? String { //支付 self.orderId = orderid #if !((arch(i386) || arch(x86_64))) guard WXApi.isWXAppInstalled() else { FFToastView.showToast(inView: UIApplication.shared.keyWindow!, withText: "尚未安装微信") return } WXApi.registerApp(appid) let payRequest = PayReq() payRequest.openID = appid payRequest.partnerId = partnerid payRequest.prepayId = prepay_id payRequest.package = package payRequest.nonceStr = noncestr payRequest.timeStamp = UInt32(timestamp) ?? 0 payRequest.sign = sign WXApi.send(payRequest) #endif } } } public final func getPrice(photoType: String, success: ((_ isExist: Bool) -> Void)?) { let params = ["user_id": SharedUserInfo.userId, "photo_id": currentPhoto.photo_id, "photo_type": photoType] as [String: AnyObject] detailPageApi.post(param: params, url: .picPrice) {[weak self] (result) in guard let status = result["status"] as? Int, let items = result["data"] as? [String: AnyObject], status == 200, let weakself = self else { return } if let price = items["price"] as? Double { if photoType == "origin"{ weakself.hdPrice = price } else { weakself.watermarkPrice = price } success!(false) } } } }