api_repository.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'dart:io';
  2. import 'package:ftrecruiter/api/api_provider.dart';
  3. import 'package:ftrecruiter/api/http_result.dart';
  4. import 'package:ftrecruiter/comm/constants/api_constants.dart';
  5. import 'package:ftrecruiter/entity/response/industry_data.dart';
  6. import 'package:ftrecruiter/entity/response/server_time.dart';
  7. import 'package:ftrecruiter/entity/response/user_login.dart';
  8. import 'package:ftrecruiter/entity/response/user_profile.dart';
  9. import 'package:get/get.dart';
  10. class ApiRepository {
  11. ApiProvider apiProvider;
  12. ApiRepository({required this.apiProvider});
  13. // //获取服务器时间
  14. // void getServerTime({NetSuccessCallback<ServerTime>? success, NetErrorCallback? onError}) {
  15. // Map<String, String> headers = {};
  16. // headers["Accept"] = "application/x.yyjobs-api.v1+json";
  17. //
  18. // //获取数据和处理数据的逻辑放在这里
  19. // apiProvider.requestNetEasy(ApiConstants.apiServiceTime, headers: headers, onSuccess: (json) {
  20. // var serverTime = ServerTime.fromJson(json);
  21. // if (success != null) {
  22. // success(serverTime);
  23. // }
  24. // }, onError: onError);
  25. // }
  26. //获取服务器时间2
  27. Future<HttpResult<ServerTime?>> getServerTime2() async {
  28. Map<String, String> headers = {};
  29. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  30. //网络请求获取原始数据
  31. final result = await apiProvider.requestNetResult(ApiConstants.apiServiceTime, headers: headers);
  32. //根据返回的结果,封装原始数据为Bean/Entity对象
  33. if (result.isSuccess) {
  34. final json = result.getDataJson();
  35. var data = ServerTime.fromJson(json!);
  36. //重新赋值data或list
  37. return result.convert<ServerTime?>(data: data);
  38. }
  39. return result.convert<ServerTime?>();
  40. }
  41. /// 获取行业列表
  42. Future<HttpResult<IndustryData?>> getIndustryList2() async {
  43. Map<String, String> headers = {};
  44. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  45. final result = await apiProvider.requestNetResult(ApiConstants.apiIndustryList, headers: headers);
  46. if (result.isSuccess) {
  47. final jsonList = result.getListJson();
  48. //获取List数据 需要转换一次
  49. var list = jsonList?.map((value) {
  50. if (value is Map<String, dynamic>) {
  51. return IndustryData.fromJson(value);
  52. } else {
  53. return null;
  54. }
  55. }).toList();
  56. return result.convert<IndustryData?>(list: list);
  57. }
  58. return result.convert<IndustryData>();
  59. }
  60. //获取用户信息
  61. Future<HttpResult<UserProfile?>> getUserProfile(String? token) async {
  62. Map<String, String> headers = {};
  63. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  64. headers["Authorization"] = "bearer$token";
  65. var result = await apiProvider.requestNetResult(ApiConstants.apiUserProfile, headers: headers);
  66. //根据返回的结果,封装原始数据为Bean/Entity对象
  67. if (result.isSuccess) {
  68. final json = result.getDataJson();
  69. var data = UserProfile.fromJson(json!);
  70. //重新赋值data或list
  71. return result.convert<UserProfile?>(data: data);
  72. }
  73. return result.convert<UserProfile?>();
  74. }
  75. //用户登陆
  76. Future<HttpResult<UserLogin?>> userLogin() async {
  77. Map<String, String> headers = {};
  78. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  79. Map<String, String> params = {};
  80. params["nric_no"] = "+8618571458165";
  81. params["password"] = "12345678";
  82. params["registration_id"] = "1234";
  83. apiProvider = apiProvider.cancelAndResetHttpClient();
  84. var result = await apiProvider.requestNetResult(ApiConstants.apiUserLogin,
  85. method: HttpMethod.POST, headers: headers, query: params);
  86. //根据返回的结果,封装原始数据为Bean/Entity对象
  87. if (result.isSuccess) {
  88. final json = result.getDataJson();
  89. var data = UserLogin.fromJson(json!);
  90. //重新赋值data或list
  91. return result.convert<UserLogin?>(data: data);
  92. }
  93. return result.convert<UserLogin?>();
  94. }
  95. //更新用户信息
  96. Future<HttpResult<UserLogin?>> userProfileUpdate(String? token) async {
  97. Map<String, String> headers = {};
  98. headers["Accept"] = "application/x.yyjobs-api.v12+json";
  99. headers["Authorization"] = "bearer$token";
  100. Map<String, String> params = {};
  101. params["mobile_no"] = "+8618571458166";
  102. params["verification_code"] = "123456 ";
  103. params["nick_name"] = "test liukai";
  104. Map<String, String> paths = {};
  105. paths['news_feed_avatar'] = '/data/user/0/com.hongyegroup.ftrecruiter/app_flutter/test_avatar.jpeg';
  106. var result = await apiProvider.requestNetResult(
  107. ApiConstants.apiUpdateProfile,
  108. method: HttpMethod.POST,
  109. headers: headers,
  110. query: params,
  111. paths: paths,
  112. );
  113. //根据返回的结果,封装原始数据为Bean/Entity对象
  114. if (result.isSuccess) {
  115. final json = result.getDataJson();
  116. var data = UserLogin.fromJson(json!);
  117. //重新赋值data或list
  118. return result.convert<UserLogin?>(data: data);
  119. }
  120. return result.convert<UserLogin?>();
  121. }
  122. //用户登陆(回调的方式)
  123. void userLoginEasy({NetSuccessCallback<UserLogin>? success, NetErrorCallback? onError}) {
  124. Map<String, String> headers = {};
  125. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  126. Map<String, String> params = {};
  127. params["nric_no"] = "+8618571458166";
  128. params["password"] = "12345678";
  129. params["registration_id"] = "1234";
  130. apiProvider.requestNetEasy(ApiConstants.apiUserLogin, method: HttpMethod.POST, headers: headers, query: params,
  131. onSuccess: (json) {
  132. var userLogin = UserLogin.fromJson(json);
  133. if (success != null) {
  134. success(userLogin);
  135. }
  136. }, onError: onError);
  137. }
  138. }