| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
- import I18nBehavior from "../../../../i18n/behavior";
- import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
- import { getDietListMethod } from "../../request";
- // module/diet/pages/diet-list/diet-list.ts
- Component({
- behaviors: [
- I18nBehavior,
- PageContainerBehavior,
- tickleBehavior,
- ],
- lifetimes: {
- attached() {
- wx.showLoading({ title: '加载中' });
- this.load();
- }
- },
- properties: {
- classify: { type: String, value: '' },
- },
- data: {
- i18n: {
- home: { tea: '茶', tonic: "菜谱" }
- },
- keyword: '',
- searchInputProps: {
- placeholderStyle: 'color: #929292;',
- cursorColor: '#1D6FF6',
- iconColor: '#1D6FF6',
- focus: false,
- placeholder: '输入搜索名称',
- },
- title: '',
- dataset: [] as AnyObject[],
- page: 1, size: 20, total: 0, loading: false, isLastPage: false,
- },
- observers: {
- 'classify,i18n.home'(value, i18n) {
- const ref = { tonic: i18n.tonic, tea: i18n.tea } as any;
- this.setData({ title: ref[value] || '' })
- },
- 'page,size,total'(page, size, total) {
- this.setData({ isLastPage: page * size >= total })
- }
- },
- methods: {
- async load() {
- try {
- const { data, total } = await getDietListMethod(1, this.data.size, {
- keyword: this.data.keyword,
- classify: <any>this.data.classify,
- });
- this.setData({ dataset: data, total });
- } catch (error) {
- getTickleContext.call(this).showWarnMessage(error.errMsg);
- }
- wx.hideLoading();
- },
- async loadMore() {
- if (this.data.isLastPage || this.data.loading) return;
- this.setData({ loading: true });
- try {
- const _page = this.data.page + 1;
- const { data, total } = await getDietListMethod(_page, this.data.size, {
- keyword: this.data.keyword,
- classify: <any>this.data.classify,
- });
- this.setData({ dataset: [...this.data.dataset, ...data], total, page: _page });
- } catch (error) {
- getTickleContext.call(this).showWarnMessage(error.errMsg);
- }
- this.setData({ loading: false });
- },
- onConfirmSearchInput(event: WechatMiniprogram.InputConfirm) {
- const _keyword = this.data.keyword;
- this.setData({ keyword: event.detail.value });
- wx.showLoading({ title: '搜索中' });
- this.load().catch(() => { this.setData({ keyword: _keyword }) });
- }
- }
- })
|