逻辑梳理:1.判断openID 和 accessToken 是否存在微信是否登录,已登录,从后台获取个人信息并显示, 未登录,点击登录按钮,请求微信登录,微信登录成功后,在AppDelegate中有成功回调方法(也是在此方法中获取的AccessToken),微信登录成功后,发通知给需要操作的类,在通知中调用方法,方法中首先请求后台接口,若后台接口无数据,则请求微信的接口。获取个人信息后立刻上传到自己后台。2.点击头像,修改头像,修改后上传到后台,在此遇到一个问题,即是cell单元格的显示问题,微信传来的一个url,而从本地选择的是一个UIImage,所以要进行不同的选择。
代码展示:
1 判断是否已登录
func initView(){
// 获取已登录标记:存在则表示已经登录
if let accessToken = UserDefaults.standard.object(forKey: WX_ACCESS_TOKEN) as? String,
let openID = UserDefaults.standard.object(forKey: OPEN_ID) as? String {
entireView.isHidden = true
// 已登录
/// 从本地取出用户信息并显示 // TODO 未解决本地为空
if let imgData = UserDefaults.standard.object(forKey: HEADIMGURL) as? Data {
self.currentImageView.image = UIImage.init(data: imgData)
} else if let headimgurl = UserDefaults.standard.object(forKey: HEADIMGURL) as? String {
self.headimgurl = UserDefaults.standard.object(forKey: HEADIMGURL) as! String
}
self.nickname = UserDefaults.standard.object(forKey: NICKNAME) as! String
self.language = UserDefaults.standard.object(forKey: LANGUAGE) as! String
self.province = UserDefaults.standard.object(forKey: PROVINCE) as! String
self.sex = UserDefaults.standard.object(forKey: SEX) as! NSInteger
self.country = UserDefaults.standard.object(forKey: COUNTRY) as! String
self.tableView.reloadData()
} else {
entireView.isHidden = false
entireView.frame = CGRect.init(x: 0, y: 64, width: self.view.frame.width, height: self.view.frame.height)
entireView.backgroundColor = UIColor.white
self.view.addSubview(entireView)
let centerBtn = UIButton()
centerBtn.frame = CGRect.init(x:self.view.frame.width/2 - 100.0 , y: self.view.frame.height/2 - 100.0, width: 200.0, height: self.view.frame.height - 200.0)
centerBtn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
centerBtn.setTitle("未登录,请登录", for: .normal)
centerBtn.setTitleColor(UIColor.blue, for: .normal)
entireView.addSubview(centerBtn)
}
}
点击未登录按钮,登录微信
func loginAction(){
// 判断是否安装微信
if WXApi.isWXAppInstalled()
{
let authReq = SendAuthReq.init()
authReq.scope = "snsapi_userinfo"
authReq.state = "App"
WXApi.send(authReq)
return
} else{
self.setUpAlertController()
}
}
登录成功后,在AppDelegate的登录成功回调方法中发送登录成功的通知,在自己的类中接收通知
NotificationCenter.default.addObserver(self, selector: #selector(requestLogin), name: NSNotification.Name(rawValue: REFRESH_PERSONAL_CENTER), object: nil)
接收到通知后的请求方法
func requestLogin(){
//请求后台数据,获取个人信息
if let headimgurl = UserDefaults.standard.object(forKey: HEADIMGURL) as? String {
self.headimgurl = UserDefaults.standard.object(forKey: HEADIMGURL) as! String
self.nickname = UserDefaults.standard.object(forKey: NICKNAME) as! String
self.language = UserDefaults.standard.object(forKey: LANGUAGE) as! String
self.province = UserDefaults.standard.object(forKey: PROVINCE) as! String
self.sex = UserDefaults.standard.object(forKey: SEX) as! NSInteger
self.country = UserDefaults.standard.object(forKey: COUNTRY) as! String
}else{
SVProgressHUD.show()
accessToken = UserDefaults.standard.object(forKey: WX_ACCESS_TOKEN) as? String
openID = UserDefaults.standard.object(forKey: OPEN_ID) as? String
let urlStr = "https://api.weixin.qq.com/sns/userinfo?access_token=\(accessToken!)&openid=\(openID!)"
let url = URL.init(string: urlStr)
var request = URLRequest.init(url: url!)
request.timeoutInterval = 45
let session = URLSession.init(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
if error == nil {
if let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
let dic = json as! Dictionary<String,Any>
if let openid = dic["openid"]{
self.openid = openid as! String
self.headimgurl = (dic["headimgurl"] as? String)!
self.unionid = (dic["unionid"] as? String)!
self.nickname = (dic["nickname"] as? String)!
self.language = (dic["language"] as? String)!
self.province = (dic["province"] as? String)!
self.sex = (dic["sex"] as? NSInteger)!
self.country = (dic["country"] as? String)!
UserDefaults.standard.set(self.openid, forKey: OPEN_ID)
UserDefaults.standard.set(self.headimgurl, forKey: HEADIMGURL)
UserDefaults.standard.set(self.unionid, forKey:UNINONID)
UserDefaults.standard.set(self.nickname, forKey: NICKNAME)
UserDefaults.standard.set(self.language, forKey: LANGUAGE)
UserDefaults.standard.set(self.province, forKey: PROVINCE)
UserDefaults.standard.set(self.sex, forKey: SEX)
UserDefaults.standard.set(self.country, forKey: COUNTRY)
DispatchQueue.main.async(execute: {
self.initView()
})
}else{
print(error)
}
}
} else {
self.codeAlertViewControoller()
}
SVProgressHUD.dismiss()
})
dataTask.resume()
}
}
获取到数据后,cell的显示方法
iconTableViewCell!.leftLabel.text = "头像"
if self.currentImageView != nil {
iconTableViewCell?.iconImageView.image = self.currentImageView.image
} else if self.headimgurl.count > 0 {
iconTableViewCell?.setIconPhoto(with: self.headimgurl)
self.currentImageView = iconTableViewCell?.iconImageView
} else {
// 设置默认头像
iconTableViewCell?.imageView?.image = nil
}
return iconTableViewCell!
}