user-table-column.vue 1.88 KB
<script setup lang="ts">
import TableColumn from '@/components/filter/table-column.vue';
import { Avatar } from '@arco-design/web-vue';

import { get } from 'lodash';
import { User } from '@/types/user';
import { isString, isUndefined } from '@/utils';

const props = withDefaults(
  defineProps<{
    title?: string;
    dataIndex?: string;
    user?: string | Pick<User, 'id' | 'nick_name' | 'real_name' | 'identity'>;
    showHref?: boolean;
    showAvatar?: boolean;
    showRealName?: boolean;
    darkValue?: string;
    nickIndex?: string;
    realIndex?: string;
    avatarIndex?: string;
    roleIndex?: string;
    linkStyle?: object;
  }>(),
  {
    dataIndex: 'id',
    nickIndex: 'nick_name',
    realIndex: 'real_name',
    avatarIndex: 'avatar',
    roleIndex: 'identity',
  }
);

const getUser = (record: object): Pick<User, 'id' | 'nick_name' | 'real_name' | 'identity'> | undefined => {
  if (isUndefined(props.user)) {
    return record as User;
  }

  if (isString(props.user)) {
    return get(record, props.user);
  }

  return get(props, 'user') as User;
};

const getName = (record: object): string => {
  const user = getUser(record);

  if (!user) {
    return '';
  }

  if (props.showRealName) {
    return `${get(user, props.nickIndex, '')}(${get(user, props.realIndex)})`;
  }

  return get(user, props.nickIndex, '');
};
</script>

<template>
  <TableColumn v-bind="$attrs" :title="title" :data-index="dataIndex">
    <template #default="{ record }">
      <template v-if="darkValue !== undefined && getName(record) === darkValue">
        <span style="color: rgba(44, 44, 44, 0.5)"></span>
      </template>
      <template v-else>
        <Avatar v-if="showAvatar" style="margin-right: 8px" :size="34" shape="circle" :image-url="getUser(record)?.[avatarIndex]" />
        {{ getName(record) }}
      </template>
    </template>
  </TableColumn>
</template>

<style scoped lang="less"></style>