import UIKit
import Kingfisher
//微信appid
let WX_APPID=""
//AppSecret
let SECRET=""
enum CHUWXShareType {
case Session, Timeline, Favorite/*会话, 朋友圈, 收藏*/
}
class WXApiManager: NSObject {
static let shared = WXApiManager()
// 用于弹出警报视图,显示成功或失败的信息()
private weak var sender:UIViewController! //(UIViewController)
// 支付成功的闭包
private var paySuccessClosure: (() -> Void)?
// 支付失败的闭包
private var payFailClosure: (() -> Void)?
//登录成功
private var loginSuccessClosure:((_ code:String) -> Void)?
//登录失败
private var loginFailClosure:(() -> Void)?
// 外部用这个方法调起微信支付
func payAlertController(_ sender:UIViewController,
request:PayReq,
paySuccess: @escaping () -> Void,
payFail:@escaping () -> Void) {
// sender 是调用这个方法的控制器,
// 用于提示用户微信支付结果,可以根据自己需求是否要此参数。
self.sender = sender
self.paySuccessClosure = paySuccess
self.payFailClosure = payFail
if checkWXInstallAndSupport() { //检查用户是否安装微信
WXApi.send(request)
}
}
//外部用这个方法调起微信登录
func login(_ sender:UIViewController,loginSuccess: @escaping ( _ code:String) -> Void,
loginFail:@escaping () -> Void){
// sender 是调用这个方法的控制器,
// 用于提示用户微信支付结果,可以根据自己需求是否要此参数。
self.sender = sender
self.loginSuccessClosure = loginSuccess
self.loginFailClosure = loginFail
if checkWXInstallAndSupport() {
let req=SendAuthReq()
req.scope="snsapi_userinfo"
req.state="app"
WXApi.send(req)
}
}
// 分享链接
func shareURL(title: String, description: String, imageStr: String, urlStr: String, scene: CHUWXShareType, loginSuccess: @escaping ( _ code:String) -> Void,
loginFail:@escaping () -> Void) {
self.loginSuccessClosure = loginSuccess
self.loginFailClosure = loginFail
ImageDownloader.default.downloadImage(with: URL(string: imageStr) ?? URL(string: "https://xxx.png")!, retrieveImageTask: nil, options: nil, progressBlock: nil, completionHandler: { (image, error, url, _) in
if let productImg = image {
let message = WXMediaMessage()
message.title = title
message.description = description
// if let url = URL(string: imageStr) {
// message.thumbData = try? Data(contentsOf: url)
// }
let newImage = productImg.resizeImageWith(newSize: CGSize(width: 100, height: 100))
message.thumbData = newImage.jpegData(compressionQuality: 0.3)//compressImageMid(maxLength: 30, image: productImg)
let obj = WXWebpageObject()
obj.webpageUrl = urlStr
message.mediaObject = obj
let req = SendMessageToWXReq()
req.bText = false
req.message = message
switch scene {
case .Session:
req.scene = Int32(WXSceneSession.rawValue)
case .Timeline:
req.scene = Int32(WXSceneTimeline.rawValue)
case .Favorite:
req.scene = Int32(WXSceneFavorite.rawValue)
}
if self.checkWXInstallAndSupport() {//检查用户是否安装微信
WXApi.send(req)
}
}
})
}
func shareURLFromLocal(title: String, description: String, image: UIImage, urlStr: String, scene: CHUWXShareType, loginSuccess: @escaping ( _ code:String) -> Void,
loginFail:@escaping () -> Void) {
self.loginSuccessClosure = loginSuccess
self.loginFailClosure = loginFail
let message = WXMediaMessage()
message.title = title
message.description = description
// if let url = URL(string: imageStr) {
// message.thumbData = try? Data(contentsOf: url)
// }
message.thumbData = image.jpegData(compressionQuality: 0.3)
let obj = WXWebpageObject()
obj.webpageUrl = urlStr
message.mediaObject = obj
let req = SendMessageToWXReq()
req.bText = false
req.message = message
switch scene {
case .Session:
req.scene = Int32(WXSceneSession.rawValue)
case .Timeline:
req.scene = Int32(WXSceneTimeline.rawValue)
case .Favorite:
req.scene = Int32(WXSceneFavorite.rawValue)
}
// if checkWXInstallAndSupport() {//检查用户是否安装微信
WXApi.send(req)
// }
}
// 分享小程序
func shareToMiniProgram(title:String, description: String, webpageUrl: String, userName: String, path: String, hdImageUrl: String, loginSuccess: @escaping ( _ code:String) -> Void,
loginFail:@escaping () -> Void) {
self.loginSuccessClosure = loginSuccess
self.loginFailClosure = loginFail
ImageDownloader.default.downloadImage(with: URL(string: hdImageUrl)!, retrieveImageTask: nil, options: nil, progressBlock: nil, completionHandler: { (image, error, url, _) in
if let productImg = image {
let obj = WXMiniProgramObject()
obj.webpageUrl = webpageUrl
obj.userName = userName
obj.path = path
obj.hdImageData = resetImgSize(sourceImage: productImg, maxImageLenght: 300, maxSizeKB: 125)//compressImageMid(maxLength: 125, image: productImg)
// obj.miniProgramType = .release
let message = WXMediaMessage()
message.title = title
message.description = description
// if let url = URL(string: hdImageUrl) {
// message.thumbData = try? Data(contentsOf: url)
// }
message.mediaObject = obj
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = Int32(WXSceneSession.rawValue)
if self.checkWXInstallAndSupport() {//检查用户是否安装微信
WXApi.send(req)
}
}
})
}
// 分享图片到微信好友
func shareImage(_ data: Data, thumbImage: UIImage, title: String, description: String, to scene: CHUWXShareType, loginSuccess: @escaping ( _ code:String) -> Void,
loginFail:@escaping () -> Void) {
self.loginSuccessClosure = loginSuccess
self.loginFailClosure = loginFail
let message = WXMediaMessage()
message.setThumbImage(thumbImage)
message.title = title
message.description = description
let obj = WXImageObject()
obj.imageData = data
message.mediaObject = obj
let req = SendMessageToWXReq()
req.bText = false
req.message = message
switch scene {
case .Session:
req.scene = Int32(WXSceneSession.rawValue)
case .Timeline:
req.scene = Int32(WXSceneTimeline.rawValue)
case .Favorite:
req.scene = Int32(WXSceneFavorite.rawValue)
}
WXApi.send(req)
}
}
extension WXApiManager: WXApiDelegate {
func onResp(_ resp: BaseResp!) {
if resp is PayResp {//支付
if resp.errCode == 0 {
self.paySuccessClosure?()
}else{
self.payFailClosure?()
}
} else if resp is SendAuthResp {//登录结果
let authResp = resp as! SendAuthResp
var strMsg: String
if authResp.errCode == 0{
NotificationCenter.default.post(name: NSNotification.Name("wxLoginSuccess"), object: self, userInfo: ["code": authResp.code])
strMsg="微信授权成功"
} else {
switch authResp.errCode{
case -4:
strMsg="您拒绝使用微信登录"
break
case -2:
strMsg="您取消了微信登录"
break
default:
strMsg="微信登录失败"
break
}
CBToast.showToastAction(message: strMsg as NSString)
}
print("授权结果----\(strMsg)")
} else if resp is SendMessageToWXResp {
if resp.errCode == WXSuccess.rawValue {
print("maybe share")
}
}
}
}
extension WXApiManager {
// 检查用户是否已经安装微信并且有支付功能
private func checkWXInstallAndSupport() -> Bool {
if !WXApi.isWXAppInstalled() {
///这里的弹窗是我写的扩展方法
CBToast.showToastAction(message: "微信未安装")
return false
}
if !WXApi.isWXAppSupport() {
///这里的弹窗是我写的扩展方法
CBToast.showToastAction(message: "当前微信版本不支持支付")
return false
}
return true
}
}
网友评论