d9f0d3feca
- 新增 RegisterScreen(左品牌面板 3 步引导 + 右表单:门店名/电话/地址/ 管理员/账号/密码/确认 + 同意条款),还原原型两栏设计,全 token 化 - AuthRepository.register → POST /api/v1/register(后端已有),成功弹门店编号 引导去登录 - 路由加 /register(公开路由);登录页「前往官网注册」→ 应用内「立即注册」 - 整屏 golden ×三主题入回归闸 (购买流/通知中心按指示暂不做) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TSKEiHsvauyxYUW2itzUXX
115 lines
3.6 KiB
Dart
115 lines
3.6 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:dio/dio.dart';
|
|
import '../core/api/api_client.dart';
|
|
import '../core/auth/auth_state.dart';
|
|
import '../core/device/device_id.dart';
|
|
|
|
typedef AuthLoginFn = Future<AuthUser> Function({
|
|
required String shopCode,
|
|
required String username,
|
|
required String password,
|
|
});
|
|
|
|
class AuthException implements Exception {
|
|
final String message;
|
|
const AuthException(this.message);
|
|
@override
|
|
String toString() => message;
|
|
}
|
|
|
|
class AuthRepository {
|
|
@visibleForTesting
|
|
static AuthLoginFn? loginOverride;
|
|
|
|
/// POST /api/v1/auth/login
|
|
/// Request: { shop_code, username, password }
|
|
/// Response: { data: { access_token, refresh_token, expires_in, user: { id, username, real_name, role } } }
|
|
static Future<AuthUser> login({
|
|
required String shopCode,
|
|
required String username,
|
|
required String password,
|
|
}) async {
|
|
final override = loginOverride;
|
|
if (override != null) {
|
|
return override(
|
|
shopCode: shopCode,
|
|
username: username,
|
|
password: password,
|
|
);
|
|
}
|
|
try {
|
|
final deviceId = await DeviceId.get();
|
|
final platform = DeviceId.platformName;
|
|
final resp = await PublicApiClient.post('/auth/login', data: {
|
|
'shop_code': shopCode,
|
|
'username': username,
|
|
'password': password,
|
|
'device_id': deviceId,
|
|
'device_name': platform,
|
|
'platform': platform,
|
|
});
|
|
|
|
final data = resp.data['data'] as Map<String, dynamic>;
|
|
final user = data['user'] as Map<String, dynamic>;
|
|
|
|
return AuthUser(
|
|
accessToken: data['access_token'] as String,
|
|
refreshToken: data['refresh_token'] as String,
|
|
id: (user['id'] as num?)?.toInt() ?? 0,
|
|
username: user['username'] as String,
|
|
realName: user['real_name'] as String? ?? username,
|
|
shopNo: shopCode,
|
|
shopId: (data['shop_id'] as num).toInt(),
|
|
role: user['role'] as String? ?? 'operator',
|
|
);
|
|
} on DioException catch (e) {
|
|
final msg = e.response?.data?['error'] as String?;
|
|
throw AuthException(msg ?? _networkError(e));
|
|
}
|
|
}
|
|
|
|
/// POST /api/v1/register —— 自助注册新门店(公开,无需认证)。
|
|
/// Response: { data: { shop_code, shop_name, username } }
|
|
static Future<({String shopCode, String shopName, String username})>
|
|
register({
|
|
required String shopName,
|
|
required String phone,
|
|
required String address,
|
|
required String managerName,
|
|
required String username,
|
|
required String password,
|
|
}) async {
|
|
try {
|
|
final resp = await PublicApiClient.post('/register', data: {
|
|
'shop_name': shopName,
|
|
'phone': phone,
|
|
'address': address,
|
|
'manager_name': managerName,
|
|
'username': username,
|
|
'password': password,
|
|
});
|
|
final data = resp.data['data'] as Map<String, dynamic>;
|
|
return (
|
|
shopCode: data['shop_code'] as String? ?? '',
|
|
shopName: data['shop_name'] as String? ?? shopName,
|
|
username: data['username'] as String? ?? username,
|
|
);
|
|
} on DioException catch (e) {
|
|
final msg = e.response?.data?['error'] as String?;
|
|
throw AuthException(msg ?? _networkError(e));
|
|
}
|
|
}
|
|
|
|
static String _networkError(DioException e) {
|
|
switch (e.type) {
|
|
case DioExceptionType.connectionTimeout:
|
|
case DioExceptionType.receiveTimeout:
|
|
return '连接超时,请检查网络';
|
|
case DioExceptionType.connectionError:
|
|
return '无法连接到服务器(localhost:8080),请先启动后端';
|
|
default:
|
|
return '网络错误:${e.message}';
|
|
}
|
|
}
|
|
}
|