liweilong
2020-12-09 efd7c8fea38ab641db8581fb8c9d910640cfc8fb
提交 | 用户 | age
2a61f6 1 <template>
L 2   <div class="app-container">
3     <!-- 搜索区 ↓↓↓↓↓↓↓↓↓↓ -->
4     <el-form v-show="showSearch" ref="searchForm" :inline="true">
5       <el-form-item label="管理员搜索">
6         <el-input
7           v-model="keyWord"
8           placeholder="请输入管理员名称/账号"
9           clearable
10           size="small"
11           style="width: 240px"
12           @keyup.enter.native="reGetList"
13         />
14       </el-form-item>
15       <el-form-item>
16         <el-button type="cyan" icon="el-icon-search" size="mini" @click="reGetList">搜索</el-button>
17         <el-button icon="el-icon-refresh" size="mini" @click="resetHandle">重置</el-button>
18       </el-form-item>
19     </el-form>
20     <!-- 搜索区 ↑↑↑↑↑↑↑↑↑↑ -->
21
22     <!-- 操作区 ↓↓↓↓↓↓↓↓↓↓ -->
23     <el-row :gutter="10" class="mb8">
24       <el-col :span="1.5">
25         <el-button
26           v-if="getAuthValueFN('sys_admin_add')"
27           type="primary"
28           icon="el-icon-plus"
29           size="mini"
30           @click="showAddAdminDialog"
31         >新增</el-button>
32       </el-col>
33       <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
34     </el-row>
35     <!-- 操作区 ↑↑↑↑↑↑↑↑↑↑ -->
36
37     <el-table :data="list">
38       <el-table-column type="index" label="序号" align="center" width="60" />
9f32bd 39       <el-table-column label="名称" prop="name" align="center" min-width="120" />
2a61f6 40       <el-table-column label="账号" prop="account" align="center" min-width="120" />
L 41       <el-table-column label="角色" prop="roleName" align="center" min-width="120" />
42       <!-- <el-table-column label="最近登录时间" prop="loginTime" align="center" min-width="180" /> -->
43       <el-table-column label="状态" align="center" min-width="100">
44         <template slot-scope="scope">
45           <el-switch
46             v-model="scope.row.isUse"
47             :active-value="1"
48             :inactive-value="0"
49             :disabled="!getAuthValueFN('sys_admin_edit')"
50             @change="handleStatusChange(scope.row)"
51           />
52         </template>
53       </el-table-column>
54       <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100">
55         <template slot-scope="scope">
56           <el-button
57             v-if="getAuthValueFN('sys_admin_edit')"
58             size="mini"
59             type="text"
60             icon="el-icon-edit"
61             @click="showEditAdminDialog(scope.row)"
62           >编辑</el-button>
63           <el-button
64             v-if="getAuthValueFN('sys_admin_del')"
65             size="mini"
66             type="text warn"
67             icon="el-icon-delete"
68             @click="handleDelete(scope.row)"
69           >删除</el-button>
70         </template>
71       </el-table-column>
72     </el-table>
73
74     <pagination
75       v-show="total>0"
76       :total="total"
77       :page.sync="pageNum"
78       :limit.sync="pageSize"
79       @pagination="getList"
80     />
81
82     <!-- 新增&编辑 -->
83     <el-dialog :title="adminDialogData.type=='add'?'新增管理员':'编辑管理员'" width="500px" :visible.sync="isShowadminDialog" append-to-body>
ad3cc5 84       <el-form ref="adminDialog" :model="adminDialogData" label-width="80px" :rules="rules" size="small">
2a61f6 85         <el-form-item label="名称" prop="name">
L 86           <el-input v-model="adminDialogData.name" placeholder="请输入名称" />
87         </el-form-item>
88         <el-form-item label="账号" prop="account">
89           <el-input v-model="adminDialogData.account" placeholder="请输入帐号" :disabled="adminDialogData.type!='add'" />
90         </el-form-item>
91         <el-form-item label="密码" prop="password">
92           <el-input v-model="adminDialogData.password" type="password" placeholder="请输入密码" />
93         </el-form-item>
94         <el-form-item label="确认密码" prop="passwordSure">
95           <el-input v-model="adminDialogData.passwordSure" type="password" placeholder="请输入确认密码" />
96         </el-form-item>
97         <!-- 角色 -->
98         <!-- 选择框 -->
99         <el-form-item label="角色" prop="sysRoleId">
100           <el-select
101             v-model="adminDialogData.sysRoleId"
102             clearable
103             placeholder="角色"
9f32bd 104             style="width: 100%"
2a61f6 105           >
L 106             <el-option v-for="item in roleArr" :key="item.id" :label="item.name" :value="item.id" />
107           </el-select>
9f32bd 108         </el-form-item>
L 109
110         <el-form-item label="是否启用" prop="sysRoleId">
111           <el-switch
112             v-model="adminDialogData.isUse"
113             :active-value="1"
114             :inactive-value="0"
115           />
2a61f6 116         </el-form-item>
L 117
118       </el-form>
119       <div slot="footer" class="dialog-footer">
120         <el-button @click="hideAdminDialog">取消</el-button>
121         <el-button type="primary" @click="submitAdmin(adminDialogData.type)">确定</el-button>
122       </div>
123     </el-dialog>
124   </div>
125 </template>
126
127 <script>
128
129 export default {
130   name: 'Admin',
131   data() {
132     const equalToPassword = (rule, value, callback) => {
133       if (this.adminDialogData.password !== value) {
134         callback(new Error('两次输入的密码不一致'))
135       } else {
136         callback()
137       }
138     }
139     return {
140       showSearch: true, // 是否显示搜索区
141       keyWord: '',
142
143       list: [],
144
145       // 角色列表
146       roleArr: [],
147
148       isShowadminDialog: false,
149       adminDialogData: {},
150
151       // 分页 ↓↓↓↓↓↓↓↓↓↓
152       total: 0,
153       pageNum: 1,
154       pageSize: 20,
155       // 分页 ↑↑↑↑↑↑↑↑↑↑
156
157       // 表单校验
158       rules: {
159         name: [
160           { required: true, message: '名称不能为空', trigger: 'change' }
161         ],
162         account: [
163           { required: true, message: '账号不能为空', trigger: 'change' }
164         ],
165         password: [
166           { required: true, message: '密码不能为空', trigger: 'change' }
167         ],
168         passwordSure: [
169           { required: true, message: '确认密码不能为空', trigger: 'change' },
170           { required: true, validator: equalToPassword, trigger: 'change' }
171         ],
172         sysRoleId: [
173           { required: true, message: '角色不能为空', trigger: 'change' }
174         ]
175       }
176     }
177   },
178   mounted() {
179     this.init()
180   },
181   methods: {
182     // 初始化
183     init() {
184       this.getList()
185       this.getRoleList()
186     },
187
188     // 获取角色列表
189     getRoleList() {
190       this.postFN({
191         url: 'admin/roleList',
192         mockData: {
193           code: 100,
194           msg: '',
195           data: [{
196             id: 'xxx',
197             name: '野马发'
198           }]
199         }
200       }, (inf) => {
201         this.roleArr = inf || []
202       })
203     },
204
205     // 获取列表
206     getList() {
207       var { pageNum, pageSize, keyWord } = this
208       this.postFN({
209         url: 'admin/list',
210         params: {
211           pageNum: pageNum,
efd7c8 212           pageSize: pageSize,
2a61f6 213           keyWord: keyWord
L 214         },
215         mockData: {
216           code: 100,
217           msg: '',
218           data: {
219             list: [{
220               id: 'xxx',
221               account: 'xasdasd2123123',
222               name: '野马发',
223               roleName: '超级管理员',
224               tel: '13760749294',
225               loginTime: '2020-08-08 12:10:10',
226               isUse: 1
227             }],
228             total: 100
229           }
230         }
231       }, (inf) => {
232         this.list = inf.list || []
233         this.total = inf.total
234       })
235     },
236     // 重新获取列表
237     reGetList() {
238       this.pageNum = 1
239       this.getList()
240     },
241     // 重置
242     resetHandle() {
243       this.keyWord = ''
244       this.reGetList()
245     },
246     // 修改状态
247     handleStatusChange(item) {
248       const text = item.isUse === 1 ? '启用' : '停用'
249       this.$confirm('确认要' + text + '"' + item.roleName + '"角色吗?', '提示', {
250         confirmButtonText: '确定',
251         cancelButtonText: '取消',
252         type: 'warning'
253       }).then(() => {
254         this.postFN({
255           url: 'admin/editUse',
256           params: {
257             id: item.id
258           },
259           mockData: {
260             code: 100,
261             msg: '',
262             data: {}
263           }
264         }, () => {
265           this.$messageSuc(text + '成功')
266         })
267       }).catch(() => {
268         item.isUse = item.isUse === 1 ? 0 : 1
269       })
270     },
271     // 删除
272     handleDelete(item) {
273       // 打开二次确认弹窗
274       this.$confirm('是否确认删除该帐号?', '提示', {
275         confirmButtonText: '确定',
276         cancelButtonText: '取消',
277         type: 'warning'
278       }).then(() => {
279         // 确定回调
280         this.postFN({
281           url: 'admin/delete/one',
282           params: {
283             id: item.id
284           },
285           mockData: {
286             code: 100,
287             msg: '',
288             data: {}
289           }
290         }, () => {
291           this.getList()
292           this.$messageSuc('删除成功')
293         })
294       }).catch(() => {})
295     },
296
297     // 打开新增分类弹窗
298     showAddAdminDialog() {
299       var adminDialogData = {
300         type: 'add',
301         name: '',
302         account: '',
303         password: '',
304         passwordSure: '',
9f32bd 305         sysRoleId: '',
L 306         isUse: 1
2a61f6 307       }
L 308       this.adminDialogData = adminDialogData
309       this.isShowadminDialog = true
310     },
311     // 打开编辑分类弹框
312     showEditAdminDialog(item) {
313       var adminDialogData = {
314         type: 'edit',
315         name: item.name,
316         account: item.account,
317         password: '',
318         passwordSure: '',
319         sysRoleId: item.roId,
9f32bd 320         id: item.id,
L 321         isUse: item.isUse
2a61f6 322       }
L 323       this.adminDialogData = adminDialogData
324       this.isShowadminDialog = true
325     },
326     // 关闭编辑弹窗
327     hideAdminDialog() {
328       this.adminDialogData = {}
329       this.isShowadminDialog = false
330     },
331     // 提交新增&编辑
332     submitAdmin(type) {
333       this.$refs['adminDialog'].validate(valid => {
334         if (valid) {
335           var { adminDialogData } = this
336           var params = {
337             name: adminDialogData.name,
338             account: adminDialogData.account,
339             password: adminDialogData.password,
9f32bd 340             sysAdRoleId: adminDialogData.sysRoleId,
L 341             isUse: adminDialogData.isUse
2a61f6 342           }
L 343           var isAdd = adminDialogData.type === 'add'
344           var url = isAdd ? 'admin/add' : 'admin/update'
345
346           !isAdd && (params.id = adminDialogData.id)
347
348           this.postFN({
349             url: url,
350             params: params,
351             mockData: {
352               code: 100,
353               msg: '',
354               data: {}
355             }
356           }, () => {
357             this.$messageSuc('保存成功')
358             this.hideAdminDialog()
359             this.getList()
360           })
361         }
362       })
363     }
364
365   }
366 }
367 </script>
368
369 <style lang="scss" scoped>
370
371 </style>