profile_repository.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import 'package:domain/entity/id_name_entity.dart';
  2. import 'package:domain/entity/user_me_entity.dart';
  3. import 'package:plugin_platform/platform_export.dart';
  4. import 'package:plugin_platform/http/dio_engine.dart';
  5. import 'package:plugin_platform/http/http_result.dart';
  6. import 'package:riverpod_annotation/riverpod_annotation.dart';
  7. import 'package:shared/utils/util.dart';
  8. import '../constants/api_constants.dart';
  9. import 'package:flutter_riverpod/flutter_riverpod.dart';
  10. import 'package:plugin_basic/provider/http_provider/http_provider.dart';
  11. part 'profile_repository.g.dart';
  12. @Riverpod(keepAlive: true)
  13. ProfileRepository profileRepository(Ref ref) {
  14. final dioEngine = ref.watch(dioEngineProvider);
  15. return ProfileRepository(dioEngine: dioEngine);
  16. }
  17. /*
  18. * 用户详情的数据仓库
  19. */
  20. class ProfileRepository {
  21. DioEngine dioEngine;
  22. ProfileRepository({required this.dioEngine});
  23. /// 获取用户详情
  24. Future<HttpResult<UserMeEntity>> fetchUserInfo({
  25. CancelToken? cancelToken,
  26. }) async {
  27. final result = await dioEngine.requestNetResult(
  28. ApiConstants.apiProfileInfo,
  29. method: HttpMethod.GET,
  30. cancelToken: cancelToken,
  31. );
  32. if (result.isSuccess) {
  33. final json = result.getDataJson();
  34. var data = UserMeEntity.fromJson(json!);
  35. return result.convert<UserMeEntity>(data: data);
  36. }
  37. return result.convert();
  38. }
  39. /// 更新用户信息
  40. Future<HttpResult> updateProfile({
  41. required String? firstName,
  42. required String? lastName,
  43. required String? email,
  44. required String? avatarPath,
  45. CancelToken? cancelToken,
  46. }) async {
  47. Map<String, String> params = {};
  48. params['first_name'] = firstName!;
  49. params['last_name'] = lastName!;
  50. params['email'] = email!;
  51. Map<String, String> paths = {};
  52. //如果是链接通过params传参
  53. if (avatarPath != null && avatarPath.startsWith("http")) {
  54. params['avatar'] = avatarPath;
  55. } else {
  56. //如果是文件通过Path传参
  57. paths['avatar'] = avatarPath ?? "";
  58. }
  59. final result = await dioEngine.requestNetResult(
  60. ApiConstants.apiUpdateProfile,
  61. params: params,
  62. paths: paths,
  63. method: HttpMethod.POST,
  64. isShowLoadingDialog: true,
  65. networkDebounce: true,
  66. cancelToken: cancelToken,
  67. );
  68. if (result.isSuccess) {
  69. return result.convert();
  70. }
  71. return result.convert();
  72. }
  73. /// 获取用户的默认头像列表
  74. Future<HttpResult<String>> fetchDefaultAvatar({
  75. CancelToken? cancelToken,
  76. }) async {
  77. final result = await dioEngine.requestNetResult(
  78. ApiConstants.apiDefaultAvatar,
  79. method: HttpMethod.GET,
  80. cancelToken: cancelToken,
  81. );
  82. if (result.isSuccess) {
  83. final jsonList = result.getListJson();
  84. List<String> stringList = [];
  85. if (jsonList != null) {
  86. stringList = jsonList
  87. .map((value) {
  88. // 假设 value 是字符串类型,直接返回
  89. if (value is String) {
  90. return value;
  91. }
  92. return null;
  93. })
  94. .where((item) => item != null)
  95. .cast<String>()
  96. .toList();
  97. }
  98. return result.convert<String>(list: stringList);
  99. }
  100. return result.convert<String>();
  101. }
  102. /// 修改手机号码
  103. Future<HttpResult> changeMobilePhone({
  104. required String? smsCode,
  105. required String? countryCode,
  106. required String? phone,
  107. CancelToken? cancelToken,
  108. }) async {
  109. Map<String, String> params = {};
  110. params['sms_code'] = smsCode!;
  111. params['country_code'] = countryCode!;
  112. params['phone'] = phone!;
  113. final result = await dioEngine.requestNetResult(
  114. ApiConstants.apiChangeMobile,
  115. params: params,
  116. method: HttpMethod.POST,
  117. isShowLoadingDialog: true,
  118. networkDebounce: true,
  119. cancelToken: cancelToken,
  120. );
  121. if (result.isSuccess) {
  122. return result.convert();
  123. }
  124. return result.convert();
  125. }
  126. /// 设置默认房产单元
  127. Future<HttpResult> setUnitAsDefault({
  128. required String? unitId,
  129. CancelToken? cancelToken,
  130. }) async {
  131. Map<String, String> params = {};
  132. params['unit_id'] = unitId!;
  133. final result = await dioEngine.requestNetResult(
  134. ApiConstants.apiSetEstateDefault,
  135. params: params,
  136. method: HttpMethod.POST,
  137. isShowLoadingDialog: true,
  138. networkDebounce: true,
  139. cancelToken: cancelToken,
  140. );
  141. if (result.isSuccess) {
  142. return result.convert();
  143. }
  144. return result.convert();
  145. }
  146. /// 删除指定的房产单元
  147. Future<HttpResult> deleteEstateUnit({
  148. required String? unitId,
  149. CancelToken? cancelToken,
  150. }) async {
  151. Map<String, String> params = {};
  152. params['unit_id'] = unitId!;
  153. final result = await dioEngine.requestNetResult(
  154. ApiConstants.apiDeleteEstateUnit,
  155. params: params,
  156. method: HttpMethod.POST,
  157. isShowLoadingDialog: true,
  158. networkDebounce: true,
  159. cancelToken: cancelToken,
  160. );
  161. if (result.isSuccess) {
  162. return result.convert();
  163. }
  164. return result.convert();
  165. }
  166. /// 搜索房产
  167. Future<HttpResult<IdNameEntity>> searchEstate({
  168. required String? keyword,
  169. CancelToken? cancelToken,
  170. }) async {
  171. Map<String, String> params = {};
  172. params['keyword'] = keyword ?? "";
  173. final result = await dioEngine.requestNetResult(
  174. ApiConstants.apiEstateSearch,
  175. params: params,
  176. method: HttpMethod.GET,
  177. cancelToken: cancelToken,
  178. );
  179. if (result.isSuccess) {
  180. final jsonList = result.getListJson();
  181. //获取List数据 需要转换一次
  182. var list = jsonList?.map((value) {
  183. if (value is Map<String, dynamic>) {
  184. return IdNameEntity.fromJson(value);
  185. } else {
  186. return null;
  187. }
  188. }).where((item) => item != null).cast<IdNameEntity>().toList();
  189. return result.convert<IdNameEntity>(list: list);
  190. }
  191. return result.convert<IdNameEntity>();
  192. }
  193. /// 加入房产之前的单元验证
  194. Future<HttpResult> estateJoinCheck({
  195. required String? estateId,
  196. required String? block,
  197. required String? unit,
  198. CancelToken? cancelToken,
  199. }) async {
  200. Map<String, String> params = {};
  201. params['estate_id'] = estateId!;
  202. params['block'] = block!;
  203. params['unit'] = unit!;
  204. final result = await dioEngine.requestNetResult(
  205. ApiConstants.apiEstateUnitCheck,
  206. params: params,
  207. method: HttpMethod.POST,
  208. isShowLoadingDialog: true,
  209. networkDebounce: true,
  210. cancelToken: cancelToken,
  211. );
  212. if (result.isSuccess) {
  213. return result.convert();
  214. }
  215. return result.convert();
  216. }
  217. /// 加入房产的单元
  218. Future<HttpResult> joinEstateUnit({
  219. required String? estateId,
  220. required String? block,
  221. required String? unit,
  222. required String? type,
  223. required List<String>? paths,
  224. CancelToken? cancelToken,
  225. }) async {
  226. Map<String, String> params = {};
  227. params['estate_id'] = estateId!;
  228. params['block'] = block!;
  229. params['unit'] = unit!;
  230. params['type'] = type!;
  231. Map<String,String> files = {};
  232. if (paths!= null){
  233. paths.asMap().forEach((index, path) {
  234. files["files[$index]"] = path;
  235. });
  236. }
  237. final result = await dioEngine.requestNetResult(
  238. ApiConstants.apiEstateUnitJoin,
  239. params: params,
  240. paths: files,
  241. method: HttpMethod.POST,
  242. isShowLoadingDialog: true,
  243. networkDebounce: true,
  244. cancelToken: cancelToken,
  245. );
  246. if (result.isSuccess) {
  247. return result.convert();
  248. }
  249. return result.convert();
  250. }
  251. }