This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sou1maker
committed
Jun 7, 2022
1 parent
81f7ea3
commit f114931
Showing
12 changed files
with
220 additions
and
169 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class Auth { | ||
/// 用户状态 | ||
static Future<bool> get userLoginState async { | ||
return await session != null || await refresh != null; | ||
} | ||
|
||
/// 获取会话令牌 | ||
static Future<String?> get session async { | ||
final prefs = await SharedPreferences.getInstance(); | ||
final list = prefs.getStringList('session'); | ||
if (list == null) return null; | ||
|
||
if (DateTime.now().isAfter(DateTime.parse(list[0]))) { | ||
await prefs.remove('session'); | ||
return null; | ||
} | ||
|
||
return list[1]; | ||
} | ||
|
||
/// 设置会话令牌 | ||
static Future<bool> setSession(String token) async { | ||
final prefs = await SharedPreferences.getInstance(); | ||
|
||
var expire = DateTime.now().add(const Duration(minutes: 14)).toIso8601String(); | ||
return await prefs.setStringList('session', [expire, token]); | ||
} | ||
|
||
/// 移除会话令牌 | ||
static Future<void> removeSession() async { | ||
final prefs = await SharedPreferences.getInstance(); | ||
await prefs.remove('session'); | ||
} | ||
|
||
/// 获取刷新令牌 | ||
static Future<String?> get refresh async { | ||
var file = await _file; | ||
if (!await file.exists()) return null; | ||
|
||
var refreshMap = json.decode(await file.readAsString()); | ||
// 过期返回null | ||
if (DateTime.now().isAfter(DateTime.parse(refreshMap['expire']))) { | ||
return null; | ||
} | ||
|
||
return refreshMap['token']; | ||
} | ||
|
||
/// 设置刷新令牌 | ||
static Future<void> setRefresh(String token) async { | ||
var file = await _file; | ||
var expire = DateTime.now().add(const Duration(days: 29)).toIso8601String(); | ||
await file.writeAsString(json.encode({'token': token, 'expire': expire})); | ||
} | ||
|
||
/// 移除会话令牌 | ||
static Future<void> removeRefresh() async { | ||
var file = await _file; | ||
if (await file.exists()) await file.delete(); | ||
} | ||
|
||
/// 获取 .token 文件 | ||
static Future<File> get _file async { | ||
final directory = await getApplicationDocumentsDirectory(); | ||
return File('${directory.path}/.token'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path_provider/path_provider.dart'; | ||
|
||
/// 本地设置 | ||
class LocalSetting { | ||
/// 查询 .setting 文件 | ||
static Future<Map<String, dynamic>> all() async { | ||
var file = await _file; | ||
return json.decode(await file.readAsString()); | ||
} | ||
|
||
/// 往 .setting 文件中插入数据 | ||
static Future<void> insert(Map<String, dynamic> map) async { | ||
var file = await _file; | ||
var oldMap = await json.decode(await file.readAsString()) as Map<String, dynamic>; | ||
|
||
for (var key in map.keys) { | ||
if (oldMap.containsKey(key)) { | ||
oldMap[key] = map[key]; | ||
} else { | ||
oldMap.addAll({key: map[key]}); | ||
} | ||
} | ||
|
||
file.writeAsString(json.encode(oldMap)); | ||
} | ||
|
||
/// 从 .setting 文件中删除数据 | ||
static Future<void> delete(key) async { | ||
var file = await _file; | ||
var oldMap = await json.decode(await file.readAsString()) as Map<String, dynamic>; | ||
|
||
if (oldMap.containsKey(key)) { | ||
oldMap.remove(key); | ||
} | ||
|
||
file.writeAsString(json.encode(oldMap)); | ||
} | ||
|
||
/// 获取 .setting 文件 | ||
static Future<File> get _file async { | ||
final directory = await getApplicationDocumentsDirectory(); | ||
var file = File('${directory.path}/.setting'); | ||
// 不存在则使用默认项新建 | ||
if (!await file.exists()) file.writeAsString(json.encode(_default)); | ||
|
||
return file; | ||
} | ||
|
||
/// 默认 .user 文件 | ||
static const _default = { | ||
// 内容分级 | ||
'content_rating': ['safe', 'suggestive', 'erotica', 'pornographic'], | ||
// 章节可用翻译语言 | ||
'translated_language': ['zh', 'zh-hk'], | ||
// 阅读图片是否压缩 | ||
'data_saver': false, | ||
}; | ||
} | ||
|
||
/// 获取内容分级 | ||
Future<List<String>> getContentRating() async { | ||
var all = await LocalSetting.all(); | ||
return all['content_rating']; | ||
} | ||
|
||
/// 设置内容分级 | ||
Future<void> setContentRating(List<String> contentRating) async => | ||
await LocalSetting.insert({'content_rating': contentRating}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.