Files
BistroFrontend/lib/common/global.dart
2023-03-20 22:24:33 +08:00

110 lines
2.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:dio_http_cache/dio_http_cache.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
// 提供五套可选主题色
const _themes = <MaterialColor>[
Colors.blue,
Colors.cyan,
Colors.teal,
Colors.green,
Colors.red,
];
///
/// 当调试模式打开时打印
///
void printWhenDebug(Object object) {
if (kDebugMode) {
print(object);
}
}
///
/// 当前环境
///
enum Env {
dev,
pro,
}
class Global {
static SharedPreferences? _prefs;
static Dio? dio;
static Map profile = {};
///
/// 当前环境
///
static const Env env = Env.dev;
///
/// 根域名
///
static const Map baseUrlList = {
Env.dev: 'http://safe.doylee.cn',
// Env.dev: 'http://192.168.31.189/campus_safety',
Env.pro: 'http://10.111.2.158',
};
///
/// 接口根路由
///
static String baseUrl = baseUrlList[env];
// 网络缓存对象
static DioCacheManager netCache = DioCacheManager(
CacheConfig(
baseUrl: baseUrl,
),
);
// 可选的主题列表
static List<MaterialColor> get themes => _themes;
// 是否为release版
static bool get isRelease => const bool.fromEnvironment("dart.vm.product");
//初始化全局信息会在APP启动时执行
static Future init() async {
dio = Dio();
// 添加拦截器
dio?.interceptors.add(netCache.interceptor);
// 初始化用户信息
_prefs = await SharedPreferences.getInstance();
var profile = _prefs?.getString("user_info");
if (profile != null) {
try {
if (kDebugMode) {
print('开始读取用户信息...');
}
profile = jsonDecode(profile);
if (kDebugMode) {
print('用户信息读取成功');
}
} catch (e) {
if (kDebugMode) {
print(e);
}
}
}
}
// 持久化Profile信息
static Future<bool>? saveProfile({required Map userInfo}) {
profile = userInfo;
final userJson = jsonEncode(userInfo);
return _prefs?.setString("user_info", userJson);
}
/// 读取持久化Profile信息
static Future<Map> getProfile() {
return jsonDecode(_prefs?.getString("user_info") ?? '{}');
}
}