RecipeComp.ets 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { IBestToast, yTRouter } from "basic"
  2. import { DiaLogPageEnum, DiaLogParam, YTDiaLogModel } from "basic/src/main/ets/models/YTDiaLogModel"
  3. import { BabyInfo, Recipe } from "../model/Index"
  4. import { AppStorageV2 } from "@kit.ArkUI"
  5. import { routerUtils } from "../utils/RouterUtils"
  6. import { emitter } from "@kit.BasicServicesKit"
  7. import { EventConstant } from "../model/Constant"
  8. @ComponentV2
  9. export struct RecipeComp {
  10. @Require @Param recipe: Recipe[] = []
  11. // 当前的宝宝信息
  12. @Local babyInfo: BabyInfo = AppStorageV2.connect<BabyInfo>(BabyInfo, () => new BabyInfo())!
  13. // 展开食谱弹窗
  14. async expandRecipe(item: Recipe){
  15. item.ingredients = ['南瓜100g', '高铁面粉100g', '高铁面粉100g']
  16. item.steps = ['1. 南瓜去皮去籽,切成小块', '2. 上锅蒸15分钟至软烂', '3. 用勺子压成泥状', '4.米粉用温水调成糊状', '5.将南瓜泥与米粉混合均匀']
  17. item.description = '富含β-胡萝卜素和膳食纤维,\n' +
  18. '有助于宝宝视力发展和消化\n' +
  19. '系统健康'
  20. let param: DiaLogParam = {
  21. pageEnum: DiaLogPageEnum.RecipePopup,
  22. align: YTDiaLogModel.Center,
  23. param: {
  24. generics: item,
  25. }
  26. }
  27. yTRouter.router2NaviDiaLog(param, (res) => {
  28. setTimeout(() => {
  29. this.addToPlan(item)
  30. }, 200)
  31. })
  32. }
  33. // 打开 添加至食谱计划弹窗
  34. async addToPlan(item: Recipe){
  35. let param: DiaLogParam = {
  36. pageEnum: DiaLogPageEnum.AddToRecipe,
  37. align: YTDiaLogModel.Center,
  38. param: {
  39. generics: item,
  40. }
  41. }
  42. yTRouter.router2NaviDiaLog(param, (res) => {
  43. /**
  44. * 0-1 周一 - 早餐
  45. * 1-2 周二 - 午餐
  46. * 2-3 周三 - 晚餐
  47. */
  48. let ans = res.result as string
  49. if(!this.babyInfo.id) {
  50. IBestToast.show('请先添加宝宝信息')
  51. return routerUtils.router2IncreaseBabyInfo(() => {
  52. this.addToPlanEvent(item, ans)
  53. })
  54. }
  55. this.addToPlanEvent(item, ans)
  56. })
  57. }
  58. // 触发添加至计划 事件
  59. addToPlanEvent(item: Recipe, ans: string){
  60. emitter.emit(EventConstant.ADD_RECIPE_TO_WEEKLY_PLAN, { data: {
  61. recipe: item,
  62. week: ans.split('-')[0],
  63. meal: ans.split('-')[1]
  64. } })
  65. }
  66. build() {
  67. Column() {
  68. Grid(){
  69. ForEach(this.recipe, (item: Recipe, index) => {
  70. GridItem(){
  71. this.RecipeBuilder(item)
  72. }
  73. })
  74. }
  75. .rowsGap(12)
  76. .width("100%")
  77. .height("100%")
  78. .scrollBar(BarState.Off)
  79. .columnsTemplate("1fr 1fr")
  80. .padding({ bottom: 25 })
  81. }
  82. .width("100%")
  83. .height("100%")
  84. }
  85. @Builder RecipeBuilder(item: Recipe){
  86. Column({space: 10}){
  87. Image(item.imageUrl)
  88. .width('100%')
  89. .height(112)
  90. .borderRadius({topLeft: 8, topRight: 8})
  91. Column({space: 4}){
  92. Text(item.name)
  93. .fontSize(12)
  94. .fontWeight(400)
  95. Text(item.monthRange)
  96. .fontSize(10)
  97. .borderRadius(10)
  98. .fontColor('#F0A040')
  99. .backgroundColor('#F8F0E6')
  100. .padding({left: 10, right: 10, top: 5, bottom: 5})
  101. }
  102. .width("100%")
  103. .alignItems(HorizontalAlign.Start)
  104. .padding({left: 12, right: 10, bottom: 8, top: 8})
  105. }
  106. .width(154)
  107. .borderRadius(8)
  108. .backgroundColor(Color.White)
  109. .shadow({
  110. radius: 15, type: ShadowType.COLOR, color: 'rgba(0, 0, 0, 0.1)', offsetY: 4
  111. })
  112. .onClick(() => { this.expandRecipe(item) })
  113. }
  114. }