import { type AccountFormState, type PeopleModel, transformAccount } from '@/model'; import type { UserModel } from '@/model/system.model'; import request from '@/request/alova'; import router from '@/router'; interface Menu { label: string; title: string; key: string; children?: Menu[]; } export interface AccountModel { token: string; local: PeopleModel; menus?: Menu[]; user?: UserModel; } export function loginMethod(data: AccountFormState) { return request.Post(`/auth/token`, data, { transform(data, headers) { return `Bearer ${data.access_token}`; }, }); } export function accountMethod(token: string) { return request.Get(`/system/user/getInfo`, { headers: { Authorization: token }, transform(data: any, headers) { return { token, local: transformAccount(data.user), user: data.user, }; }, meta: { unconvert: true }, }); } export function getMenusMethod(account: AccountModel) { const routes = new Set(router.getRoutes().map((route) => route.path)); const transformMenus = (data: any[], prefix?: string) => { const menus: Menu[] = []; if (!Array.isArray(data)) return menus; for (const menu of data) { const path = [prefix, menu?.path].filter((v) => v != null).join('/'); const title = menu?.meta?.title ?? path; const children = transformMenus(menu.children, path === '/' ? '' : path); if (children.length > 1) { menus.push({ key: path, title, label: title, children }); } else if (children.length === 1) { menus.push(children[0]); } else if (routes.has(path)) { menus.push({ key: path, title, label: title }); } } return menus; }; return request.Get(`/system/menu/getRouters`, { headers: { Authorization: account.token }, transform(data) { // data.push( // { // path: '/follow', // meta: { // title: '随访管理', // }, // children: [ // { // path: 'plan', // meta: { // title: '随访计划', // }, // }, // { // path: 'task', // meta: { // title: '随访任务', // }, // }, // { // path: 'assessment', // meta: { // title: '随访评估', // }, // }, // ], // }, // { // path: '/tcmRecuperation', // meta: { // title: '中医调养', // }, // children: [ // { // path: 'preserve', // meta: { // title: '服务项目维护', // }, // }, // { // path: 'system', // meta: { // title: '系统服务包', // }, // }, // { // path: 'institution', // meta: { // title: '机构服务包', // }, // }, // ], // } // ); // console.log(data); return { ...account, menus: transformMenus(data) }; }, }); }