GuestHelper.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (action) {
  26. action();
  27. }
  28. return YES;
  29. } else {
  30. // 未登录,弹出提示
  31. NSLog(@"[GuestHelper] User not logged in, showing login prompt");
  32. [self showLoginAlertWithViewController:viewController action:action];
  33. return NO;
  34. }
  35. }
  36. - (BOOL)isLoggedIn {
  37. UserModel *user = [UserModel shareInstance];
  38. return user.token.length > 0;
  39. }
  40. - (void)showLoginAlertWithViewController:(UIViewController *)viewController
  41. action:(void (^)(void))action {
  42. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
  43. message:@"请先登录后再进行此操作"
  44. preferredStyle:UIAlertControllerStyleAlert];
  45. // "去登录" 按钮
  46. UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"去登录"
  47. style:UIAlertActionStyleDefault
  48. handler:^(UIAlertAction * _Nonnull alertAction) {
  49. [self navigateToLoginWithViewController:viewController action:action];
  50. }];
  51. // "取消" 按钮
  52. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
  53. style:UIAlertActionStyleCancel
  54. handler:nil];
  55. [alert addAction:loginAction];
  56. [alert addAction:cancelAction];
  57. [viewController presentViewController:alert animated:YES completion:nil];
  58. }
  59. - (void)navigateToLoginWithViewController:(UIViewController *)viewController
  60. action:(void (^)(void))action {
  61. // 跳转到登录页
  62. LoginViewController *loginVC = [[LoginViewController alloc] init];
  63. // 登录成功后的回调
  64. __weak typeof(viewController) weakVC = viewController;
  65. loginVC.loginSuccessBlock = ^{
  66. NSLog(@"[GuestHelper] Login success, executing action");
  67. // 关闭登录页
  68. [weakVC dismissViewControllerAnimated:YES completion:^{
  69. // 执行原操作
  70. if (action) {
  71. action();
  72. }
  73. }];
  74. };
  75. // Present 登录页
  76. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginVC];
  77. navController.modalPresentationStyle = UIModalPresentationFullScreen;
  78. [viewController presentViewController:navController animated:YES completion:nil];
  79. }
  80. @end