Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
🎨 code finishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sou1maker committed Jun 7, 2022
1 parent 81f7ea3 commit f114931
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 169 deletions.
20 changes: 0 additions & 20 deletions lib/models/refresh.dart

This file was deleted.

6 changes: 3 additions & 3 deletions lib/provider/follow_provider.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:flutter/material.dart';
import 'package:komikku/dex/apis/manga_api.dart';
import 'package:komikku/utils/user.dart';
import 'package:komikku/utils/auth.dart';

class FollowProvider extends ChangeNotifier {
/// 订阅漫画
Future<void> followManga(String id) async {
// 未登录,直接返回
if (!await userLoginState()) return;
if (!await Auth.userLoginState) return;
await MangaApi.followMangaAsync(id);

notifyListeners();
Expand All @@ -15,7 +15,7 @@ class FollowProvider extends ChangeNotifier {
/// 退订漫画
Future<void> unfollowManga(String id) async {
// 未登录,直接返回
if (!await userLoginState()) return;
if (!await Auth.userLoginState) return;
await MangaApi.unfollowMangaAsync(id);

notifyListeners();
Expand Down
10 changes: 5 additions & 5 deletions lib/provider/user_provider.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:komikku/dex/apis/auth_api.dart';
import 'package:komikku/dex/models/login.dart';
import 'package:komikku/utils/user.dart';
import 'package:komikku/utils/auth.dart';

class UserProvider extends ChangeNotifier {
/// 登录
Expand All @@ -11,16 +11,16 @@ class UserProvider extends ChangeNotifier {
: Login(username: emailOrUsername, password: password);

var response = await AuthApi.loginAsync(login);
await setRefresh(response.token.refresh);
await setSession(response.token.session);
await Auth.setRefresh(response.token.refresh);
await Auth.setSession(response.token.session);

notifyListeners();
}

/// 登出
Future<void> logout() async {
await removeSession();
await removeRefresh();
await Auth.removeSession();
await Auth.removeRefresh();
await AuthApi.logoutAsync();

notifyListeners();
Expand Down
73 changes: 73 additions & 0 deletions lib/utils/auth.dart
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');
}
}
10 changes: 5 additions & 5 deletions lib/utils/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:dio/dio.dart';
import 'package:komikku/dex/apis/auth_api.dart';
import 'package:komikku/dex/settings.dart';
import 'package:komikku/dex/models/refresh_token.dart';
import 'package:komikku/utils/user.dart';
import 'package:komikku/utils/auth.dart';

class HttpUtil {
static final HttpUtil _instance = HttpUtil._internal();
Expand Down Expand Up @@ -93,14 +93,14 @@ class HttpUtil {
Options options = Options();

// 查看是否登录
if (await userLoginState()) {
var token = await getSession();
if (await Auth.userLoginState) {
var token = await Auth.session;

// session 为空的情况,请求刷新session
if (token == null) {
var response = await AuthApi.refreshAsync(RefreshToken(token: (await getRefresh())!));
var response = await AuthApi.refreshAsync(RefreshToken(token: (await Auth.refresh)!));
token = response.token.session;
await setSession(token);
await Auth.setSession(token);
}

options = Options(headers: {
Expand Down
71 changes: 71 additions & 0 deletions lib/utils/local_setting.dart
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});
95 changes: 0 additions & 95 deletions lib/utils/user.dart

This file was deleted.

Loading

0 comments on commit f114931

Please sign in to comment.