fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <el-dialog title="编辑用户权限" :close-on-click-modal="false" :modal-append-to-body="false"
             :visible.sync="visible">
    <div class="userAuthority-form">
      <el-form ref="ruleForm" size="mini" label-width="100px" class="demo-ruleForm">
        <el-form-item label="姓名:" prop="userName">
          <el-input :disabled="true" v-model="info.userName"></el-input>
        </el-form-item>
        <el-form-item label="用户账号:" prop="userAccount">
          <el-input autocomplete="off" :disabled="true" v-model="info.userAccount">
          </el-input>
        </el-form-item>
        <el-form-item label="权限配置:" prop="roles">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll"
                       @change="handleCheckAllChange">全选</el-checkbox>
          <el-checkbox-group v-model="valueCheckedAry" @change="handlecheckedRolesChange">
            <el-row>
              <el-col :span="8" v-for="item in roleList" :key="item.id">
                <el-checkbox class="checkbox" :label="item.id" :key="item.id">
                  {{item.name}}</el-checkbox>
              </el-col>
            </el-row>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer text-center">
      <el-button type="primary" @click="submitForm" size="mini">确 定
      </el-button>
      <el-button size="mini" @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import { mapState } from 'vuex'
import { sysRoleIds, bindUserRole } from '../../api/userAuthority'
export default {
  name: "newInfo",
  props: {
    // 显示隐藏
    visible: {
      type: Boolean,
      required: true
    },
    // 角色相关信息
    info: {
      type: Object,
      required: true
    },
  },
  computed: {
    // 获取登录信息
    ...mapState(['userInfo'])
  },
  data() {
    return {
      isIndeterminate: false,
      // 管理员列表
      roleList: [],
      // 全部选中的val的列表
      allValAry: [],
      // 设置 indeterminate 状态,只负责样式控制: true-显示正方形,false-显示钩或者空(非正方形的)
      indeterminate: false,
      // 指定当前是否选中
      checkAll: false,
      // 选中的角色数组
      valueCheckedAry: [],
    }
  },
  watch: {
    visible(newV) {
      if (newV) {
        // 回显当前角色之前选中的角色
        this.valueCheckedAry = this.info.roles ? this.info.roles : []
        // 去回显样式
        this.handlecheckedRolesChange()
      }
    }
  },
  mounted() {
    // 获取角色列表
    this.getRoleList()
  },
  methods: {
    /**
 * 获取角色列表
 */
    async getRoleList() {
      let params = {
        userId: this.userInfo.userId,
        systemId: process.env.VUE_APP_CLIENT_ID
      }
      const res = await sysRoleIds(params)
      if (res.code === '200' && res.data) {
        this.roleList = [...res.data.roleAll]
        // 全部选中的ids集合
        const idsList = []
        res.data.roleAll.forEach(item => {
          idsList.push(item.id)
        })
        this.allValAry = idsList
      }
    },
    /**
      * 全选改变
      */
    handleCheckAllChange(val) {
      Object.assign(this, {
        valueCheckedAry: val ? this.allValAry : [],
        indeterminate: false,
        checkAll: val,
      });
    },
    /**
     * 多选框改变
     * @params:
     * checkedValues:选中的角色
     */
    handlecheckedRolesChange() {
      this.indeterminate = !!this.valueCheckedAry.length && this.valueCheckedAry.length < this.roleList.length;
      this.checkAll = this.valueCheckedAry.length === this.roleList.length;
    },
    /**
   * 提交
   * @params:
   */
    async submitForm() {
      if (!this.valueCheckedAry || !this.valueCheckedAry.length) {
        this.$message.error('请选择角色!')
        return false
      }
      const res = await bindUserRole({
        roleCodes: this.valueCheckedAry, // 角色IDS
        userId: this.info.id,        // 用户ID
        systemId: process.env.VUE_APP_CLIENT_ID
      })
      if (res.code == 200) {
        this.$message.success('角色设置成功!')
        this.$emit('ok')
        // 还原
        this.restoreSettings()
      } else {
        this.$message.error(res.data.message)
      }
    },
    /**
     * 还原最初设置
     */
    restoreSettings() {
      this.indeterminate = false
      this.checkAll = false
      this.valueCheckedAry = []
    },
    /**
     * 关闭弹窗
     */
    cancel() {
      this.restoreSettings();
      this.$emit("cancel")
    }
  }
}
</script>
 
<style>
</style>