page-mine.component.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import {Component, ElementRef, ViewChild} from '@angular/core';
  2. import {ToastController} from '@ionic/angular';
  3. import {UserService} from 'src/modules/user/service-user/user.service';
  4. // import {FmodeChatCompletion} from "fmode-ng";
  5. import {
  6. AbstractControl, AsyncValidatorFn,
  7. FormBuilder,
  8. FormControl,
  9. FormGroup,
  10. NonNullableFormBuilder, ValidationErrors,
  11. ValidatorFn,
  12. Validators
  13. } from "@angular/forms";
  14. import Parse from 'parse';
  15. import {AlertController} from "@ionic/angular";
  16. import {Router} from "@angular/router";
  17. import {AuthService} from "../serve-auth/auth.service";
  18. (Parse as any).serverURL = "https://web2023.fmode.cn/parse";
  19. // https://web2023.fmode.cn/s0210490/api/user/login
  20. Parse.initialize("dev")
  21. @Component({
  22. selector: 'app-page-mine',
  23. templateUrl: './page-mine.component.html',
  24. styleUrls: ['./page-mine.component.scss']
  25. })
  26. export class PageMineComponent {
  27. account = this.userServ.currentUser.username;
  28. password = this.userServ.currentUser.password;
  29. isFirstRegister = this.userServ.currentUser.isFirstRegister;
  30. private readonly result: Promise<Parse.Object<Parse.Attributes>> | undefined;
  31. selectedTabIndex: number = 0; // 默认选中第一个标签页
  32. currentUser = Parse.User.current();
  33. constructor(private authService: AuthService, private fb: FormBuilder, private toastCtrl: ToastController, public userServ: UserService, private alertCtrl: AlertController, private router: Router) {
  34. this.myForm = this.fb.group({
  35. phoneNumber: new FormControl('', [Validators.required, Validators.pattern(/^\d{10}$/)]),
  36. name: new FormControl('', [Validators.required]),
  37. employmentStatus: new FormControl('', Validators.required),
  38. educationLevel: new FormControl('', Validators.required),
  39. gender: new FormControl('', Validators.required),
  40. });
  41. this.validateForm = this.fb.group({
  42. oldPassword: '',
  43. // ['', [Validators.required, Validators.pattern('^(?=.*[a-zA-Z])(?=.*[0-9]).{6,10}$')]],
  44. newPassword: ['', [Validators.required, Validators.pattern('^(?=.*[a-zA-Z])(?=.*[0-9]).{6,10}$')]],
  45. removePassword: '',
  46. });
  47. if (this.isFirstRegister === 1) {
  48. this.selectedTabIndex = this.getTabIndexFromUrl();
  49. } else {
  50. }
  51. console.log(this.currentUser)
  52. console.log(this.myForm.valid)
  53. }
  54. ngOnInit() {
  55. console.log(typeof this.account,)
  56. console.log(this.account)
  57. this.authService.parseCurrentNew(this.account)
  58. }
  59. validateForm: FormGroup;
  60. myForm: FormGroup;
  61. // 完善信息
  62. async submitForm() {
  63. if (this.myForm.valid) {
  64. const params = {
  65. phoneNumber: this?.myForm.controls['phoneNumber'].value,
  66. employmentStatus: this?.myForm.controls['employmentStatus'].value,
  67. educationLevel: this?.myForm.controls['educationLevel'].value,
  68. gender: this?.myForm.controls['gender'].value,
  69. name: this?.myForm.controls['name'].value,
  70. };
  71. const user: any = Parse.User.current();
  72. user.set("phoneNumber", params.phoneNumber);
  73. user.set("employmentStatus", params.employmentStatus)
  74. user.set("educationLevel", params.educationLevel)
  75. user.set("gender", params.gender)
  76. user.set("name", params.name);
  77. user.set("isFirstRegister", 2)
  78. user.save().then((updatedUser: any) => {
  79. console.log("---", updatedUser)
  80. }).catch((error: any) => {
  81. console.log("---", error)
  82. })
  83. }
  84. this.current += 1;
  85. }
  86. async logout() {
  87. localStorage.removeItem("USER_AUTH")
  88. let toast = await this.toastCtrl.create({
  89. message: "登出成功",
  90. position: "top",
  91. duration: 500
  92. })
  93. await toast.present();
  94. }
  95. //按钮弹出框组件
  96. forgetPassword = false;
  97. changePassword = false;
  98. removePassword = false;
  99. actionTypeMap: {
  100. [key: string]: boolean
  101. }
  102. = {
  103. changePassword: false,
  104. forgetPassword: false,
  105. removePassword: false,
  106. }
  107. actionChange(key: string
  108. ):
  109. void {
  110. console.log(this.actionTypeMap[key])
  111. this.actionTypeMap[key] = true
  112. }
  113. inActionChange(key: string):
  114. void {
  115. this.actionTypeMap[key] = false;
  116. }
  117. //数据获取逻辑
  118. async alertError(err: string | undefined) {
  119. const alert = await this.alertCtrl.create({
  120. header: '密码修改失败',
  121. subHeader: '请检查新旧密码是否正确',
  122. message: err,
  123. buttons: ['好的'],
  124. });
  125. await alert.present();
  126. }
  127. async searchObject(): Promise<boolean> {
  128. if (this.result) {
  129. if (this.password === this.validateForm.controls['oldPassword'].value && this.validateForm.controls['newPassword'].valid && this.validateForm.controls['oldPassword'].value != this.validateForm.controls['newPassword'].value) {
  130. (await this.result).set('password', this.validateForm.controls['newPassword'].value);
  131. // 保存修改后的密码
  132. await this.logout();
  133. await (await this.result).save();
  134. return true;
  135. } else {
  136. await this.alertError("密码错误获取新旧密码相同")
  137. return false;
  138. }
  139. } else {
  140. return false;
  141. }
  142. }
  143. async removeSearch() {
  144. if (this.result) {
  145. if (this.password == this.validateForm.controls['removePassword'].value) {
  146. (await this.result).destroy().then(() => {
  147. this.logout();
  148. return true;
  149. }).catch(() => {
  150. this.alertError("数据删除失败,密码错误")
  151. return false;
  152. });
  153. }
  154. }
  155. return true;
  156. }
  157. //显示不显密码
  158. passwordVisible = false;
  159. async handleChange(key: string):
  160. Promise<void> {
  161. //修改密码逻辑
  162. if (await this.searchObject()
  163. ) {
  164. this.actionTypeMap[key] = false;
  165. } else {
  166. this.alertError("密码错误")
  167. }
  168. }
  169. async handleRemove(key: string): Promise<void> {
  170. //删除密码逻辑
  171. if (await this.removeSearch()) {
  172. this.actionTypeMap[key] = false;
  173. }
  174. }
  175. handleForget(): void {
  176. //忘记密码逻辑
  177. this.forgetPassword = false;
  178. }
  179. //跳转到设置的方法
  180. getTabIndexFromUrl(): number {
  181. const fragment = this.router.parseUrl(this.router.url).fragment;
  182. console.log(fragment)
  183. if (fragment === 'setting') {
  184. return 2
  185. }
  186. return 0;
  187. }
  188. //设置页面跳转next-preview的逻辑
  189. current = 0;
  190. index = 'First-content';
  191. pre(): void {
  192. this.current -= 1;
  193. // this.changeContent();
  194. }
  195. next(): void {
  196. this.current += 1;
  197. // this.changeContent();
  198. }
  199. done(): void {
  200. this.router.navigate(['home/minterviews/0'])
  201. }
  202. // 聊天框逻辑
  203. messages = [
  204. {avatar: 'assets/images/page-mine/myAvatar.png', content: 'Hello', isReceived: false},
  205. // 这是左边的内容
  206. {avatar: 'assets/images/page-mine/myAvatar.png', content: 'Hi', isReceived: true},
  207. // 这是右边的内容
  208. ];
  209. inputMessages: any[] = [];
  210. inputText = '';
  211. @ViewChild('chatMessages') chatMessages!: ElementRef;
  212. newMessage: string = ""
  213. sendMessage() {
  214. if (this.inputText.trim() !== '') {
  215. const newMessage = {
  216. avatar: 'assets/images/page-mine/myAvatar.png',
  217. content: this.inputText,
  218. isReceived: true,
  219. };
  220. let messageList = [
  221. {role: "user", content: "填写你发送过的历史消息:可用于Prompt定义AI扮演的角色细节"},
  222. {role: "assistant", content: "填写AI回复的历史消息"},
  223. {role: "user", content: "FmodeAiTest测试问题"}, // 填写你要发送的消息
  224. ]
  225. this.messages.push(newMessage);
  226. this.inputText = '';
  227. // let completion = new FmodeChatCompletion(messageList);
  228. // completion.sendCompletion().subscribe((message: any) => {
  229. // this.newMessage = message;
  230. // console.log(newMessage)
  231. // console.log(this.messages)
  232. // })
  233. // Scroll to the bottom of the chat window
  234. setTimeout(() => {
  235. this.chatMessages.nativeElement.scrollTop = this.chatMessages.nativeElement.scrollHeight;
  236. }, 0);
  237. }
  238. }
  239. }