useVideoList.js 3.63 KB
/*
 * @Date: 2022-05-05 18:07:16
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-11-26 12:50:17
 * @FilePath: /tswj/src/composables/useVideoList.js
 * @Description: 文件描述
 */
import { ref } from 'vue'
import axios from '@/utils/axios';
import _ from 'lodash'
import { showToast } from 'vant';
import { useRoute } from 'vue-router'
import { bookInfoAPI } from '@/api/C/book'
import { flowFn } from '@/hooks/useFlowFn'

export const useVideoList = () => {
  const $route = useRoute();
  // 切换视频语言
  const checkMandarin = ref(true); // 普通话选项卡
  const checkLocalism = ref(false); // 方言选项卡
  const chooseLanguage = ref({ text: '普通话', val: '普通话' }); // 默认选中普通话
  /**
   * 切换视频语言回调
   * @param {*} type
   */
  const toggleLanguage = (type) => {
    if (type === 'mandarin') {
      checkMandarin.value = true;
      checkLocalism.value = false;
    } else {
      checkMandarin.value = false;
      checkLocalism.value = true;
    }
    // 修改默认语言绑定数据
    if (checkLocalism.value) {
      // tslint:disable-next-line:no-string-literal
      chooseLanguage.value = { text: columns.value[0]['text'], val: columns.value[0]['val'] }
    } else {
      chooseLanguage.value = { text: '普通话', val: '普通话' };
    }
    // 切换语言需要更新列表数据
    onReload()
  }

  // 方言选择项
  let columns = ref([
    { text: '所有方言', val: '所有方言' },
  ])
  //
  axios.get('/srv/?a=localism_list&book_id=' + $route.query.id)
  .then(res => {
    if (res.data.code === 1) {
      let arr = [];
      _.each(res.data.data, item => {
        arr.push({
          text: item,
          val: item
        })
      });
      columns.value = _.concat(columns.value, arr);
    } else {
      // tslint:disable-next-line: no-console
      console.warn(res);
      if (!res.data.show) return false;
      showToast({
        icon: 'close',
        message: res.data.msg
      });
    }
  })
  .catch(err => {
    // tslint:disable-next-line: no-console
    console.error(err);
  })

  const showPicker = ref(false);

  /**
   * 选择方言确认后回调
   * @param {*} param
   */
  const onConfirm = ({ selectedOptions }) => {
    showPicker.value = false;
    chooseLanguage.value = {
      text: selectedOptions[0].text,
      val: selectedOptions[0].val
    }
    onReload()
  };

  // 绑定页面数据
  const bookInfo = ref('');
  // tslint:disable-next-line: variable-name
  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 = async () => {
    // 异步更新数据
    const { data, code } = await bookInfoAPI({ book_id: $route.query.id, kg_id: $route.query.kg_id ? $route.query.kg_id : 0, localism_type: chooseLanguage.value.text, limit: limit.value, offset: offset.value })
    if (code === 1) {
      bookInfo.value = data;
      flowFn(data.prod_list, prod_list, offset, loading, finished, finishedTextStatus, emptyStatus);
    }
  };

  /**
   * 重载刷新程序
   */
  const onReload = () => {
    offset.value = 0
    prod_list.value = []
    loading.value = true;
    finished.value = false;
    onLoad()
  }

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