修改老版本无法登录的问题

This commit is contained in:
jyuesong
2022-01-21 10:30:57 +08:00
parent 2932ad3a64
commit abc6130940
7 changed files with 152 additions and 119 deletions

View File

@@ -24,7 +24,18 @@ class Api {
},
);
}
static Future<HttpResponse<LoginBean>> loginOld(
String userName,
String passWord,
) async {
return await Http.post<LoginBean>(
Url.loginOld,
{
"username": userName,
"password": passWord,
},
);
}
static Future<HttpResponse<LoginBean>> loginTwo(
String userName,
String passWord,

View File

@@ -5,6 +5,7 @@ import 'package:dio/dio.dart';
import 'package:dio_log/dio_log.dart';
import 'package:flutter/foundation.dart';
import 'package:qinglong_app/base/http/token_interceptor.dart';
import 'package:qinglong_app/base/http/url.dart';
import 'package:qinglong_app/base/userinfo_viewmodel.dart';
import 'package:qinglong_app/utils/extension.dart';
@@ -54,7 +55,7 @@ class Http {
return decodeResponse<T>(response, serializationName, compute);
} on DioError catch (e) {
return exceptionHandler<T>(e);
return exceptionHandler<T>(e, uri);
}
}
@@ -74,7 +75,7 @@ class Http {
compute,
);
} on DioError catch (e) {
return exceptionHandler<T>(e);
return exceptionHandler<T>(e, uri);
}
}
@@ -94,7 +95,7 @@ class Http {
compute,
);
} on DioError catch (e) {
return exceptionHandler<T>(e);
return exceptionHandler<T>(e, uri);
}
}
@@ -113,7 +114,7 @@ class Http {
compute,
);
} on DioError catch (e) {
return exceptionHandler<T>(e);
return exceptionHandler<T>(e, uri);
}
}
@@ -125,8 +126,8 @@ class Http {
}
}
static HttpResponse<T> exceptionHandler<T>(DioError e) {
if (e.response?.data!= null && e.response?.data['code'] == 401) {
static HttpResponse<T> exceptionHandler<T>(DioError e, String path) {
if (e.response?.statusCode == 401 && !Url.inWhiteList(path)) {
if (!getIt<UserInfoViewModel>().useSecretLogined) {
exitLogin();
}

View File

@@ -7,10 +7,8 @@ import '../userinfo_viewmodel.dart';
class TokenInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
if (getIt<UserInfoViewModel>().token != null &&
getIt<UserInfoViewModel>().token!.isNotEmpty) {
options.headers["Authorization"] =
"Bearer " + getIt<UserInfoViewModel>().token!;
if (getIt<UserInfoViewModel>().token != null && getIt<UserInfoViewModel>().token!.isNotEmpty) {
options.headers["Authorization"] = "Bearer " + getIt<UserInfoViewModel>().token!;
}
options.headers["User-Agent"] =
@@ -18,18 +16,9 @@ class TokenInterceptor extends Interceptor {
options.headers["Content-Type"] = "application/json;charset=UTF-8";
// '/api/user/login',
// '/open/auth/token',
// '/api/user/two-factor/login',
// '/api/system',
// '/api/user/init',
// '/api/user/notification/init'
if (options.path != Url.loginByClientId &&
options.path != Url.loginTwo &&
options.path != Url.login) {
options.queryParameters["t"] =
(DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
if (!Url.inWhiteList(options.path)) {
options.queryParameters["t"] = (DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
}
return handler.next(options);
}

View File

@@ -3,6 +3,9 @@ import '../userinfo_viewmodel.dart';
class Url {
static get login => "/api/user/login";
static get loginOld => "/api/login";
static get loginTwo => "/api/user/two-factor/login";
static const loginByClientId = "/open/auth/token";
static const user = "/api/user";
@@ -50,6 +53,7 @@ class Url {
static get taskLogDetail => getIt<UserInfoViewModel>().useSecretLogined ? "/open/logs/" : "/api/logs/";
static get scripts => getIt<UserInfoViewModel>().useSecretLogined ? "/open/scripts/files" : "/api/scripts/files";
static get scriptUpdate => getIt<UserInfoViewModel>().useSecretLogined ? "/open/scripts" : "/api/scripts";
static get scriptDetail => getIt<UserInfoViewModel>().useSecretLogined ? "/open/scripts/" : "/api/scripts/";
@@ -58,9 +62,6 @@ class Url {
static get dependencyReinstall => getIt<UserInfoViewModel>().useSecretLogined ? "/open/dependencies/reinstall" : "/api/dependencies/reinstall";
static intimeLog(String cronId) {
return getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/$cronId/log" : "/api/crons/$cronId/log";
}
@@ -68,4 +69,11 @@ class Url {
static envMove(String envId) {
return getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs/$envId/move" : "/api/envs/$envId/move";
}
static bool inWhiteList(String path) {
if (path == login || path == loginByClientId || path == loginTwo || path == loginOld) {
return true;
}
return false;
}
}