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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<template>
  <el-dialog :visible.sync="isShowEdit" width="740px" title="编辑权限" :modal-append-to-body="false"
             :close-on-click-modal="false">
    <!--自定义了页脚的按钮-->
    <el-row :gutter="20">
      <el-col :span="6">
        <span>姓名:</span>
        <span>{{ info.userName }}</span>
      </el-col>
      <el-col :span="6">
        <span>用户账号:</span>
        <span>{{ info.userAccount }}</span>
      </el-col>
    </el-row>
    <el-tabs v-model="defaultActive" type="card" style="margin-top:20px;">
      <el-tab-pane :label="item.name" :name="item.code" v-for="item in newSystemData"
                   :key="item.code">
        <role-sel-module :roleAll="item.roleAll" :checkIdList="item.checkIdList"
                         :allIdList="item.allIdList" :visible="isShowEdit" @updateData="updateData"
                         :systemId="item.code">
        </role-sel-module>
      </el-tab-pane>
    </el-tabs>
    <template slot="footer">
      <el-button size="mini" key="back" @click="isShowEdit = false">
        取消
      </el-button>
      <el-button size="mini" :loading="loading" key="submit" type="primary" @click="handleOk"
                 :disabled="newSystemData.length === 0">
        提交
      </el-button>
    </template>
  </el-dialog>
</template>
<script>
import AccountManageApi from '@/api/accountManage'
import roleSelModule from './roleSel.vue'
import systemData from "@/utils/systemConfig.js"
export default {
  name: 'AuthorityEdit',
  components: {
    roleSelModule
  },
  props: {
    // 显示隐藏
    visibleAuthority: {
      type: Boolean,
      required: true
    },
    // 角色相关信息
    info: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isShowEdit: false,
      spinLoading: false,
      // 提交按钮加载中
      loading: false,
      //平台权限下拉数据
      newSystemData: systemData,
      //默认选中tab页
      defaultActive: null,
      //============接口传参==========
      roleCodes: []
    }
  },
  watch: {
    visibleAuthority(newV) {
      if (newV) {
        this.getSyetemUserRoleData();
      }
      this.isShowEdit = newV
    },
    /**
 * 监控内部显示属性变化
 * 传递到外部调用变量
 */
    isShowEdit: function (newDialogShow, oldDialogShow) {
      this.$emit('update:visibleAuthority', newDialogShow)
    }
  },
  methods: {
    //获取用户角色信息
    getSyetemUserRoleData() {
      if (this.newSystemData.length) {
        let _code = this.newSystemData[0].code;
        this.defaultActive = _code;
        this.newSystemData.forEach((item) => {
          this.sendRequest(item);
        })
      }
    },
    //发送请求
    sendRequest(item) {
      this.spinLoading = true;
      AccountManageApi.getRoleIds(
        {
          systemId: item.code,
          userId: this.info.id,
        }
      ).then((res) => {
        if (res.code === '200' && res.data) {
          // 全部角色的id集合
          const idsList = []
          if (res.data.roleAll && res.data.roleAll.length) {
            res.data.roleAll.forEach(item => {
              idsList.push(item.id)
            })
            this.$set(item, "roleAll", res.data.roleAll)
          }
          this.$set(item, "allIdList", idsList)
          this.$set(item, "checkIdList", res.data.roleIdList)
        } else {
          this.$message.error(res.message);
        }
        this.spinLoading = false;
      })
    },
    //获取选中角色
    updateData(obj) {
      this.newSystemData.forEach((v) => {
        if (v.code === obj.systemId) {
          this.$set(v, "roleCodes", obj.roleCodes)
        }
      })
    },
    /**
     * 确定
     */
    handleOk() {
      this.loading = true
      this.newSystemData.forEach((item, index) => {
        if (!item.roleCodes || !item.roleCodes.length) {
          AccountManageApi.unBindUserRole(
            {
              systemId: item.code,
              userId: this.info.id,        // 用户ID
            }
          ).then((res) => {
            if (res.code == 200) {
              if (index === this.newSystemData.length - 1) {
                this.$message.success('角色设置成功!')
                this.$emit('ok')
                this.isShowEdit = false;
                this.loading = false
              }
            } else {
              this.$message.error(res.message)
              this.loading = false
            }
          })
        } else {
          AccountManageApi.bindUserRole(
            {
              roleCodes: item.roleCodes, // 角色IDS
              systemId: item.code,
              userId: this.info.id,        // 用户ID
            }
          ).then((res) => {
            if (res.code == 200) {
              if (index === this.newSystemData.length - 1) {
                this.$message.success('角色设置成功!')
                this.$emit('ok')
                this.isShowEdit = false;
                this.loading = false
              }
            } else {
              this.$message.error(res.message)
              this.loading = false
            }
          })
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.role-list-wrapper {
  width: 100%;
}
</style>