| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import { getHeight } from "../../utils/getHeight";
- Component({
- properties: {
- height: {
- type: Number
- },
- pullDown: {
- type: Boolean,
- value: true
- },
- safety: {
- type: Boolean,
- value: true
- },
- automatic: {
- type: Boolean,
- value: true
- },
- loading: {
- type: Boolean,
- value: false,
- observer: "onLoadingChange"
- },
- finished: {
- type: Boolean,
- value: false
- },
- empty: {
- type: Boolean,
- value: false
- },
- scrollTop: {
- type: Number,
- value: 0,
- observer: "scrollToTop"
- },
- showScrollbar: {
- type: Boolean,
- value: false
- },
- lowerThreshold: {
- type: Number,
- value: 300
- },
- finishedText: {
- type: String,
- value: "没有更多了"
- },
- emptyText: {
- type: String,
- value: "暂无数据"
- },
- loadingText: {
- type: String,
- value: "加载中..."
- }
- },
- lifetimes: {
- attached() {
- if (this.data.automatic) this.automaticSetHei();
- },
- detached() {
- this._detached = true;
- if (this._refreshTimer) {
- clearTimeout(this._refreshTimer);
- this._refreshTimer = null;
- }
- }
- },
- data: {
- inRefresh: false,
- _innerScrollTop: 0
- },
- methods: {
- /* 回到顶部 */
- goTop() {
- const ts = Date.now();
- this.setData({ _innerScrollTop: ts }, () => {
- if (!this._detached) {
- this.setData({ _innerScrollTop: 0 });
- }
- });
- },
- /* 下拉刷新 */
- pullToRefresh() {
- if (this.data.inRefresh) return;
- this.setData({ inRefresh: true });
- this.triggerEvent("getlist", true);
- },
- /* 刷新完成 - 父组件在接口返回后调用 */
- RefreshToComplete() {
- if (this._refreshTimer) clearTimeout(this._refreshTimer);
- this._refreshTimer = setTimeout(() => {
- if (this._detached) return;
- this.setData({ inRefresh: false });
- this._refreshTimer = null;
- }, 500);
- },
- /* 触底加载 - 内部立即加锁,防止接口返回前重复触发 */
- loadThePage() {
- if (this._loadingLocked || this.data.inRefresh || this.data.loading || this.data.finished) return;
- this._loadingLocked = true;
- this.triggerEvent("getlist", false);
- },
- /* 分页加载完成 - 父组件在接口返回后调用,解锁以允许下次触底 */
- completeLoad() {
- this._loadingLocked = false;
- if (!this._detached) {
- this.setData({ loading: false });
- }
- },
- /* 重置分页状态(如切换筛选项时调用) */
- resetLoad() {
- this._loadingLocked = false;
- if (!this._detached) {
- this.setData({ loading: false, finished: false, empty: false });
- }
- },
- /* loading 属性变化时同步内部锁 */
- onLoadingChange(val) {
- if (!val) {
- this._loadingLocked = false;
- }
- },
- scrollToTop(val) {
- if (val !== this.data._innerScrollTop) {
- this.setData({ _innerScrollTop: val });
- }
- },
- automaticSetHei(mode, num) {
- this.setHeight("#mylisttop", mode, num);
- },
- setHeight(element, mode, num) {
- return getHeight(element, this).then(res => {
- let height = res;
- if (mode === "add") {
- height = res + num;
- } else if (mode === "minus") {
- height = res - num;
- }
- if (!this._detached) {
- this.setData({ height });
- }
- return height;
- });
- }
- }
- });
|