123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- import 'package:domain/entity/auth_login_entity.dart';
- import 'package:domain/entity/captcha_img_entity.dart';
- import 'package:domain/entity/server_time.dart';
- import 'package:plugin_platform/platform_export.dart';
- import 'package:plugin_platform/http/dio_engine.dart';
- import 'package:plugin_platform/http/http_result.dart';
- import 'package:riverpod_annotation/riverpod_annotation.dart';
- import 'package:shared/utils/util.dart';
- import '../constants/api_constants.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:plugin_basic/provider/http_provider/http_provider.dart';
- part 'auth_repository.g.dart';
- @Riverpod(keepAlive: true)
- AuthRepository authRepository(Ref ref) {
- final dioEngine = ref.watch(dioEngineProvider);
- return AuthRepository(dioEngine: dioEngine);
- }
- /*
- * 用户模块的数据仓库
- */
- class AuthRepository {
- DioEngine dioEngine;
- AuthRepository({required this.dioEngine});
- /// 用户登录
- Future<HttpResult<AuthLoginEntity>> authLogin(
- String? userName,
- String? password, {
- CancelToken? cancelToken,
- }) async {
- Map<String, String> params = {};
- params['username'] = userName!;
- params['password'] = password!;
- final result = await dioEngine.requestNetResult(
- ApiConstants.apiUserLogin,
- params: params,
- method: HttpMethod.POST,
- isShowLoadingDialog: true,
- networkDebounce: true,
- cancelToken: cancelToken,
- );
- if (result.isSuccess) {
- final json = result.getDataJson();
- var data = AuthLoginEntity.fromJson(json!);
- return result.convert<AuthLoginEntity>(data: data);
- }
- return result.convert();
- }
- /// 图片验证码
- Future<HttpResult<CaptchaImgEntity>> fetchCaptchaImage({
- CancelToken? cancelToken,
- }) async {
- final result = await dioEngine.requestNetResult(
- ApiConstants.apiCaptchaImage,
- method: HttpMethod.GET,
- isShowLoadingDialog: true,
- networkDebounce: true,
- cancelToken: cancelToken,
- );
- if (result.isSuccess) {
- final json = result.getDataJson();
- var data = CaptchaImgEntity.fromJson(json!);
- return result.convert<CaptchaImgEntity>(data: data);
- }
- return result.convert();
- }
- /// 发送短信
- Future<HttpResult> sendSMS({
- required String? countryCode,
- required String? phone,
- required String? captchaKey,
- required String? captchaValue,
- CancelToken? cancelToken,
- }) async {
- Map<String, String> params = {};
- params['country_code'] = countryCode!;
- params['phone'] = phone!;
- params['captcha_key'] = captchaKey!;
- params['captcha_value'] = captchaValue!;
- final result = await dioEngine.requestNetResult(
- ApiConstants.apiSendSMS,
- params: params,
- method: HttpMethod.POST,
- isShowLoadingDialog: true,
- networkDebounce: true,
- cancelToken: cancelToken,
- );
- if (result.isSuccess) {
- return result.convert();
- }
- return result.convert();
- }
- /// 用户注册
- Future<HttpResult<AuthLoginEntity>> userRegister({
- required String firstName,
- required String lastName,
- required String email,
- required String countryCode,
- required String phone,
- required String password,
- required String confirmPassword,
- required String? smsCode,
- CancelToken? cancelToken,
- }) async {
- Map<String, String> params = {};
- params['first_name'] = firstName;
- params['last_name'] = lastName;
- params['email'] = email;
- params['country_code'] = countryCode;
- params['phone'] = phone;
- params['password'] = password;
- params['password_confirmation'] = confirmPassword;
- params['sms_code'] = smsCode ?? "";
- final result = await dioEngine.requestNetResult(
- ApiConstants.apiAuthRegister,
- params: params,
- method: HttpMethod.POST,
- isShowLoadingDialog: true,
- networkDebounce: true,
- cancelToken: cancelToken,
- );
- if (result.isSuccess) {
- final json = result.getDataJson();
- var data = AuthLoginEntity.fromJson(json!);
- return result.convert<AuthLoginEntity>(data: data);
- }
- return result.convert();
- }
- /// 修改密码
- Future<HttpResult> resetPassword({
- required String? smsCode,
- required String? password,
- required String? confirmPassword,
- CancelToken? cancelToken,
- }) async {
- Map<String, String> params = {};
- params['sms_code'] = smsCode!;
- params['password'] = password!;
- params['password_confirmation'] = confirmPassword!;
- final result = await dioEngine.requestNetResult(
- ApiConstants.apiResetPassword,
- params: params,
- method: HttpMethod.POST,
- isShowLoadingDialog: true,
- networkDebounce: true,
- cancelToken: cancelToken,
- );
- if (result.isSuccess) {
- return result.convert();
- }
- return result.convert();
- }
- /// 忘记密码
- Future<HttpResult> forgotPassword({
- required String? smsCode,
- required String? countryCode,
- required String? phone,
- required String? password,
- required String? confirmPassword,
- CancelToken? cancelToken,
- }) async {
- Map<String, String> params = {};
- params['sms_code'] = smsCode!;
- params['country_code'] = countryCode!;
- params['phone'] = phone!;
- params['password'] = password!;
- params['password_confirmation'] = confirmPassword!;
- final result = await dioEngine.requestNetResult(
- ApiConstants.apiForgotPassword,
- params: params,
- method: HttpMethod.POST,
- isShowLoadingDialog: true,
- networkDebounce: true,
- cancelToken: cancelToken,
- );
- if (result.isSuccess) {
- return result.convert();
- }
- return result.convert();
- }
- }
|