huangyinfeng
4 天以前 db42d08c39ae6129e2b95cd24c0d57c6769282e5
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
<template>
  <PageWrapper dense contentFullHeight fixedHeight>
    <div>
      <div class="header-container">
        <span class="button-group">
          <a-button shape="circle" size="large">
            <MailOutlined />
          </a-button>
          <a-button shape="circle" size="large">
            <UserOutlined />
          </a-button>
        </span>
        <a-button
          class="write-button"
          type="primary"
          size="large"
          shape="round"
          @click="$router.push('/email/edit')"
        >
          写信
        </a-button>
      </div>
 
      <div class="menu-container">
        <a-menu
          id="email-left-nav"
          v-model:open-keys="openKeys"
          v-model:selected-keys="selectedKeys"
          mode="inline"
        >
          <template v-for="item in menuItems" :key="item.key">
            <EmailMenuItem :item="item" @click="handleClick" />
          </template>
        </a-menu>
        <a-divider />
        <a-menu
          id="email-left-nav2"
          v-model:open-keys="openKeys2"
          v-model:selected-keys="selectedKeys2"
          mode="inline"
        >
          <template v-for="item in menuItems2" :key="item.key">
            <template v-if="!item.children">
              <a-menu-item :key="item.key" @click="$emit('click', item)">
                <div class="my-display">
                  <span>{{ item.title }}</span>
                  <span v-if="item.total > 0" class="my-left">{{ item.total }}</span>
                </div>
              </a-menu-item>
            </template>
            <template v-else>
              <MyMenu
                :item="item"
                @click.stop="handleClickMyMenu(item)"
                @updateSelectedKeys="updateSelectedKeys"
              />
            </template>
          </template>
        </a-menu>
      </div>
    </div>
  </PageWrapper>
</template>
 
<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { PageWrapper } from '@/components/Page';
  import { DingdingOutlined, MailOutlined, UserOutlined } from '@ant-design/icons-vue';
  import { getEmailModuleApi, getEmailModuleBelowApi } from '@/api/email/userList';
  import { useRouter } from 'vue-router';
  import MyMenu from './MyMenu.vue';
  import EmailMenuItem from './EmailMenuItem.vue';
 
  const selectedKeys = ref<string[]>(['Index']);
  const openKeys = ref<string[]>(['Inbox']);
  const openKeys2 = ref(['0049LM']);
  const selectedKeys2 = ref([]);
 
  const menuItems = ref([]);
  const menuItems2 = ref([]);
 
  const fetchEmailModules = async () => {
    try {
      const [res, res2] = await Promise.all([getEmailModuleApi(), getEmailModuleBelowApi()]);
      menuItems.value = convertRoutesToMenuItems(res.data);
      menuItems2.value = convertRoutesToMenuItems2(res2.data);
      console.log('menuItems:', menuItems2.value);
    } catch (error) {
      console.error('获取邮箱模块失败:', error);
    }
  };
 
  const convertRoutesToMenuItems = (routes: any[]) => {
    return routes
      .map((route) => ({
        key: route.key,
        title: route.mailName,
        total: route.total,
        children: route.children ? convertRoutesToMenuItems(route.children) : undefined,
      }))
      .filter(Boolean);
  };
 
  const convertRoutesToMenuItems2 = (routes: any[], path) => {
    return routes
      .map((route) => ({
        key: route.key,
        title: route.name,
        total: route.number,
        path: path ? path : route.key,
        children: route.list
          ? convertRoutesToMenuItems2(route.list, path ? path : route.key)
          : undefined,
      }))
      .filter(Boolean);
  };
 
  onMounted(fetchEmailModules);
 
  const routesConfig = {
    InboxPage1: '/email/index',
    receiver: '/email/Inbox/list',
    sender: '/email/outbox/list',
    IndexPage1: '/email/outbox',
    moduleBelowA: '/email/index',
    moduleBelowB: '/email/index',
    moduleBelowC: '/email/index',
  };
 
  const router = useRouter();
  // const handleClick = (item: any) => {
  //   selectedKeys2.value = [];
  //   debugger
  //   const routePath =
  //     routesConfig[item.key] || router.getRoutes().find((r) => r.name === item.key)?.path;
  //   if (routePath) {
 
  //     router.push(routePath);
  //   } else {
  //     console.warn(`Unknown key: ${item.key}`);
  //   }
  // };
  const handleClick = (e: any) => {
    selectedKeys2.value = [];
 
    let matched = false;
    const route = router.getRoutes() || [];
    route.forEach((item) => {
      if (item.name === e.key) {
        router.push(item.path);
        matched = true;
        return; // 跳出当前循环
      }
 
      if (!matched) {
        switch (e.key) {
          case 'InboxPage1':
            router.push(routesConfig[e.key]);
            matched = true;
            return; // 跳出当前循环
          case 'receiver':
            router.push(`${routesConfig[e.key]}?${e.title}`);
            matched = true;
            return; // 跳出当前循环
          case 'sender':
            router.push(`${routesConfig[e.key]}?${e.title}`);
            matched = true;
            return; // 跳出当前循环
          case 'IndexPage1':
            router.push(`${routesConfig[e.key]}`);
            matched = true;
            return; // 跳出当前循环
          default:
            // 处理默认情况,例如记录日志或抛出警告
            console.warn(`Unknown key: ${e.key}`);
        }
      }
    });
  };
 
  const updateSelectedKeys = (keys) => {
    openKeys2.value = findParentKeys(menuItems2.value[0], keys);
    selectedKeys2.value = [keys];
  };
 
  function findParentKeys(data, targetKey, path = []) {
    path.push(data.key);
    if (data.key === targetKey) {
      return path;
    }
 
    for (const item of data.children || []) {
      const result = findParentKeys(item, targetKey, path.slice());
      if (result) {
        return result;
      }
    }
    return null;
  }
  const handleClickMyMenu = (item: any) => {
    selectedKeys.value = [];
    // console.log('handleClickMyMenu:', item);
    router.push({ path: '/email/folder' });
    console.log('updateSelectedKeys111111112333:', openKeys2.value);
  };
</script>
 
<style lang="less" scoped>
  .header-container {
    height: 15vh;
    padding: 20px 40px;
    text-align: center;
  }
 
  .button-group {
    display: flex;
    justify-content: space-around;
  }
 
  .write-button {
    width: 100%;
    margin-top: 10px;
    padding: 0 30px;
  }
 
  .menu-container {
    height: 75vh;
    overflow: auto;
  }
 
  .my-display {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
 
  .my-left {
    margin-left: 5px;
  }
</style>