123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- //
- // PQThirdPlatformUtil.swift
- // PQSpeed
- //
- // Created by SanW on 2020/5/30.
- // Copyright © 2020 BytesFlow. All rights reserved.
- //
- import Kingfisher
- import UIKit
- import UserNotifications
- import WechatOpenSDK
- import Bugly
- // MARK: - 第三方相关工具类
- /// 第三方相关工具类
- class PQThirdPlatformUtil: NSObject {
- static let shared = PQThirdPlatformUtil()
- // 注册第三方
- func register() {
- }
- override private init() {
- super.init()
- }
- override func copy() -> Any {
- return self
- }
- override func mutableCopy() -> Any {
- return self
- }
- }
- // MARK: - bugly相关工具类
- public class PQSingleBuglyUtil: NSObject {
- static let shared = PQSingleBuglyUtil()
- // 注册
- func register(appID:String) {
-
- // 注册bugly
- let buglyConfig = BuglyConfig()
- buglyConfig.reportLogLevel = .warn
- buglyConfig.version = versionName + "-\(PQENVUtil.shared.envMode)"
- BFLog(message: "Bugly版本号:\(BuglyConfig.version())")
- buglyConfig.channel = channelID
- buglyConfig.deviceIdentifier = BFLoginUserInfo.shared.isLogin() ? BFLoginUserInfo.shared.uid : getMachineCode()
- buglyConfig.unexpectedTerminatingDetectionEnable = true
- buglyConfig.blockMonitorEnable = true
- buglyConfig.blockMonitorTimeout = 2
- Bugly.start(withAppId: cBuglyAppkey, config: buglyConfig)
-
- }
- override private init() {
- super.init()
- }
- public override func copy() -> Any {
- return self
- }
- public override func mutableCopy() -> Any {
- return self
- }
- }
- // MARK: - 微信相关工具类
- /// 微信相关工具类
- //初始化微信要使用的参数 e.g.
- /*
- let state: String = "com.piaoquan.pqspeed"
- let appid: String = "wxfc2fc07ab379e4bf"
- let secret: String = "06f696424accb17b7234dce32e4821b4"
- let universalLink: String = "https://speed.piaoquantv.com/"
- let scope = "snsapi_userinfo"
- */
- public class WXApiInfo:NSObject {
- public var state: String = ""
- public var appid: String = ""
- public var secret: String = ""
- public var universalLink: String = ""
- public var scope = ""
- }
- public class PQSingletoWXApiUtil: NSObject {
- public static let shared = PQSingletoWXApiUtil()
- var openId: String? // openID
- // 回调
- public var wxApiUtilHander: ((_ userData: [String: Any]?, _ errorMsg: String?) -> Void)?
- var mAppInfo:WXApiInfo = WXApiInfo.init()
- public func registerApp(appInfo:WXApiInfo) {
- mAppInfo = appInfo
- #if DEBUG
- WXApi.startLog(by: .detail) { msg in
- BFLog(message: "微信回调Log--\(msg)")
- }
- #endif
- WXApi.registerApp(mAppInfo.appid, universalLink: mAppInfo.universalLink)
- // #if DEBUG
- // WXApi.checkUniversalLinkReady { step, result in
- // BFLog(message: "微信回调自检--\(step),result = \(result)")
- // }
- // #endif
- }
- public func handleOpen(url: URL) -> Bool {
- return WXApi.handleOpen(url, delegate: self)
- }
- public func handleOpenUniversalLink(userActivity: NSUserActivity) -> Bool {
- return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
- }
- public func authorize() -> PQSingletoWXApiUtil {
- if !isInstallWX() {
- if wxApiUtilHander != nil {
- wxApiUtilHander!(nil, "您还未安装微信客户端!")
- }
- return .shared
- }
- let req = SendAuthReq()
- req.scope = mAppInfo.scope
- req.state = mAppInfo.state
- WXApi.send(req) { [weak self] isSuccess in
- if !isSuccess {
- self?.wxApiUtilHander!(nil, "您还未安装微信客户端!")
- }
- }
- return .shared
- }
- /// 分享
- /// - Parameters:
- /// - type: 类型 1- 分享视频 2-分享活动 3-分享卡点视频
- /// - scene: 场景
- /// - title: 标题
- /// - description:描述
- /// - imageUrl: 图片url
- /// - path: 网页地址
- /// - videoId: 视频ID type == 1时不为空
- /// - pageSource: 页面pageSource type == 1时不为空
- /// - Returns: <#description#>
- public func share(type: Int = 1, scene: Int32, shareWeappRawId: String? = nil, title: String?, description: String?, imageUrl: String?, path: String?, videoId: String, pageSource: PAGESOURCE, shareId: String) -> PQSingletoWXApiUtil {
- if !isInstallWX() {
- if wxApiUtilHander != nil {
- wxApiUtilHander!(nil, "您还未安装微信客户端!")
- }
- return .shared
- }
- let message = WXMediaMessage()
- message.title = title ?? ""
- message.description = description ?? ""
- ImageDownloader.default.downloadImage(with: URL(string: imageUrl ?? "")!, retrieveImageTask: nil, options: nil, progressBlock: nil) { image, _, _, _ in
- message.thumbData = zipImage(image: image, size: 64)
- if type == 1 || type == 3, scene == Int32(WXSceneSession.rawValue) {
- let wxMiniObject = WXMiniProgramObject()
- wxMiniObject.miniProgramType = PQENVUtil.shared.envMode == .ENVModeOnline ? .release : .test
- wxMiniObject.userName = shareWeappRawId ?? cShareWeappRawId
- var preParams: String = ""
- var page: String = ""
- var isRhythmVideo = 0
- if type == 3 {
- preParams = "package-point/show/show"
- page = "pages/post/post"
- isRhythmVideo = 1
- } else {
- preParams = "user-videos"
- page = "pages/category"
- isRhythmVideo = 0
- }
- var params = (preParams + "?id=\(videoId)&tp=share&rootPageSource=\(pageSource.rawValue)&shareDepth=1&rootLaunchShareId=\(shareId)&parentShareId=\(shareId)&shareId=\(shareId)&shareAppType=13&mid=\(getMachineCode())&su=\(BFLoginUserInfo.shared.uid)&isRhythmVideo=\(isRhythmVideo)").addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
- ["/", "=", "?", "&"].forEach { char in
- let custom = CharacterSet(charactersIn: char).inverted
- params = params?.addingPercentEncoding(withAllowedCharacters: custom) ?? ""
- }
- wxMiniObject.path = "\(page)?jumpPage=" + (params ?? "")
- wxMiniObject.webpageUrl = "https://piaoquan.yishihui.com/"
- wxMiniObject.hdImageData = message.thumbData
- message.mediaObject = wxMiniObject
- } else {
- let webpageObject = WXWebpageObject()
- webpageObject.webpageUrl = path ?? ""
- message.mediaObject = webpageObject
- }
- let req = SendMessageToWXReq()
- req.message = message
- req.scene = scene
- WXApi.send(req) { [weak self] isSuccess in
- if self?.wxApiUtilHander != nil {
- self?.wxApiUtilHander!(nil, isSuccess ? "分享成功~" : "分享失败了~")
- }
- }
- }
- return .shared
- }
- /// 处理发起数据
- /// - Parameter response: <#response description#>
- /// - Returns: <#description#>
- public func dealWithPayParams(response: [String: Any]) -> PQSingletoWXApiUtil {
- var partnerid: String = ""
- var prepayId: String = ""
- var nonceStr: String = ""
- var timeStamp: UInt32 = 0
- var package: String = "Sign=WXPay"
- var sign: String = ""
- if response.keys.contains("partnerid") {
- partnerid = response["partnerid"] as! String
- }
- if response.keys.contains("prepayid") {
- prepayId = response["prepayid"] as! String
- }
- if response.keys.contains("noncestr") {
- nonceStr = response["noncestr"] as! String
- }
- if response.keys.contains("timestamp") {
- timeStamp = UInt32(Int("\(response["timestamp"] ?? "0")") ?? 0)
- }
- if response.keys.contains("package") {
- package = response["package"] as! String
- }
- if response.keys.contains("sign") {
- sign = response["sign"] as! String
- }
- requestPayReq(partnerid: partnerid, prepayId: prepayId, nonceStr: nonceStr, timeStamp: timeStamp, package: package, sign: sign)
- return .shared
- }
- /// 发起
- /// - Parameters:
- /// - partnerid: 商家向财付通申请的商家id
- /// - prepayId: 预
- /// - nonceStr: 随机串,防重发
- /// - timeStamp: 时间戳,防重发
- /// - package: 商家根据财付通文档填写的数据和签名
- /// - sign: 商家根据微信开放平台文档对数据做的签名
- /// - Returns: <#description#>
- public func requestPayReq(partnerid: String, prepayId: String, nonceStr: String, timeStamp: UInt32, package: String, sign: String) {
- // 调起微信
- let payReq: PayReq = PayReq()
- payReq.partnerId = partnerid
- payReq.prepayId = prepayId
- payReq.nonceStr = nonceStr
- payReq.timeStamp = timeStamp
- payReq.package = package
- payReq.sign = sign
- WXApi.send(payReq) { _ in
- }
- }
- /// 是否安装了微信
- /// - Returns: <#description#>
- public func isInstallWX() -> Bool {
- return UIApplication.shared.canOpenURL(URL(string: "weixin://")!)
- }
- override private init() {
- super.init()
- }
- public override func copy() -> Any {
- return self
- }
- public override func mutableCopy() -> Any {
- return self
- }
- }
- extension PQSingletoWXApiUtil: WXApiDelegate {
- public func onReq(_ req: BaseReq) {
- BFLog(message: "微信回调拉起 -- \(req)")
- if req.isKind(of: LaunchFromWXReq.self) && ((req as! LaunchFromWXReq).message.messageExt?.count ?? 0) > 0 {
- BFLog(message: "小程序回调数据--\(String(describing: (req as! LaunchFromWXReq).message.messageExt))")
- // PQBoardCommandUtil.dealWithPasteboardData(messageExt: (req as! LaunchFromWXReq).message.messageExt)
- }
- }
- public func onResp(_ resp: BaseResp) {
- BFLog(message: "微信回调返回 -- \(resp)")
- if resp.isKind(of: SendAuthResp.self) {
- if (resp as! SendAuthResp).state == mAppInfo.state {
- accessToken(code: (resp as! SendAuthResp).code!)
- } else {
- if wxApiUtilHander != nil {
- wxApiUtilHander!(nil, "网络不可用")
- }
- }
- } else if resp.isKind(of: PayResp.self) {
- if wxApiUtilHander != nil {
- wxApiUtilHander!(["returnKey": (resp as! PayResp).returnKey, "code": resp.errCode], resp.errStr)
- }
- }
- }
- public func accessToken(code: String) {
- let url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=\(mAppInfo.appid)&secret=\(mAppInfo.secret)&code=\(code)&grant_type=authorization_code"
- DispatchQueue.global(qos: .userInitiated).async { [weak self] in
- let zoneStr = try? String(contentsOf: URL(string: url)!, encoding: .utf8)
- if zoneStr == nil {
- if self?.wxApiUtilHander != nil {
- self?.wxApiUtilHander!(nil, "授权失败了~")
- }
- return
- }
- let data = zoneStr!.data(using: .utf8)
- let dict: [String: Any] = try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: Any]
- self?.openId = "\(dict["openid"] ?? "")"
- BFLoginUserInfo.shared.openId = self?.openId ?? ""
- let access_token: String = "\(dict["access_token"] ?? "")"
- let userUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=\(access_token)&openid=\(self?.openId ?? "")"
- DispatchQueue.global(qos: .userInitiated).async {
- let userInfoStr = try? String(contentsOf: URL(string: userUrl)!, encoding: .utf8)
- let userInfoData = userInfoStr!.data(using: .utf8)
- let userDict: [String: Any] = try! JSONSerialization.jsonObject(with: userInfoData!, options: .mutableContainers) as! [String: Any]
- DispatchQueue.main.async { [weak self] in
- if userDict.count > 0 {
- if self?.wxApiUtilHander != nil {
- self?.wxApiUtilHander!(userDict, nil)
- }
- } else {
- if self?.wxApiUtilHander != nil {
- self?.wxApiUtilHander!(nil, "授权失败了~")
- }
- }
- }
- }
- }
- }
- }
-
|