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

export const useVideoList = ($route) => {
    // 切换视频语言
    const checkMandarin = ref(true); // 普通话选项卡
    const checkLocalism = ref(false); // 方言选项卡
    const chooseLanguage = ref({ text: '普通话', val: '普通话' }); // 默认选中普通话
    const toggleLanguage = () => {
      checkMandarin.value = !checkMandarin.value;
      checkLocalism.value = !checkLocalism.value;
      // 修改默认语言绑定数据
      if (checkLocalism.value) {
        // tslint:disable-next-line:no-string-literal
        chooseLanguage.value = { text: columns[0]['text'], val: columns[0]['val'] }
      } else {
        chooseLanguage.value = { text: '普通话', val: '普通话' };
      }
      // 切换语言需要更新列表数据
      offset.value = 0
      prod_list.value = []
      loading.value = true;
      finished.value = false;
      onLoad()
    }
    // 方言选择项
    const columns = [
      { text: '所有方言', val: '所有方言' },
      { text: '沪语', val: '沪语' },
      { text: '粤语', val: '粤语' },
    ];

    const showPicker = ref(false);

    const onConfirm = ({ selectedOptions }) => {
      showPicker.value = false;
      chooseLanguage.value = {
        text: selectedOptions[0].text,
        val: selectedOptions[0].val
      }
    };

    // 绑定页面数据
    const bookInfo = ref('');
    // tslint:disable-next-line: variable-name
    const prod_list = ref([]);
    const limit = ref(1)
    const offset = ref(0)

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

    const onLoad = () => {
      // 异步更新数据
      axios.get('/srv/?a=book_info', {
        params: {
          book_id: $route.query.id,
          localism_type: chooseLanguage.value.text,
          limit: limit.value,
          offset: offset.value
        }
      })
        .then(res => {
          if (res.data.code === 1) {
            bookInfo.value = res.data.data;
            prod_list.value = _.concat(prod_list.value, res.data.data.prod_list);
            offset.value = prod_list.value.length;
            loading.value = false;
            // 数据全部加载完成
            if (!res.data.data.prod_list.length) {
              // 加载状态结束
              finished.value = true;
            }
          } else {
            // tslint:disable-next-line: no-console
            console.warn(res);
            Toast({
              icon: 'close',
              message: res.data.msg
            });
          }
        })
        .catch(err => {
          // tslint:disable-next-line: no-console
          console.error(err);
        })
    };

    return {
      toggleLanguage,
      onLoad,
      columns,
      prod_list,
      finished,
      loading,
      bookInfo,
      showPicker,
      checkLocalism,
      checkMandarin,
      onConfirm,
      chooseLanguage
    }
}