From 49de04bb5e29baf43979338436de42a64444e8fb Mon Sep 17 00:00:00 2001 From: wangsongsole Date: Tue, 18 Apr 2023 19:54:02 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E9=89=B4=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assets/api.js | 50 ++++- src/pages/login/login.js | 16 +- src/router/index.js | 414 +++++++++++++++++++++++++++++--------- src/tools/createRouter.js | 61 ++++++ 4 files changed, 440 insertions(+), 101 deletions(-) create mode 100644 src/tools/createRouter.js diff --git a/src/assets/api.js b/src/assets/api.js index 48a073fc..bb7628ac 100644 --- a/src/assets/api.js +++ b/src/assets/api.js @@ -257,8 +257,8 @@ const req = (method, url, params, responseType) => { window.location.href = '/'; }, 2000); } else if (response.code == 401) { - // Notify.clear() - // Notify.error(response.message) + Notify.clear(); + Notify.error(response.message); } }) .catch(); @@ -274,6 +274,46 @@ const req = (method, url, params, responseType) => { .catch((error) => console.error('Error:', error)); }; +const getCodesHttp = (url) => { + var obj = null; + const token = sessionStorage.getItem('unified-token'); + obj = { + method: 'get', + headers: { + Authorization: `Bearer ${token}` + } + }; + + let http = fetch(url, obj).then((res) => { + window.timeshow = res.headers.get('Date-Time'); + if (res.status == 200) { + return res.text(); + } else { + switch (res.status) { + case 401: + res + .json() + .then((response) => { + if (response.code == 401) { + Notify.clear(); + Notify.error(response.message); + setTimeout(() => { + window.location.href = '/'; + }, 2000); + } + }) + .catch(); + break; + default: + return; + } + } + }); + return http + .then((response) => response) + .catch((error) => console.error('Error:', error)); +}; + export const handelResponse = (res, cb, errCb) => { if (res && res.code == 200) { cb(res.data, res.message); @@ -718,9 +758,9 @@ export const postLoginStandby = (data) => { return req('post', baseurl + '/auth/login/original/mobile', data); }; -//同统一登录获取菜单 -export const getMenus = () => { - return req('unified', `${ApiUrl}/v1/menu/tree/marketing`); +//获取权限codes +export const getCodes = () => { + return getCodesHttp(`${ApiUrl}/v1/menu/myCodes?systemId=2`); }; // logout退出登录 diff --git a/src/pages/login/login.js b/src/pages/login/login.js index 19b39e42..ed3d1e34 100644 --- a/src/pages/login/login.js +++ b/src/pages/login/login.js @@ -6,9 +6,10 @@ import { getProductInfoSelect, postLoginCode, getVoucherWarningAccount, - getMenus + getCodes } from '@/assets/api.js'; import { Notify, BlockLoading } from 'zent'; +import { getMenus } from '@/tools/createRouter.js'; export default class App extends Component { constructor(props) { @@ -68,18 +69,21 @@ export default class App extends Component { }); }); - getMenus().then(({ list }) => { + /* 获取codes */ + getCodes().then((data) => { + const { codes } = JSON.parse(data); + let menuList = getMenus(codes); + Notify.clear(); Notify.success(msg); this.getProductList(); this.getAccountList(); sessionStorage.setItem('showflag', true); - let menuList = list; - if (list && list.length > 0) { - sessionStorage.setItem('activeMenu', JSON.stringify(list)); + if (menuList && menuList.length > 0) { + sessionStorage.setItem('activeMenu', JSON.stringify(menuList)); let market = menuList.filter((item) => item.id == 16); - if (market && market[0].children.length > 0) { + if (market && market[0]?.children.length > 0) { //菜单有营销计划 sessionStorage.setItem('linkshowname', '营销计划管理'); sessionStorage.setItem('pathname2', '/home/plan-list'); diff --git a/src/router/index.js b/src/router/index.js index 8bd8e888..ffbd6ea8 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,262 +1,262 @@ -import Extension from "@/pages/extension/main/main"; -import Distributor from "@/pages/distributor/main/main"; -import ExchangeCodeList from "@/pages/exchangecode/list/list"; -import ExchangeCodeAdd from "@/pages/exchangecode/add/add"; -import ExchangeCommodity from "@/pages/exchangecode/commodity/commodity"; -import ProductEdit from "@/pages/exchangecode/commodity/goodedit.js"; -import ExchangePage from "@/pages/exchangepage/main/main"; -import OrderList from "@/pages/order/list/list"; -import downloadList from "@/pages/order/downloadList/list"; //订单下载队列 -import OrderDetails from "@/pages/order/more/more"; -import OrderAdd from "@/pages/order/add/add"; -import Plan from "@/pages/plan/main/main"; -import PlanList from "@/pages/plan/list/list"; -import Qrcode from "@/pages/plan/qrcode/qrcode"; -import PlanAdd from "@/pages/plan/add/add"; -import PlanEdit from "@/pages/plan/add/edit"; -import PlanEditCopy from "@/pages/plan/add/plan-copy"; -import KeyList from "@/pages/plan/key/list"; -import KeysList from "@/pages/plan/keyList"; -import KeyOrderList from "@/pages/plan/keyorder/keyorder"; -import KeyEdit from "@/pages/plan/key/edit.js"; -import keyAddEdit from "@/pages/plan/key/UseKeyAddEdit.jsx"; -import KeyDetail from "@/pages/plan/key/detail/list.js"; -import KeyLog from "@/pages/plan/key/detail/log.js"; -import AccountList from "@/pages/system/account/list/list"; -import AccountAdd from "@/pages/system/account/add/add"; -import SubAccountList from "@/pages/system/subAccount/list/list"; -import SubAccountAdd from "@/pages/system/subAccount/add/add"; -import RoleList from "@/pages/system/role/list/list"; -import RoleAdd from "@/pages/system/role/add/add"; -import Station from "@/pages/system/station/main"; -import Customize from "@/pages/system/customize/main.js"; -import MyMould from "@/pages/system/systemMould/main.js"; -import Menu from "@/pages/system/menu/main/main"; -import DistriButorList from "@/pages/distributor/list/list"; -import DistriButorAdd from "@/pages/distributor/add/add"; -import DistriButorEdit from "@/pages/distributor/add/add"; -import MyTempMouldAdd from "@/pages/exchangepage/template/main.js"; -import MyExChangeTemplate from "@/pages/exchangepage/mytemplate/main.js"; -import ExchangeCodeAdds from "@/pages/exchangecode/exchangecodeAdd"; -import CouponList from "@/pages/coupon/list"; -import CouponAddEdit from "@/pages/coupon/addEdit"; -import UseCouponCommodity from "@/pages/coupon/commodity"; +import Extension from '@/pages/extension/main/main'; +import Distributor from '@/pages/distributor/main/main'; +import ExchangeCodeList from '@/pages/exchangecode/list/list'; +import ExchangeCodeAdd from '@/pages/exchangecode/add/add'; +import ExchangeCommodity from '@/pages/exchangecode/commodity/commodity'; +import ProductEdit from '@/pages/exchangecode/commodity/goodedit.js'; +import ExchangePage from '@/pages/exchangepage/main/main'; +import OrderList from '@/pages/order/list/list'; +import downloadList from '@/pages/order/downloadList/list'; //订单下载队列 +import OrderDetails from '@/pages/order/more/more'; +import OrderAdd from '@/pages/order/add/add'; +import Plan from '@/pages/plan/main/main'; +import PlanList from '@/pages/plan/list/list'; +import Qrcode from '@/pages/plan/qrcode/qrcode'; +import PlanAdd from '@/pages/plan/add/add'; +import PlanEdit from '@/pages/plan/add/edit'; +import PlanEditCopy from '@/pages/plan/add/plan-copy'; +import KeyList from '@/pages/plan/key/list'; +import KeysList from '@/pages/plan/keyList'; +import KeyOrderList from '@/pages/plan/keyorder/keyorder'; +import KeyEdit from '@/pages/plan/key/edit.js'; +import keyAddEdit from '@/pages/plan/key/UseKeyAddEdit.jsx'; +import KeyDetail from '@/pages/plan/key/detail/list.js'; +import KeyLog from '@/pages/plan/key/detail/log.js'; +import AccountList from '@/pages/system/account/list/list'; +import AccountAdd from '@/pages/system/account/add/add'; +import SubAccountList from '@/pages/system/subAccount/list/list'; +import SubAccountAdd from '@/pages/system/subAccount/add/add'; +import RoleList from '@/pages/system/role/list/list'; +import RoleAdd from '@/pages/system/role/add/add'; +import Station from '@/pages/system/station/main'; +import Customize from '@/pages/system/customize/main.js'; +import MyMould from '@/pages/system/systemMould/main.js'; +import Menu from '@/pages/system/menu/main/main'; +import DistriButorList from '@/pages/distributor/list/list'; +import DistriButorAdd from '@/pages/distributor/add/add'; +import DistriButorEdit from '@/pages/distributor/add/add'; +import MyTempMouldAdd from '@/pages/exchangepage/template/main.js'; +import MyExChangeTemplate from '@/pages/exchangepage/mytemplate/main.js'; +import ExchangeCodeAdds from '@/pages/exchangecode/exchangecodeAdd'; +import CouponList from '@/pages/coupon/list'; +import CouponAddEdit from '@/pages/coupon/addEdit'; +import UseCouponCommodity from '@/pages/coupon/commodity'; /* 基础路由 */ const router = [ { - path: "/home/extension", + path: '/home/extension', component: Extension, meta: { exact: true } }, { - path: "/home/distributor", + path: '/home/distributor', component: Distributor, meta: { exact: true } }, { - path: "/home/exchangepage", + path: '/home/exchangepage', component: ExchangePage, meta: { exact: true } }, { - path: "/home/mytempMould", + path: '/home/mytempMould', component: MyTempMouldAdd, meta: { exact: true } }, { - path: "/home/myexchangetemplate", + path: '/home/myexchangetemplate', component: MyExChangeTemplate, meta: { exact: true } }, { - path: "/home/order-list", + path: '/home/order-list', component: OrderList, meta: { exact: true } }, { - path: "/home/download-orderlist", + path: '/home/download-orderlist', component: downloadList, meta: { exact: true } }, { - path: "/home/order-addetails", + path: '/home/order-addetails', component: OrderDetails, meta: { exact: true } }, { - path: "/home/order-add", + path: '/home/order-add', component: OrderAdd, meta: { exact: true } }, { - path: "/home/plan", + path: '/home/plan', component: Plan, meta: { exact: true } }, { - path: "/home/plan-list", + path: '/home/plan-list', component: PlanList, meta: { exact: true } }, { - path: "/home/plan-qrcode", + path: '/home/plan-qrcode', component: Qrcode, meta: { exact: true } }, { - path: "/home/plan-create", + path: '/home/plan-create', component: PlanAdd, meta: { exact: true } }, { - path: "/home/plan-edit", + path: '/home/plan-edit', component: PlanEdit, meta: { exact: true } }, { - path: "/home/plan-edit-copy", + path: '/home/plan-edit-copy', component: PlanEditCopy, meta: { exact: true } }, { - path: "/home/key-list", + path: '/home/key-list', component: KeyList, meta: { exact: true } }, { - path: "/home/plan-keyList", + path: '/home/plan-keyList', component: KeysList, meta: { exact: true } }, { - path: "/home/key-edit", + path: '/home/key-edit', component: KeyEdit, meta: { exact: true } }, { - path: "/home/key-addEdit", + path: '/home/key-addEdit', component: keyAddEdit, meta: { exact: true } }, { - path: "/home/key-detail", + path: '/home/key-detail', component: KeyDetail, meta: { exact: true } }, { - path: "/home/key-log", + path: '/home/key-log', component: KeyLog, meta: { exact: true } }, { - path: "/home/addkeyorder", + path: '/home/addkeyorder', component: KeyOrderList, meta: { exact: true } }, { - path: "/home/system/account-add", + path: '/home/system/account-add', component: AccountAdd, meta: { exact: true } }, { - path: "/home/system/subaccount-list", + path: '/home/system/subaccount-list', component: SubAccountList, meta: { exact: true } }, { - path: "/home/system/subaccount-add", + path: '/home/system/subaccount-add', component: SubAccountAdd, meta: { exact: true } }, { - path: "/home/system/role-list", + path: '/home/system/role-list', component: RoleList, meta: { exact: true } }, { - path: "/home/system/role-add", + path: '/home/system/role-add', component: RoleAdd, meta: { exact: true } }, { - path: "/home/system/station", + path: '/home/system/station', component: Station, meta: { exact: true } }, { - path: "/home/system/customize", + path: '/home/system/customize', component: Customize, meta: { exact: true } }, { - path: "/home/system/menu", + path: '/home/system/menu', component: Menu, meta: { exact: true } }, { - path: "/home/system/my-mould", + path: '/home/system/my-mould', component: MyMould, meta: { exact: true } }, { - path: "/home/distributor-list", + path: '/home/distributor-list', component: DistriButorList, meta: { exact: true } }, { - path: "/home/distributor-add", + path: '/home/distributor-add', component: DistriButorAdd, meta: { exact: true } }, { - path: "/home/distributor-edit", + path: '/home/distributor-edit', component: DistriButorEdit, meta: { exact: true } }, { - path: "/home/exchangecode-list", + path: '/home/exchangecode-list', component: ExchangeCodeList, meta: { exact: true } }, { - path: "/home/exchangecode-add", + path: '/home/exchangecode-add', component: ExchangeCodeAdd, meta: { exact: true } }, { - path: "/home/exchange-addcommodity", + path: '/home/exchange-addcommodity', component: ExchangeCommodity, meta: { exact: true } }, { - path: "/home/product-edit", + path: '/home/product-edit', component: ProductEdit, meta: { exact: true } }, { - path: "/home/system/account-list", + path: '/home/system/account-list', component: AccountList, meta: { exact: true } }, { - path: "/home/exchangecode-exchangecodeAdd", + path: '/home/exchangecode-exchangecodeAdd', component: ExchangeCodeAdds, meta: { exact: true } }, { - path: "/home/coupon-list", + path: '/home/coupon-list', component: CouponList, meta: { exact: true } }, { - path: "/home/coupon-add-edit", + path: '/home/coupon-add-edit', component: CouponAddEdit, meta: { exact: true } }, { - path: "/home/coupon-commodity", + path: '/home/coupon-commodity', component: UseCouponCommodity, meta: { exact: true } } @@ -264,10 +264,244 @@ const router = [ /* 不显示路由 */ const noShowSubMenu = [ - "/home/extension", - "/home/plan-create", - "/home/mytempMould", - "/home/edittemplate" + '/home/extension', + '/home/plan-create', + '/home/mytempMould', + '/home/edittemplate' ]; export { router, noShowSubMenu }; + +export const routers = [ + { + id: 14, + icon: '', + title: '系统管理', + level: 1, + auth: true, + router: '/system', + children: [ + { + id: 20, + title: '账号管理', + level: 5, + auth: true, + router: '/home/system/account-list', + code: ['get-auth-admin'] + }, + { + id: 23, + title: '菜单管理', + level: 5, + auth: true, + router: '/home/system/menu', + code: ['get-auth-menu-tree'] + }, + { + id: 31, + title: '部门管理', + level: 10, + auth: true, + router: '', + code: ['get-auth-department'] + }, + { + id: 22, + title: '岗位管理', + level: 5, + auth: true, + router: '/home/system/station', + code: ['get-auth-departmentjob'] + }, + { + id: 21, + title: '角色管理', + level: 5, + auth: true, + router: '/home/system/role-list', + code: ['get-auth-adminrole'] + }, + { + id: 24, + title: '定制化模板', + level: 5, + auth: true, + router: '/home/system/customize', + code: ['post-plan-theme-custom'] + }, + { + id: 133, + title: '系统营销模板', + level: 5, + auth: true, + router: '/home/system/my-mould', + code: ['get-plan-theme-system'] + }, + { + id: 138, + title: '子账号管理', + level: 5, + auth: true, + router: '/home/system/subaccount-list', + children: [ + { + id: 140, + title: '账号列表', + level: 5, + auth: true, + router: '/home/system/subaccount-list', + code: ['get-auth-admin-sonindex'] + } + ] + } + ] + }, + { + id: 15, + icon: '', + title: '分销商管理', + level: 1, + auth: true, + router: '/home/distributor-list', + children: [ + { + id: 65, + title: '分销商列表', + level: 5, + auth: true, + router: '/home/distributor-list', + code: ['get-reseller'] + } + ] + }, + { + id: 16, + icon: '', + title: '营销计划管理', + level: 1, + auth: true, + router: '/home/plan-list', + children: [ + { + id: 75, + title: '营销计划列表', + level: 5, + auth: true, + router: '/home/plan-list', + code: ['get-plan'] + }, + { + id: 184, + title: 'Keys查询', + level: 5, + auth: true, + router: '/home/plan-keyList', + code: ['get-keys'] + }, + { + id: 214, + title: '二维码小工具', + level: 5, + auth: true, + router: '/home/plan-qrcode', + code: ['qrcodetool'] + } + ] + }, + { + id: 17, + icon: '', + title: '兑换码管理', + level: 1, + auth: true, + router: '/home/exchangecode-list', + children: [ + { + id: 82, + title: '兑换码列表', + level: 5, + auth: true, + router: '/home/exchangecode-list', + code: ['get-codes'] + } + ] + }, + { + id: 195, + icon: '', + title: '优惠券管理', + level: 1, + auth: true, + router: '/home/coupon-list', + children: [ + { + id: 196, + title: '优惠券列表', + level: 5, + auth: true, + router: '/home/coupon-list', + code: ['get-coupon'] + } + ] + }, + { + id: 18, + icon: '', + title: '落地页管理', + level: 1, + auth: true, + router: '/home/exchangepage', + children: [ + { + id: 118, + title: '落地页列表', + level: 5, + auth: true, + router: '/home/exchangepage', + code: ['get-plan-theme'] + }, + { + id: 119, + title: '我的模板', + level: 5, + auth: true, + router: '/home/myexchangetemplate', + code: ['get-plan-theme'] + }, + { + id: 175, + title: '模板中心', + level: 5, + auth: true, + router: '/home/mytempMould', + code: ['get-plan-theme-getthemetype'] + } + ] + }, + { + id: 19, + icon: '', + title: '订单管理', + level: 1, + auth: true, + router: '/home/order-list', + children: [ + { + id: 98, + title: '订单列表', + level: 5, + auth: true, + router: '/home/order-list', + code: ['get-order'] + }, + { + id: 215, + title: '订单下载列表', + level: 5, + auth: true, + router: '/home/download-orderlist', + code: ['order_download_list'] + } + ] + } +]; diff --git a/src/tools/createRouter.js b/src/tools/createRouter.js new file mode 100644 index 00000000..e9c2ca29 --- /dev/null +++ b/src/tools/createRouter.js @@ -0,0 +1,61 @@ +import { routers } from '@/router'; +import intersection from 'lodash/intersection'; +import cloneDeep from 'lodash/cloneDeep'; +let asyncCodes = []; +/* 求路由自身是否有权限 */ + +function calculateAuth(data = []) { + data.map((item) => { + if (item.children && item.children.length) { + calculateAuth(item.children); + } else { + if (item.code) { + if (!intersection(asyncCodes, item.code).length) { + item.auth = false; + } + } + } + }); +} + +/* 生成菜单 */ +function createdMenus(data, newData = []) { + data.map((items) => { + if (items.auth) { + const { id, title, icon, router, level, children } = items; + newData.push({ + id, + title, + icon, + router, + level, + children: children ? createdMenus(children) : undefined + }); + } + }); + return newData; +} + +/* 控制菜单权限 */ +function setAuth(menus) { + /* 求路由父子级相关权限 */ + menus.map((items) => { + items.children.map((item) => { + if (item.children) { + item.auth = !!item.children?.filter((item) => item.auth).length; + } + }); + }); + + menus.map((items) => { + items.auth = !!items.children?.filter((item) => item.auth).length; + }); +} + +export function getMenus(codes) { + asyncCodes = codes; + const menus = cloneDeep(routers); + calculateAuth(menus); + setAuth(menus); + return createdMenus(menus); +}