science-list.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 { getScienceListMethod } from "../../request";
  5. // module/diet/pages/science-list/science-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. healthTeach: { title: '分享文章' },
  24. },
  25. keyword: '',
  26. searchInputProps: {
  27. placeholderStyle: 'color: #929292;',
  28. // cursorColor: '#1D6FF6',
  29. // iconColor: '#1D6FF6',
  30. cursorColor: '#929292',
  31. iconColor: '#929292',
  32. focus: false,
  33. placeholder: '搜索名称',
  34. },
  35. title: '',
  36. dataset: [] as AnyObject[],
  37. page: 1, size: 20, total: 0, loading: false, isLastPage: false,
  38. },
  39. observers: {
  40. 'classify'(value) {
  41. const ref = { tonic: '药膳查询', tea: '中医茶饮' } as any;
  42. this.setData({ title: ref[value] || '' })
  43. },
  44. 'page,size,total'(page, size, total) {
  45. this.setData({ isLastPage: page * size >= total })
  46. }
  47. },
  48. methods: {
  49. async load() {
  50. try {
  51. const { data, total } = await getScienceListMethod(1, this.data.size, {
  52. keyword: this.data.keyword,
  53. });
  54. this.setData({ dataset: data, total });
  55. } catch (error) {
  56. getTickleContext.call(this).showWarnMessage(error.errMsg);
  57. }
  58. wx.hideLoading();
  59. },
  60. async loadMore() {
  61. if (this.data.isLastPage || this.data.loading) return;
  62. this.setData({ loading: true });
  63. try {
  64. const _page = this.data.page + 1;
  65. const { data, total } = await getScienceListMethod(_page, this.data.size, {
  66. keyword: this.data.keyword,
  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. toInfoPage(event: WechatMiniprogram.TouchEvent) {
  81. const id = event.currentTarget.id;
  82. wx.navigateTo({ url: `/module/article/pages/science-info/science-info?id=${id}` })
  83. .then(res => {
  84. res.eventChannel.emit('load', this.data.dataset.find(item => item.id.toString() === id))
  85. })
  86. }
  87. }
  88. })