|
@@ -1,11 +1,19 @@
|
|
|
import 'dart:async';
|
|
|
|
|
|
import 'package:cpt_profile/modules/setting/setting_page.dart';
|
|
|
+import 'package:cs_resources/generated/l10n.dart';
|
|
|
+import 'package:domain/repository/auth_repository.dart';
|
|
|
+import 'package:domain/repository/profile_repository.dart';
|
|
|
import 'package:flutter/material.dart';
|
|
|
+import 'package:plugin_basic/constants/app_constant.dart';
|
|
|
import 'package:plugin_basic/dialog/verify_code_dialog.dart';
|
|
|
+import 'package:plugin_basic/provider/user_config/user_config_service.dart';
|
|
|
import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
|
|
|
+import 'package:plugin_platform/engine/notify/notify_engine.dart';
|
|
|
import 'package:plugin_platform/engine/toast/toast_engine.dart';
|
|
|
+import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
+import 'package:router/componentRouter/component_service_manager.dart';
|
|
|
import 'package:shared/utils/log_utils.dart';
|
|
|
import 'package:shared/utils/util.dart';
|
|
|
import 'package:widgets/widget_export.dart';
|
|
@@ -15,34 +23,38 @@ import 'change_mobile_state.dart';
|
|
|
part 'change_mobile_view_model.g.dart';
|
|
|
|
|
|
@riverpod
|
|
|
-class ChangeMobileViewModel extends _$ChangeMobileViewModel {
|
|
|
+class ChangeMobileViewModel extends _$ChangeMobileViewModel with DioCancelableMixin {
|
|
|
+ late final ProfileRepository _profileRepository;
|
|
|
+ late final AuthRepository _authRepository;
|
|
|
+
|
|
|
@override
|
|
|
ChangeMobileState build() {
|
|
|
+ _profileRepository = ref.read(profileRepositoryProvider);
|
|
|
+ _authRepository = ref.read(authRepositoryProvider);
|
|
|
+
|
|
|
final state = ChangeMobileState();
|
|
|
initListener(state);
|
|
|
- ref.onDispose(() {
|
|
|
+
|
|
|
+ registerCancellation(callback: () {
|
|
|
onDispose(state);
|
|
|
});
|
|
|
+
|
|
|
return state;
|
|
|
}
|
|
|
|
|
|
/// 提交表单修改电话号码
|
|
|
- void submitChangeMobile() {
|
|
|
+ void submitChangeMobile() async {
|
|
|
state = state.copyWith(oldCodeErrorText: null, newCodeErrorText: null);
|
|
|
|
|
|
- final FocusNode oldMobileFocusNode = state.formData['old']!['focusNode'];
|
|
|
final FocusNode newMobileFocusNode = state.formData['new']!['focusNode'];
|
|
|
final FocusNode newCodeFocusNode = state.formData['new_code']!['focusNode'];
|
|
|
|
|
|
- final TextEditingController oldMobileController = state.formData['old']!['controller'];
|
|
|
final TextEditingController newMobileController = state.formData['new']!['controller'];
|
|
|
final TextEditingController newCodeController = state.formData['new_code']!['controller'];
|
|
|
|
|
|
- oldMobileFocusNode.unfocus();
|
|
|
newMobileFocusNode.unfocus();
|
|
|
newCodeFocusNode.unfocus();
|
|
|
|
|
|
- final oldMobile = oldMobileController.text;
|
|
|
final newMobile = newMobileController.text;
|
|
|
final newCode = newCodeController.text;
|
|
|
|
|
@@ -59,36 +71,68 @@ class ChangeMobileViewModel extends _$ChangeMobileViewModel {
|
|
|
}
|
|
|
|
|
|
//执行密码登录
|
|
|
- ToastEngine.show('准备执行请求发送验证码 newMobile:$newMobile newCode:$newCode');
|
|
|
+ final result = await _profileRepository.changeMobilePhone(
|
|
|
+ smsCode: newCode,
|
|
|
+ countryCode: AppConstant.countryCode,
|
|
|
+ phone: newMobile,
|
|
|
+ cancelToken: cancelToken,
|
|
|
+ );
|
|
|
|
|
|
- //返回设置页面
|
|
|
- SettingPage.startInstance();
|
|
|
+ if (result.isSuccess) {
|
|
|
+ //提示成功
|
|
|
+ NotifyEngine.showSuccess(S.current.successful);
|
|
|
+ //清除用户登录信息
|
|
|
+ UserConfigService.getInstance().handleLogoutParams();
|
|
|
+ //去登录页面
|
|
|
+ ComponentServiceManager().authService.startAndPopAllLoginPage();
|
|
|
+ } else {
|
|
|
+ ToastEngine.show(result.errorMsg ?? "UnKnow Error");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//展示发送验证码弹窗
|
|
|
- showVerifyCodedDialog() {
|
|
|
-
|
|
|
- //展示对应的验证码弹窗
|
|
|
- _showCAPTCHADialog();
|
|
|
- }
|
|
|
+ void showVerifyCodedDialog() {
|
|
|
+ final TextEditingController phoneController = state.formData['new']!['controller'];
|
|
|
+ final phone = phoneController.text;
|
|
|
+ if (Utils.isEmpty(phone)) {
|
|
|
+ ToastEngine.show("Mobile Phone Error");
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- void _showCAPTCHADialog() {
|
|
|
+ //短信验证码之前的图片验证码弹窗
|
|
|
DialogEngine.show(
|
|
|
- onDismiss: () {
|
|
|
- },
|
|
|
+ onDismiss: () {},
|
|
|
widget: VerifyCodeDialog(
|
|
|
confirmAction: (key, code) {
|
|
|
- //发送验证码
|
|
|
- _startNewCountDown();
|
|
|
+ //发送短信验证码
|
|
|
+ _sendSMS(phone, key, code);
|
|
|
},
|
|
|
),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ /// 调用接口发送短信验证码
|
|
|
+ void _sendSMS(String phone, String? key, String? code) async {
|
|
|
+ final result = await _authRepository.sendSMS(
|
|
|
+ countryCode: AppConstant.countryCode,
|
|
|
+ phone: phone,
|
|
|
+ captchaKey: key,
|
|
|
+ captchaValue: code,
|
|
|
+ cancelToken: cancelToken,
|
|
|
+ );
|
|
|
+
|
|
|
+ if (result.isSuccess) {
|
|
|
+ NotifyEngine.showSuccess(S.current.send_sms_successful);
|
|
|
+ _startCountDown();
|
|
|
+ } else {
|
|
|
+ ToastEngine.show(result.errorMsg ?? "UnKnow Error");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
Timer? newCountdownTimer;
|
|
|
|
|
|
/// 开启倒计时 - New
|
|
|
- void _startNewCountDown() {
|
|
|
+ void _startCountDown() {
|
|
|
//60秒倒计时
|
|
|
state = state.copyWith(isNewCounting: true, newCountdownTime: 60);
|
|
|
|
|
@@ -108,6 +152,10 @@ class ChangeMobileViewModel extends _$ChangeMobileViewModel {
|
|
|
|
|
|
//初始化监听
|
|
|
void initListener(ChangeMobileState initState) {
|
|
|
+ final String? phone = UserConfigService.getState().user?.information?.phone;
|
|
|
+ final TextEditingController phoneController = initState.formData['old']!['controller'];
|
|
|
+ phoneController.text = phone ?? "-";
|
|
|
+
|
|
|
final FocusNode newCodeFocusNode = initState.formData['new_code']!['focusNode'];
|
|
|
|
|
|
newCodeFocusNode.addListener(() {
|
|
@@ -127,5 +175,4 @@ class ChangeMobileViewModel extends _$ChangeMobileViewModel {
|
|
|
|
|
|
Log.d("ChangeMobileViewModel 销毁 onDispose");
|
|
|
}
|
|
|
-
|
|
|
}
|