|
|
@@ -1,101 +0,0 @@
|
|
|
-import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
|
-import { Injectable } from '@angular/core';
|
|
|
-import { firstValueFrom } from 'rxjs';
|
|
|
-import { API_CONFIG } from '../../core/app.constants';
|
|
|
-import { AppUser } from '../../core/models';
|
|
|
-import { SessionService } from '../../core/session.service';
|
|
|
-import { NcloudPointer, NcloudRecord, NcloudUser } from './reading.models';
|
|
|
-
|
|
|
-const USER_CACHE_KEY = 'NCloud/dev/User';
|
|
|
-interface CloudEnvelope<T> { retcode: number; retmsg?: string; result: T }
|
|
|
-
|
|
|
-@Injectable({ providedIn: 'root' })
|
|
|
-export class NcloudReadingService {
|
|
|
- private currentUser: NcloudUser | null = this.readCachedUser();
|
|
|
-
|
|
|
- constructor(private readonly http: HttpClient, private readonly sessions: SessionService) {}
|
|
|
-
|
|
|
- get user(): NcloudUser | null { return this.currentUser; }
|
|
|
-
|
|
|
- get userPointer(): NcloudPointer {
|
|
|
- if (!this.currentUser?.objectId) throw new Error('NCloud 用户尚未登录');
|
|
|
- return this.pointer('_User', this.currentUser.objectId);
|
|
|
- }
|
|
|
-
|
|
|
- adoptSessionToken(token: string): void {
|
|
|
- if (token) this.currentUser = { ...(this.currentUser ?? { objectId: '' }), sessionToken: token };
|
|
|
- }
|
|
|
-
|
|
|
- async ensureUser(appUser: AppUser | null, tokenHint = '', mobileHint = ''): Promise<NcloudUser> {
|
|
|
- void mobileHint;
|
|
|
- const token = tokenHint || this.sessions.token();
|
|
|
- if (token) this.adoptSessionToken(token);
|
|
|
- const objectId = String(appUser?.['objectId'] || this.currentUser?.objectId || '').trim();
|
|
|
- if (token.startsWith('r:') && objectId) {
|
|
|
- return this.persistUser({
|
|
|
- ...(this.currentUser ?? { objectId }),
|
|
|
- objectId,
|
|
|
- sessionToken: token,
|
|
|
- username: String(appUser?.userName || this.currentUser?.username || ''),
|
|
|
- nickname: String(appUser?.honeyName || this.currentUser?.nickname || ''),
|
|
|
- mobile: String(appUser?.mobile || this.currentUser?.mobile || ''),
|
|
|
- });
|
|
|
- }
|
|
|
- throw new Error('阅读学习账户需要新的登录会话,请退出后重新登录');
|
|
|
- }
|
|
|
-
|
|
|
- async updateUser(patch: Partial<NcloudUser>): Promise<NcloudUser> {
|
|
|
- if (!this.currentUser?.objectId) throw new Error('NCloud 用户尚未登录');
|
|
|
- const updated = await this.call<NcloudUser>('reading/user/update', { patch });
|
|
|
- return this.persistUser({ ...this.currentUser, ...updated, ...patch });
|
|
|
- }
|
|
|
-
|
|
|
- async query<T extends NcloudRecord>(className: string, where: Record<string, unknown>, options: {
|
|
|
- order?: string; limit?: number; skip?: number;
|
|
|
- } = {}): Promise<T[]> {
|
|
|
- return this.call<T[]>('reading/query', { className, where, ...options });
|
|
|
- }
|
|
|
-
|
|
|
- async get<T extends NcloudRecord>(className: string, objectId: string): Promise<T> {
|
|
|
- return this.call<T>('reading/get', { className, objectId });
|
|
|
- }
|
|
|
-
|
|
|
- async create<T extends NcloudRecord>(className: string, value: Omit<T, 'objectId'>): Promise<T> {
|
|
|
- return this.call<T>('reading/create', { className, value });
|
|
|
- }
|
|
|
-
|
|
|
- async update<T extends NcloudRecord>(className: string, value: T, patch: Partial<T>): Promise<T> {
|
|
|
- const response = await this.call<Partial<T>>('reading/update', { className, objectId: value.objectId, patch });
|
|
|
- return { ...value, ...patch, ...response };
|
|
|
- }
|
|
|
-
|
|
|
- pointer(className: string, objectId: string): NcloudPointer {
|
|
|
- return { __type: 'Pointer', className, objectId };
|
|
|
- }
|
|
|
-
|
|
|
- private async call<T>(action: string, params: Record<string, unknown> = {}): Promise<T> {
|
|
|
- const token = String(this.currentUser?.sessionToken || this.sessions.token() || '').trim();
|
|
|
- if (!token.startsWith('r:')) throw new Error('阅读学习账户需要新的登录会话,请退出后重新登录');
|
|
|
- const response = await firstValueFrom(this.http.post<CloudEnvelope<T>>(API_CONFIG.cloudFunctionUrl, {
|
|
|
- token,
|
|
|
- params: { action, ...params },
|
|
|
- }, {
|
|
|
- headers: new HttpHeaders({ 'X-Parse-Application-Id': API_CONFIG.parseAppId, 'Content-Type': 'application/json' }),
|
|
|
- }));
|
|
|
- if (response.retcode === -1) throw new Error(response.retmsg || '阅读云函数请求失败');
|
|
|
- return response.result;
|
|
|
- }
|
|
|
-
|
|
|
- private persistUser(user: NcloudUser): NcloudUser {
|
|
|
- this.currentUser = user;
|
|
|
- localStorage.setItem(USER_CACHE_KEY, JSON.stringify(user));
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- private readCachedUser(): NcloudUser | null {
|
|
|
- try {
|
|
|
- const user = JSON.parse(localStorage.getItem(USER_CACHE_KEY) || 'null') as NcloudUser | null;
|
|
|
- return user?.sessionToken ? user : null;
|
|
|
- } catch { return null; }
|
|
|
- }
|
|
|
-}
|