zhaojs
2023-09-15 39e2103e28830337c3be4dbd3dc7ad0a829ee78b
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
<template>
  <page-header-wrapper
    :breadcrumb="false"
  >
    <a-spin
      v-bind="loadingProps"
    >
      <a-card :bordered="false">
        <a-form
          :form="form"
          :model="formData"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-item
            label="角色名称"
            name="rolename">
            <a-input style="width: 50%" v-decorator="['rolename', {initialValue:formData.rolename,rules: [{required: true, message: '请填写角色名称'}]}]"/>
          </a-form-item>
          <a-form-item
            label="备注"
            name="memo">
            <a-input style="width: 50%" v-decorator="['memo', {initialValue:formData.memo,rules: [{required: false, message: ''}]}]"/>
          </a-form-item>
          <a-form-item
            label="权限设置"
            name="theme">
            <a-tree
              style="width: 100%"
              :tree-data="treeData"
              :checkable="true"
              v-model:checkedKeys="checked"
              @check="checkNode"
            />
          </a-form-item>
 
          <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
            <a-button type="primary" @click="handleAddTask">{{ this.btntxt }}</a-button>
            <a-button @click="backList" style="margin-left: 10px">取消</a-button>
          </a-form-item>
        </a-form>
      </a-card>
    </a-spin>
 
  </page-header-wrapper>
</template>
 
    <script>
     import { GetMenuRole, AddRoleSet, GetRoleDetail, EditRole } from '@/api/rolemanage'
    export default {
      name: 'Roleset',
      components: {
      },
      data () {
        return {
            loadingProps:
            {
                spinning: false
            },
            btntxt: '创建角色',
            treeData: [],
            checked: [],
            halfChecked: [],
            formData: {
                rolename: '',
                memo: ''
            },
            roleId: '',
          labelCol: { span: 4 },
          wrapperCol: { span: 14 },
          formLayout: 'horizontal',
          form: this.$form.createForm(this, { name: 'coordinated' })
        }
      },
      created () {
        this.getRoleList()
        const roleId = this.$router.currentRoute.query.roleid
        if (roleId != undefined) {
            this.roleId = roleId
            this.btntxt = '修改角色'
        } else {
            this.roleId = ''
        }
        this.getDetail()
      },
      methods: {
        // 获取详情
        getDetail () {
            if (this.roleId != '') {
                const parameter = {
                    RoleId: this.roleId
                }
                GetRoleDetail(parameter).then(res => {
                    this.formData.rolename = res.result.name
                    this.formData.memo = res.result.memo
                    const check = res.result.roles.split(',')
                    const hasCheck = []
                    check.forEach(element => {
                        if (element.indexOf('role_') < 0) {
                            hasCheck.push(element)
                        }
                    })
                    this.checked = hasCheck
                })
            }
        },
          // 获取权限数据
          getRoleList () {
            GetMenuRole().then(res => {
                this.treeData = res.result
            })
          },
          checkNode (check, event) {
            this.checked = check
            this.halfChecked = event.halfCheckedKeys
          },
          backList () {
              this.$router.push({ path: '/opsystem/rolelist' })
          },
          // 新增
          handleAddTask () {
              this.form.validateFields((err, values) => {
              if (!err) {
               if (this.checked == null || this.checked.length == 0) {
                this.$message.error(`请设置菜单权限`)
                return
               }
               const hasCheckstr = this.checked.join(',')
               const hasCheck = hasCheckstr.split(',')
               this.halfChecked.forEach(element => {
                hasCheck.push(element)
              })
               const parameter = {
                    Name: values.rolename,
                    Roles: hasCheck.join(','),
                    Memo: values.memo
               }
               this.loadingProps.spinning = true
               if (this.roleId != '') { // 修改
                parameter.RoleId = this.roleId
                EditRole(parameter).then(res => {
                  if (res.result) {
                      this.$message.success(`修改成功!`)
                      this.$router.push({ path: '/opsystem/rolelist' })
                  }
}).finally(() => {
                        this.loadingProps.spinning = false
                    })
               } else { // 新增
                AddRoleSet(parameter).then(res => {
                  if (res.result) {
                      this.$message.success(`创建成功!`)
                      this.$router.push({ path: '/opsystem/rolelist' })
                  }
               }).finally(() => {
                  this.loadingProps.spinning = false
              })
               }
              }
          })
          }
      }
    }
    </script>