SubstationList.vue
5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<template>
<div>
<a-card title="分站管理" :bordered="false">
<template #extra>
<a-space>
<a-input-search v-model:value="keyword" placeholder="搜索账号/昵称/手机" @search="loadList" style="width:220px" />
<a-button type="primary" @click="openAdd">新增分站</a-button>
</a-space>
</template>
<a-table :dataSource="list" :columns="columns" :loading="loading" rowKey="id" :pagination="false">
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'status'">
<a-tag :color="record.userStatus === 1 ? 'green' : 'red'">
{{ record.userStatus === 1 ? '正常' : '禁用' }}
</a-tag>
</template>
<template v-if="column.key === 'action'">
<a-space>
<a @click="openEdit(record)">编辑</a>
<a @click="openChangePwd(record)">改密码</a>
<a-popconfirm
:title="record.userStatus === 1 ? '确认禁用?' : '确认启用?'"
@confirm="toggleBan(record)">
<a :style="record.userStatus === 1 ? 'color:red' : ''">
{{ record.userStatus === 1 ? '禁用' : '启用' }}
</a>
</a-popconfirm>
<a-popconfirm title="确认删除?" @confirm="handleDel(record.id)">
<a style="color:red">删除</a>
</a-popconfirm>
</a-space>
</template>
</template>
</a-table>
</a-card>
<a-modal v-model:open="modalVisible" :title="editingId ? '编辑分站' : '新增分站'"
@ok="handleSave" :confirmLoading="saving">
<a-form :model="form" layout="vertical">
<a-form-item label="管理城市">
<a-select v-model:value="form.cityId" placeholder="选择城市">
<a-select-option v-for="c in cityList" :key="c.id" :value="c.id">{{ c.name }}</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="登录账号">
<a-input v-model:value="form.userLogin" :disabled="!!editingId" />
</a-form-item>
<a-form-item label="昵称">
<a-input v-model:value="form.userNickname" />
</a-form-item>
<a-form-item label="手机号">
<a-input v-model:value="form.mobile" />
</a-form-item>
<a-form-item :label="editingId ? '新密码(不填不修改)' : '密码'">
<a-input-password v-model:value="form.userPass" />
</a-form-item>
</a-form>
</a-modal>
<!-- 改密码弹窗 -->
<a-modal v-model:open="pwdVisible" title="修改密码" @ok="handleChangePwd" :confirmLoading="pwdSaving">
<a-form layout="vertical">
<a-form-item label="原密码">
<a-input-password v-model:value="pwdForm.oldPassword" />
</a-form-item>
<a-form-item label="新密码">
<a-input-password v-model:value="pwdForm.newPassword" />
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { substationApi, cityApi } from '@/api'
const loading = ref(false)
const saving = ref(false)
const list = ref<any[]>([])
const cityList = ref<any[]>([])
const keyword = ref('')
const modalVisible = ref(false)
const editingId = ref<number | null>(null)
const form = reactive({ cityId: undefined, userLogin: '', userNickname: '', mobile: '', userPass: '' })
const columns = [
{ title: 'ID', dataIndex: 'id', width: 80 },
{ title: '账号', dataIndex: 'userLogin' },
{ title: '昵称', dataIndex: 'userNickname' },
{ title: '手机', dataIndex: 'mobile' },
{ title: '城市ID', dataIndex: 'cityId' },
{ title: '状态', key: 'status' },
{ title: '操作', key: 'action' },
]
async function loadList() {
loading.value = true
try {
const res: any = await substationApi.list(keyword.value)
list.value = res.data
} finally { loading.value = false }
}
async function loadCities() {
const res: any = await cityApi.openList()
cityList.value = res.data
}
function openAdd() {
editingId.value = null
Object.assign(form, { cityId: undefined, userLogin: '', userNickname: '', mobile: '', userPass: '' })
modalVisible.value = true
}
function openEdit(record: any) {
editingId.value = record.id
Object.assign(form, { cityId: record.cityId, userLogin: record.userLogin, userNickname: record.userNickname, mobile: record.mobile, userPass: '' })
modalVisible.value = true
}
async function handleSave() {
saving.value = true
try {
if (editingId.value) {
await substationApi.edit({ ...form, id: editingId.value })
} else {
await substationApi.add(form)
}
message.success('保存成功')
modalVisible.value = false
loadList()
} finally { saving.value = false }
}
async function toggleBan(record: any) {
if (record.userStatus === 1) {
await substationApi.ban(record.id)
} else {
await substationApi.cancelBan(record.id)
}
message.success('操作成功')
loadList()
}
async function handleDel(id: number) {
await substationApi.del(id)
message.success('删除成功')
loadList()
}
// 改密码
const pwdVisible = ref(false)
const pwdSaving = ref(false)
const pwdForm = reactive({ oldPassword: '', newPassword: '' })
const pwdTargetId = ref(0)
function openChangePwd(record: any) {
pwdTargetId.value = record.id
Object.assign(pwdForm, { oldPassword: '', newPassword: '' })
pwdVisible.value = true
}
async function handleChangePwd() {
if (!pwdForm.oldPassword || !pwdForm.newPassword) {
message.error('请填写完整密码')
return
}
pwdSaving.value = true
try {
await substationApi.changePassword(pwdForm)
message.success('密码修改成功')
pwdVisible.value = false
} finally { pwdSaving.value = false }
}
onMounted(() => { loadList(); loadCities() })
</script>