auth_repository.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import 'package:domain/entity/auth_login_entity.dart';
  2. import 'package:domain/entity/captcha_img_entity.dart';
  3. import 'package:domain/entity/server_time.dart';
  4. import 'package:plugin_platform/platform_export.dart';
  5. import 'package:plugin_platform/http/dio_engine.dart';
  6. import 'package:plugin_platform/http/http_result.dart';
  7. import 'package:riverpod_annotation/riverpod_annotation.dart';
  8. import 'package:shared/utils/util.dart';
  9. import '../constants/api_constants.dart';
  10. import 'package:flutter_riverpod/flutter_riverpod.dart';
  11. import 'package:plugin_basic/provider/http_provider/http_provider.dart';
  12. part 'auth_repository.g.dart';
  13. @Riverpod(keepAlive: true)
  14. AuthRepository authRepository(Ref ref) {
  15. final dioEngine = ref.watch(dioEngineProvider);
  16. return AuthRepository(dioEngine: dioEngine);
  17. }
  18. /*
  19. * 用户模块的数据仓库
  20. */
  21. class AuthRepository {
  22. DioEngine dioEngine;
  23. AuthRepository({required this.dioEngine});
  24. /// 用户登录
  25. Future<HttpResult<AuthLoginEntity>> authLogin(
  26. String? userName,
  27. String? password, {
  28. CancelToken? cancelToken,
  29. }) async {
  30. Map<String, String> params = {};
  31. params['username'] = userName!;
  32. params['password'] = password!;
  33. final result = await dioEngine.requestNetResult(
  34. ApiConstants.apiUserLogin,
  35. params: params,
  36. method: HttpMethod.POST,
  37. isShowLoadingDialog: true,
  38. networkDebounce: true,
  39. cancelToken: cancelToken,
  40. );
  41. if (result.isSuccess) {
  42. final json = result.getDataJson();
  43. var data = AuthLoginEntity.fromJson(json!);
  44. return result.convert<AuthLoginEntity>(data: data);
  45. }
  46. return result.convert();
  47. }
  48. /// 图片验证码
  49. Future<HttpResult<CaptchaImgEntity>> fetchCaptchaImage({
  50. CancelToken? cancelToken,
  51. }) async {
  52. final result = await dioEngine.requestNetResult(
  53. ApiConstants.apiCaptchaImage,
  54. method: HttpMethod.GET,
  55. isShowLoadingDialog: true,
  56. networkDebounce: true,
  57. cancelToken: cancelToken,
  58. );
  59. if (result.isSuccess) {
  60. final json = result.getDataJson();
  61. var data = CaptchaImgEntity.fromJson(json!);
  62. return result.convert<CaptchaImgEntity>(data: data);
  63. }
  64. return result.convert();
  65. }
  66. /// 发送短信
  67. Future<HttpResult> sendSMS({
  68. required String? countryCode,
  69. required String? phone,
  70. required String? captchaKey,
  71. required String? captchaValue,
  72. CancelToken? cancelToken,
  73. }) async {
  74. Map<String, String> params = {};
  75. params['country_code'] = countryCode!;
  76. params['phone'] = phone!;
  77. params['captcha_key'] = captchaKey!;
  78. params['captcha_value'] = captchaValue!;
  79. final result = await dioEngine.requestNetResult(
  80. ApiConstants.apiSendSMS,
  81. params: params,
  82. method: HttpMethod.POST,
  83. isShowLoadingDialog: true,
  84. networkDebounce: true,
  85. cancelToken: cancelToken,
  86. );
  87. if (result.isSuccess) {
  88. return result.convert();
  89. }
  90. return result.convert();
  91. }
  92. /// 用户注册
  93. Future<HttpResult<AuthLoginEntity>> userRegister({
  94. required String firstName,
  95. required String lastName,
  96. required String email,
  97. required String countryCode,
  98. required String phone,
  99. required String password,
  100. required String confirmPassword,
  101. required String? smsCode,
  102. CancelToken? cancelToken,
  103. }) async {
  104. Map<String, String> params = {};
  105. params['first_name'] = firstName;
  106. params['last_name'] = lastName;
  107. params['email'] = email;
  108. params['country_code'] = countryCode;
  109. params['phone'] = phone;
  110. params['password'] = password;
  111. params['password_confirmation'] = confirmPassword;
  112. params['sms_code'] = smsCode ?? "";
  113. final result = await dioEngine.requestNetResult(
  114. ApiConstants.apiAuthRegister,
  115. params: params,
  116. method: HttpMethod.POST,
  117. isShowLoadingDialog: true,
  118. networkDebounce: true,
  119. cancelToken: cancelToken,
  120. );
  121. if (result.isSuccess) {
  122. final json = result.getDataJson();
  123. var data = AuthLoginEntity.fromJson(json!);
  124. return result.convert<AuthLoginEntity>(data: data);
  125. }
  126. return result.convert();
  127. }
  128. /// 修改密码
  129. Future<HttpResult> resetPassword({
  130. required String? smsCode,
  131. required String? password,
  132. required String? confirmPassword,
  133. CancelToken? cancelToken,
  134. }) async {
  135. Map<String, String> params = {};
  136. params['sms_code'] = smsCode!;
  137. params['password'] = password!;
  138. params['password_confirmation'] = confirmPassword!;
  139. final result = await dioEngine.requestNetResult(
  140. ApiConstants.apiResetPassword,
  141. params: params,
  142. method: HttpMethod.POST,
  143. isShowLoadingDialog: true,
  144. networkDebounce: true,
  145. cancelToken: cancelToken,
  146. );
  147. if (result.isSuccess) {
  148. return result.convert();
  149. }
  150. return result.convert();
  151. }
  152. /// 忘记密码
  153. Future<HttpResult> forgotPassword({
  154. required String? smsCode,
  155. required String? countryCode,
  156. required String? phone,
  157. required String? password,
  158. required String? confirmPassword,
  159. CancelToken? cancelToken,
  160. }) async {
  161. Map<String, String> params = {};
  162. params['sms_code'] = smsCode!;
  163. params['country_code'] = countryCode!;
  164. params['phone'] = phone!;
  165. params['password'] = password!;
  166. params['password_confirmation'] = confirmPassword!;
  167. final result = await dioEngine.requestNetResult(
  168. ApiConstants.apiForgotPassword,
  169. params: params,
  170. method: HttpMethod.POST,
  171. isShowLoadingDialog: true,
  172. networkDebounce: true,
  173. cancelToken: cancelToken,
  174. );
  175. if (result.isSuccess) {
  176. return result.convert();
  177. }
  178. return result.convert();
  179. }
  180. }