2021-12-27 22:33:27 +08:00
|
|
|
//
|
|
|
|
// LoginView.swift
|
|
|
|
// PushDeer
|
|
|
|
//
|
|
|
|
// Created by HEXT on 2021/12/25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import AuthenticationServices
|
|
|
|
|
|
|
|
/// 登录界面
|
|
|
|
struct LoginView: View {
|
2022-01-10 00:26:29 +08:00
|
|
|
|
|
|
|
@EnvironmentObject private var store: AppState
|
|
|
|
@State private var showLoading = false
|
|
|
|
|
2021-12-27 22:33:27 +08:00
|
|
|
var body: some View {
|
|
|
|
VStack{
|
|
|
|
Spacer()
|
|
|
|
Image("logo.with.space")
|
|
|
|
.resizable()
|
|
|
|
.scaledToFit()
|
2022-02-14 01:13:26 +08:00
|
|
|
if Env.isSelfHosted {
|
|
|
|
Button("重置API endpoint") {
|
|
|
|
store.api_endpoint = ""
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 22:33:27 +08:00
|
|
|
Spacer()
|
2022-01-10 00:26:29 +08:00
|
|
|
if showLoading {
|
|
|
|
ProgressView()
|
|
|
|
.scaleEffect(1.5)
|
|
|
|
.frame(height: 64)
|
|
|
|
} else {
|
2022-02-27 23:02:58 +08:00
|
|
|
// 苹果登录按钮
|
2022-01-10 00:26:29 +08:00
|
|
|
AppleSignInButton(
|
|
|
|
onRequest: { request in
|
|
|
|
request.requestedScopes = [.fullName, .email]
|
|
|
|
},
|
|
|
|
onCompletion: { result in
|
|
|
|
do {
|
|
|
|
showLoading = true
|
|
|
|
store.token = try await store.appleIdLogin(result).token
|
|
|
|
// 获取成功去主页
|
2022-09-14 00:14:19 +08:00
|
|
|
// 登录成功后的处理
|
|
|
|
store.loginAfter()
|
2022-01-10 00:26:29 +08:00
|
|
|
} catch {
|
|
|
|
showLoading = false
|
2022-02-27 23:02:58 +08:00
|
|
|
if (error as NSError).code == 1001 {
|
|
|
|
// 取消登录
|
|
|
|
HToast.showWarning(error.localizedDescription)
|
|
|
|
return
|
|
|
|
}
|
2022-01-30 01:02:44 +08:00
|
|
|
HToast.showError(error.localizedDescription)
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
)
|
2022-01-16 00:10:47 +08:00
|
|
|
.overlay(RoundedRectangle(cornerRadius: 6).stroke(Color.white))
|
2022-01-10 00:26:29 +08:00
|
|
|
.frame(maxWidth: 375, minHeight: 64, maxHeight: 64)
|
|
|
|
.padding()
|
2022-02-27 23:02:58 +08:00
|
|
|
|
|
|
|
#if !targetEnvironment(macCatalyst) && !APPCLIP && !SELFHOSTED
|
|
|
|
if WXApi.isWXAppInstalled() {
|
|
|
|
// 微信登录按钮
|
|
|
|
Button {
|
|
|
|
let req = SendAuthReq()
|
|
|
|
req.scope = "snsapi_userinfo";
|
|
|
|
req.state = "login";
|
|
|
|
WXApi.send(req) { b in
|
|
|
|
print("WXApi.send:", b)
|
|
|
|
}
|
|
|
|
// 微信登录请求发出去后面的逻辑在 AppDelegate 的 onResp 回调方法中处理
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Image("weixin-login")
|
|
|
|
.resizable()
|
|
|
|
.renderingMode(.template)
|
|
|
|
.scaledToFit()
|
|
|
|
.frame(height:20)
|
|
|
|
Text("通过微信登录")
|
|
|
|
}
|
|
|
|
.font(.system(size: 26, weight: .semibold))
|
|
|
|
.foregroundColor(Color("weixinFgColor"))
|
|
|
|
.frame(maxWidth: 375, minHeight: 64, maxHeight: 64)
|
|
|
|
}
|
|
|
|
.background(Color("weixinBgColor"))
|
|
|
|
.cornerRadius(6)
|
|
|
|
.overlay(RoundedRectangle(cornerRadius: 6).stroke(Color("weixinFgColor")))
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
#endif
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
2021-12-27 22:33:27 +08:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LoginView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
LoginView()
|
|
|
|
}
|
|
|
|
}
|