zhangzeli
2022-01-05 9db558324bafc153052ab3af6d9235af6c5452d0
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
221
222
223
224
225
226
227
228
229
230
<style lang="less">
@import "./access.less";
</style>
 
<template>
  <div class="access">
    <Row :gutter="10">
      <Col :xs="24" :sm="24" :lg="24" :xl="8" style="margin: 0 0 10px 0">
        <Card>
          <p slot="title">
            <Icon type="md-contact" style="margin-right: 5px"></Icon>当前用户
          </p>
          <div class="access-user-content access-current-user-content">
            <Avatar v-if="avatar" :src="avatar" size="100"></Avatar>
            <Avatar v-else icon="md-person" size="100"></Avatar>
            <p>当前用户本页面拥有按钮权限:</p>
            <b>{{ permTypes }}</b>
          </div>
        </Card>
      </Col>
      <Col :lg="24" :xl="16" style="margin: 0 0 10px 0">
        <Card>
          <p slot="title">
            <Icon type="md-apps" style="margin-right: 5px"></Icon
            >当前用户本页面拥有的按钮操作
          </p>
          <Row :gutter="10" align="middle" class="access-user-content">
            <Col span="6" class="access-buttons">
              <Button v-has="'add'" type="primary">添加按钮</Button>
              <Button v-has="'edit'">编辑按钮</Button>
              <Button v-has="'delete'" type="error">删除按钮</Button>
            </Col>
            <Col span="18">
              <div>
                您可以通过更换测试用户账号:
                <code>test</code>或 <code>test2</code> 密码:
                <code>123456</code
                >,然后观察该页面按钮以及部门相关数据权限的变化
                <br />
                <br />自定义权限标签: <code>v-has</code>,示例:
                <code>{{ example }}</code>
                <br />
                <br />说明:该Demo大部分页面为演示功能,前端未配置根据权限隐藏按钮
              </div>
            </Col>
          </Row>
        </Card>
      </Col>
    </Row>
    <Row :gutter="10">
      <Col :sm="24" :lg="24" :xl="14" style="margin: 0 0 10px 0">
        <Card>
          <p slot="title">
            <Icon type="md-grid" style="margin-right: 5px"></Icon>表格中权限判断
          </p>
          <div class="content-table">
            <Alert show-icon>
              iView Table组件已支持 slot-scope 用法,建议使用该新用法。
              以下为Render函数中权限按钮示例
            </Alert>
            <Table border :columns="columns" :data="data"></Table>
          </div>
        </Card>
      </Col>
      <Col :sm="24" :lg="24" :xl="10" style="margin: 0 0 10px 0">
        <Card>
          <p slot="title">
            <Icon type="md-contacts" style="margin-right: 5px"></Icon
            >通过当前用户角色显示
          </p>
          <p slot="extra">无需配置,全局可用</p>
          <Row class="content-role">
            <div class="access-user-roles">
              <p>当前用户拥有角色:</p>
              <b>{{ roles }}</b>
            </div>
            <div class="btns">
              <Button
                v-hasRole="'ROLE_USER'"
                type="primary"
                style="margin-right: 5px"
                >添加按钮</Button
              >
              <Button v-hasRole="'ROLE_TEST'" type="error">删除按钮</Button>
              <Button v-hasRole="'ROLE_ADMIN'">编辑按钮</Button>
            </div>
            <div class="role-demo">
              自定义角色权限标签:
              <code>v-hasRole</code>
              <br />
              <br />示例:
              <code>{{ exampleRole }}</code>
            </div>
          </Row>
        </Card>
      </Col>
    </Row>
  </div>
</template>
 
<script>
export default {
  name: "access_index",
  data() {
    return {
      permTypes: [],
      roles: [],
      example: "<Button v-has=\"'add'\">添加按钮</Button>",
      exampleRole: "<Button v-hasRole=\"'ROLE_ADMIN'\">添加按钮</Button>",
      columns: [
        {
          title: "姓名",
          key: "name",
          render: (h, params) => {
            return h("div", [
              h("Icon", {
                props: {
                  type: "ios-person",
                },
                style: {
                  margin: "0 5px 0 0",
                },
              }),
              h("strong", params.row.name),
            ]);
          },
        },
        {
          title: "年龄",
          key: "age",
        },
        {
          title: "地址",
          key: "address",
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          align: "center",
          render: (h, params) => {
            if (this.permTypes.includes("edit")) {
              return h("div", [
                h(
                  "Button",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                    },
                    style: {
                      marginRight: "5px",
                    },
                    on: {
                      click: () => {},
                    },
                  },
                  "编辑"
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small",
                    },
                    on: {
                      click: () => {},
                    },
                  },
                  "删除"
                ),
              ]);
            } else {
              return h("div", [
                h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small",
                    },
                    on: {
                      click: () => {},
                    },
                  },
                  "删除"
                ),
              ]);
            }
          },
        },
      ],
      data: [
        {
          name: "John Brown",
          age: 18,
          address: "New York No. 1 Lake Park",
        },
        {
          name: "Jim Green",
          age: 24,
          address: "London No. 1 Lake Park",
        },
        {
          name: "Joe Black",
          age: 30,
          address: "Sydney No. 1 Lake Park",
        },
      ],
    };
  },
  computed: {
    avatar() {
      return this.$store.state.user.avatar;
    },
  },
  methods: {
    initMeta() {
      if (this.$route.meta.permTypes) {
        this.permTypes = this.$route.meta.permTypes;
      }
    },
  },
  created() {
    this.initMeta();
    this.roles = this.getStore("roles");
  },
};
</script>