| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- //
- // GuestHelper.m
- // AICity
- //
- // 游客模式管理实现
- //
- #import "GuestHelper.h"
- #import "UserModel.h"
- #import "LoginViewController.h"
- @implementation GuestHelper
- + (instancetype)sharedHelper {
- static GuestHelper *instance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [[self alloc] init];
- });
- return instance;
- }
- - (BOOL)checkLoginWithViewController:(UIViewController *)viewController
- action:(void (^)(void))action {
- // 检查登录状态
- UserModel *user = [UserModel shareInstance];
-
- if (user.token.length > 0) {
- // 已登录,直接执行操作
- if (action) {
- action();
- }
- return YES;
- } else {
- // 未登录,弹出提示
- NSLog(@"[GuestHelper] User not logged in, showing login prompt");
- [self showLoginAlertWithViewController:viewController action:action];
- return NO;
- }
- }
- - (BOOL)isLoggedIn {
- UserModel *user = [UserModel shareInstance];
- return user.token.length > 0;
- }
- - (void)showLoginAlertWithViewController:(UIViewController *)viewController
- action:(void (^)(void))action {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
- message:@"请先登录后再进行此操作"
- preferredStyle:UIAlertControllerStyleAlert];
-
- // "去登录" 按钮
- UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"去登录"
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction * _Nonnull alertAction) {
- [self navigateToLoginWithViewController:viewController action:action];
- }];
-
- // "取消" 按钮
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
- style:UIAlertActionStyleCancel
- handler:nil];
-
- [alert addAction:loginAction];
- [alert addAction:cancelAction];
-
- [viewController presentViewController:alert animated:YES completion:nil];
- }
- - (void)navigateToLoginWithViewController:(UIViewController *)viewController
- action:(void (^)(void))action {
- // 跳转到登录页
- LoginViewController *loginVC = [[LoginViewController alloc] init];
-
- // 登录成功后的回调
- __weak typeof(viewController) weakVC = viewController;
- loginVC.loginSuccessBlock = ^{
- NSLog(@"[GuestHelper] Login success, executing action");
-
- // 关闭登录页
- [weakVC dismissViewControllerAnimated:YES completion:^{
- // 执行原操作
- if (action) {
- action();
- }
- }];
- };
-
- // Present 登录页
- UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginVC];
- navController.modalPresentationStyle = UIModalPresentationFullScreen;
- [viewController presentViewController:navController animated:YES completion:nil];
- }
- @end
|