diet-list.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import I18nBehavior from "../../../../i18n/behavior";
  3. import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
  4. import { getDietListMethod } from "../../request";
  5. // module/diet/pages/diet-list/diet-list.ts
  6. Component({
  7. behaviors: [
  8. I18nBehavior,
  9. PageContainerBehavior,
  10. tickleBehavior,
  11. ],
  12. lifetimes: {
  13. attached() {
  14. wx.showLoading({ title: '加载中' });
  15. this.load();
  16. }
  17. },
  18. properties: {
  19. classify: { type: String, value: '' },
  20. },
  21. data: {
  22. i18n: {
  23. home: { tea: '茶', tonic: "菜谱" }
  24. },
  25. keyword: '',
  26. searchInputProps: {
  27. placeholderStyle: 'color: #929292;',
  28. cursorColor: '#1D6FF6',
  29. iconColor: '#1D6FF6',
  30. focus: false,
  31. placeholder: '输入搜索名称',
  32. },
  33. title: '',
  34. dataset: [] as AnyObject[],
  35. page: 1, size: 20, total: 0, loading: false, isLastPage: false,
  36. },
  37. observers: {
  38. 'classify,i18n.home'(value, i18n) {
  39. const ref = { tonic: i18n.tonic, tea: i18n.tea } as any;
  40. this.setData({ title: ref[value] || '' })
  41. },
  42. 'page,size,total'(page, size, total) {
  43. this.setData({ isLastPage: page * size >= total })
  44. }
  45. },
  46. methods: {
  47. async load() {
  48. try {
  49. const { data, total } = await getDietListMethod(1, this.data.size, {
  50. keyword: this.data.keyword,
  51. classify: <any>this.data.classify,
  52. });
  53. this.setData({ dataset: data, total });
  54. } catch (error) {
  55. getTickleContext.call(this).showWarnMessage(error.errMsg);
  56. }
  57. wx.hideLoading();
  58. },
  59. async loadMore() {
  60. if (this.data.isLastPage || this.data.loading) return;
  61. this.setData({ loading: true });
  62. try {
  63. const _page = this.data.page + 1;
  64. const { data, total } = await getDietListMethod(_page, this.data.size, {
  65. keyword: this.data.keyword,
  66. classify: <any>this.data.classify,
  67. });
  68. this.setData({ dataset: [...this.data.dataset, ...data], total, page: _page });
  69. } catch (error) {
  70. getTickleContext.call(this).showWarnMessage(error.errMsg);
  71. }
  72. this.setData({ loading: false });
  73. },
  74. onConfirmSearchInput(event: WechatMiniprogram.InputConfirm) {
  75. const _keyword = this.data.keyword;
  76. this.setData({ keyword: event.detail.value });
  77. wx.showLoading({ title: '搜索中' });
  78. this.load().catch(() => { this.setData({ keyword: _keyword }) });
  79. }
  80. }
  81. })