contact-list.page.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Component, OnInit } from '@angular/core';
  2. import * as Parse from "parse";
  3. @Component({
  4. selector: 'app-contact-list',
  5. templateUrl: './contact-list.page.html',
  6. styleUrls: ['./contact-list.page.scss'],
  7. })
  8. export class ContactListPage implements OnInit {
  9. searchName: string = '';
  10. contactList: Array<Parse.Object> = [];
  11. segment:string = "me";
  12. ngOnInit() {
  13. this.loadContact();
  14. }
  15. usernameInput:string = ""
  16. async checkIfExists(toUser:Parse.Object){
  17. let query = new Parse.Query("Contact");
  18. query.equalTo("from",Parse.User.current()?.toPointer())
  19. query.equalTo("to",toUser.toPointer())
  20. return await query.first();
  21. }
  22. async addContact(){
  23. if(this.usernameInput){
  24. let query = new Parse.Query("_User");
  25. query.equalTo("username",this.usernameInput);
  26. let user = await query.first()
  27. if(user?.id){
  28. let exists = await this.checkIfExists(user)
  29. if(exists?.id) return
  30. let Contact = Parse.Object.extend("Contact");
  31. let contact = new Contact()
  32. contact.set("from",Parse.User.current()?.toPointer())
  33. contact.set("to",user.toPointer())
  34. await contact.save();
  35. this.loadContact();
  36. }else{
  37. // 提示找不到用户
  38. }
  39. }
  40. }
  41. charGroupIndex:any = {}
  42. loadContact() {
  43. const Contact = Parse.Object.extend('Contact');
  44. const query = new Parse.Query(Contact);
  45. query.include("to")
  46. query.ascending('firstChar');
  47. if(this.segment=="me"){
  48. if(!Parse.User.current()?.id) return
  49. query.equalTo("from",Parse.User.current()?.id)
  50. }
  51. if (this.searchName) {
  52. query.contains('name', this.searchName);
  53. }
  54. query.find().then((results) => {
  55. this.contactList = results;
  56. this.contactList.forEach((contact,index)=>{
  57. if(this.charGroupIndex[contact.get("firstChar")] == undefined){
  58. this.charGroupIndex[contact.get("firstChar")] = index
  59. }
  60. })
  61. }, (error) => {
  62. console.error('Error while fetching contacts', error);
  63. });
  64. }
  65. search() {
  66. this.loadContact();
  67. }
  68. isOn:boolean = false;
  69. clicked(){
  70. this.isOn = !this.isOn;
  71. }
  72. get clickMsg(){
  73. return `The light is ${this.isOn ? 'On' : 'Off'}`;
  74. }
  75. }