gaoshp
2024-01-05 b5db49ce1e4c678ce4a7120928811a9621128b8e
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
<template>
    <div class="app-container">
      <div style="display: flex;align-items: center;margin-bottom: 16px;justify-content: space-between;">
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" round @click="operate(3,undefined)">创建用户</el-button>
        <el-input placeholder="请输入用户名" v-model="keyWord" size="mini" style="max-width: 220px;margin-left: 10px;">
          <el-button slot="append" icon="el-icon-search" style="padding: 12px;" @click="queryUser()">查询</el-button>
        </el-input>
      </div>
      <el-table :data="list" v-loading.table="listLoading" element-loading-text="Loading" fit border
                 style="width: 100%;" class="mdc-table">
        <el-table-column prop="user"label="用户名">
        </el-table-column>
        <el-table-column prop="name" label="真实姓名">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 用户编辑 -->
            <i class="mdc-icon-font el-icon-view mdc-editor" @click="operate(0, scope.row)"></i>
            <!-- 删除用户 -->
            <i class="mdc-icon-font el-icon-delete mdc-delete" @click="operate(1, scope.row, scope.$index)"></i>
            <!--用户授权-->
            <i class="mdc-icon-font el-icon-setting mdc-setting" @click="operate(2, scope.row)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding-top: 10px;display: flex;justify-content: flex-end;align-items: center;">
        <el-button-group>
          <el-button type="primary" icon="el-icon-arrow-left" size="mini" round @click="prePage()" :disabled="canPaging[0]">上一页</el-button>
          <el-button disabled  size="mini" plain>{{currentPage}}/{{totalPage}}</el-button>
          <el-button type="primary" size="mini" round @click="nextPage()" :disabled="canPaging[1]">下一页<i class="el-icon-arrow-right el-icon--right"></i></el-button>
        </el-button-group>
      </div>
      <!--  添加用户dialog -->
      <create-user-dialog @submit="addUserCallback" :is-visible="dialog.showAdd"></create-user-dialog>
      <!--用户信息编辑查看-->
      <user-detail-dialog :is-visible="dialog.showMsg" :user="selectUser" @submit="lookupUserInfoCallBack"></user-detail-dialog>
      <!--用户授权dialog-->
      <grant-auth-dialog :list="workshopList" :grant-list="grantList" @submit="grantCallback" :is-visible="dialog.showGrant"></grant-auth-dialog>
    </div>
</template>
 
