// // AccountService.swift // iOSFirst // // Created by 孙宇峰 on 2023/2/3. // import Foundation import Moya enum AccountService { case login(_ username: String, _ password: String, _ showLoading: Bool) } extension AccountService: TargetType { var baseURL: URL { return URL(string: Api.shoolurl)! } var path: String { switch self { case .login: return Api.Account.loginTeacher } } var method: Moya.Method { switch self { case .login: return .post default: return .post } } // var sampleData: Data { // return Data() // } // var task: Task { switch self { case .login(let username, let password, _): return .requestParameters(parameters: parameters, encoding: parameterEncoding) default: return .requestParameters(parameters: parameters, encoding: parameterEncoding) } } /// 参数 public var parameters: [String: Any] { var parameters: [String: Any] = [:] switch self { case .login(let username, let password, _): parameters = ["loginname": username, "loginpwd": password] default: break } for i in commParameters { parameters[i.key] = i.value } return parameters } /// 公共参数 public var commParameters: [String: Any?] { ["ultype": "p_phone_t","versionnum":"3.7.1","logaddress": "","logip":""] } var headers: [String : String]? { switch self { case .login(_, _, let showLoading): return [ "st":"true", "Content-type":"application/json"] default: return nil } } var sampleData: Data { switch self { case .login: return "should be filled properly".utf8Encoded } } var parameterEncoding: ParameterEncoding { switch self { default: // return URLEncoding.default // application/x-www-form-urlencoded return JSONEncoding.default // application/json; charset=utf-8 } } } private extension String { var utf8Encoded:Data { return data(using: .utf8)! } }