liujie
2026-09-11 4d15e6b553c448e2e07527e9c06aa4b5f9b099cf
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<template>
  <main class="visitor-page visitor-list-page">
    <section class="visitor-card">
      <header class="visitor-header">
        <h1>平台管理员</h1>
      </header>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" class="visitor-query">
      <el-form-item label="姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <el-select v-model="queryParams.roleId" placeholder="请选择角色" clearable>
          <el-option v-for="role in roles" :key="role.roleId" :label="role.roleName" :value="role.roleId" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option label="启用" value="0" />
          <el-option label="禁用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
 
    <el-row :gutter="10" class="mb8">
      <el-col v-hasPermi="['visitor:platform-admin:add']" :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col v-hasPermi="['visitor:platform-admin:edit']" :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate()">编辑</el-button>
      </el-col>
      <el-col v-hasPermi="['visitor:platform-admin:remove']" :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete()">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>
 
    <el-table v-loading="loading" class="visitor-table" :data="adminList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="姓名" prop="nickName" min-width="120" />
      <el-table-column label="账号" prop="userName" min-width="140" show-overflow-tooltip />
      <el-table-column label="手机号" prop="phonenumber" min-width="130" />
      <el-table-column label="角色" prop="roleName" min-width="140" show-overflow-tooltip />
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" :disabled="isSuperAdminUser(scope.row) || !$auth.hasPermi('visitor:platform-admin:status')" @change="handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="170">
        <template slot-scope="scope">{{ parseTime(scope.row.createTime) }}</template>
      </el-table-column>
      <el-table-column v-if="$auth.hasPermi('visitor:platform-admin:edit') || $auth.hasPermi('visitor:platform-admin:reset-password') || $auth.hasPermi('visitor:platform-admin:remove')" label="操作" align="center" width="230" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="!isSuperAdminUser(scope.row)" v-hasPermi="['visitor:platform-admin:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="!isMiddlePlatformMode && !isSuperAdminUser(scope.row)" v-hasPermi="['visitor:platform-admin:reset-password']" size="mini" type="text" icon="el-icon-key" @click="handleResetPassword(scope.row)">重置密码</el-button>
          <el-button v-if="!isSuperAdminUser(scope.row)" v-hasPermi="['visitor:platform-admin:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </section>
 
    <el-dialog :title="title" :visible.sync="open" width="620px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入账号" :disabled="Boolean(form.userId)" />
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="form.roleId" placeholder="请选择角色">
            <el-option v-for="role in roles" :key="role.roleId" :label="role.roleName" :value="role.roleId" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="!form.userId" label="密码" prop="password">
          <el-input v-model="form.password" type="password" show-password placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="open = false">取消</el-button>
      </div>
    </el-dialog>
 
    <el-dialog v-if="!isMiddlePlatformMode" title="重置密码" :visible.sync="passwordOpen" width="480px" append-to-body>
      <el-form ref="passwordForm" :model="passwordForm" :rules="passwordRules" label-width="100px">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="passwordForm.password" type="password" show-password placeholder="请输入新密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPassword">确定</el-button>
        <el-button @click="passwordOpen = false">取消</el-button>
      </div>
    </el-dialog>
  </main>
</template>
 
<script>
import { listPlatformAdmin, getPlatformAdmin, addPlatformAdmin, updatePlatformAdmin, changePlatformAdminStatus, resetPlatformAdminPassword, delPlatformAdmin, roleOptions } from '@/api/visitor-admin/account'
import { getResponseData, getPageRows, getPageTotal, toIdArray } from '@/utils/visitor-admin'
 
