MrShi
2 天以前 39fc2d6754953e41a7334a2166347baacfcfb40a
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
<template>
  <GlobalWindow :title="title" width="80%" :visible.sync="visible" :confirm-working="isWorking" @confirm="confirm">
    <p class="tip">正在为角色【 <em>{{ role.name || '-' }}</em>】 配置数据权限</p>
    <p class="tip-warn"><i class="el-icon-warning"></i>提醒:权限配置后需重新登录后生效</p>
    <el-form :model="form" ref="form" style="margin-top:15px">
      <el-form-item label="权限类型:" prop="type">
        <el-select v-model="form.type" clearable filterable placeholder="请选择权限类型">
          <el-option v-for="(item, index) in options" :key="index" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.type == 4" label="自定义部门:" prop="customData">
        <el-cascader :options="departments" v-model="form.customData" :props=defaultProps clearable></el-cascader>
      </el-form-item>
 
    </el-form>
  </GlobalWindow>
</template>
 
<script>
import BaseOpera from '@/components/base/BaseOpera'
import GlobalWindow from '@/components/common/GlobalWindow'
import { createRoleDataPermission } from '@/api/system/role'
import { fetchList } from '@/api/business/company'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'OperaSystemRoleWindow',
  extends: BaseOpera,
  components: { GlobalWindow },
  data() {
    return {
      options: [
        { name: '全部', id: 0 },
        { name: '所属部门及下属部门', id: 1 },
        { name: '所属部门及其子孙部门', id: 2 },
        { name: '仅所属部门', id: 3 },
        { name: '自定义部门', id: 4 },
        { name: '只看自己', id: -1 },
      ],
      // 表单数据
      form: {
        businessCode: null,
        createTime: null,
        createUser: null,
        customData: [],
        id: null,
        roleId: null,
        type: 0
      },
      defaultProps: {
        multiple: true,
        checkStrictly: true,
        // children: 'children',
        label: 'name',
        value: 'id',
        emitPath: false
      },
      role: {},
      departments: []
    }
  },
  created() {
    this.config({
      api: '/system/role',
      'field.id': 'id'
    })
    this.treeComList()
  },
  methods: {
    // 部门树状结构数据
    treeComList() {
      fetchList()
        .then(res => {
          // this.departments = this.tree([res])
          this.departments = this.newTree(res)
        })
    },
    open(title, target, role) {
      // console.log(title, target)
      this.title = title
      this.visible = true
      this.role = role
      // 新建
      if (target == null) {
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.form[this.configData['field.id']] = null
        })
        return
      }
      // 编辑
      this.$nextTick(() => {
        for (const key in this.form) {
          this.form[key] = target[key]
        }
        console.log(target)
        if (target.customData === undefined || target.customData === null || target.customData === '') {
          this.form.customData = []
        } else {
          const customD = this.form.customData.split(',')
          this.form.customData = customD.map((item) => { return parseInt(item) })
        }
      })
    },
    newTree(tree) {
      if (tree == null) {
        return []
      }
      return tree.map(item => {
        let newItem = { ...item }
        if (newItem) {
          newItem.children = newItem.childList
        }
        if (item.children && item.children.length == 0) {
          this.$delete(newItem, 'children')
        } else {
          newItem.children = this.newTree(newItem.children)
        }
        return newItem
      })
    },
    __confirmCreate() {
      // console.log(JSON.stringify(this.form.customData));
      // return
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
 
        this.isWorking = true
        let data = JSON.parse(JSON.stringify(this.form))
        if (this.form.type === 4) {
          data.customData = this.form.customData.join(',')
        } else {
          data.customData = ''
        }
        createRoleDataPermission(data)
          .then(() => {
            this.visible = false
            this.$tip.apiSuccess('新建成功')
            this.$emit('success')
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    },
    __confirmEdit() {
      // console.log(JSON.stringify(this.form.customData));
      // return
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        // 调用更新接口
        this.isWorking = true
        let data = JSON.parse(JSON.stringify(this.form))
        if (this.form.type === 4) {
          data.customData = this.form.customData.join(',')
        } else {
          data.customData = ''
        }
        createRoleDataPermission(data)
          .then(() => {
            this.visible = false
            this.$tip.apiSuccess('修改成功')
            this.$emit('success')
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    }
  }
}
</script>
<style scoped lang="scss">
.transfer {
  height: 600px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
 
  ::v-deep .el-transfer-panel {
    flex: 1;
    height: 100%;
  }
 
  ::v-deep .el-transfer-panel__body {
    height: 500px;
  }
 
  ::v-deep .el-transfer-panel__list.is-filterable {
    height: 480px;
  }
}
</style>