education-list.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
  3. import { getScienceNoticeListMethod } 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. cursorColor: '#929292',
  26. iconColor: '#929292',
  27. focus: false,
  28. placeholder: '输入搜索名称',
  29. },
  30. title: '',
  31. dataset: [] as AnyObject[],
  32. page: 1, size: 20, total: 0, loading: false, isLastPage: false,
  33. },
  34. observers: {
  35. 'classify'(value) {
  36. const ref = { tonic: '药膳查询', tea: '中医茶饮' } as any;
  37. this.setData({ title: ref[value] || '' })
  38. },
  39. 'page,size,total'(page, size, total) {
  40. this.setData({ isLastPage: page * size >= total })
  41. }
  42. },
  43. methods: {
  44. async load() {
  45. try {
  46. const { data, total } = await getScienceNoticeListMethod(1, this.data.size, {
  47. keyword: this.data.keyword,
  48. });
  49. this.setData({ dataset: data, total });
  50. } catch (error) {
  51. getTickleContext.call(this).showWarnMessage(error.errMsg);
  52. }
  53. wx.hideLoading();
  54. },
  55. async loadMore() {
  56. if (this.data.isLastPage || this.data.loading) return;
  57. this.setData({ loading: true });
  58. try {
  59. const _page = this.data.page + 1;
  60. const { data, total } = await getScienceNoticeListMethod(_page, this.data.size, {
  61. keyword: this.data.keyword,
  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. toInfoPage(event: WechatMiniprogram.TouchEvent) {
  76. const id = event.currentTarget.id;
  77. wx.navigateTo({ url: `/module/article/pages/science-info/science-info?id=${id}` })
  78. .then(res => {
  79. res.eventChannel.emit('load', this.data.dataset.find(item => item.id.toString() === id))
  80. })
  81. }
  82. }
  83. })