IncreaseBabyInfoViewModel.ets 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { BasicType, IBestToast, YTAvoid, yTRouter } from "basic";
  2. import { DiaLogPageEnum, DiaLogParam } from "basic/src/main/ets/models/YTDiaLogModel";
  3. import { data } from "@kit.TelephonyKit";
  4. import { BabyInfo } from "../model/Index";
  5. import { BabyFoodApi } from "../Apis/BabyFoodApi";
  6. import { Type } from "@ohos.arkui.StateManagement";
  7. @ObservedV2
  8. export class IncreaseBabyInfoViewModel{
  9. @Trace babyInfo: BabyInfo = new BabyInfo()
  10. @Trace safeTop: number = 0
  11. forEachData: Array<BasicType> = [
  12. {
  13. text: '生日',
  14. message: '请选择宝宝生日',
  15. id: 'forEach-BirthDay',
  16. date: 'birthDate',
  17. click: () => {
  18. this.selectBirthDay()
  19. },
  20. }, {
  21. text: '宝宝姓名',
  22. message: '请输入宝宝姓名',
  23. date: 'name',
  24. id: 'forEach-Name',
  25. click: () => {
  26. this.inputName()
  27. }
  28. }, {
  29. text: '性别',
  30. message: '请选择宝宝性别',
  31. date: 'gender',
  32. id: 'forEach-Gender',
  33. click: () => {
  34. this.selectGender()
  35. },
  36. }
  37. ]
  38. constructor() {
  39. this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
  40. }
  41. /**
  42. * 重写的返回逻辑
  43. * @returns
  44. */
  45. _onBackPressed(ans?: string){
  46. if(ans) yTRouter.pop(ans, true)
  47. else yTRouter.pop()
  48. return true;
  49. }
  50. // 填写完成
  51. async complete(){
  52. if(!this.babyInfo.birthday) {
  53. IBestToast.show('请选择宝宝生日')
  54. return
  55. }
  56. if(!this.babyInfo.name) {
  57. IBestToast.show('请填写宝宝姓名')
  58. return
  59. }
  60. if(!this.babyInfo.gender) {
  61. IBestToast.show('请选择宝宝性别')
  62. return
  63. }
  64. try {
  65. await BabyFoodApi.addBabyInfo(BabyInfo.clone(this.babyInfo))
  66. this._onBackPressed('success')
  67. } catch (error){
  68. }
  69. }
  70. // 跳过填写
  71. notFill(){
  72. // this._onBackPressed()
  73. }
  74. // 选择生日
  75. selectBirthDay(){
  76. const param: DiaLogParam = {
  77. pageEnum: DiaLogPageEnum.DatePicker,
  78. param: {
  79. date: this.babyInfo.birthday
  80. }
  81. }
  82. yTRouter.router2NaviDiaLog(param, (ans) => {
  83. let date = ans.result
  84. if(date){
  85. this.babyInfo.birthday = date as string
  86. }
  87. })
  88. }
  89. // 选择性别
  90. selectGender(){
  91. const param: DiaLogParam = {
  92. pageEnum: DiaLogPageEnum.SelectGender,
  93. param: {
  94. number: this.babyInfo.gender
  95. }
  96. }
  97. yTRouter.router2NaviDiaLog(param, (ans) => {
  98. let res = ans.result
  99. if(res){
  100. this.babyInfo.gender = res == '男' ? 1 : 2
  101. }
  102. })
  103. }
  104. // 输入宝宝姓名
  105. inputName(){
  106. const param: DiaLogParam = {
  107. pageEnum: DiaLogPageEnum.TextInput,
  108. param: {
  109. text: this.babyInfo.name,
  110. message: '请输入宝宝姓名'
  111. }
  112. }
  113. yTRouter.router2NaviDiaLog(param, (ans) => {
  114. let res = ans.result
  115. if(res){
  116. this.babyInfo.name = res as string
  117. }
  118. })
  119. }
  120. }