home.page.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { Component, OnInit } from '@angular/core';
  2. import { AlertController, NavController } from '@ionic/angular';
  3. import { Router } from '@angular/router';
  4. import * as Parse from 'parse';
  5. @Component({
  6. selector: 'app-home',
  7. templateUrl: './home.page.html',
  8. styleUrls: ['./home.page.scss'],
  9. })
  10. export class HomePage implements OnInit {
  11. initial: number = 0;
  12. current: number = 0;
  13. target: number = 0;
  14. reward: number = 0;
  15. username: string = ''; // 当前用户的用户名
  16. blueColor: boolean = false;
  17. taskCompleted: boolean = false; // 任务完成标志
  18. constructor(
  19. private alertController: AlertController,
  20. private router: Router,
  21. private navCtrl: NavController,
  22. ) {}
  23. ngOnInit() {
  24. this.loadUserData();
  25. }
  26. ionViewDidEnter() {
  27. this.loadUserData();
  28. }
  29. async loadUserData() {
  30. const currentUser = Parse.User.current();
  31. if (currentUser) {
  32. this.username = currentUser.getUsername()!;
  33. this.loadData();
  34. } else {
  35. this.username = '未登录';
  36. this.loadData();
  37. }
  38. console.log("tab1:" + this.username);
  39. }
  40. async loadData() {
  41. try {
  42. const query = new Parse.Query('weight_status');
  43. query.equalTo('username', this.username); // 查找当前用户的数据
  44. const weightStatus = await query.first();
  45. if (weightStatus) {
  46. this.initial = weightStatus.get('initial') || 0;
  47. this.current = weightStatus.get('current') || 0;
  48. this.target = weightStatus.get('target') || 0;
  49. this.reward = this.calculateReward(this.initial, this.target);
  50. // 检查任务是否完成
  51. if (this.current === this.target && this.initial !== this.target) {
  52. await this.completeTask(weightStatus);
  53. } else if (this.current === this.initial && this.current === this.target && this.current != 0 ) {
  54. this.taskCompleted = true; // 设置任务完成标志
  55. } else {
  56. this.taskCompleted = false; // 重置任务完成标志
  57. }
  58. } else {
  59. // 没有找到用户数据时,将所有体重数据重置为0
  60. this.initial = 0;
  61. this.current = 0;
  62. this.target = 0;
  63. this.reward = 0;
  64. }
  65. } catch (error) {
  66. console.error('Error loading data', error);
  67. // 出现错误时,将所有体重数据重置为0
  68. this.initial = 0;
  69. this.current = 0;
  70. this.target = 0;
  71. this.reward = 0;
  72. }
  73. }
  74. async completeTask(weightStatus: Parse.Object) {
  75. // 更新 weight_status 数据库中的初始体重和目标体重
  76. weightStatus.set('initial', this.current);
  77. weightStatus.set('target', this.current);
  78. await weightStatus.save();
  79. // 记录完成任务的赏金到 account_history 数据库
  80. const AccountHistory = Parse.Object.extend('account_history');
  81. const accountHistory = new AccountHistory();
  82. const historyQuery = new Parse.Query('account_history');
  83. historyQuery.equalTo('username', this.username);
  84. const latestHistory = await historyQuery.descending('historyId').first();
  85. const historyId = latestHistory ? latestHistory.get('historyId') + 1 : 1;
  86. const currentBounty = latestHistory ? latestHistory.get('account') : 1000;
  87. const rewardAmount = this.calculateReward(this.initial, this.target);
  88. accountHistory.set('username', this.username);
  89. accountHistory.set('amount', rewardAmount);
  90. accountHistory.set('type', 'income');
  91. accountHistory.set('remark', '完成任务');
  92. accountHistory.set('account', currentBounty + rewardAmount);
  93. accountHistory.set('historyId', historyId);
  94. accountHistory.set('expenseRecord', latestHistory ? latestHistory.get('expenseRecord') : 0); // 保留现有的支出记录
  95. await accountHistory.save();
  96. await this.loadUserData();
  97. // 显示任务完成消息
  98. this.taskCompleted = true;
  99. }
  100. calculateReward(initial: number, target: number): number {
  101. return (initial - target) * 10;
  102. }
  103. async openEditModal() {
  104. if (this.username == '未登录') {
  105. // 如果用户未登录,则显示登录提示
  106. this.presentLoginAlert();
  107. return;
  108. }
  109. const alert = await this.alertController.create({
  110. header: '编辑体重信息',
  111. subHeader: '请根据您的需求编辑以下信息',
  112. inputs: [
  113. {
  114. name: 'initial',
  115. type: 'number',
  116. placeholder: '初始',
  117. value: this.initial.toString()
  118. },
  119. {
  120. name: 'current',
  121. type: 'number',
  122. placeholder: '当前',
  123. value: this.current.toString()
  124. },
  125. {
  126. name: 'target',
  127. type: 'number',
  128. placeholder: '目标',
  129. value: this.target.toString()
  130. },
  131. {
  132. name: 'reward',
  133. type: 'number',
  134. placeholder: '赏金',
  135. value: this.reward.toString(),
  136. disabled: true // 禁用输入框,显示但不能编辑
  137. }
  138. ],
  139. buttons: [
  140. {
  141. text: '取消',
  142. role: 'cancel'
  143. },
  144. {
  145. text: '保存',
  146. handler: async (data) => {
  147. try {
  148. const query = new Parse.Query('weight_status');
  149. query.equalTo('username', this.username); // 查找当前用户的数据
  150. let weightStatus = await query.first();
  151. if (!weightStatus) {
  152. weightStatus = new Parse.Object('weight_status');
  153. }
  154. const rewardValue = this.calculateReward(Number(data.initial), Number(data.target));
  155. weightStatus.set('username', this.username);
  156. weightStatus.set('initial', Number(data.initial));
  157. weightStatus.set('current', Number(data.current));
  158. weightStatus.set('target', Number(data.target));
  159. weightStatus.set('reward', rewardValue);
  160. await weightStatus.save();
  161. this.loadData(); // 更新本地数据
  162. } catch (error) {
  163. console.error('Error updating data', error);
  164. }
  165. }
  166. }
  167. ]
  168. });
  169. await alert.present();
  170. }
  171. async presentLoginAlert() {
  172. const alert = await this.alertController.create({
  173. header: '未登录',
  174. message: '请先登录以继续操作',
  175. buttons: [
  176. {
  177. text: '取消',
  178. role: 'cancel'
  179. },
  180. {
  181. text: '去登录',
  182. handler: () => {
  183. this.navCtrl.navigateForward('/user/login');
  184. }
  185. }
  186. ]
  187. });
  188. await alert.present();
  189. }
  190. toggleButtonColor(habit: string) {
  191. this.blueColor = !this.blueColor;
  192. }
  193. //每日习惯按钮颜色变化
  194. buttons = [
  195. { text: '足量饮水', color: 'light', icon: 'water' },
  196. { text: '干净饮食', color: 'light', icon: 'nutrition' },
  197. { text: '健康作息', color: 'light', icon: 'bed' },
  198. { text: '有氧', color: 'light', icon: 'bicycle' },
  199. { text: '力量', color: 'light', icon: 'barbell' },
  200. { text: '拉伸', color: 'light', icon: 'body' }
  201. ];
  202. habitText = '今日习惯待保持';
  203. onButtonClick(index: number) {
  204. const button = this.buttons[index];
  205. switch(button.color) {
  206. case 'light':
  207. button.color = 'success';
  208. break;
  209. case 'success':
  210. button.color = 'danger';
  211. break;
  212. case 'danger':
  213. button.color = 'light';
  214. break;
  215. }
  216. this.checkHabitText();
  217. }
  218. checkHabitText() {
  219. if (this.buttons.every(btn => btn.color === 'success')) {
  220. this.habitText = '你成功保持了良好的习惯';
  221. } else if (this.buttons.some(btn => btn.color === 'danger')) {
  222. this.habitText = '今日好习惯保持失败,再接再厉';
  223. } else {
  224. this.habitText = '今日习惯待保持';
  225. }
  226. }
  227. get allButtonsSuccess() {
  228. return this.buttons.every(btn => btn.color === 'success');
  229. }
  230. get anyButtonFailure() {
  231. return this.buttons.some(btn => btn.color === 'danger');
  232. }
  233. navigateToTreePage() {
  234. this.router.navigate(['/tabs/timer']);
  235. }
  236. }