| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <template>
- <el-tag
- :type="elTagType"
- :size="size"
- :effect="effect"
- :class="{ 'adm-tag--primary': resolvedType === 'primary' }"
- disable-transitions
- >{{ resolvedText }}</el-tag>
- </template>
- <script>
- /**
- * StatusTag —— 统一状态标签
- *
- * 设计目的:后台 129 个页面直接使用 <el-tag> 且状态→文案/颜色的映射散落各处
- * (有 levelTagType() 函数式,也有 row.x ? 'success' : 'info' 内联三元式),
- * 导致同一状态在不同页面颜色/文案不一致。本组件把映射收敛到 options。
- *
- * 用法一(映射式,推荐):
- * const STATUS_OPTIONS = [
- * { value: 0, label: '待审核', type: 'warning' },
- * { value: 1, label: '已通过', type: 'success' }
- * ]
- * <StatusTag :value="row.status" :options="STATUS_OPTIONS" />
- *
- * 用法二(简单式,等价原 el-tag):
- * <StatusTag :type="row.enabled ? 'success' : 'info'" :text="row.enabled ? '是' : '否'" />
- *
- * type 取值:primary / success / info / warning / danger
- */
- export default {
- name: 'StatusTag',
- props: {
- /** 当前状态值(映射式必填) */
- value: { type: [String, Number, Boolean], default: '' },
- /** 状态映射表:[{ value, label, type }] */
- options: { type: Array, default: null },
- /** 直接指定文案(优先于 options 的 label) */
- text: { type: String, default: '' },
- /** 直接指定类型:primary/success/info/warning/danger */
- type: { type: String, default: '' },
- /** 尺寸:medium / small / mini */
- size: { type: String, default: 'small' },
- /** 主题:light / dark / plain */
- effect: { type: String, default: 'light' },
- /** 命中不到映射时的兜底文案 */
- fallback: { type: String, default: '-' }
- },
- computed: {
- matched() {
- if (!this.options || !this.options.length) return null
- return this.options.find(item => item.value === this.value) || null
- },
- resolvedText() {
- if (this.text) return this.text
- if (this.matched && this.matched.label !== undefined) return this.matched.label
- if (this.value === '' || this.value === null || this.value === undefined) return this.fallback
- return this.value
- },
- resolvedType() {
- if (this.type) return this.type
- if (this.matched && this.matched.type) return this.matched.type
- return 'info'
- },
- // Element UI 的 el-tag 默认态即品牌蓝,primary 交由自定义 class 精确控制配色
- elTagType() {
- return this.resolvedType === 'primary' ? 'info' : this.resolvedType
- }
- }
- }
- </script>
- <style scoped>
- /* 品牌蓝状态标签:与 admin-tokens 的 Ant Design 5 令牌保持一致 */
- .adm-tag--primary {
- background: #e6f4ff;
- border-color: #91caff;
- color: var(--adm-primary);
- }
- </style>
|