SwiftUI实战-网络请求工具封装

SwiftUI实战系列

  • SwiftUI实战-使用ViewModifier自定义微信TabBar底部导航和NavigationView
  • SwiftUI实战-NavigationLink图片和文字显示蓝色或者图片无显示
  • SwiftUI实战-去除List分割线
  • SwiftUI实战-自定义弹窗
  • SwiftUI实战-自定义加载指示器HUD
  • SwiftUI实战-广告页、欢迎页
  • SwiftUI实战-NavigationView + TabView基本框架搭建
  • SwiftUI实战-Pager分页查看器
  • SwiftUI实战-隐私政策和用户协议弹窗
  • SwiftUI实战-多图、大图图片浏览
  • SwiftUI实战-下拉菜单
  • SwiftUI实战-系统NavigationView自定义titleView
  • SwiftUI实战-WKWebView的使用
  • SwiftUI实战-自定义转圈指示器
  • SwiftUI实战-自定义TextField搭建登录页面UI
  • SwiftUI实战-自定义底部弹窗
  • SwiftUI实战-显示星级评分
  • SwiftUI实战-类似新闻头条轮播滚动
  • SwiftUI实战-仿用户协议确认页面支持点击文字显示协议
  • SwiftUI实战-新特性、新版本介绍
  • SwiftUI实战-多图选择、图片选择器
  • SwiftUI实战-轮播图
  • SwiftUI实战-网络请求工具封装
  • SwiftUI实战-多级联动地址选择
  • SwiftUI实战-使用UIActivityIndicatorView
  • SwiftUI实战-Expanded可伸缩的分组列表List
  • SwiftUI实战-滚动列表内容返回顶部、底部、指定位置
  • SwiftUI实战-List列表内容动态改变更新
  • SwiftUI-绘制气泡图
  • SwiftUI-仿微信加号Popover气泡弹窗
  • SwiftUI实战-单边圆角单个圆角

本章内容

实现的请求方式:

  • 1、GET请求
  • 2、POST请求
  • 3、RAW请求
  • 4、FORM请求

使用示例:
1、GET请求

    func GETRequest() {
        let request: NNetworkRequest = NNetworkRequest()
        request.host = "http://www.weather.com.cn/data/sk/101010100.html"
        //request.host = "https://livetest.ofweek.com/api/web"
        request.requestMethod = .GET
        NetworkService.shared.fetch(with: request)
            .catch { error -> AnyPublisher in
            // 如果发生错误,则不会往下走
            Fail(error: error).eraseToAnyPublisher()
        } .sink { completion in
            switch completion {
            case .failure(let error):
                if let error = error as? NetworkError {
                    debugPrint(error.description)
                } else {
                    debugPrint(error.localizedDescription)
                }
            default:
                print(completion)
            }
        } receiveValue: { weatherModel in
            print("成功")
        }
        .store(in: &cancellables)
    }

2、POST请求

    func POSTRequest() {
        let request: NNetworkRequest = NNetworkRequest()
        request.host = ""
        request.methodName = "/login/memberlogin"
        request.requestMethod = .POST
        let params = ["account": "", "password": "123456"]
        request.params = params
        NetworkService.shared.fetch(with: request)
            .catch { error -> AnyPublisher in
            // 如果发生错误,则不会往下走
            Fail(error: error).eraseToAnyPublisher()
        } .sink { completion in
            switch completion {
            case .failure(let error):
                if let error = error as? NetworkError {
                    debugPrint(error.description)
                } else {
                    debugPrint(error.localizedDescription)
                }
            default:
                print(completion)
            }
        } receiveValue: { userInfoModel in
            print("成功")
        }
        .store(in: &cancellables)
    }

3、RAW请求

    func RAWRequest() {
        let request: NNetworkRequest = NNetworkRequest()
        request.host = ""
        request.methodName = "/login/memberlogin"
        request.requestMethod = .RAW
        let params = ["account": "", "password": "123456"]
        request.params = params
        NetworkService.shared.fetch(with: request)
            .catch { error -> AnyPublisher in
            // 如果发生错误,则不会往下走
            Fail(error: error).eraseToAnyPublisher()
        } .sink { completion in
            switch completion {
            case .failure(let error):
                if let error = error as? NetworkError {
                    debugPrint(error.description)
                } else {
                    debugPrint(error.localizedDescription)
                }
            default:
                print(completion)
            }
        } receiveValue: { userInfoModel in
            print("成功")
        }
        .store(in: &cancellables)
    }

4、FORM请求

    func FORMRequest() {
        let request: NFileNetworkRequest = NFileNetworkRequest()
        request.host = ""
        request.methodName = "/file/uploadfile2"
        request.requestMethod = .FORM
        let params = ["filetype": "0", "module": "msgchat"]
        request.params = params
        let fileData: NFileData = NFileData.init()
        fileData.name = "file"
        fileData.filename = "file.png"
        fileData.contentType = "image/png"
        guard let data = UIImage.init(named: "test")?.jpegData(compressionQuality: 1.0) else {
            return
        }
        fileData.data = data
        request.files = ["file": fileData]
        NetworkService.shared.fetch(with: request)
            .catch { error -> AnyPublisher in
            // 如果发生错误,则不会往下走
            Fail(error: error).eraseToAnyPublisher()
        } .sink { completion in
            switch completion {
            case .failure(let error):
                if let error = error as? NetworkError {
                    debugPrint(error.description)
                } else {
                    debugPrint(error.localizedDescription)
                }
            default:
                print(completion)
            }
        } receiveValue: { userInfoModel in
            print("成功")
        }
        .store(in: &cancellables)
    }

相关源码如下:

你可能感兴趣的:(SwiftUI实战-网络请求工具封装)