GuestHelper.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // GuestHelper.m
  3. // AICity
  4. //
  5. // 游客模式管理实现
  6. //
  7. #import "GuestHelper.h"
  8. #import "UserModel.h"
  9. #import "LoginViewController.h"
  10. @implementation GuestHelper
  11. + (instancetype)sharedHelper {
  12. static GuestHelper *instance = nil;
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. instance = [[self alloc] init];
  16. });
  17. return instance;
  18. }
  19. - (BOOL)checkLoginWithViewController:(UIViewController *)viewController
  20. action:(void (^)(void))action {
  21. // 检查登录状态
  22. UserModel *user = [UserModel shareInstance];
  23. if (user.token.length > 0) {
  24. // 已登录,直接执行操作
  25. NSLog(@"[GuestHelper] User is logged in, executing action");
  26. if (action) {
  27. action();
  28. }
  29. return YES;
  30. } else {
  31. // 未登录,弹出提示
  32. NSLog(@"[GuestHelper] User not logged in, showing login prompt");
  33. [self showLoginAlertWithViewController:viewController action:action];
  34. return NO;
  35. }
  36. }
  37. - (BOOL)isLoggedIn {
  38. UserModel *user = [UserModel shareInstance];
  39. return user.token.length > 0;
  40. }
  41. - (void)showLoginAlertWithViewController:(UIViewController *)viewController
  42. action:(void (^)(void))action {
  43. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
  44. message:@"请先登录后再进行此操作"
  45. preferredStyle:UIAlertControllerStyleAlert];
  46. // "去登录" 按钮
  47. UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"去登录"
  48. style:UIAlertActionStyleDefault
  49. handler:^(UIAlertAction * _Nonnull alertAction) {
  50. [self navigateToLoginWithViewController:viewController action:action];
  51. }];
  52. // "取消" 按钮
  53. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
  54. style:UIAlertActionStyleCancel
  55. handler:nil];
  56. [alert addAction:loginAction];
  57. [alert addAction:cancelAction];
  58. [viewController presentViewController:alert animated:YES completion:nil];
  59. }
  60. - (void)navigateToLoginWithViewController:(UIViewController *)viewController
  61. action:(void (^)(void))action {
  62. // 跳转到登录页
  63. LoginViewController *loginVC = [[LoginViewController alloc] init];
  64. // 登录成功后的回调
  65. __weak typeof(viewController) weakVC = viewController;
  66. loginVC.loginSuccessBlock = ^{
  67. NSLog(@"[GuestHelper] Login success, executing action");
  68. // 关闭登录页
  69. [weakVC dismissViewControllerAnimated:YES completion:^{
  70. // 执行原操作
  71. if (action) {
  72. action();
  73. }
  74. }];
  75. };
  76. // Present 登录页
  77. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginVC];
  78. navController.modalPresentationStyle = UIModalPresentationFullScreen;
  79. [viewController presentViewController:navController animated:YES completion:nil];
  80. }
  81. @end