science-list.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
  3. import { getScienceListMethod } from "../../request";
  4. // module/diet/pages/science-list/science-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 getScienceListMethod(1, this.data.size, {
  45. keyword: this.data.keyword,
  46. });
  47. this.setData({ dataset: data, total });
  48. } catch (error) {
  49. getTickleContext.call(this).showWarnMessage(error.errMsg);
  50. }
  51. wx.hideLoading();
  52. },
  53. async loadMore() {
  54. if (this.data.isLastPage || this.data.loading) return;
  55. this.setData({ loading: true });
  56. try {
  57. const _page = this.data.page + 1;
  58. const { data, total } = await getScienceListMethod(_page, this.data.size, {
  59. keyword: this.data.keyword,
  60. });
  61. this.setData({ dataset: [...this.data.dataset, ...data], total, page: _page });
  62. } catch (error) {
  63. getTickleContext.call(this).showWarnMessage(error.errMsg);
  64. }
  65. this.setData({ loading: false });
  66. },
  67. onConfirmSearchInput(event: WechatMiniprogram.InputConfirm) {
  68. const _keyword = this.data.keyword;
  69. this.setData({ keyword: event.detail.value });
  70. wx.showLoading({ title: '搜索中' });
  71. this.load().catch(() => { this.setData({ keyword: _keyword }) });
  72. },
  73. toInfoPage(event: WechatMiniprogram.TouchEvent) {
  74. const id = event.currentTarget.id;
  75. wx.navigateTo({ url: `/module/article/pages/science-info/science-info?id=${id}` })
  76. .then(res => {
  77. res.eventChannel.emit('load', this.data.dataset.find(item => item.id.toString() === id))
  78. })
  79. }
  80. }
  81. })