import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpResponseBase } from '@angular/common/http'; import { Store, select } from '@ngrx/store'; import { KstConfig } from '../config/chat.config'; import { Observable, throwError as _observableThrow, of as _observableOf } from 'rxjs'; import { mergeMap as _observableMergeMap, catchError as _observableCatch, finalize } from 'rxjs/operators'; import { processStatus } from './http-service-base'; import { LoginChat, ChannelChanged } from '../store/chat.actions'; import { ChatSession } from './chat-session'; import { ChatSocketClient } from '../socket/chat-socket'; import { ChatMessage } from '../store/entities'; import { MessageChanged } from '../store/chat.actions/actions'; /** * 授权服务 */ @Injectable() export class CharAuthorizeService { protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined; constructor( private http: HttpClient, private store: Store<{ chat: any }>, private session: ChatSession, private socket: ChatSocketClient) { } login(username: string, password: string, channelNameOrId: string) { const self = this; if (!self.session.authToken) { self.http.post(KstConfig.url + '/api/v1/login', { user: username, password, }).subscribe(data => { if (data['status'] === 'success') { // 登陆成功 const result = data['data']; self.store.dispatch(new LoginChat({ userId: result['userId'], accessToken: result['authToken'], me: result['me'] })); self.session.init(result['authToken'], result['userId'], result['me']); self.channelInfo(channelNameOrId); } }, (error) => { // console.log(error); }); } else { self.store.dispatch(new LoginChat({ userId: self.session.userid, accessToken: self.session.authToken, me: self.session.me })); self.channelInfo(channelNameOrId); } } register(username: string, password: string, email: string) { } /** * 获取频道/房间信息 * @param name */ channelInfo(name: string) { const self = this; const options_: any = { observe: 'response', responseType: 'blob', headers: new HttpHeaders({ 'X-Auth-Token': self.session.authToken, 'X-User-Id': self.session.userid, Accept: 'application/json' }) }; self.http.request('get', KstConfig.url + '/api/v1/channels.info?roomName=' + name, options_) .pipe(_observableMergeMap((response_: any) => { return processStatus(response_); })) .pipe(finalize(() => { self.getChannelHistoryMessages(name); })) .subscribe((data) => { if (data.success) { self.store.dispatch(new ChannelChanged({ name, id: data.channel['_id'] })); self.socket.subRoom(self.session.authToken, data.channel['_id']); } }); } /** * 获取历史消息 * @param name 房间名称 */ getChannelHistoryMessages(name: string) { const self = this; const options_: any = { observe: 'response', responseType: 'blob', headers: new HttpHeaders({ 'X-Auth-Token': self.session.authToken, 'X-User-Id': self.session.userid, Accept: 'application/json' }) }; self.http.request('get', KstConfig.url + '/api/v1/channels.messages?roomName=' + name, options_) .pipe(_observableMergeMap((response_: any) => { return processStatus(response_); })) .subscribe((data) => { if (data.success) { const msgs = data.messages; self.store.dispatch(new MessageChanged(msgs.reverse())); } }); } }