zhangxiao
2024-08-05 f6ba464aa7856b8201e313fe79e651aab34e6509
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<template>
  <div class="container">
    <authheader :items="menuTips"></authheader>
    <a-row :gutter="20">
      <a-col :span="8">
        <a-card
          :title="$t('menu.resource.title')"
          :bordered="false"
          :style="{ 'width': '100%', 'height': '900px', 'overflow-y': 'auto' }"
        >
          <a-tree
            class="tree-demo"
            draggable
            blockNode
            :data="treeData"
            :show-line="showLine"
            :fieldNames="{
              key: 'menuId',
              title: 'menuName',
              children: 'children',
            }"
            @drop="onDrop"
            @select="showDetail"
          >
            <template #extra="nodeData">
              <IconPlus
                style="
                  position: absolute;
                  right: 60px;
                  font-size: 12px;
                  top: 10px;
                  color: #3370ff;
                "
                @click="() => onIconClick(nodeData)"
              />
              <IconDelete
                style="
                  position: absolute;
                  right: 40px;
                  font-size: 12px;
                  top: 10px;
                  color: #3370ff;
                "
                @click="() => onIconClickDelete(nodeData)"
              />
            </template>
          </a-tree>
        </a-card>
      </a-col>
      <a-col :span="16">
        <a-card
          :title="$t('menu.resource.detail')"
          :bordered="false"
          :style="{ width: '100%' }"
        >
          <a-form :model="resourceform" layout="horizontal">
            <a-form-item field="parentName" label="上级资源" disabled>
              <a-input v-model="resourceform.parentName" />
            </a-form-item>
            <a-form-item field="status" label="资源状态">
              <a-switch
                checked-value="0"
                unchecked-value="1"
                v-model="resourceform.status"
              ></a-switch>
            </a-form-item>
            <a-form-item field="menuName" label="资源名称">
              <a-input v-model="resourceform.menuName" />
            </a-form-item>
            <a-form-item field="menuType" label="资源类型">
              <a-select
                v-model="resourceform.menuType"
                :options="options"
                :field-names="fieldNames"
                :style="{ width: '320px' }"
                placeholder="请选择"
              />
            </a-form-item>
            <a-form-item field="perms" label="资源控制权限字符">
              <a-input v-model="resourceform.perms" />
            </a-form-item>
            <a-form-item
              field="component"
              label="资源地址"
              style="align: start"
            >
              <a-input v-model="resourceform.component" />
            </a-form-item>
            <a-form-item>
              <a-space>
                <a-button @click="editresource">保存</a-button>
                <a-button @click="reset(resourceform.menuId)">重置</a-button>
              </a-space>
            </a-form-item>
          </a-form>
        </a-card>
      </a-col>
    </a-row>
    <a-modal
      width="50%"
      v-model:visible="visible"
      title="新增"
      @cancel="handleCancel"
      @ok="addresource"
    >
      <a-form :model="resourceform" layout="horizontal">
        <a-form-item field="parentName" label="上级资源" disabled>
          <a-input v-model="resourceform.parentName" />
        </a-form-item>
        <a-form-item field="status" label="资源状态">
          <a-switch
            checked-value="0"
            unchecked-value="1"
            v-model="resourceform.status"
          ></a-switch>
        </a-form-item>
        <a-form-item field="menuName" label="资源名称">
          <a-input v-model="resourceform.menuName" />
        </a-form-item>
        <a-form-item field="menuType" label="资源类型">
          <a-select
            v-model="resourceform.menuType"
            :options="options"
            :field-names="fieldNames"
            :style="{ width: '320px' }"
            placeholder="请选择"
          />
        </a-form-item>
        <a-form-item field="perms" label="资源控制权限字符">
          <a-input v-model="resourceform.perms" />
        </a-form-item>
        <a-form-item field="component" label="资源地址" style="align: start">
          <a-input v-model="resourceform.component" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
 
