pushdeer/ios/PushDeer-iOS/PushDeer/Service/WXDelegate.swift

61 lines
1.8 KiB
Swift
Raw Normal View History

//
// WXDelegate.swift
// PushDeer
//
// Created by HEXT on 2022/2/26.
//
import Foundation
#if !targetEnvironment(macCatalyst) && !APPCLIP && !SELFHOSTED
@MainActor
class WXDelegate: NSObject, WXApiDelegate {
static let shared = WXDelegate()
private override init() { super.init() }
func onReq(_ req: BaseReq) {
print(#function, req.type, req.openID)
}
func onResp(_ resp: BaseResp) {
print(#function, resp.type, resp.errCode, resp.errStr)
if let resp = resp as? SendAuthResp { //
print(resp.code as Any, resp.state as Any, resp.lang as Any, resp.country as Any)
switch resp.errCode {
case 0: //
if let code = resp.code, let state = resp.state {
Task {
do {
if state == "login" {
AppState.shared.token = try await HttpRequest.wechatLogin(code: code).token
// AppState token , SwiftUI ContentView
//
AppState.shared.loginAfter()
} else if state == "bind" {
_ = try await HttpRequest.mergeUser(type: "wechat", tokenorcode: code)
// ,
AppState.shared.userInfo = try await HttpRequest.getUserInfo()
}
} catch {
HToast.showError(error.localizedDescription)
}
}
}
break
case -2: //
HToast.showWarning(NSLocalizedString("你已取消授权", comment: ""))
break
case -4: //
HToast.showError(NSLocalizedString("你已拒绝授权", comment: ""))
break
default:
break
}
}
}
}
#endif