|
|
@@ -3,12 +3,13 @@ import { ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit, signal }
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
|
|
|
import { Eye, EyeOff, Leaf, LockKeyhole, LucideAngularModule, MessageSquareText, Phone, UserPlus } from 'lucide-angular';
|
|
|
-import { finalize } from 'rxjs';
|
|
|
+import { finalize, timeout } from 'rxjs';
|
|
|
import { ApiError, ApiService } from '../../core/api.service';
|
|
|
import { AuthService } from '../../core/auth.service';
|
|
|
import { AppUser } from '../../core/models';
|
|
|
import { PlatformService } from '../../core/platform.service';
|
|
|
import { SessionService } from '../../core/session.service';
|
|
|
+import { LiveVersions } from '../../core/live-data-refresh.service';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-login-page',
|
|
|
@@ -117,14 +118,10 @@ export class LoginPageComponent implements OnInit, OnDestroy {
|
|
|
private loginRequest(action: 'user_login_passwd' | 'user_login_mobile', payload: Record<string, unknown>): void {
|
|
|
this.platform.loading.set(true);
|
|
|
this.auditMessage.set('');
|
|
|
- this.auth.login(action, payload).pipe(finalize(() => this.platform.loading.set(false))).subscribe({
|
|
|
- next: (user) => {
|
|
|
- this.session.setLogin(user);
|
|
|
- this.platform.toast('登录成功', 'success');
|
|
|
- const returnUrl = this.route.snapshot.queryParamMap.get('returnUrl');
|
|
|
- void this.router.navigateByUrl(returnUrl || '/pages/home/index');
|
|
|
- },
|
|
|
+ this.auth.login(action, payload).subscribe({
|
|
|
+ next: (user) => this.completeLogin(user, false),
|
|
|
error: (error: ApiError) => {
|
|
|
+ this.platform.loading.set(false);
|
|
|
if (error.message.includes('审核中')) this.auditMessage.set(error.message);
|
|
|
this.platform.toast(error.message, 'error');
|
|
|
},
|
|
|
@@ -133,13 +130,33 @@ export class LoginPageComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
private registerRequest(action: 'user_register' | 'user_register_mobile', payload: Record<string, unknown>): void {
|
|
|
this.platform.loading.set(true);
|
|
|
- this.api.post<AppUser>(action, payload).pipe(finalize(() => this.platform.loading.set(false))).subscribe({
|
|
|
- next: ({ result }) => {
|
|
|
- this.session.setLogin(result);
|
|
|
- this.platform.toast('注册成功', 'success');
|
|
|
- void this.router.navigateByUrl('/pages/home/index');
|
|
|
+ this.api.post<AppUser>(action, payload).subscribe({
|
|
|
+ next: ({ result }) => this.completeLogin(result, true),
|
|
|
+ error: (error: ApiError) => {
|
|
|
+ this.platform.loading.set(false);
|
|
|
+ this.platform.toast(`注册失败:${error.message}`, 'error');
|
|
|
},
|
|
|
- error: (error: ApiError) => this.platform.toast(`注册失败:${error.message}`, 'error'),
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private completeLogin(user: AppUser, registered: boolean): void {
|
|
|
+ this.session.setLogin(user);
|
|
|
+ if (!this.session.isLoggedIn()) {
|
|
|
+ this.platform.loading.set(false);
|
|
|
+ this.platform.toast('登录状态无效,请重新登录', 'error');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const enter = (synced: boolean) => {
|
|
|
+ this.platform.toast(synced ? (registered ? '注册成功' : '登录成功') : '登录成功,数据同步暂不可用,请稍后手动刷新', synced ? 'success' : 'error');
|
|
|
+ const returnUrl = this.route.snapshot.queryParamMap.get('returnUrl');
|
|
|
+ void this.router.navigateByUrl(registered ? '/pages/home/index' : returnUrl || '/pages/home/index');
|
|
|
+ };
|
|
|
+ this.api.post<LiveVersions>('app_live_versions', {
|
|
|
+ studentId: Number(user.userId),
|
|
|
+ scopes: ['learning', 'courses', 'appointments', 'reading', 'review', 'account'],
|
|
|
+ }).pipe(timeout({ first: 45_000 }), finalize(() => this.platform.loading.set(false))).subscribe({
|
|
|
+ next: ({ result }) => enter(result.syncStatus?.state !== 'unavailable'),
|
|
|
+ error: () => enter(false),
|
|
|
});
|
|
|
}
|
|
|
|