扫码结果处理
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'package:ln_jq_app/common/model/vehicle_info.dart';
|
||||
|
||||
/// 定义登录渠道的枚举类型
|
||||
enum LoginChannel {
|
||||
@@ -15,10 +16,11 @@ class StorageService extends GetxService {
|
||||
static const String _tokenKey = 'user_token';
|
||||
static const String _userIdKey = 'user_id';
|
||||
static const String _channelKey = 'login_channel';
|
||||
// 为新字段增加键名
|
||||
static const String _nameKey = 'user_name';
|
||||
static const String _phoneKey = 'user_phone';
|
||||
static const String _idCardKey = 'user_id_card';
|
||||
// 车辆信息的键名
|
||||
static const String _vehicleInfoKey = 'vehicle_info';
|
||||
|
||||
// 提供一个静态的 'to' 方法,方便全局访问
|
||||
static StorageService get to => Get.find();
|
||||
@@ -31,30 +33,27 @@ class StorageService extends GetxService {
|
||||
|
||||
/// --- Getter 方法,用于读取状态 ---
|
||||
|
||||
/// 判断是否已登录 (通过检查 token 是否存在且不为空)
|
||||
bool get isLoggedIn =>
|
||||
_box
|
||||
.read<String?>(_tokenKey)
|
||||
?.isNotEmpty ?? false;
|
||||
|
||||
/// 获取 Token
|
||||
bool get isLoggedIn => _box.read<String?>(_tokenKey)?.isNotEmpty ?? false;
|
||||
String? get token => _box.read<String?>(_tokenKey);
|
||||
|
||||
/// 获取 UserId
|
||||
String? get userId => _box.read<String?>(_userIdKey);
|
||||
|
||||
/// 获取 Name
|
||||
String? get name => _box.read<String?>(_nameKey);
|
||||
|
||||
/// 获取 Phone
|
||||
String? get phone => _box.read<String?>(_phoneKey);
|
||||
|
||||
/// 获取 ID Card
|
||||
String? get idCard => _box.read<String?>(_idCardKey);
|
||||
|
||||
/// 判断是否已有车辆信息缓存
|
||||
bool get hasVehicleInfo => _box.hasData(_vehicleInfoKey);
|
||||
|
||||
/// 获取已缓存的车辆信息
|
||||
VehicleInfo? get vehicleInfo {
|
||||
final vehicleJson = _box.read<String?>(_vehicleInfoKey);
|
||||
if (vehicleJson != null) {
|
||||
// 使用我们之前创建的辅助函数来解析
|
||||
return vehicleInfoFromJson(vehicleJson);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
///获取当前登录渠道
|
||||
/// 从存储中读取字符串,并转换为枚举类型
|
||||
LoginChannel get loginChannel {
|
||||
final channelString = _box.read<String?>(_channelKey);
|
||||
if (channelString == 'station') {
|
||||
@@ -66,12 +65,10 @@ class StorageService extends GetxService {
|
||||
}
|
||||
|
||||
/// --- 操作方法 ---
|
||||
/// 保存用户信息
|
||||
/// @param channel - 登录渠道,station 或 driver
|
||||
Future<void> saveLoginInfo({
|
||||
required String token,
|
||||
required String userId,
|
||||
required String channel, // 接收一个字符串类型的渠道
|
||||
required String channel,
|
||||
String? name,
|
||||
String? phone,
|
||||
String? idCard,
|
||||
@@ -79,20 +76,31 @@ class StorageService extends GetxService {
|
||||
await _box.write(_tokenKey, token);
|
||||
await _box.write(_userIdKey, userId);
|
||||
await _box.write(_channelKey, channel);
|
||||
// 保存新增的字段,如果为 null, GetStorage 会移除该键
|
||||
await _box.write(_nameKey, name);
|
||||
await _box.write(_phoneKey, phone);
|
||||
await _box.write(_idCardKey, idCard);
|
||||
}
|
||||
|
||||
///删除用户信息,增加删除渠道信息
|
||||
/// 保存车辆信息
|
||||
Future<void> saveVehicleInfo(VehicleInfo data) async {
|
||||
// 使用辅助函数将对象转换为 JSON 字符串并保存
|
||||
await _box.write(_vehicleInfoKey, vehicleInfoToJson(data));
|
||||
}
|
||||
|
||||
/// 清除车辆信息
|
||||
Future<void> clearVehicleInfo() async {
|
||||
await _box.remove(_vehicleInfoKey);
|
||||
}
|
||||
|
||||
/// 删除用户信息 (登出)
|
||||
Future<void> clearLoginInfo() async {
|
||||
await _box.remove(_tokenKey);
|
||||
await _box.remove(_userIdKey);
|
||||
await _box.remove(_channelKey);
|
||||
// 退出时一并清除新增的字段
|
||||
await _box.remove(_nameKey);
|
||||
await _box.remove(_phoneKey);
|
||||
await _box.remove(_idCardKey);
|
||||
// 登出时一并清除车辆信息
|
||||
await clearVehicleInfo();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user