user-table-column.vue 3.02 KB
<script setup lang="ts">
  import { Avatar, Link } from '@arco-design/web-vue';
  import TableColumn from '@/components/filter/table-column.vue';
  import { User } from '@/utils/model';
  import { isString, isUndefined } from '@/utils/is';
  import openNewTab from '@/utils/route-blank';
  import { get } from 'lodash';
  import { useRouter } from 'vue-router';

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

  const router = useRouter();

  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 `${props.prefix}${get(user, props.nickIndex, '')}(${get(user, props.realIndex)})`;
    }

    return props.prefix + get(user, props.nickIndex, '');
  };

  const onHref = (record: object) => {
    const user = getUser(record);
    const key = get(record, props.dataIndex);
    const identity = get(user, props.roleIndex);

    if (identity === 1) {
      // return router.push({ name: 'user-singer-show', params: { id: key } });
      openNewTab(router, 'user-singer-show', { id: key });
    }
    if ([2, 3].includes(Number(identity))) {
      // return router.push({ name: 'user-business-show', params: { id: key } });
      openNewTab(router, 'user-business-show', { id: key });
    }
    // return router.push({ name: 'user-register-show', params: { id: key } });
    openNewTab(router, 'user-register-show', { id: key });
  };
</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]" />
        <Link v-if="showHref" class="link-hover" :style="linkStyle" :hoverable="false" @click.stop="onHref(record)">
          {{ getName(record) }}
        </Link>
        <template v-else>{{ getName(record) }}</template>
      </template>
    </template>
  </TableColumn>
</template>

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