jazzzone
2022-03-05 f2a2b4a4258cc1c88b897024bc5c49f31273c0ca
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
<template>
  <div class="app-container">
    <el-page-header class="mb20" @back="jumpBack" />
    <el-form ref="roleAddForm" label-position="left" :model="mData" label-width="150px" :rules="rules" size="small">
      <el-form-item label="角色名称:" prop="name">
        <el-input v-model="mData.name" placeholder="请输入名称" maxlength="50" class="com-edit-input" />
      </el-form-item>
      <el-form-item label="是否启用:">
        <el-switch v-model="mData.isUse" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="权限选择:">
        <div v-for="(item, index) in authorityGroups" :key="index" class="flex">
          <div style="min-width: 140px">
            <el-checkbox v-model="item.checked" @change="chooseAll(index)">{{ item.name }}:</el-checkbox>
          </div>
          <div class="flex flex-ver flex-wrap role-list">
            <div v-for="(auth, a_idx) in item.authoritys" :key="a_idx" class="item flex" :class="{active: auth.active}" @click="chooseAuth(index, a_idx)">
              <div class="name">{{ auth.name }}</div>
              <i class="el-icon-check" />
            </div>
          </div>
        </div>
      </el-form-item>
    </el-form>
 
    <div class="com-edit-bottom-btns flex flex-align-center">
      <el-button size="mini" @click="jumpBack">取消</el-button>
      <el-button type="primary" size="mini" @click="submit">提交</el-button>
    </div>
 
  </div>
</template>
 
<script>
 
export default {
  name: 'RoleAdd',
  data() {
    return {
      mData: {
        isUse: 1, // 是否启用
        name: '' // 角色名称
      },
      authorityGroups: [], // 权限选项
      // 表单校验
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    // 已选中的权限id
    authIds() {
      const arr = []
      this.authorityGroups.forEach(item => {
        item.authoritys.forEach(auth => {
          if (auth.active) {
            arr.push(auth.id)
          }
        })
      })
      return arr
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getAuthData()
    },
 
    jumpBack() {
      this.$router.go(-1)
    },
 
    // 获取权限数组
    getAuthData() {
      this.postFN({
        url: 'admin/role/add/page',
        mockData: {
          code: 100, msg: '',
          data: {
            'authorityGroups': [
              {
                'name': '分类1',
                'authoritys': [
                  {
                    'name': '权限1',
                    'id': '1'
                  }
                ]
              }, {
                'name': '分类2',
                'authoritys': [
                  {
                    'name': '权限21',
                    'id': '21'
                  }, {
                    'name': '权限22',
                    'id': '22'
                  }
                ]
              }
            ]
          }
        }
      }, (inf) => {
        this.authorityGroups = inf.authorityGroups
      })
    },
 
    // 选中权限
    // @param idx1 一级
    // @param idx2 二级
    chooseAuth(idx1, idx2) {
      const value = this.authorityGroups[idx1].authoritys[idx2].active
      this.$set(this.authorityGroups[idx1].authoritys[idx2], 'active', !value)
      // 处理全选
      this.$set(this.authorityGroups[idx1], 'checked', this.checkAllChecked(this.authorityGroups[idx1].authoritys))
    },
 
    // 检查数组是否全部已选
    checkAllChecked(arr) {
      let flag = true
      arr.find(item => {
        if (!item.active) {
          flag = false
          return true
        }
      })
      return flag
    },
 
    // 点击全选
    chooseAll(index) {
      var value = this.authorityGroups[index].checked
      this.authorityGroups[index].authoritys.forEach((item, idx2) => {
        this.$set(this.authorityGroups[index].authoritys[idx2], 'active', value)
      })
    },
 
    submit() {
      this.$refs['roleAddForm'].validate(valid => {
        if (valid) {
          var { mData, authIds } = this
          var params = {
            name: mData.name,
            isUse: mData.isUse,
            authIds: JSON.stringify(authIds)
          }
 
          this.postFN({
            url: 'admin/role/add',
            params: params,
            mockData: {
              code: 100,
              msg: '',
              data: {}
            }
          }, () => {
            this.$messageSuc('保存成功')
            this.jumpBack()
          })
        }
      })
    }
  }
}
</script>
 
<style lang="scss" scoped>
.role-list{
  .item{
    cursor: pointer;
    margin: 6px 15px 6px 0;
    .name{
      color: #fff;
      background: #ccc;
      text-align: center;
      height: 26px;
      line-height: 26px;
      padding: 0 12px;
      font-size: 14px;
    }
    .el-icon-check{
      color: #fff;
      font-size: 20px;
      border: 1px solid #ccc;
      text-align: center;
      display: block;
      line-height: 24px;
      height: 24px;
      width: 24px;
      box-sizing: content-box;
    }
 
    // &:hover{
    //   .name {
    //     background: #999;
    //   }
    //   .el-icon-check{
    //     color: #999;
    //   }
    // }
  }
  .active, .active:hover{
    .name{
      background: #409EFF;
      // background: #999;
    }
    .el-icon-check{
      background: #409EFF;
      // background: #999;
    }
  }
}
</style>