export default {
  name: 'VisitorPlatformAdmin',
  // 目的:初始化管理员列表、查询条件、编辑表单和重置密码表单状态。
  data() {
    const strongPasswordMessage = '密码至少12位,且包含大小写字母和特殊字符'
    const validateStrongPassword = (rule, value, callback) => {
      if (!value || (value.length >= 12 && /[A-Z]/.test(value) && /[a-z]/.test(value) && /[^A-Za-z0-9]/.test(value))) {
        callback()
        return
      }
      callback(new Error(strongPasswordMessage))
    }
    return {
      loading: false,
      showSearch: true,
      open: false,
      passwordOpen: false,
      title: '',
      ids: [],
      single: true,
      multiple: true,
      total: 0,
      roles: [],
      adminList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: '',
        phonenumber: '',
        roleId: undefined,
        status: ''
      },
      form: {},
      passwordForm: {},
      rules: {
        nickName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        userName: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        phonenumber: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        roleId: [{ required: true, message: '请选择角色', trigger: 'change' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validateStrongPassword, trigger: 'blur' }
        ]
      },
      passwordRules: {
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validateStrongPassword, trigger: 'blur' }
        ]
      }
    }
  },
  // 目的:页面初始化时加载角色下拉和管理员列表。
  created() {
    this.getRoleOptions()
    this.getList()
  },
  methods: {
    // 目的:加载角色下拉选项,供管理员新增和编辑使用。
    getRoleOptions() {
      roleOptions().then(res => {
        this.roles = getResponseData(res) || []
      })
    },
    // 目的:按当前查询条件加载平台管理员分页数据。
    getList() {
      this.loading = true
      listPlatformAdmin(this.queryParams).then(res => {
        this.adminList = getPageRows(res)
        this.total = getPageTotal(res)
      }).finally(() => {
        this.loading = false
      })
    },
    // 目的:从第一页重新执行管理员查询。
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 目的:清空筛选条件并刷新管理员列表。
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 目的:同步表格勾选状态,控制批量操作按钮可用性。
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1 || this.isSuperAdminUser(selection[0])
      this.multiple = !selection.length || selection.some(item => this.isSuperAdminUser(item))
    },
    isSuperAdminUser(row) {
      return row && (row.userId === 1 || row.roleId === 1)
    },
    // 目的:打开新增管理员弹窗并初始化默认状态。
    handleAdd() {
      this.title = '新增管理员'
      this.form = { status: '0' }
      this.open = true
    },
    // 目的:加载管理员详情并打开编辑弹窗。
    handleUpdate(row) {
      if (this.isSuperAdminUser(row)) {
        return
      }
      const userId = row ? row.userId : this.ids[0]
      getPlatformAdmin(userId).then(res => {
        this.title = '编辑管理员'
        this.form = getResponseData(res) || {}
        this.open = true
      })
    },
    // 目的:校验并提交管理员新增或编辑表单。
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        const request = this.form.userId ? updatePlatformAdmin : addPlatformAdmin
        request(this.form).then(() => {
          this.$modal.msgSuccess('保存成功')
          this.open = false
          this.getList()
        })
      })
    },
    // 目的:切换管理员启停状态,失败时恢复原状态。
    handleStatusChange(row) {
      if (this.isSuperAdminUser(row)) {
        return
      }
      changePlatformAdminStatus(row.userId, row.status).then(() => {
        this.$modal.msgSuccess('状态已更新')
      }).catch(() => {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    // 目的:打开重置密码弹窗;中台模式下按业务要求禁止入口和调用。
    handleResetPassword(row) {
      if (this.isSuperAdminUser(row)) {
        return
      }
      // 账号管理是本地白名单,但中台模式业务要求不显示、不允许调用重置密码。
      if (this.isMiddlePlatformMode) {
        this.passwordOpen = false
        this.denyMiddlePlatformWrite('集群版对接中台时,不支持重置密码')
        return
      }
      this.passwordForm = { userId: row.userId, password: '' }
      this.passwordOpen = true
    },
    // 目的:提交管理员重置密码请求,并在中台模式下做二次兜底拦截。
    submitPassword() {
      // 防止通过控制台或旧弹窗状态绕过按钮隐藏直接提交重置密码。
      if (this.isMiddlePlatformMode) {
        this.passwordOpen = false
        this.denyMiddlePlatformWrite('集群版对接中台时,不支持重置密码')
        return
      }
      this.$refs.passwordForm.validate(valid => {
        if (!valid) {
          return
        }
        resetPlatformAdminPassword(this.passwordForm.userId, this.passwordForm.password).then(() => {
          this.$modal.msgSuccess('密码已重置')
          this.passwordOpen = false
        })
      })
    },
    // 目的:删除选中的管理员账号并刷新列表。
    handleDelete(row) {
      if (this.isSuperAdminUser(row)) {
        return
      }
      const ids = row ? [row.userId] : toIdArray(this.ids)
      this.$modal.confirm('是否确认删除选中的管理员?').then(() => delPlatformAdmin(ids)).then(() => {
        this.$modal.msgSuccess('删除成功')
        this.getList()
      })
    }
  }
}
</script>