Chat.service.ts 847 B

12345678910111213141516171819202122232425
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. @Injectable({
  4. providedIn: 'root'
  5. })
  6. export class ChatService {
  7. private apiUrl = 'https://web2023.fmode.cn/parse'; // Parse后端API地址
  8. constructor(private http: HttpClient) {}
  9. // 获取聊天记录方法
  10. async getChatHistory(userId: string): Promise<any> {
  11. const url = `${this.apiUrl}/classes/Gzpmessage`; // Message为存储聊天记录的Parse类名
  12. const query = { senderId: userId }; // 假设使用senderId字段来表示发送者ID
  13. const headers = { 'X-Parse-Application-Id': 'dev' }; // Parse应用ID
  14. try {
  15. const response = await this.http.get(url, { headers, params: query }).toPromise();
  16. console.log(response)
  17. } catch (error) {
  18. throw new Error('Failed to get chat history.');
  19. }
  20. }
  21. }