WEB/src/views/auth/approve.vue
2026-03-18 17:47:43 +08:00

830 lines
20 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.

<template>
<div class="approve-container">
<!-- 审核表单 -->
<div v-if="!loading" class="approve-form">
<!-- 头部信息 -->
<div class="form-header">
<h1>{{ storeInfo?.company_name || '门店代号' }}</h1>
<div class="status-tag">
<el-tag :type="statusType" style="line-height: 1;">{{ statusText }}</el-tag>
</div>
</div>
<!-- 申请者信息 -->
<el-card class="info-card">
<template #header>
<div class="card-header">
<span>申请者信息</span>
</div>
</template>
<template v-if="info">
<div class="info-grid">
<div class="info-item">
<div class="info-label">申请人</div>
<div class="info-value">{{ info.employees_name || info.name || '--' }}</div>
</div>
<div class="info-item">
<div class="info-label">申请时间</div>
<div class="info-value">{{ formatDate(info.apply_time) }}</div>
</div>
</div>
<!-- 申请备注 -->
<div class="remark-section">
<h4>申请备注</h4>
<div class="remark-content">
{{ info.remark || '无' }}
</div>
</div>
</template>
<div class="no-role-content" v-else>
<p>没有找到对应的申请信息,可能已被他人审核或者申请者重新提交了申请资料</p>
</div>
</el-card>
<!-- 角色选择(仅显示在通过申请时) -->
<el-card class="role-card" v-if="info && roles && roles.length > 0">
<template #header>
<div class="card-header">
<span>选择角色(可多选)</span>
<div class="header-actions">
<el-tooltip content="可以为申请者分配多个角色" placement="top">
<el-icon>
<QuestionFilled />
</el-icon>
</el-tooltip>
<span class="selection-count" v-if="selectedRoleIds.length > 0">
已选择 {{ selectedRoleIds.length }} 个角色
</span>
</div>
</div>
</template>
<div class="role-selection-note">
<el-alert type="info" :closable="false">
<template #title>
温馨提示:通过申请时需要至少选择一个角色,拒绝申请时不需要选择角色
</template>
</el-alert>
</div>
<el-checkbox-group v-model="selectedRoleIds">
<el-row :gutter="20">
<el-col :span="8" v-for="role in roles" :key="role.id" :xs="24" :sm="12" :md="8">
<el-card
class="role-option"
:class="{ 'selected': selectedRoleIds.includes(role.id) }"
@click.stop="toggleRoleSelection(role.id)"
>
<div class="role-content">
<el-checkbox :label="role.id" class="role-checkbox" :value="role.id">
<div class="role-info">
<div class="role-name">{{ role.role_name }}</div>
<div class="role-desc">{{ role.description || '暂无描述' }}</div>
</div>
</el-checkbox>
</div>
</el-card>
</el-col>
</el-row>
</el-checkbox-group>
<!-- 选择提示 -->
<div class="selection-tips" v-if="selectedRoleIds.length > 0 && info">
<div class="selected-roles-list">
<strong>已选角色:</strong>
<span v-for="roleId in selectedRoleIds" :key="roleId" class="selected-role-tag">
{{ getRoleName(roleId) }}
</span>
</div>
<div class="selection-action">
<el-button size="small" @click="clearSelection">清空选择</el-button>
<el-button size="small" type="primary" @click="selectAll" v-if="selectedRoleIds.length < roles.length">
全选
</el-button>
</div>
</div>
<div class="no-selection-tips" v-else>
<el-alert type="warning" :closable="false">
<template #title>
请至少选择一个角色才能通过申请
</template>
</el-alert>
</div>
</el-card>
<!-- 无角色提示 -->
<el-card v-else-if="(!roles || roles.length === 0)" class="no-role-card">
<template #header>
<div class="card-header">
<span>{{ storeInfo?.company_name || '门店' }}未创建角色</span>
<el-tooltip content="请创建角色后再审核" placement="top">
<el-icon>
<QuestionFilled />
</el-icon>
</el-tooltip>
</div>
</template>
<div class="no-role-content">
<p>当前门店尚未创建任何角色,无法通过申请。</p>
<p>您可以拒绝申请,或者联系管理员创建角色后重新审核。</p>
</div>
</el-card>
<!-- 审核备注 -->
<el-card class="remark-card" v-if="info">
<template #header>
<div class="card-header">
<span>审核备注</span>
<span class="tip-text">(选填)</span>
</div>
</template>
<el-input
v-model="auditRemark"
type="textarea"
:rows="4"
placeholder="请输入审核意见或备注"
maxlength="500"
show-word-limit
/>
</el-card>
<!-- 操作按钮 -->
<div class="action-buttons" v-if="info && info.audit_status === 'pending'">
<el-button type="danger" @click="handleReject" :loading="rejectLoading">
拒绝申请
</el-button>
<el-button
type="primary"
:disabled="!(roles && roles.length > 0 && selectedRoleIds.length > 0)"
@click="handleApprove"
:loading="approveLoading"
>
通过申请
</el-button>
</div>
<!-- 已审核显示 -->
<div v-if="info && info.audit_status !== 'pending'" class="audited-info">
<el-alert :title="auditResultTitle" :type="auditResultType" :closable="false" show-icon center />
<div class="audit-detail">
<p v-if="info.audited_roles && info.audited_roles.length > 0">
<strong>分配角色:</strong>
<span v-for="role in info.audited_roles" :key="role.id" class="role-tag">
{{ role.role_name }}
</span>
</p>
<p v-if="info.audit_remark">
<strong>审核备注:</strong>{{ info.audit_remark }}
</p>
<p v-if="info.audit_time">
<strong>审核时间:</strong>{{ formatDate(info.audit_time) }}
</p>
<p v-if="info.audit_user_name">
<strong>审核人:</strong>{{ info.audit_user_name }}
</p>
</div>
<el-button type="primary" @click="closePage">关闭页面</el-button>
</div>
</div>
<div v-if="loading" class="loading-state">
<el-skeleton :rows="10" animated />
</div>
<!-- 拒绝理由弹窗 -->
<el-dialog v-model="rejectDialogVisible" title="请输入拒绝理由" width="500px" :close-on-click-modal="false">
<div class="reject-dialog-content">
<el-alert type="warning" :closable="false" class="reject-alert">
<template #title>
拒绝申请时不需要选择角色
</template>
</el-alert>
<el-input
v-model="rejectReason"
type="textarea"
:rows="4"
placeholder="请详细说明拒绝的理由"
maxlength="500"
show-word-limit
/>
</div>
<template #footer>
<span class="dialog-footer">
<el-button @click="rejectDialogVisible = false">取消</el-button>
<el-button type="danger" @click="confirmReject" :loading="rejectLoading" :disabled="!rejectReason.trim()">
确认拒绝
</el-button>
</span>
</template>
</el-dialog>
<!-- 成功提示弹窗 -->
<el-dialog
v-model="successDialogVisible"
:title="dialogTitle"
width="400px"
:show-close="false"
:close-on-click-modal="false"
:close-on-press-escape="false"
>
<div style="text-align: center;">
<el-icon size="60" :color="dialogIconColor">
<component :is="dialogIconComponent" />
</el-icon>
<p style="margin: 20px 0; font-size: 16px;">{{ successMessage }}</p>
<div v-if="isApproved && successRoles.length > 0" class="success-roles">
<p>分配角色:</p>
<div class="success-roles-list">
<el-tag v-for="role in successRoles" :key="role.id" type="success" size="small">
{{ role.role_name }}
</el-tag>
</div>
</div>
</div>
<template #footer>
<div style="text-align: center;">
<el-button type="primary" @click="handleDialogClose">关闭页面</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getStoreRoles, auditApply } from '@/api/store'
import { ElMessage, ElMessageBox } from 'element-plus'
import { QuestionFilled, CircleCheck, CircleClose } from '@element-plus/icons-vue'
import dayjs from 'dayjs'
const route = useRoute()
const router = useRouter()
// 状态定义
const loading = ref(true)
const info = ref<any>(null)
const roles = ref<any[]>([])
const storeInfo = ref<any>(null)
// 表单数据
const selectedRoleIds = ref<number[]>([])
const auditRemark = ref('')
// 拒绝相关
const rejectDialogVisible = ref(false)
const rejectReason = ref('')
// 加载状态
const approveLoading = ref(false)
const rejectLoading = ref(false)
// 成功提示
const successDialogVisible = ref(false)
const successMessage = ref('')
const dialogTitle = ref('操作成功')
const dialogIconComponent = ref<any>(CircleCheck)
const dialogIconColor = ref('#67C23A')
const successRoles = ref<any[]>([])
const isApproved = ref(true)
// 计算属性
const statusType = computed(() => {
const status = info.value?.audit_status
const map: Record<string, string> = {
'pending': 'warning',
'approved': 'success',
'rejected': 'danger',
'processing': 'info'
}
return map[status] || 'info'
})
const statusText = computed(() => {
const status = info.value?.audit_status
const map: Record<string, string> = {
'pending': '待审核',
'approved': '已通过',
'rejected': '已拒绝',
'processing': '审核中'
}
return map[status] || '未知状态'
})
const auditResultTitle = computed(() => {
if (info.value?.audit_status === 'approved') {
return '申请已通过'
} else if (info.value?.audit_status === 'rejected') {
return '申请已拒绝'
}
return ''
})
const auditResultType = computed(() => {
return info.value?.audit_status === 'approved' ? 'success' : 'error' as any
})
// 方法
const getData = async (code: string) => {
if (!code) return
loading.value = true
try {
const response: any = await getStoreRoles({ code })
if (response.code === 200) {
info.value = response.data.info
roles.value = response.data.roles || []
storeInfo.value = response.data.store || {}
} else {
ElMessage.error(response.msg || '获取数据失败')
}
} catch (error) {
console.error('获取数据失败:', error)
ElMessage.error('网络错误,请稍后重试')
} finally {
loading.value = false
}
}
const toggleRoleSelection = (roleId: number) => {
const index = selectedRoleIds.value.indexOf(roleId)
if (index > -1) {
selectedRoleIds.value.splice(index, 1)
} else {
selectedRoleIds.value.push(roleId)
}
}
const selectAll = () => {
selectedRoleIds.value = roles.value.map(role => role.id)
}
const clearSelection = () => {
selectedRoleIds.value = []
}
const getRoleName = (roleId: number) => {
const role = roles.value.find(r => r.id === roleId)
return role ? role.role_name : '未知角色'
}
const getSelectedRoles = () => {
return roles.value.filter(role => selectedRoleIds.value.includes(role.id))
}
const handleApprove = async () => {
if (selectedRoleIds.value.length === 0) {
ElMessage.warning('请至少选择一个角色')
return
}
try {
await ElMessageBox.confirm(
`确认通过申请并为用户分配 ${selectedRoleIds.value.length} 个角色吗?`,
'提示',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}
)
approveLoading.value = true
const submitData = {
code: route.query.code,
role_ids: selectedRoleIds.value,
store_id: storeInfo.value.id,
audit_status: 'approved',
audit_remark: auditRemark.value
}
const response: any = await auditApply(submitData)
if (response.code === 200) {
successMessage.value = response.msg || '申请已通过,页面即将关闭'
dialogTitle.value = '操作成功'
dialogIconComponent.value = CircleCheck
dialogIconColor.value = '#67C23A'
successRoles.value = getSelectedRoles()
isApproved.value = true
successDialogVisible.value = true
// 更新本地状态
if (info.value) {
info.value.audit_status = 'approved'
if (response.data) {
Object.assign(info.value, response.data)
}
}
} else {
ElMessage.error(response.msg || '操作失败')
}
} catch (error) {
if (error !== 'cancel') {
console.error('审核失败:', error)
ElMessage.error('网络错误,请稍后重试')
}
} finally {
approveLoading.value = false
}
}
const handleReject = () => {
rejectDialogVisible.value = true
}
const confirmReject = async () => {
if (!rejectReason.value.trim()) {
ElMessage.warning('请填写拒绝理由')
return
}
rejectLoading.value = true
try {
const submitData = {
code: route.query.code,
audit_status: 'rejected',
audit_remark: rejectReason.value
}
const response: any = await auditApply(submitData)
if (response.code === 200) {
successMessage.value = response.msg || '申请已拒绝,页面即将关闭'
dialogTitle.value = '操作成功'
dialogIconComponent.value = CircleClose
dialogIconColor.value = '#F56C6C'
successRoles.value = []
isApproved.value = false
successDialogVisible.value = true
rejectDialogVisible.value = false
// 更新本地状态
if (info.value) {
info.value.audit_status = 'rejected'
if (response.data) {
Object.assign(info.value, response.data)
}
}
} else {
ElMessage.error(response.msg || '操作失败')
}
} catch (error) {
console.error('拒绝失败:', error)
ElMessage.error('网络错误,请稍后重试')
} finally {
rejectLoading.value = false
}
}
const handleDialogClose = () => {
successDialogVisible.value = false
closePage()
}
const closePage = () => {
ElMessage({
message: '页面即将关闭',
type: 'info',
duration: 1000
})
setTimeout(() => {
if (window.self !== window.top) {
try {
window.parent.postMessage({
type: 'closeApprovePage',
code: route.query.code,
action: 'closed'
}, '*')
} catch (e) {
console.log('postMessage失败:', e)
}
}
if (window.opener && !window.opener.closed) {
window.close()
} else {
router.push('/').catch(() => {
window.location.href = '/'
})
}
}, 1500)
}
const formatDate = (dateStr: string) => {
if (!dateStr) return '--'
try {
return dayjs(dateStr).format('YYYY-MM-DD HH:mm:ss')
} catch (e) {
return dateStr
}
}
// 监听路由参数
watch(() => route.query.code, (newCode) => {
if (newCode) {
getData(newCode as string)
}
}, { immediate: true })
onMounted(() => {
if (route.query.code) {
getData(route.query.code as string)
}
})
</script>
<style scoped>
.approve-container {
max-width: 1200px;
margin: 20px auto;
padding: 20px;
}
.loading-state {
text-align: center;
padding: 100px 0;
}
.form-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 1px solid #ebeef5;
position: relative;
}
.form-header h1 {
margin: 0 0 10px 0;
font-size: 24px;
color: #303133;
font-weight: bold;
}
.status-tag {
position: absolute;
right: 0;
top: 0;
}
.status-tag .el-tag {
font-size: 16px;
padding: 8px 16px;
}
.info-card,
.role-card,
.remark-card,
.no-role-card {
margin-bottom: 20px;
}
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
}
.header-actions {
display: flex;
align-items: center;
gap: 10px;
}
.card-header .el-icon {
color: #909399;
cursor: help;
}
.selection-count {
color: #409eff;
font-size: 14px;
}
.role-selection-note {
margin-bottom: 20px;
}
.no-role-content {
padding: 20px;
text-align: center;
color: #e6a23c;
}
.no-role-content p {
margin: 10px 0;
}
.remark-section {
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #ebeef5;
}
.remark-section h4 {
margin: 0 0 10px 0;
color: #606266;
}
.remark-content {
padding: 12px;
background: #f5f7fa;
border-radius: 4px;
color: #606266;
}
.role-option {
cursor: pointer;
transition: all 0.3s;
margin-bottom: 10px;
}
.role-option:hover {
border-color: #409eff;
}
.role-option.selected {
border-color: #409eff;
background-color: #f0f7ff;
}
.role-content {
display: flex;
align-items: flex-start;
}
.role-checkbox {
width: 100%;
}
.role-info {
margin-left: 10px;
white-space: normal;
}
.role-name {
font-weight: bold;
color: #303133;
margin-bottom: 5px;
}
.role-desc {
font-size: 12px;
color: #909399;
line-height: 1.4;
margin-bottom: 5px;
}
.selection-tips {
margin-top: 20px;
padding: 15px;
background-color: #f5f7fa;
border-radius: 4px;
}
.selected-roles-list {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 10px;
}
.selected-role-tag {
padding: 4px 10px;
background-color: #409eff;
color: white;
border-radius: 12px;
font-size: 12px;
}
.selection-action {
display: flex;
gap: 10px;
}
.no-selection-tips {
margin-top: 20px;
}
.tip-text {
color: #909399;
font-size: 14px;
}
.action-buttons {
text-align: center;
margin-top: 30px;
padding-top: 30px;
border-top: 1px solid #ebeef5;
}
.action-buttons .el-button {
width: 120px;
margin: 0 10px;
}
.audited-info {
text-align: center;
padding: 40px 0;
}
.audit-detail {
margin: 20px 0 30px 0;
text-align: left;
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
.audit-detail p {
margin: 10px 0;
color: #606266;
}
.role-tag {
display: inline-block;
margin: 2px 5px;
padding: 2px 8px;
background-color: #f0f9eb;
color: #67c23a;
border-radius: 4px;
font-size: 12px;
}
.reject-dialog-content {
padding: 10px 0;
}
.reject-alert {
margin-bottom: 20px;
}
.success-roles {
margin-top: 15px;
}
.success-roles-list {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 8px;
margin-top: 10px;
}
.info-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 16px;
margin-bottom: 20px;
}
.info-item {
display: flex;
flex-direction: column;
padding: 12px;
background-color: #f9f9f9;
border-radius: 6px;
border: 1px solid #ebeef5;
}
.info-label {
font-size: 12px;
color: #909399;
margin-bottom: 4px;
font-weight: 500;
}
.info-value {
font-size: 14px;
color: #606266;
font-weight: 500;
}
@media (max-width: 768px) {
.approve-container {
padding: 10px;
}
.form-header {
flex-direction: column;
text-align: center;
}
.form-header .status-tag {
position: static;
margin-top: 10px;
}
.action-buttons .el-button {
width: 100%;
margin: 5px 0;
}
}
</style>