cloud.evaluate.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { CloudObject, CloudUser } from './ncloud';
  2. // CloudEvaluate.ts
  3. export class CloudEvaluate extends CloudObject {
  4. userData: Record<string, any>;
  5. currentUser: CloudUser; // 添加当前用户的引用
  6. modalCtrl: any; // 假设 modalCtrl 是通过构造函数传入的
  7. constructor(userData: Record<string, any>, currentUser: CloudUser,modalCtrl: any) {
  8. super("_ChatEvaluate");
  9. this.userData = userData; // 保存用户评价数据
  10. this.currentUser = currentUser; // 保存当前用户
  11. this.modalCtrl = modalCtrl; // 保存模态控制器
  12. }
  13. /** 保存评价 */
  14. override async save() {
  15. // 确保 rating 是数字类型
  16. if (this.userData['rating']) {
  17. this.userData['rating'] = Number(this.userData['rating']);
  18. }
  19. console.log('保存评价被调用'); // 调试日志
  20. // 调用后端 API 保存评价
  21. const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/ChatEvaluate`, {
  22. headers: {
  23. "x-parse-application-id": "dev",
  24. "Content-Type": "application/json"
  25. },
  26. body: JSON.stringify({
  27. content: this.userData['content'],
  28. rating: this.userData['rating'],
  29. avatar: this.currentUser.data?.['avatar'], // 假设用户头像存储在 data.avatar 中
  30. user: { __type: "Pointer", className: "_User", objectId: this.currentUser.id } // 添加用户指针
  31. }),
  32. method: "POST"
  33. });
  34. const result = await response.json();
  35. console.log('保存结果:', result); // 输出保存结果
  36. if (result?.error) {
  37. console.error(result?.error);
  38. console.error('评价保存失败');
  39. return null;
  40. }
  41. console.log('评价保存成功:', result);
  42. this.modalCtrl.dismiss(result, "confirm"); // 关闭模态框并传递结果
  43. return result; // 返回保存的结果
  44. }
  45. }