import 'package:flutter/foundation.dart'; import '../payment/order_api.dart'; import 'session_store.dart'; enum AuthMode { login, register } class AuthViewModel extends ChangeNotifier { final OrderApi orderApi; final SessionStore sessionStore; AuthMode _mode = AuthMode.login; bool _loading = false; String? _error; AuthMode get mode => _mode; bool get loading => _loading; String? get error => _error; AuthViewModel({required this.orderApi, required this.sessionStore}); void switchMode() { _mode = _mode == AuthMode.login ? AuthMode.register : AuthMode.login; _error = null; notifyListeners(); } /// 本地输入校验失败提示(不进网络请求) void showError(String message) { _error = message; notifyListeners(); } /// 登录或注册;成功返回 true(调用方负责跳转主界面) Future submit({required String phone, required String password}) async { _loading = true; _error = null; notifyListeners(); try { if (_mode == AuthMode.login) { final token = await orderApi.login(phone: phone, password: password); await sessionStore.save(phone, token); } else { await orderApi.register(phone: phone, password: password); final token = await orderApi.login(phone: phone, password: password); await sessionStore.save(phone, token); } return true; } on OrderApiException catch (e) { _error = e.message; return false; } finally { _loading = false; notifyListeners(); } } }