useMessageList.ts 1.9 KB
/*
 * @Date: 2022-06-12 22:51:21
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-06-12 23:00:04
 * @FilePath: /tswj/src/composables/useMessageList.ts
 * @Description: 文件描述
 */
import { ref } from 'vue'
import { myCommentAPI } from '@/api/C/me'
import _ from 'lodash'

export const useMessageList = () => {
  // 我的留言接口联调
  interface commentListType {
    id: string;
    avatar: string;
    name: string;
    kg_name: string;
    comment_time: string;
    note: string;
    c_action: string;
    c_name: string;
    cover: string;
    prod_id: string;
    perf_id: string;
    book_id: string;
    perf_name: string;
    book_name: string;
    localism_type: string;
    is_new: number;
  }
  const commentList = ref<commentListType[]>([])
  const loading = ref(false);
  const finished = ref(false);
  const limit = ref(10);
  const offset = ref(0)
  const finishedTextStatus = ref(false);
  const emptyStatus = ref(false);
  const onLoad = async () => {
    const { data } = await myCommentAPI({ limit: limit.value, offset: offset.value });
    data.forEach((item: { target_name: string | null | undefined; c_action: string; c_name: string; }) => {
      let arr = _.split(item.target_name, '@'); // 分割评论的动作和姓名
      item.c_action = arr[0]; // 评论动作
      item.c_name = arr[1]; // 评论姓名
    });
    // 数据全部加载完成
    if (!data.length) {
      // 加载状态结束
      finished.value = true;
    }
    commentList.value = [...commentList.value, ...data];
    offset.value = commentList.value.length;
    loading.value = false;
    // 隐藏loading标识,空列表图标
    if (!commentList.value.length) {
      finishedTextStatus.value = false;
      emptyStatus.value = true;
    } else {
      emptyStatus.value = false;
    }
  }
  return {
    onLoad,
    commentList,
    loading,
    finished,
    finishedTextStatus,
    emptyStatus,
  };
}