86738c903e
后端: - User.Role enum 增加 superadmin,启动时 ALTER TABLE 兼容已有 DB - 新增 SuperAdminOnly() 中间件 - 新增 POST /api/v1/admin/clear-data 接口(按表名白名单删除,多租户隔离) 前端: - AppUser/AuthUser 支持 role 字段持久化与传递 - 数据导入 Tab 末尾增加「危险操作 — 数据清空」区块(仅 superadmin 可见) - 支持多选:入库单/出库单/库存管理/商品详情/往来单位 - 红色警告框 + 二次确认弹窗(需手动输入"确认清空") Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
76 lines
2.2 KiB
Dart
76 lines
2.2 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:dio/dio.dart';
|
|
import '../core/api/api_client.dart';
|
|
import '../core/auth/auth_state.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 resp = await PublicApiClient.post('/auth/login', data: {
|
|
'shop_code': shopCode,
|
|
'username': username,
|
|
'password': password,
|
|
});
|
|
|
|
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,
|
|
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));
|
|
}
|
|
}
|
|
|
|
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}';
|
|
}
|
|
}
|
|
}
|