main_repository.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import 'package:domain/entity/feedback_detail_entity.dart';
  2. import 'package:domain/entity/home_list_entity.dart';
  3. import 'package:domain/entity/latest_news_detail_entity.dart';
  4. import 'package:domain/entity/latest_news_page_entity.dart';
  5. import 'package:domain/entity/visitor_page_entity.dart';
  6. import 'package:plugin_platform/platform_export.dart';
  7. import 'package:plugin_platform/http/dio_engine.dart';
  8. import 'package:plugin_platform/http/http_result.dart';
  9. import 'package:riverpod_annotation/riverpod_annotation.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. import 'package:shared/utils/util.dart';
  12. import '../constants/api_constants.dart';
  13. import 'package:flutter_riverpod/flutter_riverpod.dart';
  14. import 'package:plugin_basic/provider/http_provider/http_provider.dart';
  15. import '../entity/feedback_list_entity.dart';
  16. import '../entity/id_name_entity.dart';
  17. import '../entity/property_news_entity.dart';
  18. part 'main_repository.g.dart';
  19. @Riverpod(keepAlive: true)
  20. MainRepository mainRepository(Ref ref) {
  21. final dioEngine = ref.watch(dioEngineProvider);
  22. return MainRepository(dioEngine: dioEngine);
  23. }
  24. /*
  25. * 首页的数据仓库
  26. */
  27. class MainRepository {
  28. DioEngine dioEngine;
  29. MainRepository({required this.dioEngine});
  30. /// 获取反馈的分类
  31. Future<HttpResult<IdNameEntity>> fetchFeedbackCategory({
  32. CancelToken? cancelToken,
  33. }) async {
  34. final result = await dioEngine.requestNetResult(
  35. ApiConstants.apiFeedbackCategory,
  36. method: HttpMethod.GET,
  37. isShowLoadingDialog: true,
  38. networkDebounce: true,
  39. cancelToken: cancelToken,
  40. );
  41. if (result.isSuccess) {
  42. final jsonList = result.getListJson();
  43. //获取List数据 需要转换一次
  44. var list = jsonList
  45. ?.map((value) {
  46. if (value is Map<String, dynamic>) {
  47. return IdNameEntity.fromJson(value);
  48. } else {
  49. return null;
  50. }
  51. })
  52. .where((item) => item != null)
  53. .cast<IdNameEntity>()
  54. .toList();
  55. return result.convert<IdNameEntity>(list: list);
  56. }
  57. return result.convert<IdNameEntity>();
  58. }
  59. /// 发布反馈
  60. Future<HttpResult> postFeedback({
  61. required String? categoryId,
  62. required String? title,
  63. required String? content,
  64. List<String>? paths,
  65. CancelToken? cancelToken,
  66. }) async {
  67. Map<String, String> params = {};
  68. params['category_id'] = categoryId!;
  69. params['title'] = title!;
  70. params['content'] = content!;
  71. Map<String, String> files = {};
  72. if (paths != null && paths.isNotEmpty) {
  73. paths.asMap().forEach((index, path) {
  74. files["resources[$index]"] = path;
  75. });
  76. }
  77. final result = await dioEngine.requestNetResult(
  78. ApiConstants.apiFeedbackPost,
  79. params: params,
  80. paths: files,
  81. method: HttpMethod.POST,
  82. isShowLoadingDialog: true,
  83. networkDebounce: true,
  84. cancelToken: cancelToken,
  85. );
  86. if (result.isSuccess) {
  87. return result.convert();
  88. }
  89. return result.convert();
  90. }
  91. /// 已发布的反馈列表
  92. Future<HttpResult<FeedbackListEntity>> fetchFeedbackList({
  93. required String status,
  94. required int curPage,
  95. CancelToken? cancelToken,
  96. }) async {
  97. Map<String, String> params = {};
  98. params['status'] = status;
  99. params['page'] = curPage.toString();
  100. params['limit'] = "10";
  101. final result = await dioEngine.requestNetResult(
  102. ApiConstants.apiFeedbackList,
  103. params: params,
  104. method: HttpMethod.GET,
  105. cancelToken: cancelToken,
  106. );
  107. if (result.isSuccess) {
  108. final json = result.getDataJson();
  109. var data = FeedbackListEntity.fromJson(json!);
  110. return result.convert<FeedbackListEntity>(data: data);
  111. }
  112. return result.convert();
  113. }
  114. /// 获取反馈详情
  115. Future<HttpResult<FeedbackDetailEntity>> fetchFeedbackDetail({
  116. required String? id,
  117. CancelToken? cancelToken,
  118. }) async {
  119. Map<String, String> params = {};
  120. params['id'] = id ?? "";
  121. final result = await dioEngine.requestNetResult(
  122. ApiConstants.apiFeedbackDetail,
  123. method: HttpMethod.GET,
  124. params: params,
  125. isShowLoadingDialog: true,
  126. networkDebounce: true,
  127. cancelToken: cancelToken,
  128. );
  129. if (result.isSuccess) {
  130. final json = result.getDataJson();
  131. var data = FeedbackDetailEntity.fromJson(json!);
  132. return result.convert<FeedbackDetailEntity>(data: data);
  133. }
  134. return result.convert();
  135. }
  136. /// 创建访客的信息
  137. Future<HttpResult> createVisitor({
  138. required String? name,
  139. required String? phone,
  140. required String? nric,
  141. required String? vehicleNumber,
  142. required String? accessDate,
  143. required String? note,
  144. CancelToken? cancelToken,
  145. }) async {
  146. Map<String, String> params = {};
  147. params['name'] = name ?? "";
  148. params['phone'] = phone ?? "";
  149. params['nric'] = nric ?? "";
  150. params['access_date'] = accessDate ?? "";
  151. if (Utils.isNotEmpty(vehicleNumber)) {
  152. params['vehicle_number'] = vehicleNumber!;
  153. }
  154. if (Utils.isNotEmpty(note)) {
  155. params['note'] = note!;
  156. }
  157. final result = await dioEngine.requestNetResult(
  158. ApiConstants.apiCreateVisitor,
  159. params: params,
  160. method: HttpMethod.POST,
  161. isShowLoadingDialog: true,
  162. networkDebounce: true,
  163. cancelToken: cancelToken,
  164. );
  165. if (result.isSuccess) {
  166. return result.convert();
  167. }
  168. return result.convert();
  169. }
  170. /// 已创建的访客列表
  171. Future<HttpResult<VisitorPageEntity>> fetchVisitorList({
  172. required String type,
  173. required int curPage,
  174. CancelToken? cancelToken,
  175. }) async {
  176. Map<String, String> params = {};
  177. params['type'] = type;
  178. params['page'] = curPage.toString();
  179. params['limit'] = "10";
  180. final result = await dioEngine.requestNetResult(
  181. ApiConstants.apiVisitorList,
  182. params: params,
  183. method: HttpMethod.GET,
  184. cancelToken: cancelToken,
  185. );
  186. if (result.isSuccess) {
  187. final json = result.getDataJson();
  188. var data = VisitorPageEntity.fromJson(json!);
  189. return result.convert<VisitorPageEntity>(data: data);
  190. }
  191. return result.convert();
  192. }
  193. /// 首页列表的获取
  194. Future<HttpResult<HomeListEntity>> fetchHomeIndex({
  195. bool needLoading = true,
  196. CancelToken? cancelToken,
  197. }) async {
  198. final result = await dioEngine.requestNetResult(
  199. ApiConstants.apiHomeList,
  200. method: HttpMethod.GET,
  201. isShowLoadingDialog: needLoading,
  202. networkDebounce: true,
  203. cancelToken: cancelToken,
  204. );
  205. if (result.isSuccess) {
  206. final json = result.getDataJson();
  207. var data = HomeListEntity.fromJson(json!);
  208. return result.convert<HomeListEntity>(data: data);
  209. }
  210. return result.convert();
  211. }
  212. /// 获取首页最新新闻的列表
  213. Future<HttpResult<LatestNewsPageEntity>> fetchLatestNewsList({
  214. required String type,
  215. required int curPage,
  216. CancelToken? cancelToken,
  217. }) async {
  218. Map<String, String> params = {};
  219. params['page'] = curPage.toString();
  220. params['limit'] = "10";
  221. params['type'] = type.toString();
  222. final result = await dioEngine.requestNetResult(
  223. ApiConstants.apiLatestNewsList,
  224. params: params,
  225. method: HttpMethod.GET,
  226. cancelToken: cancelToken,
  227. );
  228. if (result.isSuccess) {
  229. final json = result.getDataJson();
  230. var data = LatestNewsPageEntity.fromJson(json!);
  231. return result.convert<LatestNewsPageEntity>(data: data);
  232. }
  233. return result.convert();
  234. }
  235. /// 获取首页最新新闻的详情
  236. Future<HttpResult<LatestNewsDetailEntity>> fetchLatestNewsDetail({
  237. required String id,
  238. CancelToken? cancelToken,
  239. }) async {
  240. Map<String, String> params = {};
  241. params['id'] = id;
  242. final result = await dioEngine.requestNetResult(
  243. ApiConstants.apiLatestNewsDetail,
  244. params: params,
  245. method: HttpMethod.GET,
  246. isShowLoadingDialog: true,
  247. networkDebounce: true,
  248. cancelToken: cancelToken,
  249. );
  250. if (result.isSuccess) {
  251. final json = result.getDataJson();
  252. var data = LatestNewsDetailEntity.fromJson(json!);
  253. return result.convert<LatestNewsDetailEntity>(data: data);
  254. }
  255. return result.convert();
  256. }
  257. // 获取 房产 news 列表
  258. Future<HttpResult<PropertyNewsEntity>> fetchPropertyNewsList({
  259. required int curPage,
  260. CancelToken? cancelToken,
  261. }) async {
  262. Map<String, dynamic> params = {};
  263. params['page'] = curPage.toString();
  264. params['limit'] = "10";
  265. final result = await dioEngine.requestNetResult(
  266. '/api/v1/user/property/news/list', // api 地址
  267. params: params,
  268. method: HttpMethod.GET,
  269. cancelToken: cancelToken,
  270. );
  271. if (result.isSuccess) {
  272. final json = result.getDataJson();
  273. var data = PropertyNewsEntity.fromJson(json!);
  274. return result.convert<PropertyNewsEntity>(data: data);
  275. }
  276. return result.convert();
  277. }
  278. /// 获取首页管理员指引的列表
  279. Future<HttpResult<LatestNewsPageEntity>> fetchManageGuideList({
  280. required int curPage,
  281. CancelToken? cancelToken,
  282. }) async {
  283. Map<String, String> params = {};
  284. params['page'] = curPage.toString();
  285. params['limit'] = "10";
  286. final result = await dioEngine.requestNetResult(
  287. ApiConstants.apiManageGuideList,
  288. params: params,
  289. method: HttpMethod.GET,
  290. cancelToken: cancelToken,
  291. );
  292. if (result.isSuccess) {
  293. final json = result.getDataJson();
  294. var data = LatestNewsPageEntity.fromJson(json!);
  295. return result.convert<LatestNewsPageEntity>(data: data);
  296. }
  297. return result.convert();
  298. }
  299. /// 获取首页管理员指引的详情
  300. Future<HttpResult<LatestNewsDetailEntity>> fetchManageGuideDetail({
  301. required String id,
  302. CancelToken? cancelToken,
  303. }) async {
  304. Map<String, String> params = {};
  305. params['id'] = id;
  306. final result = await dioEngine.requestNetResult(
  307. ApiConstants.apiManageGuideDetail,
  308. params: params,
  309. method: HttpMethod.GET,
  310. isShowLoadingDialog: true,
  311. networkDebounce: true,
  312. cancelToken: cancelToken,
  313. );
  314. if (result.isSuccess) {
  315. final json = result.getDataJson();
  316. var data = LatestNewsDetailEntity.fromJson(json!);
  317. return result.convert<LatestNewsDetailEntity>(data: data);
  318. }
  319. return result.convert();
  320. }
  321. }