setting.component.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { Component, OnInit, ViewChild } from '@angular/core';
  2. import {
  3. AlertController,
  4. LoadingController,
  5. ToastController,
  6. } from '@ionic/angular';
  7. import { NavComponent } from '../../../app/components/nav/nav.component';
  8. import * as Parse from 'parse';
  9. import { Router } from '@angular/router';
  10. import { UploadComponent } from '../../../app/components/upload/upload.component';
  11. import { AuthService } from '../../../services/auth.service';
  12. import { ionicStandaloneModules } from '../../ionic-standalone.modules';
  13. import { province } from '../../../services/address';
  14. @Component({
  15. selector: 'app-setting',
  16. templateUrl: './setting.component.html',
  17. styleUrls: ['./setting.component.scss'],
  18. standalone: true,
  19. imports: [...ionicStandaloneModules, NavComponent, UploadComponent],
  20. })
  21. export class SettingComponent implements OnInit {
  22. @ViewChild('upload') upload!: UploadComponent;
  23. profile?: Parse.Object; //身份信息
  24. formData: any = {
  25. avatar: '',
  26. name: '',
  27. nickname: '',
  28. sex: '',
  29. age: '',
  30. remark: '',
  31. };
  32. mobile: string = '';
  33. loading: any;
  34. isOpen: boolean = false;
  35. address: string = '';
  36. provinceColumns = province.map((item) => item.provinceName);
  37. cityColumns = province[0].citys.map((item) => item.cityName);
  38. province: string = ''; //省份
  39. city: string = ''; //市
  40. constructor(
  41. private alertController: AlertController,
  42. public loadingCtrl: LoadingController,
  43. public toastController: ToastController,
  44. private authServ: AuthService,
  45. private router: Router
  46. ) {}
  47. ngOnInit() {
  48. this.getProfile();
  49. }
  50. // 获取用户信息
  51. async getProfile() {
  52. this.loading = await this.loadingCtrl.create({
  53. message: '加载中',
  54. });
  55. this.loading.present();
  56. let user = Parse.User.current();
  57. let query = new Parse.Query('Profile');
  58. query.equalTo('user', user?.id);
  59. query.notEqualTo('isDeleted', true);
  60. this.profile = await query.first();
  61. this.formData.avatar =
  62. user?.get('avatar') ||
  63. 'https://file-cloud.fmode.cn/DXNgcD6zo6/20221202/j6p8kb034039.png';
  64. this.formData.nickname = user?.get('nickname') || '';
  65. this.formData.sex = user?.get('sex') || '';
  66. this.formData.age = user?.get('age') || '';
  67. this.mobile = user?.get('mobile');
  68. this.formData.name = this.profile?.get('name') || '';
  69. this.formData.age = this.profile?.get('birthdate') || '';
  70. this.formData.remark = this.profile?.get('remark') || '';
  71. this.loading.dismiss();
  72. if(user?.get('city') && user?.get('province')){
  73. this.address = user?.get('province') +'-'+user?.get('city');
  74. }
  75. this.province = user?.get('province') || this.provinceColumns[0]
  76. this.city = user?.get('city') || this.cityColumns[0]
  77. }
  78. onChange(e: any, type: string) {
  79. this.formData[type] = e.detail.value;
  80. }
  81. async saveEdit(e: any) {
  82. this.loading = await this.loadingCtrl.create({
  83. message: '正在保存修改',
  84. });
  85. this.loading.present();
  86. console.log(e);
  87. console.log(this.formData);
  88. let urls = e?.map((item: any) => item.url);
  89. let user = Parse.User.current();
  90. if (!user?.id) {
  91. this.loading.dismiss();
  92. const alert = await this.alertController.create({
  93. header: '提示',
  94. message: '登录信息已过期,请重新登录',
  95. buttons: [
  96. {
  97. text: '好的',
  98. role: 'confirm',
  99. handler: () => {
  100. this.authServ.logout();
  101. },
  102. },
  103. ],
  104. });
  105. await alert.present();
  106. return;
  107. }
  108. user.set('avatar', urls[0]);
  109. user.set('nickname', this.formData.nickname);
  110. user.set('sex', this.formData.sex);
  111. // user.set('age', Number(this.formData.age));
  112. user.set('address', this.address);
  113. user.set('city', this.city);
  114. user.set('province', this.province);
  115. await user.save();
  116. if (!this.profile?.id) {
  117. let obj = Parse.Object.extend('Profile');
  118. this.profile = new obj();
  119. this.profile?.set('user', Parse.User.current()?.toPointer());
  120. this.profile?.set('company', {
  121. __type: 'Pointer',
  122. className: 'Company',
  123. objectId: this.authServ.company,
  124. });
  125. }
  126. this.profile?.set('mobile', Parse.User.current()?.get('mobile'));
  127. this.profile?.set('sex', this.formData.sex);
  128. this.profile?.set('birthdate', this.formData.age);
  129. this.profile?.set('remark', this.formData.remark);
  130. this.profile?.set('address', this.address);
  131. await this.profile?.save();
  132. this.loading.dismiss();
  133. this.getProfile();
  134. this.presentAlert('修改成功');
  135. }
  136. async presentToast(title: string, time: number, color: string) {
  137. const toast = await this.toastController.create({
  138. message: title,
  139. duration: time,
  140. color: color,
  141. });
  142. toast.present();
  143. }
  144. async presentAlert(msg: string) {
  145. const alert = await this.alertController.create({
  146. header: '提示',
  147. message: msg,
  148. buttons: [
  149. {
  150. text: '好的',
  151. role: 'confirm',
  152. handler: () => {},
  153. },
  154. ],
  155. });
  156. await alert.present();
  157. }
  158. cancel(type: string) {
  159. if (type === 'cancel') {
  160. this.province = this.address.split('-')[0];
  161. this.city = this.address.split('-')[1];
  162. } else {
  163. this.address = this.province + '-' + this.city;
  164. }
  165. this.isOpen = false;
  166. console.log(this.address);
  167. }
  168. onIonChange(event: CustomEvent, type: string) {
  169. let val = event.detail.value;
  170. switch (type) {
  171. case 'province':
  172. this.province = event.detail.value;
  173. this.cityColumns = province
  174. .find((item) => item.provinceName === val)
  175. ?.citys.map((item) => item.cityName)!;
  176. console.log(this.cityColumns);
  177. this.city = this.cityColumns[0];
  178. break;
  179. case 'city':
  180. this.city = event.detail.value;
  181. break;
  182. }
  183. console.log(this.province, this.city);
  184. }
  185. }