api_repository.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/comm/utils/log_utils.dart';
  6. import 'package:ftrecruiter/entity/response/UserLogin.dart';
  7. import 'package:ftrecruiter/entity/response/hotel_login.dart';
  8. import 'package:ftrecruiter/entity/response/industry_data.dart';
  9. import 'package:ftrecruiter/entity/response/server_time.dart';
  10. import 'package:ftrecruiter/entity/response/user_profile.dart';
  11. class ApiRepository {
  12. final ApiProvider apiProvider;
  13. ApiRepository({required this.apiProvider});
  14. // //获取服务器时间
  15. // void getServerTime({NetSuccessCallback<ServerTime>? success, NetErrorCallback? onError}) {
  16. // Map<String, String> headers = {};
  17. // headers["Accept"] = "application/x.yyjobs-api.v1+json";
  18. //
  19. // //获取数据和处理数据的逻辑放在这里
  20. // apiProvider.requestNetEasy(ApiConstants.apiServiceTime, headers: headers, onSuccess: (json) {
  21. // var serverTime = ServerTime.fromJson(json);
  22. // if (success != null) {
  23. // success(serverTime);
  24. // }
  25. // }, onError: onError);
  26. // }
  27. //获取服务器时间2
  28. Future<HttpResult<ServerTime?>> getServerTime2() async {
  29. Map<String, String> headers = {};
  30. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  31. //网络请求获取原始数据
  32. final result = await apiProvider.requestNetResult(ApiConstants.apiServiceTime, headers: headers);
  33. //根据返回的结果,封装原始数据为Bean/Entity对象
  34. if (result.isSuccess) {
  35. final json = result.getDataJson();
  36. var data = ServerTime.fromJson(json!);
  37. //重新赋值data或list
  38. return result.convert<ServerTime?>(data: data);
  39. }
  40. return result.convert<ServerTime?>();
  41. }
  42. /// 获取行业列表
  43. Future<HttpResult<IndustryData?>> getIndustryList2() async {
  44. Map<String, String> headers = {};
  45. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  46. final result = await apiProvider.requestNetResult(ApiConstants.apiIndustryList, headers: headers);
  47. if (result.isSuccess) {
  48. final jsonList = result.getListJson();
  49. //获取List数据 需要转换一次
  50. var list = jsonList?.map((value) {
  51. if (value is Map<String, dynamic>) {
  52. return IndustryData.fromJson(value);
  53. } else {
  54. return null;
  55. }
  56. }).toList();
  57. return result.convert<IndustryData?>(list: list);
  58. }
  59. return result.convert<IndustryData>();
  60. }
  61. //获取用户信息
  62. Future<HttpResult<UserProfile?>> getUserProfile(String? token) async {
  63. Map<String, String> headers = {};
  64. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  65. headers["Authorization"] = "bearer$token";
  66. var result = await apiProvider.requestNetResult(ApiConstants.apiUserProfile, headers: headers);
  67. //根据返回的结果,封装原始数据为Bean/Entity对象
  68. if (result.isSuccess) {
  69. final json = result.getDataJson();
  70. var data = UserProfile.fromJson(json!);
  71. //重新赋值data或list
  72. return result.convert<UserProfile?>(data: data);
  73. }
  74. return result.convert<UserProfile?>();
  75. }
  76. //用户登陆
  77. Future<HttpResult<UserLogin?>> userLogin() async {
  78. Map<String, String> headers = {};
  79. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  80. Map<String, String> params = {};
  81. params["nric_no"] = "+8618571458165";
  82. params["password"] = "12345678";
  83. params["registration_id"] = "1234";
  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. }