long
2021-06-11 92f93fb7396cd7981276d401eb4345e6931e1fb7
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<template>
  <div class="app-container">
    <!-- 搜索区 ↓↓↓↓↓↓↓↓↓↓ -->
    <el-form v-show="showSearch" ref="searchForm" :inline="true">
      <el-form-item label="管理员搜索">
        <el-input
          v-model="keyWord"
          placeholder="请输入管理员名称/账号"
          clearable
          size="small"
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="reGetList"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="reGetList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetHandle">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 搜索区 ↑↑↑↑↑↑↑↑↑↑ -->
 
    <!-- 操作区 ↓↓↓↓↓↓↓↓↓↓ -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="getAuthValueFN('sys_admin_add')"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="showAddAdminDialog"
        >新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>
    <!-- 操作区 ↑↑↑↑↑↑↑↑↑↑ -->
 
    <el-table :data="list">
      <el-table-column type="index" label="序号" align="center" width="60" />
      <el-table-column label="名称" prop="name" align="center" min-width="120" />
      <el-table-column label="账号" prop="account" align="center" min-width="120" />
      <el-table-column label="账号类型" prop="account" align="center" min-width="120">
        <template slot-scope="scope">
          <span v-if="accountTypeOptions[scope.row.type]">{{ accountTypeOptions[scope.row.type].name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色" prop="roleName" align="center" min-width="120" />
      <!-- <el-table-column label="最近登录时间" prop="loginTime" align="center" min-width="180" /> -->
      <el-table-column label="状态" align="center" min-width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isUse"
            :active-value="1"
            :inactive-value="0"
            :disabled="!getAuthValueFN('sys_admin_edit')"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100">
        <template slot-scope="scope">
          <el-button
            v-if="getAuthValueFN('sys_admin_edit')"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showEditAdminDialog(scope.row)"
          >编辑</el-button>
          <el-button
            v-if="getAuthValueFN('sys_admin_del')"
            size="mini"
            type="text warn"
            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="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />
 
    <!-- 新增&编辑 -->
    <el-dialog v-el-drag-dialog :title="adminDialogData.type==='add'?'新增管理员':'编辑管理员'" width="500px" :visible.sync="adminDialogVisible" append-to-body :close-on-click-modal="false">
      <el-form ref="adminDialog" :model="adminDialogData" label-width="80px" :rules="rules" size="small">
        <el-form-item label="名称" prop="name">
          <el-input v-model="adminDialogData.name" placeholder="请输入名称" maxlength="50" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="adminDialogData.account" placeholder="请输入帐号" maxlength="20" :disabled="adminDialogData.type!='add'" />
        </el-form-item>
        <el-form-item label="密码" :prop="adminDialogData.type==='add'||adminDialogData.password||adminDialogData.passwordSure?'password':'none'">
          <el-input v-model="adminDialogData.password" type="password" placeholder="请输入密码" maxlength="20" />
        </el-form-item>
        <el-form-item label="确认密码" :prop="adminDialogData.type==='add'||adminDialogData.password||adminDialogData.passwordSure?'passwordSure':'none'">
          <el-input v-model="adminDialogData.passwordSure" type="password" placeholder="请输入确认密码" maxlength="20" />
        </el-form-item>
        <!-- 账号类型 -->
        <el-form-item label="账号类型" prop="accountType">
          <el-select
            v-model="adminDialogData.accountType"
            clearable
            placeholder="账号类型"
            style="width: 100%"
          >
            <el-option v-for="item in accountTypeOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <!-- 角色 -->
        <el-form-item label="角色" prop="sysRoleId">
          <el-select
            v-model="adminDialogData.sysRoleId"
            clearable
            placeholder="角色"
            style="width: 100%"
          >
            <el-option v-for="item in roleArr" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
 
        <el-form-item label="是否启用" prop="isUse">
          <el-switch
            v-model="adminDialogData.isUse"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
 
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideAdminDialog">取消</el-button>
        <el-button type="primary" @click="submitAdmin(adminDialogData.type)">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
export default {
  name: 'Admin',
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.adminDialogData.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      showSearch: true, // 是否显示搜索区
      keyWord: '',
 
      list: [],
 
      // 角色列表
      roleArr: [],
      // 账号类型
      accountTypeOptions: [
        { name: '普通管理员', id: 1 },
        { name: '超级管理员', id: 0 }
      ],
 
      adminDialogVisible: false,
      adminDialogData: {},
 
      // 分页 ↓↓↓↓↓↓↓↓↓↓
      total: 0,
      pageNum: 1,
      pageSize: 20,
      // 分页 ↑↑↑↑↑↑↑↑↑↑
 
      // 表单校验
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'change' }
        ],
        account: [
          { required: true, message: '账号不能为空', trigger: 'change' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'change' }
        ],
        passwordSure: [
          { required: true, message: '确认密码不能为空', trigger: 'change' },
          { required: true, validator: equalToPassword, trigger: 'change' }
        ],
        sysRoleId: [
          { required: true, message: '角色不能为空', trigger: 'change' }
        ],
        accountType: [
          { required: true, message: '账号类型不能为空', trigger: 'change' }
        ],
        isUse: [
          { required: true, message: '是否启用不能为空', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      this.getList()
      this.getRoleList()
    },
 
    // 获取角色列表
    getRoleList() {
      this.postFN({
        url: 'admin/role/list',
        params: {
          pageNum: 1,
          pageSize: 9999
        },
        mockData: {
          code: 100,
          msg: '',
          data: {
            list: [{
              id: 'xxx',
              name: '野马发'
            }]
          }
        }
      }, (inf) => {
        this.roleArr = inf.list || []
      })
    },
 
    // 获取列表
    getList() {
      var { pageNum, pageSize, keyWord } = this
      this.postFN({
        url: 'admin/list',
        params: {
          pageNum: pageNum,
          pageSize: pageSize,
          keyWord: keyWord
        },
        mockData: {
          code: 100,
          msg: '',
          data: {
            list: [{
              id: 'xxx',
              account: 'xasdasd2123123',
              name: '野马发',
              roleName: '超级管理员',
              tel: '13760749294',
              loginTime: '2020-08-08 12:10:10',
              isUse: 1
            }],
            total: 100
          }
        }
      }, (inf) => {
        this.list = inf.list || []
        this.total = inf.total
      })
    },
    // 重新获取列表
    reGetList() {
      this.pageNum = 1
      this.getList()
    },
    // 重置
    resetHandle() {
      this.keyWord = ''
      this.reGetList()
    },
    // 修改状态
    handleStatusChange(item) {
      const text = item.isUse === 1 ? '启用' : '停用'
      this.$confirm('确认要' + text + '"' + item.roleName + '"角色吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.postFN({
          url: 'admin/editUse',
          params: {
            id: item.id
          },
          mockData: {
            code: 100,
            msg: '',
            data: {}
          }
        }, () => {
          this.$messageSuc(text + '成功')
        })
      }).catch(() => {
        item.isUse = item.isUse === 1 ? 0 : 1
      })
    },
    // 删除
    handleDelete(item) {
      // 打开二次确认弹窗
      this.$confirm('是否确认删除该帐号?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确定回调
        this.postFN({
          url: 'admin/delete/one',
          params: {
            id: item.id
          },
          mockData: {
            code: 100,
            msg: '',
            data: {}
          }
        }, () => {
          this.getList()
          this.$messageSuc('删除成功')
        })
      }).catch(() => {})
    },
 
    // 打开新增分类弹窗
    showAddAdminDialog() {
      var adminDialogData = {
        type: 'add',
        name: '',
        account: '',
        password: '',
        passwordSure: '',
        sysRoleId: '',
        isUse: 1,
        accountType: 1
      }
      this.adminDialogVisible = true
      this.$nextTick(() => {
        this.adminDialogData = adminDialogData
        this.$refs['adminDialog'].resetFields()
      })
    },
    // 打开编辑分类弹框
    showEditAdminDialog(item) {
      var adminDialogData = {
        type: 'edit',
        name: item.name,
        account: item.account,
        password: '',
        passwordSure: '',
        sysRoleId: item.roId,
        id: item.id,
        isUse: item.isUse,
        accountType: item.type
      }
      this.adminDialogVisible = true
      this.$nextTick(() => {
        this.adminDialogData = adminDialogData
        this.$refs['adminDialog'].resetFields()
      })
    },
    // 关闭编辑弹窗
    hideAdminDialog() {
      this.adminDialogData = {}
      this.adminDialogVisible = false
    },
    // 提交新增&编辑
    submitAdmin(type) {
      this.$refs['adminDialog'].validate(valid => {
        if (valid) {
          var { adminDialogData } = this
          var params = {
            name: adminDialogData.name,
            account: adminDialogData.account,
            sysAdRoleId: adminDialogData.sysRoleId,
            isUse: adminDialogData.isUse,
            type: adminDialogData.accountType
          }
          if (adminDialogData.password) params.password = adminDialogData.password
          var isAdd = adminDialogData.type === 'add'
          var url = isAdd ? 'admin/add' : 'admin/update'
 
          !isAdd && (params.id = adminDialogData.id)
 
          this.postFN({
            url: url,
            params: params,
            mockData: {
              code: 100,
              msg: '',
              data: {}
            }
          }, () => {
            this.$messageSuc('保存成功')
            this.hideAdminDialog()
            isAdd ? this.reGetList() : this.getList()
          })
        }
      })
    }
 
  }
}
</script>
 
<style lang="scss" scoped>
 
</style>