102 lines
2.1 KiB
Dart
102 lines
2.1 KiB
Dart
import 'dart:convert';
|
||
|
||
import 'package:dio/dio.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(context) async {
|
||
dio = Dio();
|
||
|
||
// 初始化用户信息
|
||
_prefs = await SharedPreferences.getInstance();
|
||
var profile = _prefs?.getString("user_info");
|
||
if (profile != null) {
|
||
try {
|
||
printWhenDebug('开始读取用户信息...');
|
||
profile = jsonDecode(profile);
|
||
printWhenDebug('用户信息读取成功');
|
||
} catch (e) {
|
||
printWhenDebug(e);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 持久化Profile信息
|
||
static Future<bool>? saveProfile({required Map userInfo}) {
|
||
profile = userInfo;
|
||
final userJson = jsonEncode(userInfo);
|
||
return _prefs?.setString("user_info", userJson);
|
||
}
|
||
|
||
/// 读取持久化Profile信息
|
||
static Map<String, dynamic> getProfile() {
|
||
return jsonDecode(_prefs?.getString("user_info") ?? '{}');
|
||
}
|
||
}
|