opt: login status processing
This commit is contained in:
@ -3,6 +3,7 @@
|
|||||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||||
|
import 'package:pilipala/utils/login.dart';
|
||||||
|
|
||||||
class ApiInterceptor extends Interceptor {
|
class ApiInterceptor extends Interceptor {
|
||||||
@override
|
@override
|
||||||
@ -18,6 +19,9 @@ class ApiInterceptor extends Interceptor {
|
|||||||
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||||
try {
|
try {
|
||||||
// 在响应之后处理数据
|
// 在响应之后处理数据
|
||||||
|
if (response.data['code'] == -101) {
|
||||||
|
LoginUtils.loginOut();
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
print('ApiInterceptor: $err');
|
print('ApiInterceptor: $err');
|
||||||
}
|
}
|
||||||
|
@ -61,16 +61,7 @@ class SettingController extends GetxController {
|
|||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
// 清空cookie
|
await LoginUtils.loginOut();
|
||||||
await Request.cookieManager.cookieJar.deleteAll();
|
|
||||||
Request.dio.options.headers['cookie'] = '';
|
|
||||||
|
|
||||||
// 清空本地存储的用户标识
|
|
||||||
userInfoCache.put('userInfoCache', null);
|
|
||||||
localCache
|
|
||||||
.put(LocalCacheKey.accessKey, {'mid': -1, 'value': ''});
|
|
||||||
|
|
||||||
await LoginUtils.refreshLoginStatus(false);
|
|
||||||
SmartDialog.dismiss().then((value) => Get.back());
|
SmartDialog.dismiss().then((value) => Get.back());
|
||||||
},
|
},
|
||||||
child: const Text('确认'),
|
child: const Text('确认'),
|
||||||
|
@ -7,15 +7,20 @@ import 'package:flutter/services.dart';
|
|||||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
|
import 'package:pilipala/http/index.dart';
|
||||||
import 'package:pilipala/http/user.dart';
|
import 'package:pilipala/http/user.dart';
|
||||||
import 'package:pilipala/pages/dynamics/index.dart';
|
import 'package:pilipala/pages/dynamics/index.dart';
|
||||||
import 'package:pilipala/pages/home/index.dart';
|
import 'package:pilipala/pages/home/index.dart';
|
||||||
import 'package:pilipala/pages/mine/index.dart';
|
import 'package:pilipala/pages/mine/index.dart';
|
||||||
import 'package:pilipala/utils/cookie.dart';
|
import 'package:pilipala/utils/cookie.dart';
|
||||||
|
import 'package:pilipala/utils/global_data_cache.dart';
|
||||||
import 'package:pilipala/utils/storage.dart';
|
import 'package:pilipala/utils/storage.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
class LoginUtils {
|
class LoginUtils {
|
||||||
|
static Box userInfoCache = GStrorage.userInfo;
|
||||||
|
static Box localCache = GStrorage.localCache;
|
||||||
|
|
||||||
static Future refreshLoginStatus(bool status) async {
|
static Future refreshLoginStatus(bool status) async {
|
||||||
try {
|
try {
|
||||||
// 更改我的页面登录状态
|
// 更改我的页面登录状态
|
||||||
@ -109,4 +114,14 @@ class LoginUtils {
|
|||||||
Clipboard.setData(ClipboardData(text: content));
|
Clipboard.setData(ClipboardData(text: content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 退出登录
|
||||||
|
static loginOut() async {
|
||||||
|
await Request.cookieManager.cookieJar.deleteAll();
|
||||||
|
Request.dio.options.headers['cookie'] = '';
|
||||||
|
userInfoCache.put('userInfoCache', null);
|
||||||
|
localCache.put(LocalCacheKey.accessKey, {'mid': -1, 'value': ''});
|
||||||
|
GlobalDataCache().userInfo = null;
|
||||||
|
await refreshLoginStatus(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user