896 lines
29 KiB
Vue
896 lines
29 KiB
Vue
<template>
|
||
<div class="account-container app-container">
|
||
<div class="filter-container">
|
||
<el-form :inline="true" :model="listQuery" class="demo-form-inline">
|
||
<el-form-item label="状态">
|
||
<el-select v-model="listQuery.status" placeholder="状态" clearable style="width: 120px">
|
||
<el-option label="启用" :value="1" />
|
||
<el-option label="停用" :value="0" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="包含已删除">
|
||
<el-switch v-model="listQuery.includeDisabled" />
|
||
</el-form-item>
|
||
<el-form-item>
|
||
<el-button type="primary" icon="Search" @click="handleFilter">搜索</el-button>
|
||
<el-button type="success" icon="Plus" @click="handleCreate">新增工号</el-button>
|
||
</el-form-item>
|
||
</el-form>
|
||
</div>
|
||
|
||
<upper-table
|
||
v-loading="listLoading"
|
||
:data="list"
|
||
:columns="columns"
|
||
row-key="employee_id"
|
||
>
|
||
<template #column-avatar_url="{ row }">
|
||
<el-avatar :size="30" :src="row.avatar_url">
|
||
<img src="https://cube.elemecdn.com/e/fd/0fc7693967a94116a3f11b0106734664jpg.png" />
|
||
</el-avatar>
|
||
</template>
|
||
<template #column-status="{ row }">
|
||
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
|
||
{{ row.status === 1 ? '启用' : '停用' }}
|
||
</el-tag>
|
||
</template>
|
||
<template #column-roles="{ row }">
|
||
<template v-if="row.roles && row.roles.length">
|
||
<el-tag
|
||
v-for="r in row.roles"
|
||
:key="r.role_id"
|
||
size="small"
|
||
style="margin:2px"
|
||
:type="roleTagMap[r.role_name] || 'info'"
|
||
>
|
||
{{ r.role_name }}
|
||
</el-tag>
|
||
</template>
|
||
<el-text v-else type="info" size="small">未分配</el-text>
|
||
</template>
|
||
<template #column-actions="{ row }">
|
||
<el-button type="primary" size="small" icon="Edit" @click="handleUpdate(row)">编辑</el-button>
|
||
<el-button type="warning" size="small" icon="Lock" @click="handleAssignRole(row)">分配角色</el-button>
|
||
<el-button size="small" icon="Key" @click="handleResetPassword(row)">重置密码</el-button>
|
||
<el-button v-if="row.status === 1" type="danger" size="small" icon="CircleClose" @click="handleDelete(row)">停用</el-button>
|
||
<el-button v-else type="success" size="small" icon="CircleCheck" @click="handleEnable(row)">启用</el-button>
|
||
<el-button v-if="row.is_wechat_bound" type="danger" plain size="small" icon="CircleClose" @click="handleUnbindWechat(row)">解除绑定</el-button>
|
||
<el-button v-else type="info" size="small" icon="Link" @click="handleBindWechat(row)">绑定微信</el-button>
|
||
</template>
|
||
</upper-table>
|
||
|
||
<!-- 新增/编辑弹窗 -->
|
||
<el-dialog :title="textMap[dialogStatus]" v-model="dialogFormVisible" width="1000px" align-center custom-class="account-dialog" @close="handleDialogClose">
|
||
<el-form ref="dataForm" :rules="rules" :model="temp" label-position="top" class="custom-form">
|
||
<el-tabs v-model="activeTab" class="form-tabs">
|
||
<el-tab-pane label="基本信息" name="base">
|
||
<div class="form-section no-shadow">
|
||
<el-row :gutter="20">
|
||
<el-col :span="12">
|
||
<el-form-item label="手机号" prop="phone">
|
||
<el-input v-model="temp.phone" :disabled="dialogStatus==='update'" placeholder="请输入手机号" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="姓名" prop="real_name">
|
||
<el-input v-model="temp.real_name" placeholder="请输入工号姓名" />
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
|
||
<el-row :gutter="20" v-if="dialogStatus === 'create'">
|
||
<el-col :span="12">
|
||
<el-form-item label="用户名" prop="username">
|
||
<el-input v-model="temp.username" placeholder="系统自动生成" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="密码" prop="password">
|
||
<el-input v-model="temp.password" type="password" show-password placeholder="默认 111111" />
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
|
||
<el-row :gutter="20">
|
||
<el-col :span="12">
|
||
<el-form-item label="邮箱" prop="email">
|
||
<el-input v-model="temp.email" placeholder="请输入邮箱" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="角色配置" prop="role_ids">
|
||
<el-select
|
||
v-model="temp.role_ids"
|
||
multiple
|
||
collapse-tags
|
||
collapse-tags-tooltip
|
||
placeholder="请选择角色(必填)"
|
||
style="width:100%"
|
||
v-loading="rolesForFormLoading"
|
||
>
|
||
<el-option
|
||
v-for="r in availableRoles"
|
||
:key="r.role_id"
|
||
:label="r.role_name"
|
||
:value="r.role_id"
|
||
/>
|
||
</el-select>
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
</div>
|
||
</el-tab-pane>
|
||
|
||
<el-tab-pane v-if="dialogStatus === 'update'" label="工号头像" name="avatar">
|
||
<div class="form-section no-shadow">
|
||
<div class="avatar-upload-wrapper full-width">
|
||
<images-upload
|
||
v-if="temp.employee_id"
|
||
ref="imagesUploadRef"
|
||
:id="temp.employee_id"
|
||
type="avatar"
|
||
@refresh="getList"
|
||
/>
|
||
</div>
|
||
<div class="upload-tip">支持本地上传及摄像头拍摄</div>
|
||
</div>
|
||
</el-tab-pane>
|
||
</el-tabs>
|
||
</el-form>
|
||
<template #footer>
|
||
<div class="dialog-footer">
|
||
<el-button @click="dialogFormVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="listLoading" @click="dialogStatus==='create'?createData():updateData()">确认保存</el-button>
|
||
</div>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 角色分配弹窗 -->
|
||
<el-dialog title="分配角色" v-model="roleDialogVisible" width="900px" align-center>
|
||
<div v-loading="roleLoading">
|
||
<el-alert
|
||
:title="`正在为工号「${currentEmployeeRow?.real_name || currentEmployeeId}」分配角色。已勾选项为当前已绑定角色,修改后点击确认分配即整体替换。`"
|
||
type="info"
|
||
:closable="false"
|
||
style="margin-bottom:12px"
|
||
/>
|
||
<!-- 过滤栏 -->
|
||
<el-form :inline="true" :model="roleFilter" style="margin-bottom:10px">
|
||
<el-form-item label="关键词">
|
||
<el-input v-model="roleFilter.keyword" placeholder="角色名称" clearable style="width:200px" @input="applyRoleFilter" />
|
||
</el-form-item>
|
||
<el-form-item>
|
||
<el-button @click="toggleSelectAll">{{ isAllSelected ? '取消全选' : '全选当前页' }}</el-button>
|
||
</el-form-item>
|
||
</el-form>
|
||
<!-- 角色表格 -->
|
||
<el-table
|
||
ref="roleTableRef"
|
||
:data="filteredRoles"
|
||
border
|
||
size="small"
|
||
max-height="450"
|
||
row-key="role_id"
|
||
@selection-change="handleRoleSelectionChange"
|
||
>
|
||
<el-table-column type="selection" width="45" />
|
||
<el-table-column label="角色名称" prop="role_name" min-width="130" />
|
||
<el-table-column label="描述" prop="description" min-width="220" show-overflow-tooltip />
|
||
<el-table-column label="初始状态" width="90">
|
||
<template #default="{ row }">
|
||
<el-tag v-if="row.selected" type="success" size="small">已分配</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
<div style="margin-top:8px;color:#999;font-size:12px">已选 {{ selectedRoleIds.length }} 项</div>
|
||
</div>
|
||
<template #footer>
|
||
<div class="dialog-footer">
|
||
<el-button @click="roleDialogVisible = false">关闭</el-button>
|
||
<el-button type="primary" :loading="roleSubmitting" @click="submitRoleUpdate">确认分配</el-button>
|
||
</div>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 重置密码弹窗 -->
|
||
<el-dialog title="重置密码" v-model="resetPwdDialogVisible" width="400px" align-center>
|
||
<el-form ref="resetPwdFormRef" :model="resetPwdTemp" label-width="80px">
|
||
<el-form-item label="工号" >
|
||
<el-input :value="resetPwdTemp.real_name || resetPwdTemp.phone" disabled />
|
||
</el-form-item>
|
||
<el-form-item
|
||
label="新密码"
|
||
prop="password"
|
||
:rules="[{ required: true, message: '请输入新密码', trigger: 'blur' }, { min: 6, message: '密码至少6位', trigger: 'blur' }]"
|
||
>
|
||
<el-input v-model="resetPwdTemp.password" type="password" show-password placeholder="请输入新密码(至少6位)" />
|
||
</el-form-item>
|
||
</el-form>
|
||
<template #footer>
|
||
<div class="dialog-footer">
|
||
<el-button @click="resetPwdDialogVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="resetPwdSubmitting" @click="submitResetPassword">确认重置</el-button>
|
||
</div>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 绑定微信弹窗 -->
|
||
<el-dialog title="绑定微信" v-model="bindWechatDialogVisible" width="400px" align-center @close="handleBindWechatDialogClose">
|
||
<div class="bind-wechat-body">
|
||
<!-- 已绑定状态 -->
|
||
<div v-if="bindWechatIsBound" class="bound-info">
|
||
<el-icon class="bound-icon"><CircleCheckFilled /></el-icon>
|
||
<span>已绑定微信:{{ bindWechatNickname || '微信用户' }}</span>
|
||
</div>
|
||
|
||
<!-- 未绑定:二维码区域 -->
|
||
<template v-else>
|
||
<div class="qrcode-wrapper">
|
||
<div v-if="bindWechatLoading" class="status-overlay loading-overlay">
|
||
<el-icon class="is-loading"><Loading /></el-icon>
|
||
<span>加载中...</span>
|
||
</div>
|
||
<div v-else-if="bindWechatQrStatus === 'expired'" class="status-overlay expired-overlay" @click="refreshBindWechatQr">
|
||
<el-icon><RefreshRight /></el-icon>
|
||
<span>二维码已过期,点击刷新</span>
|
||
</div>
|
||
<div v-else-if="bindWechatQrStatus === 'scanned'" class="status-overlay scanned-overlay">
|
||
<el-icon><CircleCheckFilled /></el-icon>
|
||
<span>扫码成功,正在确认...</span>
|
||
</div>
|
||
<img v-else-if="bindWechatQrCodeUrl" :src="bindWechatQrCodeUrl" alt="微信二维码" class="qrcode-img" />
|
||
<div v-else class="status-overlay loading-overlay">
|
||
<el-icon class="is-loading"><Loading /></el-icon>
|
||
<span>正在获取二维码...</span>
|
||
</div>
|
||
|
||
<p class="qrcode-tip">
|
||
<template v-if="bindWechatQrStatus === 'scanned'">请在微信中确认绑定</template>
|
||
<template v-else-if="bindWechatQrStatus === 'expired'">二维码已失效,点击上方刷新</template>
|
||
<template v-else>请使用微信扫码关注公众号完成绑定</template>
|
||
</p>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
<template #footer>
|
||
<el-button @click="bindWechatDialogVisible = false">关闭</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, onMounted, onUnmounted, reactive, nextTick, computed } from 'vue'
|
||
import { accountList, accountAdd, accountUpdate, accountdelete, accountResetPassword, getBindWechatCode, unbindWechat } from '@/api/setting.account/account'
|
||
import { roleList } from '@/api/setting.role/role'
|
||
import UpperTable from '@/components/UpperTable/index.vue'
|
||
import ImagesUpload from '@/components/ImagesUpload/index.vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import { CircleCheckFilled, Loading, RefreshRight, User, Picture } from '@element-plus/icons-vue'
|
||
import { accountColumns, defaultTemp, accountRules, roleTagMap } from './account-config'
|
||
|
||
const list = ref([])
|
||
const listLoading = ref(true)
|
||
const listQuery = reactive({
|
||
status: 1,
|
||
includeDisabled: false
|
||
})
|
||
|
||
const imagesUploadRef = ref()
|
||
const dataForm = ref()
|
||
|
||
const columns = accountColumns
|
||
|
||
const temp = reactive<{
|
||
employee_id?: number
|
||
phone: string
|
||
real_name: string
|
||
username: string
|
||
password: string
|
||
role_ids: number[]
|
||
email: string
|
||
file_id?: number
|
||
avatar_url: string
|
||
}>({ ...defaultTemp })
|
||
|
||
// 表单中可用角色列表
|
||
const availableRoles = ref<any[]>([])
|
||
const rolesForFormLoading = ref(false)
|
||
|
||
const activeTab = ref('base')
|
||
const dialogFormVisible = ref(false)
|
||
const dialogStatus = ref('')
|
||
const textMap: Record<string, string> = {
|
||
update: '编辑工号',
|
||
create: '新增工号'
|
||
}
|
||
|
||
const rules = computed(() => {
|
||
const baseRules = { ...accountRules }
|
||
// 编辑状态下,手机号不可修改且允许为空(不校验必填)
|
||
if (dialogStatus.value === 'update') {
|
||
baseRules.phone = baseRules.phone.filter(r => !r.required)
|
||
}
|
||
return baseRules
|
||
})
|
||
|
||
const flattenRoles = (rawRoles: any[]) => {
|
||
const tempMap = new Map<number, any>()
|
||
const buildFlat = (arr: any[]) => {
|
||
arr.forEach(item => {
|
||
if (!tempMap.has(item.role_id)) {
|
||
const { children, ...rest } = item
|
||
tempMap.set(item.role_id, { ...rest, children: [] })
|
||
}
|
||
if (item.children && item.children.length) buildFlat(item.children)
|
||
})
|
||
}
|
||
buildFlat(rawRoles)
|
||
return Array.from(tempMap.values())
|
||
}
|
||
|
||
const getList = async () => {
|
||
listLoading.value = true
|
||
try {
|
||
const response = await accountList(listQuery)
|
||
list.value = response.data || []
|
||
} catch (error) {
|
||
console.error(error)
|
||
} finally {
|
||
listLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleFilter = () => {
|
||
getList()
|
||
}
|
||
|
||
const resetTemp = () => {
|
||
Object.assign(temp, defaultTemp)
|
||
}
|
||
|
||
const loadAvailableRoles = async () => {
|
||
rolesForFormLoading.value = true
|
||
try {
|
||
const res = await roleList({ includeDisabled: false })
|
||
availableRoles.value = flattenRoles(res.data?.roles || [])
|
||
} catch (e) {
|
||
console.error(e)
|
||
} finally {
|
||
rolesForFormLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleCreate = () => {
|
||
resetTemp()
|
||
activeTab.value = 'base'
|
||
dialogStatus.value = 'create'
|
||
dialogFormVisible.value = true
|
||
loadAvailableRoles()
|
||
}
|
||
|
||
const createData = async () => {
|
||
if (!dataForm.value) return
|
||
await dataForm.value.validate(async (valid: boolean) => {
|
||
if (!valid) return
|
||
try {
|
||
const { phone, real_name, username, password, email, role_ids } = temp
|
||
await accountAdd({ phone, real_name, ids: role_ids, username, password, email })
|
||
dialogFormVisible.value = false
|
||
ElMessage.success('创建成功')
|
||
getList()
|
||
} catch (error) {
|
||
console.error(error)
|
||
}
|
||
})
|
||
}
|
||
|
||
const handleUpdate = (row: any) => {
|
||
Object.assign(temp, { ...row, role_ids: row.role_ids ? [...row.role_ids] : [] })
|
||
activeTab.value = 'base'
|
||
dialogStatus.value = 'update'
|
||
dialogFormVisible.value = true
|
||
loadAvailableRoles()
|
||
}
|
||
|
||
const handleDialogClose = () => {
|
||
imagesUploadRef.value?.stopCamera()
|
||
}
|
||
|
||
const updateData = async () => {
|
||
if (!dataForm.value) return
|
||
await dataForm.value.validate(async (valid: boolean) => {
|
||
if (!valid) return
|
||
try {
|
||
const { employee_id, real_name, file_id, role_ids } = temp
|
||
// 接口文档 setting.account.update 仅支持 employee_id, real_name, file_id, ids
|
||
// 手机号 (phone) 在接口中不可修改,因此更新时不传
|
||
await accountUpdate({
|
||
employee_id: employee_id!,
|
||
real_name,
|
||
file_id,
|
||
ids: role_ids
|
||
})
|
||
dialogFormVisible.value = false
|
||
ElMessage.success('更新成功')
|
||
getList()
|
||
} catch (error) {
|
||
console.error(error)
|
||
}
|
||
})
|
||
}
|
||
|
||
const handleDelete = (row: any) => {
|
||
ElMessageBox.confirm('确认停用该工号吗?', '提示', {
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning'
|
||
}).then(async () => {
|
||
await accountdelete({ employee_id: row.employee_id })
|
||
ElMessage.success('已停用')
|
||
getList()
|
||
})
|
||
}
|
||
|
||
const handleEnable = (row: any) => {
|
||
ElMessageBox.confirm('确认启用该工号吗?', '提示', {
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'info'
|
||
}).then(async () => {
|
||
// 启用时保留原有角色组,ids 必传
|
||
await accountUpdate({ employee_id: row.employee_id, ids: row.role_ids || [] })
|
||
ElMessage.success('已启用')
|
||
getList()
|
||
})
|
||
}
|
||
|
||
// ---------------- 重置密码 ----------------
|
||
const resetPwdDialogVisible = ref(false)
|
||
const resetPwdSubmitting = ref(false)
|
||
const resetPwdFormRef = ref()
|
||
const resetPwdTemp = reactive({ employee_id: 0, phone: '', real_name: '', password: '' })
|
||
|
||
const handleResetPassword = (row: any) => {
|
||
Object.assign(resetPwdTemp, { employee_id: row.employee_id, phone: row.phone, real_name: row.real_name, password: '' })
|
||
resetPwdDialogVisible.value = true
|
||
}
|
||
|
||
const submitResetPassword = async () => {
|
||
if (!resetPwdFormRef.value) return
|
||
await resetPwdFormRef.value.validate(async (valid: boolean) => {
|
||
if (!valid) return
|
||
resetPwdSubmitting.value = true
|
||
try {
|
||
await accountResetPassword({ employee_id: resetPwdTemp.employee_id, password: resetPwdTemp.password })
|
||
ElMessage.success('密码重置成功')
|
||
resetPwdDialogVisible.value = false
|
||
} catch (error) {
|
||
console.error(error)
|
||
} finally {
|
||
resetPwdSubmitting.value = false
|
||
}
|
||
})
|
||
}
|
||
|
||
const handleAvatarSuccess = (res: any) => {
|
||
temp.file_id = res.file_id
|
||
temp.avatar_url = res.url
|
||
}
|
||
|
||
// ---------------- 角色分配相关 ----------------
|
||
const roleDialogVisible = ref(false)
|
||
const roleLoading = ref(false)
|
||
const roleSubmitting = ref(false)
|
||
const currentEmployeeRow = ref<any>(null)
|
||
const allRoles = ref<any[]>([])
|
||
const filteredRoles = ref<any[]>([])
|
||
const selectedRoleIds = ref<number[]>([])
|
||
const currentEmployeeId = ref<number | null>(null)
|
||
const roleTableRef = ref()
|
||
|
||
const roleFilter = reactive({ keyword: '' })
|
||
|
||
const applyRoleFilter = () => {
|
||
const { keyword } = roleFilter
|
||
filteredRoles.value = allRoles.value.filter(p => {
|
||
const matchKeyword = !keyword || [
|
||
p.role_name, p.description
|
||
].some(val => val && val.toLowerCase().includes(keyword.toLowerCase()))
|
||
return matchKeyword
|
||
})
|
||
nextTick(() => {
|
||
if (roleTableRef.value) {
|
||
filteredRoles.value.forEach(row => {
|
||
roleTableRef.value!.toggleRowSelection(row, selectedRoleIds.value.includes(row.role_id))
|
||
})
|
||
}
|
||
})
|
||
}
|
||
|
||
const handleRoleSelectionChange = (rows: any[]) => {
|
||
const visibleIds = filteredRoles.value.map((r: any) => r.role_id)
|
||
const notVisible = selectedRoleIds.value.filter(id => !visibleIds.includes(id))
|
||
selectedRoleIds.value = [...notVisible, ...rows.map((r: any) => r.role_id)]
|
||
}
|
||
|
||
const isAllSelected = computed(() => {
|
||
if (!filteredRoles.value.length) return false
|
||
return filteredRoles.value.every(p => selectedRoleIds.value.includes(p.role_id))
|
||
})
|
||
|
||
const toggleSelectAll = () => {
|
||
if (isAllSelected.value) {
|
||
const visibleIds = filteredRoles.value.map((r: any) => r.role_id)
|
||
selectedRoleIds.value = selectedRoleIds.value.filter(id => !visibleIds.includes(id))
|
||
roleTableRef.value?.clearSelection()
|
||
} else {
|
||
filteredRoles.value.forEach(row => {
|
||
if (!selectedRoleIds.value.includes(row.role_id)) {
|
||
selectedRoleIds.value.push(row.role_id)
|
||
}
|
||
})
|
||
filteredRoles.value.forEach(row => roleTableRef.value?.toggleRowSelection(row, true))
|
||
}
|
||
}
|
||
|
||
const handleAssignRole = async (row: any) => {
|
||
currentEmployeeId.value = row.employee_id
|
||
// 保存当前行引用,以便提交时获取姓名等字段
|
||
currentEmployeeRow.value = row
|
||
roleFilter.keyword = ''
|
||
selectedRoleIds.value = []
|
||
roleDialogVisible.value = true
|
||
roleLoading.value = true
|
||
try {
|
||
// roleList 返回所有可分配角色(selected 字段表示当前登录工号自身的绑定状态)
|
||
// 目标工号的已绑定角色从 account list 中的 role_ids 字段获取
|
||
const res = await roleList({ includeDisabled: false })
|
||
if (res.data && res.data.roles) {
|
||
allRoles.value = flattenRoles(res.data.roles)
|
||
filteredRoles.value = [...allRoles.value]
|
||
// 使用 row.role_ids 作为预选状态(目标工号的角色)
|
||
selectedRoleIds.value = row.role_ids ? [...row.role_ids] : []
|
||
await nextTick()
|
||
filteredRoles.value.forEach((p: any) => {
|
||
if (selectedRoleIds.value.includes(p.role_id)) {
|
||
roleTableRef.value?.toggleRowSelection(p, true)
|
||
}
|
||
})
|
||
}
|
||
} catch (error) {
|
||
console.error(error)
|
||
} finally {
|
||
roleLoading.value = false
|
||
}
|
||
}
|
||
|
||
const submitRoleUpdate = async () => {
|
||
if (!currentEmployeeId.value) return
|
||
roleSubmitting.value = true
|
||
try {
|
||
// 按接口文档:accountUpdate 的 ids 字段整体替换角色组,用于分配角色
|
||
const row = currentEmployeeRow.value
|
||
await accountUpdate({
|
||
employee_id: currentEmployeeId.value,
|
||
real_name: row?.real_name,
|
||
ids: selectedRoleIds.value,
|
||
})
|
||
ElMessage.success('角色分配成功')
|
||
roleDialogVisible.value = false
|
||
getList()
|
||
} catch (error) {
|
||
console.error(error)
|
||
} finally {
|
||
roleSubmitting.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(() => {
|
||
getList()
|
||
})
|
||
|
||
onUnmounted(() => {
|
||
stopBindWechatPoll()
|
||
})
|
||
|
||
// ---------------- 绑定微信 ----------------
|
||
type QrStatus = 'waiting' | 'pending' | 'scanned' | 'bound' | 'expired'
|
||
|
||
const qrBaseUrl = 'https://ye4s.wms.gztbyc.com/qrcode/build?text='
|
||
const bindLinkTemplate = 'https://ye4s.wms.gztbyc.com/mini/?spm=0.3.${code}.1.4'
|
||
|
||
const bindWechatDialogVisible = ref(false)
|
||
const bindWechatUserId = ref<number | null>(null)
|
||
const bindWechatIsBound = ref(false)
|
||
const bindWechatNickname = ref('')
|
||
const bindWechatLoading = ref(false)
|
||
const bindWechatQrCodeUrl = ref('')
|
||
const bindWechatQrStatus = ref<QrStatus>('waiting')
|
||
|
||
let bindWechatStopPollFlag = false
|
||
let bindWechatPollVersion = 0
|
||
|
||
const getFullQrUrl = (code: string) => {
|
||
const targetUrl = bindLinkTemplate.replace('${code}', code)
|
||
return qrBaseUrl + encodeURIComponent(targetUrl)
|
||
}
|
||
|
||
const processBindWechatData = (data: any, showSuccessMsg = false): boolean => {
|
||
const boundNow = data.status === 'bound' || data.is_bound === true
|
||
if (boundNow) {
|
||
bindWechatIsBound.value = true
|
||
bindWechatNickname.value = data.nickname || ''
|
||
if (showSuccessMsg) {
|
||
ElMessage.success(`微信绑定成功:${data.nickname || '微信用户'}`)
|
||
getList()
|
||
}
|
||
return true
|
||
}
|
||
|
||
if (data.code && bindWechatQrCodeUrl.value !== getFullQrUrl(data.code)) {
|
||
bindWechatQrCodeUrl.value = getFullQrUrl(data.code)
|
||
bindWechatQrStatus.value = 'waiting'
|
||
} else if (data.status) {
|
||
const mapped: QrStatus = data.status === 'pending' ? 'waiting' : (data.status as QrStatus)
|
||
bindWechatQrStatus.value = mapped
|
||
}
|
||
return false
|
||
}
|
||
|
||
const startBindWechatPoll = () => {
|
||
bindWechatStopPollFlag = false
|
||
const version = bindWechatPollVersion
|
||
const poll = async () => {
|
||
if (bindWechatStopPollFlag || bindWechatPollVersion !== version || bindWechatIsBound.value || !bindWechatUserId.value) return
|
||
try {
|
||
const res = await getBindWechatCode({
|
||
user_id: bindWechatUserId.value,
|
||
longPoll: true,
|
||
waitForChange: true,
|
||
waitTimeoutMs: 60000
|
||
}, { timeout: 65000 })
|
||
if (bindWechatStopPollFlag || bindWechatPollVersion !== version || bindWechatIsBound.value) return
|
||
const data = res?.data
|
||
if (data) {
|
||
const bound = processBindWechatData(data, true)
|
||
if (bound) return
|
||
}
|
||
poll()
|
||
} catch {
|
||
if (!bindWechatStopPollFlag && bindWechatPollVersion === version && !bindWechatIsBound.value) {
|
||
setTimeout(poll, 3000)
|
||
}
|
||
}
|
||
}
|
||
poll()
|
||
}
|
||
|
||
const stopBindWechatPoll = () => {
|
||
bindWechatStopPollFlag = true
|
||
bindWechatPollVersion++
|
||
}
|
||
|
||
const initBindWechatQr = async () => {
|
||
if (!bindWechatUserId.value) return
|
||
bindWechatLoading.value = true
|
||
bindWechatQrStatus.value = 'waiting'
|
||
bindWechatQrCodeUrl.value = ''
|
||
try {
|
||
const res = await getBindWechatCode({ user_id: bindWechatUserId.value })
|
||
const data = res?.data
|
||
if (!data) throw new Error('无返回数据')
|
||
const bound = processBindWechatData(data)
|
||
if (!bound) startBindWechatPoll()
|
||
} catch {
|
||
ElMessage.error('获取二维码失败,请重试')
|
||
} finally {
|
||
bindWechatLoading.value = false
|
||
}
|
||
}
|
||
|
||
const refreshBindWechatQr = async () => {
|
||
if (!bindWechatUserId.value) return
|
||
stopBindWechatPoll()
|
||
bindWechatLoading.value = true
|
||
bindWechatQrStatus.value = 'waiting'
|
||
bindWechatQrCodeUrl.value = ''
|
||
try {
|
||
const res = await getBindWechatCode({ user_id: bindWechatUserId.value, forceRefresh: true })
|
||
const data = res?.data
|
||
if (!data) throw new Error('无返回数据')
|
||
const bound = processBindWechatData(data)
|
||
if (!bound) startBindWechatPoll()
|
||
} catch {
|
||
ElMessage.error('获取二维码失败,请重试')
|
||
} finally {
|
||
bindWechatLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleBindWechat = async (row: any) => {
|
||
stopBindWechatPoll()
|
||
bindWechatUserId.value = row.account_id
|
||
bindWechatIsBound.value = false
|
||
bindWechatNickname.value = ''
|
||
bindWechatQrCodeUrl.value = ''
|
||
bindWechatQrStatus.value = 'waiting'
|
||
bindWechatDialogVisible.value = true
|
||
await initBindWechatQr()
|
||
}
|
||
|
||
const handleBindWechatDialogClose = () => {
|
||
stopBindWechatPoll()
|
||
bindWechatUserId.value = null
|
||
}
|
||
|
||
const handleUnbindWechat = (row: any) => {
|
||
ElMessageBox.confirm(`确定要解除工号「${row.real_name}」的微信绑定吗?`, '提示', {
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning'
|
||
}).then(async () => {
|
||
try {
|
||
await unbindWechat({ user_id: row.account_id })
|
||
ElMessage.success('解绑成功')
|
||
getList()
|
||
} catch (error) {
|
||
console.error(error)
|
||
}
|
||
}).catch(() => {})
|
||
}
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.account-container {
|
||
height: calc(100vh - 130px);
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.form-section {
|
||
margin-bottom: 20px;
|
||
padding: 24px;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 8px;
|
||
background-color: var(--el-fill-color-blank);
|
||
box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
|
||
height: 100%;
|
||
|
||
&.no-shadow {
|
||
box-shadow: none;
|
||
border: none;
|
||
padding: 20px 0;
|
||
}
|
||
|
||
.section-title {
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: var(--el-text-color-primary);
|
||
margin-bottom: 24px;
|
||
padding-bottom: 12px;
|
||
border-bottom: 1px solid var(--el-border-color-lighter);
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
|
||
.el-icon {
|
||
color: var(--el-color-primary);
|
||
}
|
||
}
|
||
}
|
||
|
||
.avatar-upload-wrapper {
|
||
background: #fff;
|
||
min-height: 400px;
|
||
border-radius: 4px;
|
||
overflow: hidden;
|
||
border: 1px dashed var(--el-border-color);
|
||
margin-bottom: 12px;
|
||
|
||
&.full-width {
|
||
min-height: 500px;
|
||
}
|
||
}
|
||
|
||
.upload-tip {
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
text-align: center;
|
||
}
|
||
|
||
.custom-form {
|
||
:deep(.el-form-item__label) {
|
||
font-weight: 600;
|
||
padding-bottom: 4px;
|
||
}
|
||
}
|
||
|
||
:deep(.account-dialog) {
|
||
.el-dialog__body {
|
||
padding: 24px;
|
||
max-height: 80vh;
|
||
overflow-y: auto;
|
||
background-color: #f8fafc;
|
||
}
|
||
}
|
||
|
||
.bind-wechat-body {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
padding: 10px 0;
|
||
|
||
.bound-info {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
font-size: 16px;
|
||
color: var(--el-color-success);
|
||
padding: 30px 0;
|
||
|
||
.bound-icon {
|
||
font-size: 28px;
|
||
}
|
||
}
|
||
|
||
.qrcode-wrapper {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
|
||
.qrcode-img {
|
||
width: 220px;
|
||
height: 220px;
|
||
border: 1px solid var(--el-border-color);
|
||
border-radius: 4px;
|
||
}
|
||
|
||
.qrcode-tip {
|
||
margin-top: 15px;
|
||
font-size: 13px;
|
||
color: var(--el-text-color-secondary);
|
||
text-align: center;
|
||
}
|
||
|
||
.status-overlay {
|
||
width: 220px;
|
||
height: 220px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
gap: 10px;
|
||
font-size: 13px;
|
||
border: 1px solid var(--el-border-color);
|
||
border-radius: 4px;
|
||
|
||
.el-icon { font-size: 36px; }
|
||
}
|
||
|
||
.loading-overlay {
|
||
background: var(--el-fill-color-light);
|
||
color: var(--el-text-color-secondary);
|
||
.el-icon { color: var(--el-color-primary); }
|
||
}
|
||
|
||
.expired-overlay {
|
||
background: var(--el-fill-color);
|
||
color: var(--el-text-color-secondary);
|
||
cursor: pointer;
|
||
transition: background 0.2s;
|
||
&:hover { background: var(--el-fill-color-dark); }
|
||
.el-icon { color: var(--el-color-warning); }
|
||
}
|
||
|
||
.scanned-overlay {
|
||
background: var(--el-color-success-light-9);
|
||
color: var(--el-color-success);
|
||
.el-icon { color: var(--el-color-success); }
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
</style> |