router.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import store from './store'
  4. import url from 'url'
  5. import AdminLTE from 'components/AdminLTE.vue'
  6. const Dashboard = () => import( /* webpackChunkName: 'dashboard' */ 'components/Dashboard.vue');
  7. const ContentRoot = () => import( /* webpackChunkName: 'root' */ 'components/ContentRoot.vue')
  8. const Screen = () => import(/* webpackChunkName: 'screen' */ 'components/Screen.vue')
  9. const DeviceList = () => import( /* webpackChunkName: 'device' */ 'components/DeviceList.vue')
  10. const DeviceTree = () => import( /* webpackChunkName: 'device' */ 'components/DeviceTree.vue')
  11. const ChannelList = () => import( /* webpackChunkName: 'device' */ 'components/ChannelList.vue')
  12. const PlaybackList = () => import(/* webpackChunkName: 'device' */ 'components/PlaybackList.vue')
  13. const PlaybackTimebox = () => import(/* webpackChunkName: 'device' */ 'components/PlaybackTimebox.vue')
  14. const AlarmList = () => import(/* webpackChunkName: 'alarm' */ 'components/AlarmList.vue')
  15. const CascadeList = () => import(/* webpackChunckName: 'cascade' */ 'components/CascadeList.vue')
  16. const UserList = () => import(/* webpackChunckName: 'user' */ 'components/UserList.vue')
  17. const Config = () => import(/* webpackChunkName: 'config' */ 'components/Config.vue')
  18. const About = () => import(/* webpackChunkName: 'about' */ 'components/About.vue')
  19. const CloudRecordChannelList = () => import(/* webpackChunkName: 'cloudrecord' */ 'components/CloudRecordChannelList.vue')
  20. const CloudRecordListBox = () => import(/* webpackChunkName: 'cloudrecord' */ 'components/CloudRecordListBox.vue')
  21. const CloudRecordTimeBox = () => import(/* webpackChunkName: 'cloudrecord' */ 'components/CloudRecordTimeBox.vue')
  22. const Tester = () => import(/* webpackChunkName: 'tester' */ 'components/Tester.vue')
  23. const TesterReport =() => import(/* webpackChunkName: 'tester' */ 'components/TesterReport.vue')
  24. const WhiteList = () => import(/* webpackChunkName: 'white' */ 'components/WhiteList.vue')
  25. const BlackList = () => import(/* webpackChunkName: 'black' */ 'components/BlackList.vue')
  26. Vue.use(Router);
  27. const router = new Router({
  28. routes: [
  29. {
  30. path: '/',
  31. component: AdminLTE,
  32. children: [{
  33. path: '',
  34. component: Dashboard,
  35. props: true
  36. }, {
  37. path: 'screen',
  38. component: ContentRoot,
  39. children: [
  40. {
  41. path: '',
  42. component: Screen,
  43. props: true
  44. }
  45. ]
  46. }, {
  47. path: 'test',
  48. component: ContentRoot,
  49. children: [
  50. {
  51. path: '',
  52. component: Tester,
  53. props: true
  54. }
  55. ]
  56. }, {
  57. path: 'testreport',
  58. component: ContentRoot,
  59. children: [
  60. {
  61. path: '',
  62. component: TesterReport,
  63. props: true
  64. }
  65. ]
  66. }, {
  67. path: 'devices',
  68. component: ContentRoot,
  69. children: [
  70. {
  71. path: '',
  72. redirect: '1'
  73. }, {
  74. path: 'tree',
  75. component: DeviceTree,
  76. props: true,
  77. }, {
  78. path: ':page',
  79. component: DeviceList,
  80. props: true
  81. }, {
  82. path: 'channels/:devid/:page',
  83. component: ChannelList,
  84. props: true
  85. }, {
  86. path: 'playback/list/:devid/:channel/:day?',
  87. component: PlaybackList,
  88. props: true
  89. }, {
  90. path: 'playback/timebox/:devid/:channel/:day?',
  91. component: PlaybackTimebox,
  92. props: true
  93. }
  94. ]
  95. }, {
  96. path: 'cloudrecord',
  97. component: ContentRoot,
  98. children: [{
  99. path: '',
  100. redirect: '1'
  101. },{
  102. path: ':page',
  103. component: CloudRecordChannelList,
  104. props: true
  105. }, {
  106. path: 'listview/:serial/:code/:day?',
  107. component: CloudRecordListBox,
  108. props: true
  109. }, {
  110. path: 'timeview/:serial/:code/:day?',
  111. component: CloudRecordTimeBox,
  112. props: true
  113. }]
  114. },{
  115. path: 'alarms',
  116. meta: {},
  117. component: ContentRoot,
  118. children: [
  119. {
  120. path: '',
  121. redirect: '1'
  122. }, {
  123. path: ':page',
  124. component: AlarmList,
  125. props: true
  126. }
  127. ]
  128. }, {
  129. path: 'cascade',
  130. meta: { needLogin: true },
  131. component: ContentRoot,
  132. children: [
  133. {
  134. path: '',
  135. redirect: '1'
  136. }, {
  137. path: ':page',
  138. component: CascadeList,
  139. props: true
  140. }
  141. ]
  142. }, {
  143. path: 'user',
  144. meta: { needLogin: true },
  145. component: ContentRoot,
  146. children: [
  147. {
  148. path: '',
  149. redirect: '1'
  150. }, {
  151. path: ':page',
  152. component: UserList,
  153. props: true
  154. }
  155. ]
  156. }, {
  157. path: 'white',
  158. meta: { needLogin: true },
  159. component: ContentRoot,
  160. children: [
  161. {
  162. path: '',
  163. redirect: '1'
  164. }, {
  165. path: ':page',
  166. component: WhiteList,
  167. props: true
  168. }
  169. ]
  170. },{
  171. path: 'black',
  172. meta: { needLogin: true },
  173. component: ContentRoot,
  174. children: [
  175. {
  176. path: '',
  177. redirect: '1'
  178. }, {
  179. path: ':page',
  180. component: BlackList,
  181. props: true
  182. }
  183. ]
  184. }, {
  185. path: 'config',
  186. meta: { needLogin: true },
  187. component: Config
  188. }, {
  189. path: 'about',
  190. component: About
  191. }, {
  192. path: 'logout',
  193. async beforeEnter(to, from, next) {
  194. await store.dispatch("logout");
  195. window.location.href = `/login.html`;
  196. }
  197. }, {
  198. path: '*',
  199. redirect: '/'
  200. }
  201. ]
  202. }
  203. ],
  204. linkActiveClass: 'active'
  205. })
  206. router.beforeEach(async (to, from, next) => {
  207. var serverInfo = await store.dispatch("getServerInfo");
  208. var userInfo = await store.dispatch("getUserInfo");
  209. // if (serverInfo && serverInfo.APIAuth === false && !userInfo) {
  210. // next();
  211. // return;
  212. // }
  213. var menuMap = store.state.menus.reduce((pval, cval) => {
  214. pval[cval.path] = cval;
  215. return pval;
  216. }, {})
  217. var pageRoles = []; // 前往页面要求角色列表
  218. var menu = menuMap[to.path];
  219. if (menu) {
  220. pageRoles.push(...(menu.roles || []));
  221. }
  222. if (!userInfo) {
  223. if ((serverInfo && serverInfo.APIAuth === true) || to.matched.some(record => (record.meta.needLogin || record.meta.roles))) {
  224. if (to.fullPath == '/') {
  225. window.location.href = `/login.html`;
  226. } else {
  227. var _url = url.parse(window.location.href, true);
  228. _url.hash = to.fullPath
  229. window.location.href = `/login.html?r=${encodeURIComponent(url.format(_url))}`;
  230. }
  231. return;
  232. }
  233. next();
  234. return;
  235. }
  236. if (pageRoles.length == 0) {
  237. next();
  238. return;
  239. }
  240. if (!pageRoles.some(pr => (userInfo.Roles.some(ur => (ur == pr || ur == '超级管理员'))))) { // 两个角色列表没有交集
  241. console.log("page", to.path, "require roles", pageRoles.join(','));
  242. console.log("user", userInfo.Name, "has roles", userInfo.Roles.join(','));
  243. for(var menu of store.state.menus) { // 取首个有交集的菜单
  244. if(menu.path) {
  245. if(!menu.roles || menu.roles.some(pr => (userInfo.Roles.some(ur => (ur == pr))))) {
  246. next(menu.path);
  247. return;
  248. }
  249. }
  250. }
  251. // next('/devices')
  252. // window.location.href = `/#/devices/1`;
  253. return;
  254. }
  255. next();
  256. })
  257. export default router;