edit-info.page.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Component, OnInit } from '@angular/core';
  2. import { NavController } from '@ionic/angular';
  3. import Parse from "parse";
  4. @Component({
  5. selector: 'app-edit-info',
  6. templateUrl: './edit-info.page.html',
  7. styleUrls: ['./edit-info.page.scss'],
  8. })
  9. export class EditInfoPage implements OnInit {
  10. editedUser:any = {
  11. nickname:"",
  12. building:"",
  13. campus:"",
  14. gender:"",
  15. avatarUrl:""
  16. }; // Clone of user for editing
  17. editing = false;
  18. currentUser:Parse.User|undefined
  19. // navController: any;
  20. constructor(private navController: NavController) {}
  21. ngOnInit() {
  22. this.currentUser = Parse.User.current();
  23. if (this.currentUser) {
  24. // 修改uesrInfo赋值逻辑,仅加载被编辑的字段属性值
  25. let json = this.currentUser.toJSON();
  26. console.log(json)
  27. for (const key in this.editedUser) {
  28. this.editedUser[key] = json[key]
  29. }
  30. console.log(this.editedUser)
  31. }
  32. }
  33. onFileSelected(event: any) {
  34. const file: File = event.target.files[0];
  35. const reader = new FileReader();
  36. reader.onload = () => {
  37. this.editedUser.avatarUrl = reader.result as string;
  38. };
  39. reader.readAsDataURL(file);
  40. }
  41. startEditing() {
  42. this.editing = true;
  43. }
  44. save() {
  45. this.currentUser = Parse.User.current();
  46. if (this.currentUser) {
  47. console.log(this.editedUser);
  48. this.editedUser.birthday = this.editedUser.birthday || new Date();
  49. this.editedUser.birthday = new Date(this.editedUser.birthday);
  50. for (const key in this.editedUser) {
  51. if (this.editedUser.hasOwnProperty(key)) {
  52. this.currentUser.set(key, this.editedUser[key]);
  53. }
  54. }
  55. this.currentUser.save().then(() => {
  56. this.navController.back();
  57. }).catch((error) => {
  58. console.error('Error saving user data: ', error);
  59. });
  60. }
  61. console.log(this.editedUser);
  62. }
  63. cancelEditing() {
  64. this.navController.back();
  65. }
  66. }