diet-list.ts 2.4 KB

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