pushdeer/ios/PushDeer-iOS/PushDeer/Service/WXDelegate.swift
hext 2136883536 支持微信登录, 修复一些bug:
支持微信登录, 支持同时绑定微信和苹果账号;
解决markdown标题文字显示不全的问题;
markdown中的链接支持内嵌浏览器打开;
增加是否使用内置浏览器的设置项;
改名输入框, 失去焦点的时候也保存;
2022-02-27 23:02:58 +08:00

58 lines
1.7 KiB
Swift

//
// 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
} 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