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> authLogin( String? userName, String? password, { CancelToken? cancelToken, }) async { Map 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(data: data); } return result.convert(); } /// 图片验证码 Future> 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(data: data); } return result.convert(); } /// 发送短信 Future sendSMS({ required String? countryCode, required String? phone, required String? captchaKey, required String? captchaValue, CancelToken? cancelToken, }) async { Map 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> 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 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(data: data); } return result.convert(); } /// 修改密码 Future resetPassword({ required String? smsCode, required String? password, required String? confirmPassword, CancelToken? cancelToken, }) async { Map 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 forgotPassword({ required String? smsCode, required String? countryCode, required String? phone, required String? password, required String? confirmPassword, CancelToken? cancelToken, }) async { Map 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(); } }