|
|
@@ -1,6 +1,7 @@
|
|
|
package com.fujica.abk.api;
|
|
|
|
|
|
import com.fujica.abk.common.EventBus;
|
|
|
+import com.fujica.abk.component.AuthorizationDialog;
|
|
|
import com.fujica.abk.model.out.LoginRes;
|
|
|
import com.fujica.abk.utils.Log;
|
|
|
import com.fujica.abk.utils.R;
|
|
|
@@ -25,6 +26,9 @@ import java.util.concurrent.CompletableFuture;
|
|
|
* 认证工具类
|
|
|
*/
|
|
|
public class auth {
|
|
|
+ // 登录状态标志,防止并发登录
|
|
|
+ private static boolean authing = false;
|
|
|
+
|
|
|
/**
|
|
|
* 登录
|
|
|
*
|
|
|
@@ -34,11 +38,51 @@ public class auth {
|
|
|
public static CompletableFuture<Boolean> login(Context context) {
|
|
|
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
|
|
|
|
|
+ // 检查是否正在登录
|
|
|
+ if (authing) {
|
|
|
+ Log.info("正在登录中,请勿重复操作");
|
|
|
+ Toast.info(context, "正在登录中...");
|
|
|
+ future.complete(false);
|
|
|
+ return future;
|
|
|
+ }
|
|
|
+
|
|
|
if (cache.isAuth(context)) {
|
|
|
future.complete(true);
|
|
|
return future;
|
|
|
}
|
|
|
+
|
|
|
+ // 设置登录状态
|
|
|
+ authing = true;
|
|
|
+
|
|
|
+ // 登录之前先显示授权确认弹框
|
|
|
+ // 确保在主线程中执行
|
|
|
+ context.getUITaskDispatcher().asyncDispatch(() -> {
|
|
|
+ AuthorizationDialog authDialog = new AuthorizationDialog(context);
|
|
|
+ authDialog.setOnAuthorizationListener(new AuthorizationDialog.OnAuthorizationListener() {
|
|
|
+ @Override
|
|
|
+ public void onAuthorized() {
|
|
|
+ // 用户确认授权后,继续登录流程
|
|
|
+ proceedWithLogin(context, future);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCancelled() {
|
|
|
+ // 用户取消授权
|
|
|
+ Toast.error(context, "已取消授权");
|
|
|
+ authing = false; // 重置登录状态
|
|
|
+ future.complete(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ authDialog.show();
|
|
|
+ });
|
|
|
+
|
|
|
+ return future;
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 执行实际的登录流程
|
|
|
+ */
|
|
|
+ private static void proceedWithLogin(Context context, CompletableFuture<Boolean> future) {
|
|
|
if (true) {
|
|
|
HWResult hwResult = new HWResult();
|
|
|
|
|
|
@@ -62,7 +106,7 @@ public class auth {
|
|
|
data.put("unionId", hwResult.getUnionId());
|
|
|
option.setData(data);
|
|
|
processCodeLogin(context, option, hwResult, future);
|
|
|
- return future;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
@@ -89,16 +133,22 @@ public class auth {
|
|
|
|
|
|
processCodeLogin(context, option, hwResult, future);
|
|
|
}).exceptionally(e -> {
|
|
|
- Log.error(e);
|
|
|
- future.complete(false);
|
|
|
- return null;
|
|
|
+ try {
|
|
|
+ Log.error(e);
|
|
|
+ future.complete(false);
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ authing = false;
|
|
|
+ }
|
|
|
});
|
|
|
} catch (Exception e) {
|
|
|
- Log.error(e);
|
|
|
- future.complete(false);
|
|
|
+ try {
|
|
|
+ Log.error(e);
|
|
|
+ future.complete(false);
|
|
|
+ } finally {
|
|
|
+ authing = false;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- return future;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -112,21 +162,31 @@ public class auth {
|
|
|
private static void processCodeLogin(Context context, ApiOption option, HWResult hwResult, CompletableFuture<Boolean> future) {
|
|
|
api.http(context, option, new TypeToken<R<LoginRes>>() {
|
|
|
}).thenAccept((R<LoginRes> result) -> {
|
|
|
- if (result != null && result.isSuccess() && result.getData() != null) {
|
|
|
- LoginRes res = result.getData();
|
|
|
- cache.setOpenId(context, res.getOpenId());
|
|
|
- cache.setToken(context, res.getToken());
|
|
|
- cache.setMobile(context, res.getMobile());
|
|
|
- // 发布登录成功事件,通知UI更新
|
|
|
- EventBus.getInstance().emit("onLoginSuccess");
|
|
|
- future.complete(true);
|
|
|
- } else {
|
|
|
- future.complete(false);
|
|
|
+ try {
|
|
|
+ if (result != null && result.isSuccess() && result.getData() != null) {
|
|
|
+ LoginRes res = result.getData();
|
|
|
+ cache.setOpenId(context, res.getOpenId());
|
|
|
+ cache.setToken(context, res.getToken());
|
|
|
+ cache.setMobile(context, res.getMobile());
|
|
|
+ // 发布登录成功事件,通知UI更新
|
|
|
+ EventBus.getInstance().emit("onLoginSuccess");
|
|
|
+ future.complete(true);
|
|
|
+ } else {
|
|
|
+ future.complete(false);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ // 确保最终重置登录状态
|
|
|
+ authing = false;
|
|
|
}
|
|
|
}).exceptionally(e -> {
|
|
|
- Log.error(e);
|
|
|
- future.complete(false);
|
|
|
- return null;
|
|
|
+ try {
|
|
|
+ Log.error(e);
|
|
|
+ future.complete(false);
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ // 确保最终重置登录状态
|
|
|
+ authing = false;
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
|