You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
weibo/Weibo/LoginView.swift

139 lines
4.2 KiB

3 years ago
//
// LoginView.swift
// Weibo
//
// Created by Qihua Pan on 2020/8/17.
// Copyright © 2020 Qihua Pan. All rights reserved.
//
import SwiftUI
import CoreData
import SwiftyJSON
import Alamofire
3 years ago
struct LoginView: View {
@Environment(\.managedObjectContext) var contenxt:NSManagedObjectContext
@EnvironmentObject var user:User
let queue=OperationQueue()
@State var showView=false
3 years ago
func login() {
let request=WBAuthorizeRequest()
request.redirectURI="https://api.weibo.com/oauth2/default.html"
WeiboSDK.send(request)
}
func loadUserInfo(){
print("toekn=\(self.user.access_token),date=\(self.user.expiration_Date)")
self.showView=false
AF.request("https://api.weibo.com/2/users/show.json",parameters: ["access_token":self.user.access_token,"uid":self.user.uid]).responseJSON { response in
if let data=response.data,let json = try? JSON(data: data){
if let name=json["name"].string,
let avatar_hd=json["avatar_hd"].string,
let desc=json["description"].string,
let statuses_count=json["statuses_count"].int32,
let friends_count=json["friends_count"].int32,
let followers_count=json["followers_count"].int32{
self.user.name=name
self.user.avatar_hd=avatar_hd
self.user.desc=desc
self.user.statuses_count=statuses_count
self.user.friends_count=friends_count
self.user.followers_count=followers_count
UserData.saveContext()
self.showView=true
}
}else{
print("loadUserInfo error")
}
}
}
3 years ago
var body: some View {
VStack {
if self.user.access_token==nil{
Text("正在跳转到授权页面").onAppear(perform: {
3 years ago
self.login()
})
}
else if self.user.expiration_Date<Date().timeIntervalSince1970{
Text("正在跳转到授权页面").onAppear(perform: {
self.login()
})
}
else if self.showView{
UserView()
}else{
Text("正在获取用户信息").onAppear(perform: {
self.loadUserInfo()
})
3 years ago
}
}
}
}
struct UserView:View {
@EnvironmentObject var user:User
var body:some View{
VStack {
HStack {
ImageView(imageUrl: self.user.avatar_hd!)
.frame(width: 60.0, height: 60.0)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
VStack {
Text(self.user.name!)
Text("简介:\(self.user.desc!)")
}
Spacer()
Button(action: {
print("会员")
}) {
HStack(spacing:0) {
Image("crown")
Text("会员")
Image("arrow-right")
}
}
}
Rectangle()
.frame(height: 1)
HStack(spacing: 100.0){
VStack{
Text("\(self.user.statuses_count)")
Text("微博")
}
VStack{
Text("\(self.user.friends_count)")
Text("关注")
}
VStack{
Text("\(self.user.followers_count)")
Text("粉丝")
}
}
.padding(.horizontal, 20.0)
Rectangle()
.frame(height: 10)
.foregroundColor(Color(red: 220/255, green: 220/255, blue: 220/255, opacity: 1.0))
}
}
}
3 years ago
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
// LoginView().environment(\.managedObjectContext, UserData.getContext()).environmentObject(UserData.getUser())
UserView()
3 years ago
}
}