UserProfile.vue 7.1 KB
Newer Older
E
Evan 已提交
1
<template>
2
  <div>
E
Evan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    <el-dialog
      title="修改用户信息"
      :visible.sync="dialogFormVisible">
      <el-form v-model="selectedUser" style="text-align: left" ref="dataForm">
        <el-form-item label="用户名" label-width="120px" prop="username">
          <label>{{selectedUser.username}}</label>
        </el-form-item>
        <el-form-item label="真实姓名" label-width="120px" prop="name">
          <el-input v-model="selectedUser.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" label-width="120px" prop="phone">
          <el-input v-model="selectedUser.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="120px" prop="email">
          <el-input v-model="selectedUser.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px" prop="password">
          <el-button type="warning" @click="resetPassword(selectedUser.username)">重置密码</el-button>
        </el-form-item>
E
Evan 已提交
22
        <el-form-item label="角色分配" label-width="120px" prop="roles">
E
Evan 已提交
23 24 25 26
          <el-checkbox-group v-model="selectedRoles">
              <el-checkbox v-for="(role,i) in roles" :key="i" :label="role.id">{{role.nameZh}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
E
Evan 已提交
27 28 29 30 31 32
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit(selectedUser)">确 定</el-button>
      </div>
    </el-dialog>
33 34
    <el-row style="margin: 18px 0px 0px 18px ">
      <el-breadcrumb separator-class="el-icon-arrow-right">
E
Evan 已提交
35
        <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">管理中心</el-breadcrumb-item>
36 37 38 39
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户信息</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
E
Evan 已提交
40
    <bulk-registration @onSubmit="listUsers()"></bulk-registration>
E
Evan 已提交
41 42
    <el-card style="margin: 18px 2%;width: 95%">
      <el-table
E
Evan 已提交
43
        :data="users"
E
Evan 已提交
44
        stripe
E
Evan 已提交
45
        :default-sort = "{prop: 'id', order: 'ascending'}"
E
Evan 已提交
46 47 48
        style="width: 100%"
        :max-height="tableHeight">
        <el-table-column
E
Evan 已提交
49 50
          type="selection"
          width="55">
E
Evan 已提交
51 52
        </el-table-column>
        <el-table-column
E
Evan 已提交
53
          prop="id"
E
Evan 已提交
54
          label="id"
E
Evan 已提交
55
          sortable
E
Evan 已提交
56 57 58
          width="100">
        </el-table-column>
        <el-table-column
E
Evan 已提交
59
          prop="username"
E
Evan 已提交
60
          label="用户名"
E
Evan 已提交
61
          fit>
E
Evan 已提交
62 63
        </el-table-column>
        <el-table-column
E
Evan 已提交
64
          prop="name"
E
Evan 已提交
65
          label="真实姓名"
E
Evan 已提交
66
          fit>
E
Evan 已提交
67 68
        </el-table-column>
        <el-table-column
E
Evan 已提交
69
          prop="phone"
E
Evan 已提交
70
          label="手机号"
E
Evan 已提交
71
          fit>
E
Evan 已提交
72 73
        </el-table-column>
        <el-table-column
E
Evan 已提交
74 75
          prop="email"
          label="邮箱"
E
Evan 已提交
76
          show-overflow-tooltip
E
Evan 已提交
77
          fit>
E
Evan 已提交
78 79 80
        </el-table-column>
        <el-table-column
          label="状态"
E
Evan 已提交
81
          sortable
E
Evan 已提交
82
          width="100">
E
Evan 已提交
83 84 85 86 87
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
E
Evan 已提交
88
              @change="(value) => commitStatusChange(value, scope.row)">
E
Evan 已提交
89 90
            </el-switch>
          </template>
E
Evan 已提交
91 92 93 94 95 96
        </el-table-column>
        <el-table-column
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
E
Evan 已提交
97
              @click="editUser(scope.row)"
E
Evan 已提交
98 99 100 101 102 103 104 105 106 107 108 109
              type="text"
              size="small">
              编辑
            </el-button>
            <el-button
              type="text"
              size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
E
Evan 已提交
110 111 112 113
      <div style="margin: 20px 0 20px 0;float: left">
        <el-button>取消选择</el-button>
        <el-button>批量删除</el-button>
      </div>
E
Evan 已提交
114
    </el-card>
115
  </div>
E
Evan 已提交
116 117 118
</template>

<script>
E
Evan 已提交
119
  import BulkRegistration from './BulkRegistration'
E
Evan 已提交
120
    export default {
E
Evan 已提交
121 122
      name: 'UserProfile',
      components: {BulkRegistration},
E
Evan 已提交
123 124
      data () {
          return {
E
Evan 已提交
125
            users: [],
E
Evan 已提交
126
            roles: [],
E
Evan 已提交
127
            dialogFormVisible: false,
E
Evan 已提交
128 129
            selectedUser: [],
            selectedRoles: []
E
Evan 已提交
130 131 132 133
          }
      },
      mounted () {
        this.listUsers()
E
Evan 已提交
134
        this.listRoles()
E
Evan 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
      },
      computed: {
        tableHeight () {
          return window.innerHeight - 320
        }
      },
      methods: {
        listUsers () {
          var _this = this
          this.$axios.get('/admin/user').then(resp => {
            if (resp && resp.status === 200) {
              _this.users = resp.data
            }
          })
E
Evan 已提交
149
        },
E
Evan 已提交
150 151 152 153 154 155 156 157
        listRoles () {
          var _this = this
          this.$axios.get('/admin/role').then(resp => {
            if (resp && resp.status === 200) {
              _this.roles = resp.data
            }
          })
        },
E
Evan 已提交
158
        commitStatusChange (value, user) {
E
Evan 已提交
159
          if (user.username !== 'admin') {
E
Evan 已提交
160
            this.$axios.put('/admin/user/status', {
E
Evan 已提交
161 162 163 164 165 166 167 168 169
              enabled: value,
              username: user.username
            }).then(resp => {
              if (resp && resp.status === 200) {
                if (value) {
                  this.$message('用户 [' + user.username + '] 已启用')
                } else {
                  this.$message('用户 [' + user.username + '] 已禁用')
                }
E
Evan 已提交
170
              }
E
Evan 已提交
171 172 173 174 175
            })
          } else {
            user.enabled = true
            this.$alert('不能禁用管理员账户')
          }
E
Evan 已提交
176 177
        },
        onSubmit (user) {
E
Evan 已提交
178 179 180 181 182 183 184 185 186 187
          let _this = this
          // 根据视图绑定的角色 id 向后端传送角色信息
          let roles = []
          for (let i = 0; i < _this.selectedRoles.length; i++) {
            for (let j = 0; j < _this.roles.length; j++) {
              if (_this.selectedRoles[i] === _this.roles[j].id) {
                roles.push(_this.roles[j])
              }
            }
          }
E
Evan 已提交
188 189 190 191
          this.$axios.put('/admin/user', {
            username: user.username,
            name: user.name,
            phone: user.phone,
E
Evan 已提交
192
            email: user.email,
E
Evan 已提交
193
            roles: roles
E
Evan 已提交
194 195 196 197
          }).then(resp => {
            if (resp && resp.status === 200) {
              this.$alert('用户信息修改成功')
              this.dialogFormVisible = false
E
Evan 已提交
198 199
              // 主要是修改角色后与视图联动
              this.listUsers()
E
Evan 已提交
200 201 202 203 204 205
            }
          })
        },
        editUser (user) {
          this.dialogFormVisible = true
          this.selectedUser = user
E
Evan 已提交
206 207 208 209 210
          let roleIds = []
          for (let i = 0; i < user.roles.length; i++) {
            roleIds.push(user.roles[i].id)
          }
          this.selectedRoles = roleIds
E
Evan 已提交
211 212
        },
        resetPassword (username) {
E
Evan 已提交
213
          this.$axios.put('/admin/user/password', {
E
Evan 已提交
214 215 216 217 218 219
            username: username
          }).then(resp => {
            if (resp && resp.status === 200) {
              this.$alert('密码已重置为 123')
          }
          })
E
Evan 已提交
220 221
        }
      }
E
Evan 已提交
222 223 224 225 226 227
    }
</script>

<style scoped>

</style>