import BSL from 'bsl-core/typings'; import C from 'bsl-mp/app/Component'; import { clearTimeout, ListenerCallback, setTimeout } from 'bsl-mp/app/Scheduler'; import Event from 'bsl-mp/typings/event'; import * as Export from './export'; const computed = require('miniprogram-computed').behavior; type State = 'loading' | 'empty' | 'over' | 'fail' | ''; const state = { state: 'loading' as State, isSourceDataEmpty: true, loaderRefreshId: 0, _loaderComplete: null as boolean | null, _loaderState: '' as State, _timer: null as ListenerCallback | null, _isReady: false, _intersectionObserver: null as WechatMiniprogram.IntersectionObserver | null, _prevEvent: '' as BSL.RequestState }; class Instance extends C { constructor() { super(state, { pageSize: Number, onLoader: { type: Function as any, value: (() => Promise.resolve()) as any }, overText: { type: String, value: '已经加载到底了' }, sourceDataLength: { type: Number, value: 0 }, api: { type: String, value: '', }, header: { type: Object, value: undefined }, method: { type: String, value: 'get' }, /** 请求的参数 */ data: { type: Object, value: {} }, /** 用于重新发起请求 */ refreshId: { type: Number, value: 0, }, /** 是否缓存请求成功后的数据 * page: 页面级,request组件被卸载时清除 * app: 应用级,整个应用生命周期内存在 */ cache: { type: String, value: '' }, disabled: { type: Boolean, value: false }, useRetry: Boolean, transformResponse: { type: Function as any, value: null }, dataType: String, responseType: String, timeout: Number }); } public externalClasses = ['wrap-cls', 'over-cls', 'fail-cls', 'loading-cls']; public behaviors = [computed]; public options = { // 在组件定义时的选项中启用多slot支持 multipleSlots: true, // 指定所有 _ 开头的数据字段为纯数据字段 pureDataPattern: /^_/ }; public watch = { 'sourceDataLength'() { const self = this as any as Instance; const props = self.properties; if (self.data._isReady) { let state = ''; if (props.sourceDataLength % props.pageSize !== 0 && props.sourceDataLength !== 0 ) { state = 'over'; } else if (self.data._prevEvent === 'empty') { state = 'empty'; } self.setData({ isSourceDataEmpty: props.sourceDataLength === 0, state: state, _prevProps: self.createPrevProps(self) }); } }, 'refreshId'() { const self = this as any as Instance; self.setData({ state: 'loading' }); } }; public ready() { const intersectionObserver = this.createIntersectionObserver(); intersectionObserver.relativeToViewport({ bottom: 100 }); intersectionObserver.observe('.root', (res) => { const state = this.data.state; const loaderComplete = this.data._loaderComplete; if (res.intersectionRatio <= 0) { return; } if (loaderComplete !== true && (state === 'loading' || state === '')) { this.setData({ state: 'loading', _loaderComplete: true, _timer: setTimeout(() => { this.properties.onLoader(); }, 500) }); } }); this.setData({ _isReady: true, _intersectionObserver: intersectionObserver }); } public detache() { if (this.data._timer) { clearTimeout(this.data._timer); } if (this.data._intersectionObserver) { this.data._intersectionObserver.disconnect(); } } private createPrevProps(instance: Instance) { const propsStr = JSON.stringify({ api: instance.properties.api, data: instance.properties.data, sourceDataLength: instance.properties.sourceDataLength }); return propsStr; } public bindComplete(e: Event.Custom) { this.setData({ state: '', _prevEvent: 'complete' }); this.triggerEvent('complete', e.detail); } public bindEmpty(e: Event.Custom) { this.setData({ state: this.data.isSourceDataEmpty ? 'empty' : 'over', _prevEvent: 'empty' }); this.triggerEvent('empty', e.detail); } public bindFail(e: Event.Custom) { this.setData({ state: 'fail', _prevEvent: 'fail' }); this.triggerEvent('fail', e.detail); } public bindTimeout(e: Event.Custom) { this.setData({ state: 'timeout', _prevEvent: 'timeout' }); this.triggerEvent('timeout', e.detail); } public bindFinally(e: Event.Custom) { this.setData({ _loaderComplete: false }); this.triggerEvent('finally', e.detail); } public bindRetry() { this.setData({ loaderRefreshId: this.data.loaderRefreshId + 1, state: 'loading' }); } } Component(new Instance().init());