MainViewModel.ets 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { DateFormat, IBestToast, userInfo, UserInfo, YTAvoid, YTDateUtil, yTRouter } from "basic"
  2. import { BabyFoodApi } from "../Apis/BabyFoodApi"
  3. import { routerUtils } from "../utils/RouterUtils"
  4. import { AppStorageV2 } from "@kit.ArkUI"
  5. import {
  6. AddRecipeEventData,
  7. BabyInfo, Cuisine, DayPlan, PurchaseList, WeeklyPlan, WeeklyPlanPurchaseList } from "../model/Index"
  8. import { emitter } from "@kit.BasicServicesKit"
  9. import { EventConstant } from "../model/Constant"
  10. @ObservedV2
  11. export class MainViewModel{
  12. @Trace safeTop: number = 0
  13. @Trace babyList: BabyInfo[] = []
  14. // 当前选择的宝宝信息
  15. @Trace selectedBabyIndex: number = -1
  16. // 当前的宝宝信息
  17. @Trace babyInfo: BabyInfo = AppStorageV2.connect<BabyInfo>(BabyInfo, () => new BabyInfo())!
  18. // 用户信息
  19. @Trace userInfo: UserInfo = AppStorageV2.connect<UserInfo>(UserInfo, 'UserInfo', () => userInfo)!
  20. // 菜品下拉菜单
  21. @Trace dishList: Cuisine[] = []
  22. // 本周的食谱计划
  23. @Trace allPlan: WeeklyPlan = {}
  24. // 本周的食谱详情
  25. @Trace planList?: WeeklyPlanPurchaseList
  26. // 本周的采购清单
  27. @Trace purchaseList?: PurchaseList[]
  28. // 一周
  29. week: string[] = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  30. // 钩子函数列表
  31. eventFunc: Array<() => void> = []
  32. constructor() {
  33. this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
  34. //
  35. emitter.on(EventConstant.ADD_RECIPE_TO_WEEKLY_PLAN, (ans) => {
  36. this.addRecipeEvent(ans.data as AddRecipeEventData)
  37. })
  38. }
  39. // 登录状态改变
  40. async LoginStatusChange(){
  41. this.userInfo.setUserInfoAndLogin(userInfo, false)
  42. if(this.userInfo.checkLogin()){
  43. await this.getBabyList()
  44. await this.getDishList()
  45. } else {
  46. this.clearDataSource(true)
  47. }
  48. }
  49. // 登录校验 - 宝宝信息校验
  50. checkLogin(f: () => void, isCheckBaby: boolean = false) {
  51. if(this.userInfo.checkLogin()) {
  52. if(this.babyInfo?.id || !isCheckBaby){
  53. f()
  54. } else {
  55. IBestToast.show('请先添加宝宝信息吧!')
  56. this.increaseBabyInfo()
  57. }
  58. } else {
  59. yTRouter.router2LoginPage(true)
  60. }
  61. }
  62. // 修改选中的宝宝信息
  63. async updateBabyInfo(index: number) {
  64. if(index == this.selectedBabyIndex) return
  65. this.clearDataSource()
  66. BabyInfo.override(this.babyInfo, this.babyList[index])
  67. this.selectedBabyIndex = index
  68. await this.getWeeklyPlan()
  69. await this.getWeeklyPurchaseList()
  70. this.eventFunc.forEach(f => f())
  71. }
  72. // 添加事件 -
  73. addEvent(f: () => void) {
  74. this.eventFunc.push(f)
  75. f()
  76. }
  77. // 返回菜品信息
  78. getCuisineInfo(id: number): Cuisine {
  79. let index = this.dishList.findIndex(item => item.id === id)
  80. return this.dishList[index]
  81. }
  82. // 清空数据源
  83. clearDataSource(isLoginOut: boolean = false) {
  84. this.allPlan = {}
  85. this.planList = {}
  86. this.purchaseList = []
  87. if(isLoginOut){
  88. BabyInfo.override(this.babyInfo)
  89. this.babyList = []
  90. }
  91. }
  92. // 返回对应天数的菜谱计划
  93. getDayPlan(week: number | string): DayPlan {
  94. let switchFlag: string = ''
  95. if(typeof week === 'string') {
  96. switchFlag = week
  97. } else {
  98. switchFlag = this.week[week]
  99. }
  100. if(!this.planList) this.planList = new WeeklyPlanPurchaseList()
  101. switch (switchFlag) {
  102. case '周一':
  103. if(this.planList.monday == undefined) this.planList.monday = new DayPlan()
  104. return this.planList.monday
  105. case '周二':
  106. if(this.planList.tuesday == undefined) this.planList.tuesday = new DayPlan()
  107. return this.planList.tuesday
  108. case '周三':
  109. if(this.planList.wednesday == undefined) this.planList.wednesday = new DayPlan()
  110. return this.planList.wednesday
  111. case '周四':
  112. if(this.planList.thursday == undefined) this.planList.thursday = new DayPlan()
  113. return this.planList?.thursday
  114. case '周五':
  115. if(this.planList.friday == undefined) this.planList.friday = new DayPlan()
  116. return this.planList?.friday
  117. case '周六':
  118. if(this.planList.saturday == undefined) this.planList.saturday = new DayPlan()
  119. return this.planList?.saturday
  120. case '周日':
  121. if(this.planList.sunday == undefined) this.planList.sunday = new DayPlan()
  122. return this.planList?.sunday
  123. }
  124. return new DayPlan()
  125. }
  126. // 监听添加食谱事件
  127. addRecipeEvent(data: AddRecipeEventData){
  128. console.log(`监听添加食谱事件 data = ${JSON.stringify(data)}`)
  129. let week: number = Number.parseInt(data.week!)
  130. let meal: number = Number.parseInt(data.meal!)
  131. let dayPlan: DayPlan = this.getDayPlan(week)
  132. const cuisine: Cuisine = this.getCuisineInfo(data.recipe!.id!)
  133. if(meal == 0) {
  134. dayPlan.breakfast = cuisine
  135. } else if(meal == 1) {
  136. dayPlan.lunch = cuisine
  137. } else if(meal == 2) {
  138. dayPlan.dinner = cuisine
  139. }
  140. this.updateWeeklyPlan()
  141. }
  142. // 获取本周食谱计划
  143. async getWeeklyPlan(startDate?: string){
  144. try {
  145. if(!startDate) startDate = YTDateUtil.formatDate(new Date(), DateFormat.UNDERLINE)
  146. this.allPlan = await BabyFoodApi.getWeeklyPlan(this.babyInfo.id, startDate)
  147. this.planList = this.allPlan.weeklyPlan
  148. this.planList!.id = this.allPlan.id
  149. } catch (e){
  150. console.log(`e = ${JSON.stringify(e)}`)
  151. }
  152. }
  153. // 更新本周食谱计划
  154. async updateWeeklyPlan(isReload: boolean = true): Promise<boolean>{
  155. try {
  156. IBestToast.showLoading({message: "修改中"})
  157. await BabyFoodApi.updateWeeklyPlan(WeeklyPlanPurchaseList.clone(this.planList!))
  158. // 修改食谱清单后需刷新采购清单
  159. this.getWeeklyPurchaseList()
  160. // 刷新数据源
  161. if(isReload) this.getWeeklyPlan()
  162. return true
  163. } catch (e) {
  164. // 如果在更新食谱时发生错误,则刷新数据源
  165. this.getWeeklyPlan()
  166. return false
  167. } finally {
  168. IBestToast.hide()
  169. }
  170. }
  171. // 获取本周所有采购清单
  172. async getWeeklyPurchaseList(): Promise<void>{
  173. try {
  174. this.purchaseList = await BabyFoodApi.getWeeklyPurchaseList(this.babyInfo.id!)
  175. } catch (e) {
  176. console.log(`e = ${JSON.stringify(e)}`)
  177. }
  178. }
  179. // 获取菜品列表
  180. async getDishList(){
  181. try {
  182. this.dishList = await BabyFoodApi.getDishList()
  183. } catch (e){
  184. console.log(`e = ${JSON.stringify(e)}`)
  185. }
  186. }
  187. // 获取宝宝信息列表
  188. async getBabyList(){
  189. try {
  190. this.babyList = await BabyFoodApi.getBabyList()
  191. let index = this.babyList.findIndex(item => item.id === this.babyInfo.id)
  192. if(index != -1) {
  193. this.updateBabyInfo(index)
  194. } else {
  195. if(this.babyList.length > 0) {
  196. this.updateBabyInfo(0)
  197. } else {
  198. this.clearDataSource()
  199. }
  200. }
  201. } catch (e) {
  202. console.log(`e = ${JSON.stringify(e)}`)
  203. }
  204. }
  205. // 修改购买状态 - 清单 id
  206. async updateBuyStatus(Id: number, index: number){
  207. try {
  208. let ans = await BabyFoodApi.updatePurchaseStatus(Id)
  209. let l = this.purchaseList![index]
  210. l.purchased = l.purchased == '1' ? '0' : '1'
  211. this.purchaseList?.splice(index, 1, l)
  212. } catch (e) {
  213. console.log(`e = ${JSON.stringify(e)}`)
  214. }
  215. }
  216. // 进入添加宝宝信息页面
  217. increaseBabyInfo(){
  218. routerUtils.router2IncreaseBabyInfo((res) => {
  219. this.getBabyList()
  220. })
  221. }
  222. // 跳转至采购详情页
  223. goToPurchaseDetail() {
  224. routerUtils.router2PurchasingPage((res) => {
  225. this.getWeeklyPurchaseList()
  226. })
  227. }
  228. // 进入编辑宝宝信息页面
  229. editBabyInfo() {
  230. routerUtils.router2BabyInfoPage((res) => {
  231. this.getBabyList()
  232. })
  233. }
  234. /**
  235. * 重写的返回逻辑
  236. * @returns
  237. */
  238. _onBackPressed(){
  239. yTRouter.pop('')
  240. return true;
  241. }
  242. }