Files
crawler-plugin/admin-frontend-vue/src/pages/account/CompanyEditorDialog.vue
T

98 lines
3.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref, watch } from 'vue'
import type { PlatformTabId } from '@/config/platform-scope'
import { createCompany, updateCompany } from '@/api/companies'
import {
COMPANY_NAME_MAX_LENGTH,
COMPANY_STATUS_ACTIVE,
COMPANY_STATUS_DISABLED,
validateCompanyDraft,
type CompanyDraft,
type CompanyItem,
} from '@/api/companies-dto'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { actionableErrorText } from '@/components/admin-feedback'
/** 新建/编辑公司(改名 + 启停 + 备注);写操作仅超管可用,权限由后端把关。
* 所属平台不设手选(2026-11-17 用户口径「有京东就不需要亚马逊了,亚马逊那边也是」):
* 平台跟随当前平台 tab——新建取当前 tab(props.platform),编辑保持行值原样下发。 */
const props = defineProps<{ modelValue: boolean; company: CompanyItem | null; platform?: PlatformTabId }>()
const emit = defineEmits<{ (e: 'update:modelValue', v: boolean): void; (e: 'saved'): void }>()
const form = ref<CompanyDraft>({ companyName: '', status: COMPANY_STATUS_ACTIVE, remark: '', platform: 'amazon' })
const errorText = ref('')
const busy = ref(false)
watch(
() => props.modelValue,
(open) => {
if (!open) return
form.value = props.company
? {
companyName: props.company.companyName,
status: props.company.status,
remark: props.company.remark || '',
platform: props.company.platform === 'jd' ? 'jd' : 'amazon',
}
: { companyName: '', status: COMPANY_STATUS_ACTIVE, remark: '', platform: props.platform ?? 'amazon' }
errorText.value = ''
},
)
function close() {
emit('update:modelValue', false)
}
async function save() {
errorText.value = validateCompanyDraft(form.value)
if (errorText.value) return
busy.value = true
try {
if (props.company) await updateCompany(props.company.id, form.value)
else await createCompany(form.value)
showAdminFeedback(props.company ? '公司已更新' : '公司已创建', 'success')
emit('saved')
close()
} catch (error) {
errorText.value = actionableErrorText(error)
showAdminFeedback(errorText.value, 'error')
} finally {
busy.value = false
}
}
</script>
<template>
<el-dialog
:model-value="modelValue"
:title="company ? '编辑公司' : '新建公司'"
width="520px"
:close-on-click-modal="false"
destroy-on-close
@update:model-value="(v: boolean) => emit('update:modelValue', v)"
>
<el-form label-position="top" @submit.prevent>
<el-form-item label="公司名称" :error="errorText || undefined">
<el-input
v-model="form.companyName"
:maxlength="COMPANY_NAME_MAX_LENGTH"
placeholder="请输入公司名称(全平台唯一)"
/>
</el-form-item>
<el-form-item label="状态">
<el-select v-model="form.status" style="width: 100%">
<el-option :value="COMPANY_STATUS_ACTIVE" label="启用" />
<el-option :value="COMPANY_STATUS_DISABLED" label="停用" />
</el-select>
</el-form-item>
<el-form-item label="备注">
<el-input v-model="form.remark" type="textarea" :rows="2" placeholder="可留空" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="close">取消</el-button>
<el-button type="primary" :loading="busy" @click="save">保存</el-button>
</template>
</el-dialog>
</template>