science-list.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. import { getFullImageUrl } from "../../../../utils/util";
  6. // module/diet/pages/science-list/science-list.ts
  7. Component({
  8. behaviors: [
  9. I18nBehavior,
  10. PageContainerBehavior,
  11. tickleBehavior,
  12. ],
  13. lifetimes: {
  14. attached() {
  15. wx.showLoading({ title: '加载中' });
  16. this.load();
  17. }
  18. },
  19. properties: {
  20. classify: { type: String, value: '' },
  21. },
  22. data: {
  23. i18n: {
  24. healthTeach: { title: '分享文章' },
  25. },
  26. keyword: '',
  27. searchInputProps: {
  28. placeholderStyle: 'color: #929292;',
  29. // cursorColor: '#1D6FF6',
  30. // iconColor: '#1D6FF6',
  31. cursorColor: '#929292',
  32. iconColor: '#929292',
  33. focus: false,
  34. placeholder: '搜索名称',
  35. },
  36. title: '',
  37. dataset: [] as AnyObject[],
  38. page: 1, size: 20, total: 0, loading: false, isLastPage: false,
  39. },
  40. observers: {
  41. 'classify'(value) {
  42. const ref = { tonic: '药膳查询', tea: '中医茶饮' } as any;
  43. this.setData({ title: ref[value] || '' })
  44. },
  45. 'page,size,total'(page, size, total) {
  46. this.setData({ isLastPage: page * size >= total })
  47. }
  48. },
  49. methods: {
  50. async load() {
  51. try {
  52. const { data, total } = await getScienceListMethod(1, this.data.size, {
  53. keyword: this.data.keyword,
  54. });
  55. data.forEach((item: any) => {
  56. item.briefImg = getFullImageUrl(item.briefImg);
  57. });
  58. this.setData({ dataset: data, total });
  59. } catch (error) {
  60. getTickleContext.call(this).showWarnMessage(error.errMsg);
  61. }
  62. wx.hideLoading();
  63. },
  64. async loadMore() {
  65. if (this.data.isLastPage || this.data.loading) return;
  66. this.setData({ loading: true });
  67. try {
  68. const _page = this.data.page + 1;
  69. const { data, total } = await getScienceListMethod(_page, this.data.size, {
  70. keyword: this.data.keyword,
  71. });
  72. data.forEach((item: any) => {
  73. item.briefImg = getFullImageUrl(item.briefImg);
  74. });
  75. this.setData({ dataset: [...this.data.dataset, ...data], total, page: _page });
  76. } catch (error) {
  77. getTickleContext.call(this).showWarnMessage(error.errMsg);
  78. }
  79. this.setData({ loading: false });
  80. },
  81. onConfirmSearchInput(event: WechatMiniprogram.InputConfirm) {
  82. const _keyword = this.data.keyword;
  83. this.setData({ keyword: event.detail.value });
  84. wx.showLoading({ title: '搜索中' });
  85. this.load().catch(() => { this.setData({ keyword: _keyword }) });
  86. },
  87. toInfoPage(event: WechatMiniprogram.TouchEvent) {
  88. const id = event.currentTarget.id;
  89. wx.navigateTo({ url: `/module/article/pages/science-info/science-info?id=${id}` })
  90. .then(res => {
  91. res.eventChannel.emit('load', this.data.dataset.find(item => item.id.toString() === id))
  92. })
  93. }
  94. }
  95. })