tab4.page.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { Component } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { AlertController } from '@ionic/angular';
  4. import { CloudUser } from 'src/lib/ncloud'; // 确保路径正确
  5. @Component({
  6. selector: 'app-tab4',
  7. templateUrl: './tab4.page.html',
  8. styleUrls: ['./tab4.page.scss'],
  9. standalone: false,
  10. })
  11. export class Tab4Page {
  12. isLoggedIn = false; // 初始状态为未登录
  13. user = {
  14. name: '未登录',
  15. avatar: 'assets/icon/default-avatar.png',
  16. type: '',
  17. fields: 0,
  18. solved: 0,
  19. exp: 0
  20. };
  21. private cloudUser = new CloudUser(); // 创建CloudUser实例
  22. constructor(
  23. private router: Router,
  24. private alertCtrl: AlertController
  25. ) {
  26. this.checkLoginStatus(); // 初始化时检查登录状态
  27. }
  28. // 检查登录状态
  29. private async checkLoginStatus() {
  30. const currentUser = await this.cloudUser.current();
  31. if (currentUser) {
  32. this.isLoggedIn = true;
  33. this.updateUserData(currentUser);
  34. }
  35. }
  36. // 更新用户数据
  37. private updateUserData(userData: any) {
  38. // 确保username存在,如果不存在则显示"未命名用户"
  39. const username = userData.data.username || '未命名用户';
  40. this.user = {
  41. name: username, // 直接使用用户名,不使用默认值"爱睡觉的"
  42. avatar: userData.data.avatar || 'assets/icon/farmer-avatar.png',
  43. type: userData.data.type || 'farmer',
  44. fields: userData.data.fields || 0,
  45. solved: userData.data.solved || 0,
  46. exp: userData.data.exp || 0
  47. };
  48. }
  49. // 导航到设置页面
  50. goToSettings() {
  51. this.router.navigate(['/settings']);
  52. }
  53. // 导航到登录页面
  54. async login() {
  55. const alert = await this.alertCtrl.create({
  56. header: '登录',
  57. inputs: [
  58. {
  59. name: 'username',
  60. type: 'text',
  61. placeholder: '用户名'
  62. },
  63. {
  64. name: 'password',
  65. type: 'password',
  66. placeholder: '密码'
  67. }
  68. ],
  69. buttons: [
  70. {
  71. text: '取消',
  72. role: 'cancel'
  73. },
  74. {
  75. text: '登录',
  76. handler: async (data) => {
  77. if (!data.username || !data.password) {
  78. this.showAlert('错误', '用户名和密码不能为空');
  79. return false;
  80. }
  81. const loadingAlert = await this.alertCtrl.create({
  82. message: '登录中...'
  83. });
  84. await loadingAlert.present();
  85. try {
  86. const user = await this.cloudUser.login(data.username, data.password);
  87. loadingAlert.dismiss();
  88. if (user) {
  89. this.isLoggedIn = true;
  90. this.updateUserData(user);
  91. this.showAlert('成功', '登录成功');
  92. return true;
  93. } else {
  94. this.showAlert('错误', '用户名或密码错误');
  95. return false;
  96. }
  97. } catch (error) {
  98. loadingAlert.dismiss();
  99. this.showAlert('错误', '登录失败,请稍后重试');
  100. return false;
  101. }
  102. }
  103. }
  104. ]
  105. });
  106. await alert.present();
  107. }
  108. // 导航到注册页面
  109. async signUp() {
  110. const alert = await this.alertCtrl.create({
  111. header: '注册',
  112. inputs: [
  113. {
  114. name: 'username',
  115. type: 'text',
  116. placeholder: '用户名'
  117. },
  118. {
  119. name: 'password',
  120. type: 'password',
  121. placeholder: '密码'
  122. },
  123. {
  124. name: 'confirmPassword',
  125. type: 'password',
  126. placeholder: '确认密码'
  127. }
  128. ],
  129. buttons: [
  130. {
  131. text: '取消',
  132. role: 'cancel'
  133. },
  134. {
  135. text: '注册',
  136. handler: async (data) => {
  137. if (!data.username || !data.password || !data.confirmPassword) {
  138. this.showAlert('错误', '请填写完整信息');
  139. return false;
  140. }
  141. if (data.password !== data.confirmPassword) {
  142. this.showAlert('错误', '两次输入的密码不一致');
  143. return false;
  144. }
  145. const loadingAlert = await this.alertCtrl.create({
  146. message: '注册中...'
  147. });
  148. await loadingAlert.present();
  149. try {
  150. const user = await this.cloudUser.signUp(data.username, data.password);
  151. loadingAlert.dismiss();
  152. if (user) {
  153. this.isLoggedIn = true;
  154. this.updateUserData({
  155. data: {
  156. ...user.data,
  157. username: data.username // 明确传递用户名
  158. }
  159. });
  160. this.showAlert('成功', '注册成功');
  161. return true;
  162. } else {
  163. this.showAlert('错误', '注册失败,用户名可能已被占用');
  164. return false;
  165. }
  166. } catch (error) {
  167. loadingAlert.dismiss();
  168. this.showAlert('错误', '注册失败,请稍后重试');
  169. return false;
  170. }
  171. }
  172. }
  173. ]
  174. });
  175. await alert.present();
  176. }
  177. // 导航到农田管理
  178. goToFields() {
  179. if (!this.isLoggedIn) {
  180. this.showLoginPrompt();
  181. return;
  182. }
  183. this.router.navigate(['/my-fields']);
  184. }
  185. // 导航到我的提问
  186. goToQuestions() {
  187. if (!this.isLoggedIn) {
  188. this.showLoginPrompt();
  189. return;
  190. }
  191. this.router.navigate(['/my-questions']);
  192. }
  193. // 导航到收藏
  194. goToFavorites() {
  195. if (!this.isLoggedIn) {
  196. this.showLoginPrompt();
  197. return;
  198. }
  199. this.router.navigate(['/favorites']);
  200. }
  201. // 导航到反馈
  202. goToFeedback() {
  203. this.router.navigate(['/feedback']);
  204. }
  205. // 导航到关于我们
  206. goToAbout() {
  207. this.router.navigate(['/about']);
  208. }
  209. // 退出登录
  210. async logout() {
  211. const alert = await this.alertCtrl.create({
  212. header: '确认退出',
  213. message: '确定要退出当前账号吗?',
  214. buttons: [
  215. {
  216. text: '取消',
  217. role: 'cancel'
  218. },
  219. {
  220. text: '退出',
  221. handler: async () => {
  222. const success = await this.cloudUser.logout();
  223. if (success) {
  224. this.isLoggedIn = false;
  225. this.user = {
  226. name: '未登录',
  227. avatar: 'assets/icon/default-avatar.png',
  228. type: '',
  229. fields: 0,
  230. solved: 0,
  231. exp: 0
  232. };
  233. this.showAlert('成功', '已退出登录');
  234. } else {
  235. this.showAlert('错误', '退出登录失败');
  236. }
  237. }
  238. }
  239. ]
  240. });
  241. await alert.present();
  242. }
  243. // 显示提示登录的对话框
  244. private async showLoginPrompt() {
  245. const alert = await this.alertCtrl.create({
  246. header: '需要登录',
  247. message: '此功能需要登录后才能使用',
  248. buttons: [
  249. {
  250. text: '取消',
  251. role: 'cancel'
  252. },
  253. {
  254. text: '去登录',
  255. handler: () => {
  256. this.login();
  257. }
  258. }
  259. ]
  260. });
  261. await alert.present();
  262. }
  263. // 显示通用提示框
  264. private async showAlert(header: string, message: string) {
  265. const alert = await this.alertCtrl.create({
  266. header,
  267. message,
  268. buttons: ['确定']
  269. });
  270. await alert.present();
  271. }
  272. }