<script>
  import createUserDialog from './dialog/CreateUser'
  import grantAuthDialog from './dialog/GrantAuth'
  import { mapGetters } from 'vuex'
  import UserDetailDialog from './dialog/UserDetail'
  import { getUserList, addUser, delUser, userAuth } from '@/api/MdcApi'
  export default {
    name: 'ums',
    components: {
      UserDetailDialog,
      grantAuthDialog,
      createUserDialog
    },
    data: function() {
      return {
        keyWord: '', // 关键字 用于搜索
        listLoading: true,
        list: [], // 用户列表
        dialog: {
          showAdd: false, // 添加
          showMsg: false, // 详情
          showGrant: false // 授权
        },
        selectUser: {},
        grantList: [],
        currentPage: 1,
        totalPage: 1
      }
    },
    computed: {
      ...mapGetters({
        workshopList: 'workshopList'
      }),
      canPaging() {
        const tem = [false, false]
        tem[0] = (this.currentPage <= 1)
        tem[1] = (this.currentPage >= this.totalPage)
        return tem
      }
    },
    methods: {
      /**
       * 初始化已有用户
       */
      initUsers() {
        getUserList(this.currentPage, this.keyWord).then(res => {
          this.listLoading = false
          let totalNum = parseInt(res.totalPage)
          if (totalNum + '' === 'NaN') {
            totalNum = 1
          }
          this.totalPage = totalNum
          this.list = res.list || []
        })
      },
      /**
       *  查询用户
       */
      queryUser() {
        this.currentPage = 1
        this.initUsers()
      },
      /**
       * 检查是否有相同用户名
       * @param userName
       * */
      hasSameUserName(userName) {
        for (let i = 0; i < this.list.length; i++) {
          if (userName === this.list[i].name) {
            return true
          }
        }
        return false
      },
      /**
       *  添加用户回调
       *  action 'confirm','cancel'   data  action 为 confirm 时候的回调数据
       */
      addUserCallback({ action, data }) {
        switch (action) {
          case 'confirm':
            if (this.hasSameUserName(data.user)) {
              this.$message({
                type: 'error',
                message: '该用户名已存在,请重新输入'
              })
              return
            } else {
              // todo 网络请求 创建用户
              addUser(data).then(res => {
                if (res.result === 'SUCCESS') {
                  this.list.push(data)
                } else {
                  this.$message({
                    type: 'error',
                    message: '用户创建失败,请重试...'
                  })
                }
              })
            }
            break
        }
        this.dialog.showAdd = false
      },
      /**
       *  授权对话框回调
       *  @param { action:'confirm'或者'cancel' }
       * */
      grantCallback({ action, data }) {
        switch (action) {
          case 'confirm':
            // todo 调用用户授权接口
            userAuth(this.selectUser.user, JSON.parse(data)).then(res => {
              if (res.result === 'SUCCESS') {
                this.selectUser.workshops = data
                this.dialog.showGrant = false
              } else {
                this.$message({
                  type: 'error',
                  message: '授权失败,请后重试!'
                })
              }
            })
            break
          case 'cancel':
            this.dialog.showGrant = false
            break
        }
      },
      /**
       *  查看用户信息话框回调
       *  @param { action:'confirm'或者'cancel' }
       * */
      lookupUserInfoCallBack({ action, data }) {
        switch (action) {
          case 'confirm':
            break
          case 'cancel':
            break
        }
        this.dialog.showMsg = false
      },
      /**
       * 用户操作
       * @param type
       * @param user { userID, userName }
       * @param index
       */
      operate(type, user, index) {
        this.selectUser = user || {}
        switch (type) {
          case 0: // 编辑
            this.dialog.showMsg = true
            break
          case 1: // 删除
            this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              customClass: 'mdc-confirm-dialog',
              type: 'warning',
              beforeClose: (action, instance, done) => {
                switch (action) {
                  case 'confirm':
                    instance.confirmButtonLoading = true
                    instance.confirmButtonText = '执行中...'
                    // todo 模拟调用接口 删除用户
                    delUser(this.selectUser.user).then(res => {
                      // {"result":"SUCCESS"}
                      if (res.result === 'SUCCESS') {
                        instance.confirmButtonLoading = false
                        instance.confirmButtonText = '确定'
                        done()
                        this.list.splice(index, 1)
                        this.$message({
                          type: 'success',
                          message: '删除成功!'
                        })
                      } else {
                        this.$message({
                          type: 'error',
                          message: '删除失败,请收稍后重试!'
                        })
                      }
                    })
                    break
                  case 'cancel':
                    done()
                    this.$message({
                      type: 'info',
                      message: '已取消删除'
                    })
                    break
                }
              }
            }).catch(() => {})
            break
          case 2: // 授权教室
            this.grantList = JSON.parse(this.selectUser.workshops || '[]')
            this.dialog.showGrant = true
            break
          case 3: // 添加用户
            this.dialog.showAdd = true
            break
        }
      }
    },
    /**
     * 上一页
     */
    prePage() {
      this.currentPage--
      this.initUsers()
    },
    /**
     * 下一页
     */
    nextPage() {
      this.currentPage++
      this.initUsers()
    },
    /**
     * 初始化数据
     */
    created() {
      this.initUsers()
    }
  }
</script>
 
<style scoped>
   .mdc-icon-font{
     font-size: 18px;margin-right: 6px;
   }
   .mdc-icon-font:active{
     opacity: 0.5;
   }
  .mdc-editor{
    color: #409EFF;
  }
   .mdc-delete{
     color: #F56C6C;
   }
   .mdc-setting{
     color: #67C23A;
   }
   .mdc-table{}
   .mdc-table>>> thead th{
     background-color: #e7f1fd;color:#606266;
   }
   .mdc-table>>> .el-table__body tr:hover>td{
    background-color: transparent;
  }
</style>