Skip to content

Commit

Permalink
Merge pull request #125 from NKUST-ITC/issue/#122
Browse files Browse the repository at this point in the history
Fix check login state and re-try method
  • Loading branch information
abc873693 authored Aug 13, 2020
2 parents f6d55cf + 90114d8 commit 7a65040
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 61 deletions.
7 changes: 5 additions & 2 deletions lib/api/ap_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class WebApHelper {

static int reLoginReTryCountsLimit = 3;
static int reLoginReTryCounts = 0;
int timeoutMs = 5000;
bool isLogin;

bool isLogin = false;

static WebApHelper get instance {
if (_instance == null) {
Expand Down Expand Up @@ -126,6 +126,9 @@ class WebApHelper {
statusCode: ApStatusCode.NETWORK_CONNECT_FAIL,
message: "Login exceeded retry limit");
}
if (isLogin != true) {
await apLogin(username: Helper.username, password: Helper.password);
}
String url =
"https://webap.nkust.edu.tw/nkust/${queryQid.substring(0, 2)}_pro/${queryQid}.jsp";

Expand Down
26 changes: 14 additions & 12 deletions lib/api/bus_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:cookie_jar/cookie_jar.dart';

//overwrite origin Cookie Manager.
import 'package:nkust_ap/api/private_cookie_manager.dart';

//parser
import 'package:nkust_ap/api/parser/bus_parser.dart';

Expand Down Expand Up @@ -77,10 +79,10 @@ class BusEncrypt {
var j = "1088434686";
var k = "260123741";

g = generateMd5("J${g}");
i = generateMd5("E${i}");
j = generateMd5("R${j}");
k = generateMd5("Y${k}");
g = generateMd5("J$g");
i = generateMd5("E$i");
j = generateMd5("R$j");
k = generateMd5("Y$k");
username = generateMd5(username + encA1(g));
password = generateMd5(username + password + "JERRY" + encA1(i));

Expand Down Expand Up @@ -113,10 +115,10 @@ class BusHelper {
static BusHelper _instance;
static CookieJar cookieJar;

static int reLoginReTryCountsLimit = 3;
static int reLoginReTryCountsLimit = 5;
static int reLoginReTryCounts = 0;

bool isLogin;
bool isLogin = false;

static BusEncrypt busEncryptObject;
static String busHost = "http://bus.kuas.edu.tw/";
Expand Down Expand Up @@ -151,7 +153,7 @@ class BusHelper {
dio.options.receiveTimeout = 5000;
}

void loginPrepare() async {
Future<void> loginPrepare() async {
// Get global cookie. Only cookies get from the root directory can be used.
await dio.head(busHost);
// This function will download encrypt js bus login required.
Expand Down Expand Up @@ -205,7 +207,7 @@ class BusHelper {
throw NullThrownError;
}

if (isLogin == false) {
if (!isLogin) {
await busLogin();
}
if (fromDateTime != null) {
Expand Down Expand Up @@ -244,7 +246,7 @@ class BusHelper {
throw NullThrownError;
}

if (isLogin == false) {
if (!isLogin) {
await busLogin();
}
Response res = await dio.post(
Expand All @@ -268,7 +270,7 @@ class BusHelper {
throw NullThrownError;
}

if (isLogin == false) {
if (!isLogin) {
await busLogin();
}
Response res = await dio.post(
Expand All @@ -292,7 +294,7 @@ class BusHelper {
throw NullThrownError;
}

if (isLogin == false) {
if (!isLogin) {
await busLogin();
}

Expand All @@ -319,7 +321,7 @@ class BusHelper {
throw NullThrownError;
}

if (isLogin == false) {
if (!isLogin) {
await busLogin();
}

Expand Down
77 changes: 30 additions & 47 deletions lib/api/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,11 @@ class Helper {
return (callback == null) ? data : callback.onSuccess(data);
} on DioError catch (dioError) {
if (dioError.hasResponse) {
if (dioError.isExpire && canReLogin && await reLogin(callback)) {
reLoginCount++;
return getBusTimeTables(dateTime: dateTime, callback: callback);
} else {
if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
}
BusHelper.reLoginReTryCounts = 0;
if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
} else
callback?.onFailure(dioError);
if (callback == null) throw dioError;
Expand All @@ -552,15 +548,12 @@ class Helper {
return (callback == null) ? data : callback.onSuccess(data);
} on DioError catch (dioError) {
if (dioError.hasResponse) {
if (dioError.isExpire && canReLogin && await reLogin(callback)) {
reLoginCount++;
return getBusReservations(callback: callback);
} else {
if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
}
BusHelper.reLoginReTryCounts = 0;

if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
} else
callback?.onFailure(dioError);
if (callback == null) throw dioError;
Expand All @@ -575,22 +568,18 @@ class Helper {
String busId,
GeneralCallback<BookingBusData> callback,
}) async {
if (isExpire()) await login(username: username, password: password);
try {
BookingBusData data = await BusHelper.instance.busBook(busId: busId);
reLoginCount = 0;
return (callback == null) ? data : callback.onSuccess(data);
} on DioError catch (dioError) {
if (dioError.hasResponse) {
if (dioError.isExpire && canReLogin && await reLogin(callback)) {
reLoginCount++;
return bookingBusReservation(busId: busId, callback: callback);
} else {
if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
}
BusHelper.reLoginReTryCounts = 0;

if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
} else
callback?.onFailure(dioError);
if (callback == null) throw dioError;
Expand All @@ -605,21 +594,19 @@ class Helper {
String cancelKey,
GeneralCallback<CancelBusData> callback,
}) async {
print(cancelKey);
try {
CancelBusData data = await BusHelper.instance.busUnBook(busId: cancelKey);
reLoginCount = 0;
return (callback == null) ? data : callback.onSuccess(data);
} on DioError catch (dioError) {
if (dioError.hasResponse) {
if (dioError.isExpire && canReLogin && await reLogin(callback)) {
reLoginCount++;
return cancelBusReservation(cancelKey: cancelKey, callback: callback);
} else {
if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
}
BusHelper.reLoginReTryCounts = 0;

if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
} else
callback?.onFailure(dioError);
if (callback == null) throw dioError;
Expand All @@ -633,7 +620,6 @@ class Helper {
Future<BusViolationRecordsData> getBusViolationRecords({
GeneralCallback<BusViolationRecordsData> callback,
}) async {
if (isExpire()) await login(username: username, password: password);
try {
BusViolationRecordsData data =
await BusHelper.instance.busViolationRecords();
Expand All @@ -642,15 +628,12 @@ class Helper {
return (callback == null) ? data : callback.onSuccess(data);
} on DioError catch (dioError) {
if (dioError.hasResponse) {
if (dioError.isExpire && canReLogin && await reLogin(callback)) {
reLoginCount++;
return getBusViolationRecords(callback: callback);
} else {
if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
}
BusHelper.reLoginReTryCounts = 0;

if (dioError.isServerError)
callback?.onError(dioError.serverErrorResponse);
else
callback?.onFailure(dioError);
} else
callback?.onFailure(dioError);
if (callback == null) throw dioError;
Expand Down

0 comments on commit 7a65040

Please sign in to comment.