<script lang="ts" setup>
  import { ref } from 'vue';
  import { IconPlus } from '@arco-design/web-vue/es/icon';
  import {
    Resource,
    ResourceAdd,
    ResourceById,
    ResourceDelete,
    ResourceList,
    ResourceUpdate,
  } from '@/api/authority';
  import Authheader from '@/views/authority/components/authheader.vue';
  import { Modal } from '@arco-design/web-vue';
 
  let visible = ref(false);
  let treeData = ref([]);
  let showLine = ref(true);
  let menuTips = ref(['权限管理', '资源']);
  const fieldNames = { value: 'key', label: 'value' };
  // 0目录 1菜单 2按钮
  let options = ref([
    {
      key: '3',
      value: '目录',
    },
    {
      key: '0',
      value: '菜单',
    },
    {
      key: '1',
      value: '按钮',
    },
  ]);
  let resourceform = ref<Resource>({
    component: '',
    createTime: '',
    description: '',
    icon: '',
    menuId: '',
    menuName: '',
    menuType: '',
    orderNum: '',
    parentId: '',
    parentName: '',
    children: [],
    path: '',
    perms: '',
    status: '',
    syesourcetype: '',
    target: '',
    updateTime: '',
  });
 
  const onIconClick = (nodeData) => {
    resourceform.value.parentId = nodeData.menuId;
    resourceform.value.component = '';
    resourceform.value.createTime = '';
    resourceform.value.description = '';
    resourceform.value.icon = '';
    resourceform.value.menuId = '';
    resourceform.value.menuName = '';
    resourceform.value.menuType = '';
    resourceform.value.orderNum = '0';
    resourceform.value.parentName = nodeData.menuName;
    resourceform.value.path = '';
    resourceform.value.perms = '';
    resourceform.value.status = '';
    resourceform.value.syesourcetype = '';
 
    visible.value = true;
  };
 
  const addresource = async () => {
    await ResourceAdd({
      ...resourceform.value,
    } as unknown as Resource).then((res) => {
      ResourceData('');
    });
  };
 
  const onIconClickDelete = (nodeData) => {
    ResourceDelete(nodeData.menuId).then(() => {
      ResourceData('');
    });
  };
 
  const showDetail = (id) => {
    ResourceById(id).then((res) => {
      resourceform.value = { ...res.data };
    });
  };
  const editresource = () => {
    ResourceUpdate({
      ...resourceform.value,
    } as unknown as Resource).then((res) => {
      ResourceData('');
      Modal.success({
        title: '保存成功',
        content: '保存成功',
      });
    });
  };
  const reset = (id) => {
    ResourceById(id).then((res) => {
      resourceform.value = { ...res.data };
    });
  };
 
  const onDrop = ({ dragNode, dropNode, dropPosition }) => {
    const data = treeData.value;
    ResourceUpdate({
      orderNum: '0',
      parentId: dropNode.menuId,
      menuId: dragNode.menuId,
    });
    const loop = (data, key, callback) => {
      data.some((item, index, arr) => {
        if (item.menuId === key) {
          callback(item, index, arr);
          return true;
        }
        if (item.children) {
          return loop(item.children, key, callback);
        }
        return false;
      });
    };
 
    loop(data, dragNode.menuId, (_, index, arr) => {
      arr.splice(index, 1);
    });
 
    if (dropPosition === 0) {
      loop(data, dropNode.menuId, (item) => {
        item.children = item.children || [];
        item.children.push(dragNode);
      });
    } else {
      loop(data, dropNode.menuId, (_, index, arr) => {
        arr.splice(dropPosition < 0 ? index : index + 1, 0, dragNode);
      });
    }
  };
 
  const handleCancel = (type) => {
    if (type == 1) {
      visible.value = false;
    }
  };
 
  const ResourceData = async (key) => {
    await ResourceList(key).then((res) => {
      treeData.value = [...res.rows];
    });
  };
 
  ResourceData('');
</script>
 
<style scoped>
  .tree-demo :deep(.tree-node-dropover) > :deep(.arco-tree-node-title),
  .tree-demo :deep(.tree-node-dropover) > :deep(.arco-tree-node-title):hover {
    animation: blinkBg 0.4s 2;
  }
 
  @keyframes blinkBg {
    0% {
      background-color: transparent;
    }
 
    100% {
      background-color: var(--color-primary-light-1);
    }
  }
</style>