api_repository.dart 5.5 KB

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