iOS-study
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccountService.swift 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // AccountService.swift
  3. // iOSFirst
  4. //
  5. // Created by 孙宇峰 on 2023/2/3.
  6. //
  7. import Foundation
  8. import Moya
  9. enum AccountService {
  10. case login(_ username: String, _ password: String, _ showLoading: Bool)
  11. }
  12. extension AccountService: TargetType {
  13. var baseURL: URL {
  14. return URL(string: Api.shoolurl)!
  15. }
  16. var path: String {
  17. switch self {
  18. case .login:
  19. return Api.Account.loginTeacher
  20. }
  21. }
  22. var method: Moya.Method {
  23. switch self {
  24. case .login:
  25. return .post
  26. default:
  27. return .post
  28. }
  29. }
  30. // var sampleData: Data {
  31. // return Data()
  32. // }
  33. //
  34. var task: Task {
  35. switch self {
  36. case .login(let username, let password, _):
  37. return .requestParameters(parameters: parameters,
  38. encoding: parameterEncoding)
  39. default:
  40. return .requestParameters(parameters: parameters, encoding: parameterEncoding)
  41. }
  42. }
  43. /// 参数
  44. public var parameters: [String: Any] {
  45. var parameters: [String: Any] = [:]
  46. switch self {
  47. case .login(let username, let password, _):
  48. parameters = ["loginname": username, "loginpwd": password]
  49. default:
  50. break
  51. }
  52. for i in commParameters {
  53. parameters[i.key] = i.value
  54. }
  55. return parameters
  56. }
  57. /// 公共参数
  58. public var commParameters: [String: Any?] {
  59. ["ultype": "p_phone_t","versionnum":"3.7.1","logaddress": "","logip":""]
  60. }
  61. var headers: [String : String]? {
  62. switch self {
  63. case .login(_, _, let showLoading):
  64. return [
  65. "st":"true",
  66. "Content-type":"application/json"]
  67. default:
  68. return nil
  69. }
  70. }
  71. var sampleData: Data {
  72. switch self {
  73. case .login:
  74. return "should be filled properly".utf8Encoded
  75. }
  76. }
  77. var parameterEncoding: ParameterEncoding {
  78. switch self {
  79. default:
  80. // return URLEncoding.default // application/x-www-form-urlencoded
  81. return JSONEncoding.default // application/json; charset=utf-8
  82. }
  83. }
  84. }
  85. private extension String {
  86. var utf8Encoded:Data {
  87. return data(using: .utf8)!
  88. }
  89. }