import UIKit
import AFNetworking
class NetworkTools: AFHTTPSessionManager {
// static let WB_App_Key = "861417731"
// static let WB_App_Secret = "891b8e2950f0daaca048c9fc6c46a535"
// static let WB_Redirect_URI = "http://www.520it.com"
static let WB_App_Key = "2507721026"
static let WB_App_Secret = "dd92b400310fa5eb9e2fe5bc9c1caa6c"
static let WB_Redirect_URI = "http://my.oschina.net/u/2333251"
static let shareInstance: NetworkTools = {
let url = NSURL(string: "https://api.weibo.com/")!
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let instance = NetworkTools(baseURL: url, sessionConfiguration: configuration)
instance.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/json", "text/javascript", "text/plain") as Set
return instance
}()
// MARK: - 外部控制器方法
/**
获取授权信息
- parameter code: 已经授权的RequestToken
- parameter finfished: 回调
- parameter error: 错误信息
*/
func loadAccessToken(code: String, finfished: (objc: [String: AnyObject]?, error: NSError?)->())
{
let path = "oauth2/access_token"
// 1.拼接参数
let parameters = ["client_id": NetworkTools.WB_App_Key, "client_secret": NetworkTools.WB_App_Secret, "grant_type": "authorization_code", "code": code, "redirect_uri": NetworkTools.WB_Redirect_URI]
// 2.访问网络
POST(path, parameters: parameters, success: { (_, JSON) -> Void in
guard let dict = JSON as? [String: AnyObject] else
{
finfished(objc: nil, error: NSError(domain: "com.520it.lnj", code: 998, userInfo: ["message": "获取到的授权信息是nil"]))
return
}
finfished(objc: dict, error: nil)
}) { (_, error) -> Void in
finfished(objc: nil, error: error)
}
}
/**
获取用户信息
- parameter userAccount: 授权模型
- parameter finfished: 回调
- parameter error: 错误信息
*/
func loadUserInfo(userAccount: UserAccountModel, finfished: (objc: [String: AnyObject]?, error: NSError?)->())
{
let path = "2/users/show.json"
// 1.封装参数
let parameters = ["access_token": userAccount.access_token!, "uid": userAccount.uid!]
// 2.发送请求
NetworkTools.shareInstance.GET(path, parameters: parameters, success: { (_, JSON) -> Void in
// 2.1进行安全校验
guard let dict = JSON as? [String: AnyObject] else
{
finfished(objc: nil, error: NSError(domain: "com.520it.lnj", code: 999, userInfo: ["message": "获取到的用户信息是nil"]))
return
}
finfished(objc: dict, error: nil)
}) { (_, error) -> Void in
finfished(objc: nil, error: error)
}
}
/**
获取微博数据
- parameter since_id 用于下拉刷新, 返回比指定微博ID大的微博
- parameter finfished: 回调
- parameter error: 错误信息
*/
func loadStauts(since_id: String, max_id: String, finfished: (objc: [[String: AnyObject]]?, error: NSError?)->())
{
let path = "2/statuses/home_timeline.json"
/*
since_id: 返回比指定的since_id大的微博(获取最新的微博数据)
max_id: 返回小于等于max_id的微博(获取以前的微博数据)
特点: 如果since_id或者max_id等于0, 那么无效
*/
// 1.封装请求参数
let parameters = ["access_token": UserAccountViewModel.shareInstance.userAccountModel!.access_token!, "since_id": since_id, "max_id": max_id]
// 2.发送请求
GET(path, parameters: parameters, success: { (_, JSON) -> Void in
/*
服务器返回的数据格式:
字典
|--statuses(key) 字典数组
|-- 字典(一条微博数据)
*/
guard let arr = JSON["statuses"] as? [[String: AnyObject]] else
{
finfished(objc: nil, error: NSError(domain: "com.520it.lnj", code: 997, userInfo: ["message": "获取微博数组出错"]))
return
}
// 返回数据
finfished(objc: arr, error: nil)
}) { (_, error) -> Void in
finfished(objc: nil, error: error)
}
}
/**
发送微博
- parameter status: 微博正文
- parameter image: 微博配图
- parameter finfished: 回调
- parameter error: 失败信息
*/
func postStatus(status: String, image: UIImage?, finfished: (objc: [String: AnyObject]?, error: NSError?)->())
{
var path = "2/statuses/"
// 1.封装请求参数
let parameters = ["access_token": UserAccountViewModel.shareInstance.userAccountModel!.access_token!, "status": status]
if image == nil
{
// 发送微博微博
path += "update.json"
// 2.发送请求
POST(path, parameters: parameters, success: { (_, JSON) -> Void in
guard let dict = JSON as? [String: AnyObject] else
{
finfished(objc: nil, error: NSError(domain: "com.520it.lnj", code: 250, userInfo: ["message": "服务器没有返回已经发送的微博"]))
return
}
finfished(objc: dict, error: nil)
}) { (_, error) -> Void in
finfished(objc: nil, error: error)
}
}else
{
// 发送图片微博
path += "upload.json"
// 2.发送请求
POST(path, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
let data = UIImagePNGRepresentation(image!)!
formData.appendPartWithFileData(data, name:"pic",fileName:"abc123", mimeType:"application/octet-stream")
}, success: { (_, JSON) -> Void in
guard let dict = JSON as? [String: AnyObject] else
{
finfished(objc: nil, error: NSError(domain: "com.520it.lnj", code: 250, userInfo: ["message": "服务器没有返回已经发送的微博"]))
return
}
finfished(objc: dict, error: nil)
}) { (_, error) -> Void in
finfished(objc: nil, error: error)
}
}
}
}