useBookList.js 3.33 KB
/*
 * @Author: hookehuyr hookehuyr@gmail.com
 * @Date: 2022-05-07 17:46:54
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-06-09 11:26:42
 * @FilePath: /tswj/src/composables/useBookList.js
 * @Description: 
 */
import { ref } from 'vue'
import axios from '@/utils/axios';
import { Toast } from 'vant';
import { useRoute } from 'vue-router';
import { kgBookListAPI } from '@/api/C/kg'

export const useBookList = () => {
  const $route = useRoute();
  const emptyStatus = ref(false);
  // tslint:disable-next-line: variable-name
  const kg_id = $route.query.kg_id ? $route.query.kg_id : '';
  const kgInfo = ref({
    id: '',
    logo: '',
    name: '',
    multi_name: '',
    book_list: []
  });
  if (kg_id) { // 从学校列表进入
    axios.get('/srv/?a=kg_book_list', {
      params: {
        kg_id
      }
    })
      .then(res => {
        if (res.data.code === 1) {
          kgInfo.value = res.data.data;
          kgInfo.value.book_list.forEach(item => {
            item.show = true; // 默认显示所有,给搜索功能留的hook
          });
          // 有空格分割name
          if (kgInfo.value.name.indexOf(' ') > -1) {
            kgInfo.value.multi_name = kgInfo.value.name.split(' ');
          }
          if (!kgInfo.value.book_list.length) {
            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);
      })
  } else { // 从访客进入
    axios.get('/srv/?a=book_list')
      .then(res => {
        if (res.data.code === 1) {
          res.data.data.forEach(item => {
            item.show = true; // 默认显示所有,给搜索功能留的hook
          });
          kgInfo.value = {
            book_list: res.data.data
          }
          if (!kgInfo.value.book_list.length) {
            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 {
    kg_id,
    kgInfo,
    emptyStatus
  }
}

// !!废弃方法
// 前端使用方式过于复杂
export const useAsyncBookList = async () => {
  const $route = useRoute();
  const emptyStatus = ref(false);
  // tslint:disable-next-line: variable-name
  const kg_id = $route.query.kg_id ? $route.query.kg_id : '';
  const kgInfo = ref({
    id: '',
    logo: '',
    name: '',
    multi_name: [],
    book_list: []
  });
  const { data } = await kgBookListAPI({ kg_id });
  if (data) kgInfo.value = data;
  kgInfo.value.book_list.forEach(item => {
    item.show = true; // 默认显示所有,给搜索功能留的hook
  });
  // 有空格分割name
  if (kgInfo.value.name.indexOf(' ') > -1) {
    kgInfo.value.multi_name = kgInfo.value.name.split(' ');
  }
  if (!kgInfo.value.book_list.length) {
    emptyStatus.value = true;
  }
  return {
    kg_id,
    kgInfo,
    emptyStatus
  }
}