useUnwatchList.js 1.88 KB
import { ref } from 'vue'
import axios from '@/utils/axios';
import _ from 'lodash'
import { Toast } from 'vant';

export const useUnwatchList = () => {
  // 绑定页面数据
  const prod_list = ref([]);
  const limit = ref(10)
  const offset = ref(0)

  // 处理书籍下作品列表
  const loading = ref(false);
  const finished = ref(false);

  // 因为不能让空图标提前出来的写法
  const finishedTextStatus = ref(false);
  const emptyStatus = ref(false);

  /**
   * 向下滚动查询数据
   */
  const onLoad = () => {
    // 异步更新数据
    axios.get('/srv/?a=my_unplay', {
      params: {
        limit: limit.value,
        offset: offset.value
      }
    })
      .then(res => {
        if (res.data.code === 1) {
          prod_list.value = _.concat(prod_list.value, res.data.data.prod);
          prod_list.value = _.uniqBy(prod_list.value, 'id');
          _.each(prod_list.value, (item) => {
            item.type = 'read-only' // 特殊标识,判断入口 为keepAlive使用
          })
          offset.value = prod_list.value.length;
          loading.value = false;
          // 数据全部加载完成
          if (!res.data.data.prod.length) {
            // 加载状态结束
            finished.value = true;
          }
          // 空数据提示
          if (!prod_list.value.length) {
            finishedTextStatus.value = false;
            emptyStatus.value = true;
          }
        } else {
          // tslint:disable-next-line: no-console
          console.warn(res);
          if (!res.data.show) return false;
          Toast({
            icon: 'close',
            message: res.data.msg
          });
        }
      })
      .catch(err => {
        // tslint:disable-next-line: no-console
        console.error(err);
      })
  };

  return {
    onLoad,
    prod_list,
    finished,
    loading,
    finishedTextStatus,
    emptyStatus,
  }
}