setting.component.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. import { CommonModule } from '@angular/common';
  15. import { AccountService } from '../../../services/account.service';
  16. @Component({
  17. selector: 'app-setting',
  18. templateUrl: './setting.component.html',
  19. styleUrls: ['./setting.component.scss'],
  20. standalone: true,
  21. imports: [
  22. ...ionicStandaloneModules,
  23. NavComponent,
  24. UploadComponent,
  25. CommonModule,
  26. ],
  27. })
  28. export class SettingComponent implements OnInit {
  29. @ViewChild('upload') upload!: UploadComponent;
  30. profile?: Parse.Object; //身份信息
  31. formData: any = {
  32. avatar: '',
  33. name: '',
  34. nickname: '',
  35. sex: '',
  36. age: '',
  37. remark: '',
  38. auditing:false
  39. };
  40. mobile: string = '';
  41. loading: any;
  42. isOpen: boolean = false;
  43. address: string = '';
  44. provinceColumns = province.map((item) => item.provinceName);
  45. cityColumns = province[0].citys.map((item) => item.cityName);
  46. province: string = ''; //省份
  47. city: string = ''; //市
  48. isOpenTag: boolean = false;
  49. tags: any[] = [
  50. { val: '篮球', check: false },
  51. { val: '音乐', check: false },
  52. { val: '90后', check: false },
  53. { val: '00后', check: false },
  54. { val: '可爱', check: false },
  55. { val: '旅行', check: false },
  56. { val: '摄影', check: false },
  57. { val: '电影', check: false },
  58. { val: '阅读', check: false },
  59. { val: '美食', check: false },
  60. { val: '健身', check: false },
  61. { val: '游戏', check: false },
  62. { val: '编程', check: false },
  63. { val: '绘画', check: false },
  64. { val: '写作', check: false },
  65. { val: '舞蹈', check: false },
  66. { val: '时尚', check: false },
  67. { val: '宠物', check: false },
  68. { val: '手工', check: false },
  69. { val: '咖啡', check: false },
  70. { val: '动漫', check: false },
  71. { val: '历史', check: false },
  72. { val: '科技', check: false },
  73. { val: '运动', check: false },
  74. { val: '瑜伽', check: false },
  75. { val: '文学', check: false },
  76. { val: '心理学', check: false },
  77. { val: '创业', check: false },
  78. { val: '投资', check: false },
  79. { val: '环保', check: false },
  80. { val: '公益', check: false },
  81. { val: '书法', check: false },
  82. { val: '棋类', check: false },
  83. { val: '骑行', check: false },
  84. { val: '游泳', check: false },
  85. { val: '登山', check: false },
  86. { val: '滑板', check: false },
  87. { val: '电竞', check: false },
  88. { val: '手办', check: false },
  89. { val: '汉服', check: false },
  90. { val: '古风', check: false },
  91. { val: '民谣', check: false },
  92. { val: '摇滚', check: false },
  93. { val: '街舞', check: false },
  94. { val: '爵士', check: false },
  95. { val: '古典乐', check: false },
  96. { val: '流行乐', check: false },
  97. { val: '户外', check: false },
  98. { val: '露营', check: false },
  99. ];
  100. get getTag(){
  101. return this.tags.filter(item=>item.check);
  102. }
  103. vip:any
  104. constructor(
  105. private alertController: AlertController,
  106. public loadingCtrl: LoadingController,
  107. public toastController: ToastController,
  108. private authServ: AuthService,
  109. private accServ: AccountService,
  110. // private router: Router
  111. ) {}
  112. ngOnInit() {
  113. this.getProfile();
  114. }
  115. // 获取用户信息
  116. async getProfile() {
  117. this.loading = await this.loadingCtrl.create({
  118. message: '加载中',
  119. });
  120. this.loading.present();
  121. let user = Parse.User.current();
  122. let query = new Parse.Query('Profile');
  123. query.equalTo('user', user?.id);
  124. query.notEqualTo('isDeleted', true);
  125. this.profile = await query.first();
  126. this.formData.avatar =
  127. user?.get('avatar') ||
  128. 'https://file-cloud.fmode.cn/DXNgcD6zo6/20221202/j6p8kb034039.png';
  129. this.formData.nickname = user?.get('nickname') || '';
  130. this.formData.sex = user?.get('sex') || '';
  131. this.formData.age = user?.get('age') || '';
  132. this.mobile = user?.get('mobile');
  133. this.formData.name = this.profile?.get('name') || '';
  134. this.formData.age = this.profile?.get('birthdate') || '';
  135. this.formData.remark = this.profile?.get('remark') || '';
  136. this.formData.auditing = this.profile?.get('auditing');
  137. this.loading.dismiss();
  138. if (user?.get('city') && user?.get('province')) {
  139. this.address = user?.get('province') + '-' + user?.get('city');
  140. }
  141. this.province = user?.get('province') || this.provinceColumns[0];
  142. this.city = user?.get('city') || this.cityColumns[0];
  143. this.tags.map(item=>{
  144. item.check = this.profile?.get('tags')?.includes(item.val) || false;
  145. })
  146. this.vip = this.accServ.userVip;
  147. }
  148. onChange(e: any, type: string) {
  149. this.formData[type] = e.detail.value;
  150. }
  151. async onChangeTag() {
  152. this.isOpenTag = true;
  153. }
  154. async saveEdit(e: any) {
  155. this.loading = await this.loadingCtrl.create({
  156. message: '正在保存修改',
  157. });
  158. this.loading.present();
  159. console.log(e);
  160. console.log(this.formData);
  161. let urls = e?.map((item: any) => item.url);
  162. let user = Parse.User.current();
  163. if (!user?.id) {
  164. this.loading.dismiss();
  165. const alert = await this.alertController.create({
  166. header: '提示',
  167. message: '登录信息已过期,请重新登录',
  168. buttons: [
  169. {
  170. text: '好的',
  171. role: 'confirm',
  172. handler: () => {
  173. this.authServ.logout();
  174. },
  175. },
  176. ],
  177. });
  178. await alert.present();
  179. return;
  180. }
  181. user.set('avatar', urls[0]);
  182. user.set('nickname', this.formData.nickname);
  183. user.set('sex', this.formData.sex);
  184. // user.set('age', Number(this.formData.age));
  185. user.set('address', this.address);
  186. user.set('city', this.city);
  187. user.set('province', this.province);
  188. await user.save();
  189. if (!this.profile?.id) {
  190. let obj = Parse.Object.extend('Profile');
  191. this.profile = new obj();
  192. this.profile?.set('user', Parse.User.current()?.toPointer());
  193. this.profile?.set('company', {
  194. __type: 'Pointer',
  195. className: 'Company',
  196. objectId: this.authServ.company,
  197. });
  198. }
  199. this.profile?.set('mobile', Parse.User.current()?.get('mobile'));
  200. this.profile?.set('sex', this.formData.sex);
  201. this.profile?.set('birthdate', this.formData.age);
  202. this.profile?.set('remark', this.formData.remark);
  203. this.profile?.set('auditing', this.formData.auditing);
  204. this.profile?.set('address', this.address);
  205. let t = this.tags.filter((x) => x.check)?.map(item=> item.val)
  206. this.profile?.set('tag', t);
  207. await this.profile?.save();
  208. this.loading.dismiss();
  209. this.getProfile();
  210. this.presentAlert('修改成功');
  211. }
  212. async presentToast(title: string, time: number, color: string) {
  213. const toast = await this.toastController.create({
  214. message: title,
  215. duration: time,
  216. color: color,
  217. });
  218. toast.present();
  219. }
  220. async presentAlert(msg: string) {
  221. const alert = await this.alertController.create({
  222. header: '提示',
  223. message: msg,
  224. buttons: [
  225. {
  226. text: '好的',
  227. role: 'confirm',
  228. handler: () => {},
  229. },
  230. ],
  231. });
  232. await alert.present();
  233. }
  234. cancel(type: string) {
  235. if (type === 'cancel') {
  236. this.province = this.address.split('-')[0];
  237. this.city = this.address.split('-')[1];
  238. } else {
  239. this.address = this.province + '-' + this.city;
  240. }
  241. this.isOpen = false;
  242. console.log(this.address);
  243. }
  244. onCheck(index: number) {
  245. let n = this.tags.filter((item) => item.check).length;
  246. if (!this.tags[index].check && n > 10) {
  247. this.presentToast('最多选择10个标签', 1500, 'warning');
  248. return;
  249. }
  250. this.tags[index].check = !this.tags[index].check;
  251. }
  252. onIonChange(event: CustomEvent, type: string) {
  253. let val = event.detail.value;
  254. switch (type) {
  255. case 'province':
  256. this.province = event.detail.value;
  257. this.cityColumns = province
  258. .find((item) => item.provinceName === val)
  259. ?.citys.map((item) => item.cityName)!;
  260. console.log(this.cityColumns);
  261. this.city = this.cityColumns[0];
  262. break;
  263. case 'city':
  264. this.city = event.detail.value;
  265. break;
  266. }
  267. console.log(this.province, this.city);
  268. }
  269. }