1 . Alamofire的封装
//
// AlamofireHelper.swift
// JackUChat
//
// Created by liy on 2018/12/28.
//
import Alamofire
import SwiftyJSON
let baseUrl = "https://api.uchat.com.cn/test/"
var baseParams:Parameters = ["device":"app","ver":"1"]
// MARK: - 创建单例模式
class AlamofireHelper{
class var shareInstance: AlamofireHelper {
let alamofireHelper = AlamofireHelper()
return alamofireHelper
}
}
enum MethodType{
case get
case post
}
// MARK: - 网络请求封装
extension AlamofireHelper{
// MARK: - 无参测试/get
func requestTest() {
Alamofire.request("https://httpbin.org/get").responseJSON{ response in
print("responseJSON->原始的URL请求:\(String(describing: response.request))")
print("responseJSON->HTTP URL响应:\(String(describing: response.response))")
print("responseJSON->服务器返回数据:\(String(describing: response.data))")
if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
print("responseJSON->UTF-8格式的原始服务器数据:\(utf8Text)")
}
print("responseJSON->响应序列化结果:\(response.result)")
switch response.result{
case .success:
if let json = response.result.value{
print("jresponseJSON->son数据:","JSON:\(json)")
}
case .failure(let error):
print(error)
}
}
}
// MARK: - 有参测试/post
func requestTest1() {
//let paras:Parameters = ["username":"001","password":"123456"]
let params:Parameters = ["userId":"128","companyId":"50"]
Alamofire.request("https://api.uchat.com.cn/test/app", method: .post, parameters: params)
.responseJSON{ response in
print("responseJSON->原始的URL请求:\(String(describing: response.request))")
print("responseJSON->HTTP URL响应:\(String(describing: response.response))")
print("responseJSON->服务器返回数据:\(String(describing: response.data))")
if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
print("responseJSON->UTF-8格式的原始服务器数据:\(utf8Text)")
}
print("responseJSON->响应序列化结果:\(response.result)")
switch response.result{
case .success:
if let json = response.result.value{
print("jresponseJSON->son数据:","JSON:\(json)")
}
case .failure(let error):
print(error)
}
}
}
// MARK: - 登录测试
static func verifityUserLoginTest(username:String,password:String,completion:@escaping (String?) -> Void) {
let params:Parameters = ["username":username,"password":password]
let url = baseUrl
Alamofire.request(url, method: .post, parameters: params)
.validate(statusCode: 200..<300)//认证失败则可在响应处理中处理相关错误
.validate(contentType: ["application/json"])
.response{response in
print("response->Request:\(String(describing: response.request))")
print("response->Response:\(String(describing: response.response))")
print("response->Error:\(String(describing: response.error))")
if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
print("response->Data:\(utf8Text)")
completion(utf8Text)
}
}
.responseData{response in
debugPrint("responseData->All Response Info:\(response)")
switch response.result{
case .success:
print("responseData->Validation Successful")
case .failure(let error):
print(error)
}
}
.responseString{response in
print("responseString->Success:\(response.result.isSuccess)")
print("responseString->Response String:\(String(describing: response.result.value))")
if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
print("responseString->UTF-8格式的原始服务器数据:\(utf8Text)")
}
}
.responseJSON{ response in
debugPrint(response)
print("responseJSON->原始的URL请求:\(String(describing: response.request))")
print("responseJSON->HTTP URL响应:\(String(describing: response.response))")
print("responseJSON->服务器返回数据:\(String(describing: response.data))")
if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
print("responseJSON->UTF-8格式的原始服务器数据:\(utf8Text)")
}
print("responseJSON->响应序列化结果:\(response.result)")
switch response.result{
case .success:
if let json = response.result.value{
print("jresponseJSON->son数据:","JSON:\(json)")
}
case .failure(let error):
print(error)
}
}
}
// MARK: - 登录(正式版)
static func verifityUserLogin(url:String,username:String,password:String,completion:@escaping (_ result:Any) -> Void) {
let url = baseUrl + url
var params:Parameters = ["username":username,"password":password]
for (key , value) in baseParams {
params[key] = value
}
Alamofire.request(url, method: .post, parameters: params)
.validate(statusCode: 200..<300)//认证失败则可在响应处理中处理相关错误
.validate(contentType: ["application/json"])
// .response{response in
// print("response->Request:\(String(describing: response.request))")
// print("response->Response:\(String(describing: response.response))")
// print("response->Error:\(String(describing: response.error))")
// if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
// print("response->Data:\(utf8Text)")
// }
// }
.responseJSON{ response in
//debugPrint(response)
print("responseJSON->原始的URL请求:\(String(describing: response.request))")
print("responseJSON->HTTP URL响应:\(String(describing: response.response))")
print("responseJSON->服务器返回数据:\(String(describing: response.data))")
if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
print("responseJSON->UTF-8格式的原始服务器数据:\(utf8Text)")
//{"code":"0","msg":"success","res":{"status":1}}
//"code":"1","msg":"提示信息"}
}
print("responseJSON->响应序列化结果:\(response.result)")
switch response.result{
case .success:
if let json = response.result.value{
print("jresponseJSON->son数据:","JSON:\(json)")
completion(json)
}
case .failure(let error):
print(error)
}
}
}
// MARK: - 发送POST请求
func postRequest(url:String,params:[String:Any],completion:@escaping(_ response:[String:AnyObject]?,_ error:NSError?)->()) {
let url = baseUrl + url
Alamofire.request(url, method: .post, parameters: params)
.responseJSON { (response) in
if response.result.isSuccess{
completion(response.result.value as? [String:AnyObject],nil)
}else{
completion(nil,response.result.error as NSError?)
}
}
}
// MARK: - 发送GET请求
func getRequest(url:String,params:[String:Any]?,completion:@escaping(_ response:[String:AnyObject]?,_ error:NSError?) -> ()){
let url = baseUrl + url
Alamofire.request(url, method: .get, parameters: params)
.responseJSON{ response in
switch response.result {
case .success(let value):
if let value = response.result.value as? [String:AnyObject]{
completion(value,nil)
}
let json = JSON(value)
print(json)
case .failure(let error):
print(error)
completion(nil,response.result.error as NSError?)
}
}
}
// MARK: - GET和POST请求整合(正式版)
func requestData(_ type:MethodType,url:String,parameters:[String:Any]? = nil,completion:@escaping (_ result:Any) -> Void) {
//获取类型
let method = type == .get ? HTTPMethod.get : HTTPMethod.post
//发送网络请求
let url = baseUrl + url
var params:Parameters = parameters!
for (key , value) in baseParams {
params[key] = value
}
Alamofire.request(url, method: method, parameters: params)
.responseJSON{ response in
print("responseJSON->原始的URL请求:\(String(describing: response.request))")
print("responseJSON->HTTP URL响应:\(String(describing: response.response))")
print("responseJSON->服务器返回数据:\(String(describing: response.data))")
if let data = response.data,let utf8Text = String(data: data, encoding: .utf8){
print("responseJSON->UTF-8格式的原始服务器数据:\(utf8Text)")
}
print("responseJSON->响应序列化结果:\(response.result)")
//获取结果
guard let json = response.result.value else{
print("jresponseJSON->error:",response.result.error!)
return
}
print("jresponseJSON->son数据:","JSON:\(json)")
completion(json)
}
}
}
2 . 调用
2.1 登录(简单json)
import SwiftyJSON
@IBAction func loginBtnTap(_ sender: UIButton) {
let phone = inputPhoneField.text!
let password = inputPasswordField.text!
if !phone.isEmpty && !password.isEmpty{
AlamofireHelper.verifityUserLogin(url: "login",username: phone,password: password, completion: {(result) in
let jsonDictory = JSON(result as Any)
let code = jsonDictory["code"].string
let msg = jsonDictory["msg"].string
print(code!+","+msg!)
if(code == "0"){
print("成功")
}else{
print("失败")
}
})
}else{
print("请输入完整信息!")
}
}
备注:
(1)登录成功返回
{"code":"0","msg":"success","res":{"status":1}}
(1)登录失败返回
{"code":"1","msg":"提示信息"}
2.2 获取并解析数据(复杂json)
var devices:[Device] = []
func getListByAlomafire() {
let params:Parameters = ["userId":"128","companyId":"50"]
AlamofireHelper.shareInstance.requestData(.post, url: "app", parameters: params) { (result) in
let jsonDictory = JSON(result as Any)
let code = jsonDictory["code"].string
let msg = jsonDictory["msg"].string
if(code == "0"){
print("成功获取设备列表:"+code!+","+msg!)
let machineList = jsonDictory["res"]["machineList"].array
for machi in machineList!{
// let machine = JSON(machi["machine"] as Any)
// let device = Device(deviceId: machine["sn"].string ?? "", deviceName: machine["name"].string ?? "", deviceStatus: machine["status"].string ?? "", deviceCount: "", deviceImage: machine["image"].string ?? "", date: machine["addTime"].string ?? "")
let device = Device(deviceId: machi["machine"]["sn"].string ?? "", deviceName: machi["machine"]["name"].string ?? "", deviceStatus: machi["machine"]["status"].string ?? "", deviceCount: "", deviceImage: machi["machine"]["image"].string ?? "", date: machi["machine"]["addTime"].string ?? "")
self.devices.append(device)
}
dump(self.devices)//打印
}else{
print("失败")
}
}
}
struct Device:Codable {
var deviceId:String
var deviceName:String
var deviceStatus:String
var deviceCount:String
var deviceImage:String
var date:String
}
备注:
(1)获取到的网络json数据为
{
"code": "0",
"msg": "success",
"res": {
"machineList": [
{
"cm_id": "319",
"company_id": "50",
"machine_id": "32",
"cate": {
"cate_id": "102",
"sort": "0",
"parent_id": "2",
"cate_name": "MA04自动连续锁眼机",
"image": "/upload/image/2018/09/60d9f57d939c310894db9f819128e36c.jpg",
"machine_num": "0",
"unit": "个",
"time_unit": "秒",
"is_jtj": "0",
"tiered_num": "0",
"tiered_discount": "0.00"
},
"cust_group": "10",
"is_lock": "0",
"is_rent": "0",
"status": "1",
"machine": {
"machine_id": "32",
"cate": "2",
"cate2": "102",
"name": "问题测试 ",
"sn": "Qtest",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/upload/image/2018/09/60d9f57d939c310894db9f819128e36c_150x150.jpg",
"tags": null,
"protocol": null,
"location": "",
"device_id": "39590889",
"imsi": null,
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "22706",
"ci": "48888",
"csq": "0",
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1534904725",
"rent_out": "1",
"status": "0",
"status_time": "1535339005",
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": "{\"\":null}",
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"use_time": 0,
"i": 1
},
{
"cm_id": "318",
"company_id": "50",
"machine_id": "33",
"cate": "0",
"cust_group": "10",
"is_lock": "0",
"is_rent": "0",
"status": "1",
"machine": {
"machine_id": "33",
"cate": "2",
"cate2": "0",
"name": "迈卡袖衩机",
"sn": "0109",
"channel": "1",
"price": "0.300",
"description": null,
"image": "http://s.uchat.com.cn/public/images/nopic300.png",
"tags": null,
"protocol": null,
"location": "",
"device_id": "39597199",
"imsi": null,
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "0",
"ci": "0",
"csq": "0",
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1534917235",
"rent_out": "1",
"status": "0",
"status_time": null,
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": "{\"\":null}",
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"use_time": 0,
"i": 2
},
{
"cm_id": "306",
"company_id": "50",
"machine_id": "25",
"cate": "0",
"cust_group": "0",
"is_lock": "0",
"is_rent": "1",
"status": "1",
"machine": {
"machine_id": "25",
"cate": "2",
"cate2": "0",
"name": "开发测试(agent)",
"sn": "LHCS02",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/public/images/nopic300.png",
"tags": null,
"protocol": null,
"location": "",
"device_id": "38785262",
"imsi": "460040148224239",
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "22706",
"ci": "48888",
"csq": "12",
"lat": "",
"lng": "",
"software": "macia_datacollector_v2.0.0",
"build_time": "0",
"hardware": "Protocol_Converter_v3.5",
"ota_time": "0",
"add_time": "1533892588",
"rent_out": "1",
"status": "0",
"status_time": "1545917649",
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": "{\"\":null}",
"devctrl": "1",
"sale_type": "1",
"is_del": "0"
},
"use_time": 0,
"i": 3
},
{
"cm_id": "189",
"company_id": "50",
"machine_id": "142",
"cate": {
"cate_id": "106",
"sort": "0",
"parent_id": "2",
"cate_name": "MCD门襟卷缝机",
"image": "/upload/image/2018/09/09e8b04e355df957610c6e33d81275de.jpg",
"machine_num": "0",
"unit": "个",
"time_unit": "秒",
"is_jtj": "0",
"tiered_num": "0",
"tiered_discount": "0.00"
},
"cust_group": "0",
"is_lock": "0",
"is_rent": "0",
"status": "1",
"machine": {
"machine_id": "142",
"cate": "2",
"cate2": "106",
"name": "门襟卷缝机(一楼测试2)",
"sn": "MCD201793",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/upload/image/2018/09/09e8b04e355df957610c6e33d81275de_150x150.jpg",
"tags": null,
"protocol": null,
"location": "",
"device_id": "505776789",
"imsi": "460040148225988",
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "26475",
"ci": "12266",
"csq": "0",
"lat": "",
"lng": "",
"software": "macia_datacollector_v2.0.0",
"build_time": "0",
"hardware": "Protocol_Converter_v3.5",
"ota_time": "0",
"add_time": "1544418000",
"rent_out": "1",
"status": "0",
"status_time": "1546847997",
"login_type": "7",
"maintain_time": "1000",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": null,
"devctrl": "0",
"sale_type": "2",
"is_del": "0"
},
"use_time": 0,
"i": 4
}
],
"machineNum": 4,
"machineGroup": [
{
"group_id": "10",
"name": "包缝机"
}
],
"onlineNum": 0,
"company_id": 50
}
}
(2)完整代码为:
//
// DeviceViewController.swift
// JackUChat
//
// Created by 徐云 on 2019/1/2.
// Copyright © 2019 Liy. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
import Just
import Kingfisher
class DeviceViewController: UIViewController,UITableViewDataSource,UISearchResultsUpdating {
@IBOutlet weak var tableView: UITableView!
var devices:[Device] = []
//var sc : UISearchController!
//var searchResults : [Device] = []//定义空数组保存筛选结果
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devices.count
//return sc.isActive ? searchResults.count : devices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellId = String(describing: DeviceCell.self)
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DeviceCell
let device = devices[indexPath.row]
//let device = sc.isActive ? searchResults[indexPath.row] : devices[indexPath.row]
cell.deviceNameLabel.text = device.deviceName
cell.deviceNoLabel.text = device.deviceId
cell.deviceStatusLabel.text = device.deviceStatus
let imgUrl = URL(string: device.deviceImage)
cell.deviceImageView.kf.setImage(with: imgUrl)
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// sc = UISearchController(searchResultsController: nil)
// sc.searchResultsUpdater = self
// tableView.tableHeaderView = sc.searchBar
// sc.searchBar.showsCancelButton = true
// sc.searchBar.showsScopeBar = true
// sc.searchBar.selectedScopeButtonIndex = 0
// sc.searchBar.scopeButtonTitles = ["所有设备","平缝机","包缝机"]
// self.navigationController?.extendedLayoutIncludesOpaqueBars = true
//loadJson()
getListByAlomafire()
//getListByJust()
//下拉刷新(tableView控制器自带下拉刷新功能)
//self.tableView.refreshControl = UIRefreshControl()
//self.tableView.refreshControl?.addTarget(self, action: #selector(getListByJust), for: .valueChanged)
}
//Json解码:json文本转对象(这里是从主包中获取的json数据,真实json数据应从服务器获取)
func loadJson() {
let coder = JSONDecoder()
do {
//从主包中找到weapons.json文件
let url = Bundle.main.url(forResource: "devices", withExtension: "json")!
//从地址中取到数据
let data = try Data(contentsOf: url)
//将json数据解码为Weapon数据(对象)
devices = try coder.decode([Device].self, from: data)
print("解码成功:")
} catch {
print("解码错误:",error)
}
}
func getListByAlomafire() {
let params:Parameters = ["userId":"128","companyId":"50"]
AlamofireHelper.shareInstance.requestData(.post, url: "app", parameters: params) { (result) in
let jsonDictory = JSON(result as Any)
let code = jsonDictory["code"].string
let msg = jsonDictory["msg"].string
if(code == "0"){
print("成功获取设备列表:"+code!+","+msg!)
let machineList = jsonDictory["res"]["machineList"].array
for machi in machineList!{
// let machine = JSON(machi["machine"] as Any)
// let device = Device(deviceId: machine["sn"].string ?? "", deviceName: machine["name"].string ?? "", deviceStatus: machine["status"].string ?? "", deviceCount: "", deviceImage: machine["image"].string ?? "", date: machine["addTime"].string ?? "")
let device = Device(deviceId: machi["machine"]["sn"].string ?? "", deviceName: machi["machine"]["name"].string ?? "", deviceStatus: machi["machine"]["status"].string ?? "", deviceCount: "", deviceImage: machi["machine"]["image"].string ?? "", date: machi["machine"]["addTime"].string ?? "")
self.devices.append(device)
}
dump(self.devices)//打印
//异步获取数据,需在主线程中更新
OperationQueue.main.addOperation {
self.tableView.reloadData()
self.tableView.refreshControl?.endRefreshing()//加载完数据后停止下拉刷新动画
}
}else{
print("失败")
}
}
}
@objc func getListByJust() {
let paras = ["userId":"128","companyId":"50"]
Just.post("https://api.uchat.com.cn/test/app", params: paras) { (r) in
guard let json = r.json as? NSDictionary else{
print("返回")
return
}
//let res = RootClass(fromDictionary: json).res!
let machineList = RootClass(fromDictionary: json).res.machineList!
//数组转换:map方法接受一个闭包作为参数, 然后它会遍历整个数组,并对数组中每一个元素执行闭包中定义的操作,相当于对数组中的所有元素做了一个映射.
self.devices = machineList.map{ (machi) -> Device in
return Device(deviceId: machi.machine.sn, deviceName: machi.machine.name, deviceStatus: machi.machine.status, deviceCount: "", deviceImage: machi.machine.image, date: machi.machine.addTime)
}
dump(self.devices)//打印
//异步获取数据,需在主线程中更新
OperationQueue.main.addOperation {
self.tableView.reloadData()
self.tableView.refreshControl?.endRefreshing()//加载完数据后停止下拉刷新动画
}
}
}
func updateSearchResults(for searchController: UISearchController) {
//获取搜索栏文字,筛选后刷新列表
// if let text = searchController.searchBar.text {
// searchFilter(text: text)
// tableView.reloadData()
// }
}
//添加一个筛选器方法:使用Swift数组自带filter方法,返回一个符合条件的新数组
// func searchFilter(text:String) {
// if (sc.searchBar.selectedScopeButtonIndex == 0) {
// //devices = devices1
// print("所有设备")
// }else if (sc.searchBar.selectedScopeButtonIndex == 1) {
// //devices = devices2
// print("平缝机")
// }else if (sc.searchBar.selectedScopeButtonIndex == 2) {
// //devices = devices2
// print("包缝机")
// }
// searchResults = devices.filter({ (device) -> Bool in
// return device.deviceName.localizedCaseInsensitiveContains(text)
// })
// }
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if segue.identifier == "showDeviceDetail" {
let row = tableView.indexPathForSelectedRow!.row
//let destination = segue.destination as! WeaponDetailViewController
let destination = segue.destination as! DeviceDetailViewController
destination.device = devices[row]//转场传值
}
}
}
网友评论