main.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <Layout style="height: 100%" class="main">
  3. <Header class="header-con" v-if="!headMenuNoShow">
  4. <div class="logo-con">
  5. <img :src="maxLogo" key="max-logo" />
  6. <!-- <img v-show="collapsed" :src="minLogo" key="min-logo" /> -->
  7. </div>
  8. <header-bar :collapsed="collapsed" @on-coll-change="handleCollapsedChange">
  9. <user :message-unread-count="unreadCount" :user-avatar="userAvatar" />
  10. <language v-if="$config.useI18n" @on-lang-change="setLocal" style="margin-right: 10px" :lang="local" />
  11. <header-notice></header-notice>
  12. <fullscreen v-model="isFullscreen" style="margin-right: 10px" />
  13. <Reload @on-reload="handleReload"></Reload>
  14. <error-store
  15. v-if="$config.plugin['error-store'] && $config.plugin['error-store'].showInHeader"
  16. :has-read="hasReadErrorPage"
  17. :count="errorCount"
  18. ></error-store>
  19. <header-search></header-search>
  20. </header-bar>
  21. </Header>
  22. <Layout>
  23. <Sider
  24. hide-trigger
  25. collapsible
  26. :width="childMenuList.length ? 220 : 90"
  27. :collapsed-width="isMobile ? 0 : 90"
  28. v-model="collapsed"
  29. class="left-sider"
  30. :style="{ overflow: 'hidden' }"
  31. v-if="!headMenuNoShow"
  32. >
  33. <side-menu
  34. accordion
  35. ref="sideMenu"
  36. :active-name="$route.path"
  37. :collapsed="collapsed"
  38. @on-select="turnToPage"
  39. :menu-list="menuList"
  40. >
  41. <!-- 需要放在菜单上面的内容,如Logo,写在side-menu标签内部,如下 -->
  42. </side-menu>
  43. </Sider>
  44. <Content class="main-content-con">
  45. <Layout class="main-layout-con">
  46. <Content class="content-wrapper">
  47. <div class="tag-nav-wrapper" v-if="!headMenuNoShow">
  48. <tags-nav :value="$route" @input="handleClick" :list="tagNavList" @on-close="handleCloseTag" />
  49. </div>
  50. <!-- <keep-alive :include="cacheList">
  51. <router-view v-if="reload" style="min-height: 600px" />
  52. </keep-alive> -->
  53. <keep-alive>
  54. <router-view v-if="$route.meta.keepAlive && reload" style="min-height: 600px"></router-view>
  55. </keep-alive>
  56. <router-view v-if="!$route.meta.keepAlive && reload" style="min-height: 600px"></router-view>
  57. <!-- <router-view v-if="reload" style="min-height: 600px" /> -->
  58. <!--<ABackTop :height="100" :bottom="80" :right="50" container=".content-wrapper"></ABackTop>-->
  59. </Content>
  60. <i-copyright v-if="!headMenuNoShow" />
  61. </Layout>
  62. </Content>
  63. </Layout>
  64. <!-- <div class="open-image" @click="clear" v-if="openImage">
  65. <img src="@/assets/images/wechat_demo.png" alt="" />
  66. </div> -->
  67. </Layout>
  68. </template>
  69. <script>
  70. import iCopyright from '@/components/copyright';
  71. import SideMenu from './components/side-menu';
  72. import HeaderBar from './components/header-bar';
  73. import TagsNav from './components/tags-nav';
  74. import User from './components/user';
  75. import ABackTop from './components/a-back-top';
  76. import Fullscreen from './components/fullscreen';
  77. import Language from './components/language';
  78. import ErrorStore from './components/error-store';
  79. import HeaderSearch from './components/header-search';
  80. import HeaderNotice from './components/header-notice';
  81. import Reload from './components/reload';
  82. import Setting from '@/setting';
  83. import iView from 'iview';
  84. import { mapMutations, mapActions, mapGetters, mapState } from 'vuex';
  85. import { getNewTagList, routeEqual, getMenuopen, getCookies, setCookies } from '@/libs/util';
  86. import { getLogo } from '@/api/common';
  87. import routers from '@/router/routers';
  88. import minLogo from '@/assets/images/logo-small.png';
  89. import maxLogo from '@/assets/images/logo.png';
  90. import './main.less';
  91. export default {
  92. name: 'Main',
  93. components: {
  94. SideMenu,
  95. HeaderBar,
  96. Language,
  97. TagsNav,
  98. Fullscreen,
  99. ErrorStore,
  100. User,
  101. ABackTop,
  102. iCopyright,
  103. HeaderSearch,
  104. HeaderNotice,
  105. Reload
  106. },
  107. data() {
  108. return {
  109. collapsed: JSON.parse(getCookies('collapsed') || 'true'),
  110. minLogo,
  111. maxLogo,
  112. isFullscreen: false,
  113. reload: true,
  114. screenWidth: '',
  115. openImage: true,
  116. headMenuNoShow: false,
  117. };
  118. },
  119. computed: {
  120. ...mapState('menus', ['childMenuList']),
  121. ...mapGetters(['errorCount']),
  122. ...mapState('media', ['isMobile']),
  123. tagNavList() {
  124. return this.$store.state.app.tagNavList;
  125. },
  126. tagRouter() {
  127. return this.$store.state.app.tagRouter;
  128. },
  129. userAvatar() {
  130. return this.$store.state.user.avatarImgPath;
  131. },
  132. cacheList() {
  133. const list = [
  134. 'ParentView',
  135. ...(this.tagNavList.length
  136. ? this.tagNavList.filter((item) => !(item.meta && item.meta.notCache)).map((item) => item.name)
  137. : []),
  138. ];
  139. return list;
  140. },
  141. menuList() {
  142. let menus = this.$store.state.menus.menusName;
  143. let newArray = [];
  144. menus.forEach((now, index) => {
  145. newArray[index] = now;
  146. if (newArray[index].children && now.children) {
  147. newArray[index].children = now.children.filter((item) => {
  148. return !item.auth;
  149. });
  150. }
  151. });
  152. return newArray;
  153. // return this.$store.state.menus.menusName
  154. },
  155. local() {
  156. return this.$store.state.app.local;
  157. },
  158. hasReadErrorPage() {
  159. return this.$store.state.app.hasReadErrorPage;
  160. },
  161. unreadCount() {
  162. return this.$store.state.user.unreadCount;
  163. },
  164. },
  165. methods: {
  166. ...mapMutations(['setBreadCrumb', 'setTagNavList', 'addTag', 'setLocal', 'setHomeRoute', 'closeTag']),
  167. ...mapActions(['handleLogin', 'getUnreadMessageCount']),
  168. turnToPage(route, all) {
  169. let { path, name, params, query } = {};
  170. if (typeof route === 'string' && !all) path = route;
  171. else if (typeof route === 'string' && all) name = route;
  172. else {
  173. path = route.path;
  174. name = route.name;
  175. params = route.params;
  176. query = route.query;
  177. }
  178. this.$router.push({
  179. path,
  180. name,
  181. params,
  182. query,
  183. });
  184. },
  185. handleCollapsedChange(state) {
  186. this.collapsed = state;
  187. setCookies('collapsed', state);
  188. },
  189. handleCloseTag(res, type, route) {
  190. if (type !== 'others') {
  191. if (type === 'all') {
  192. this.turnToPage(this.$config.homeName, 'all');
  193. } else {
  194. if (routeEqual(this.$route, route)) {
  195. this.closeTag(route);
  196. }
  197. }
  198. }
  199. if (res.length === 1 && res[0].name === this.$config.homeName) {
  200. this.$router.push({ name: this.$config.homeName });
  201. }
  202. this.setTagNavList(res);
  203. },
  204. handleClick(item) {
  205. this.turnToPage(item);
  206. },
  207. getLogo() {
  208. let logo = this.$store.state.userInfo.logo;
  209. let logoSmall = this.$store.state.userInfo.logoSmall;
  210. this.maxLogo = logo || this.maxLogo;
  211. this.minLogo = logoSmall || this.minLogo;
  212. getLogo().then((res) => {
  213. localStorage.setItem('ADMIN_TITLE', res.data.site_name);
  214. this.minLogo = res.data.logo_square;
  215. this.maxLogo = res.data.logo;
  216. });
  217. },
  218. handleReload() {
  219. this.reload = false;
  220. // if (Setting.showProgressBar) iView.LoadingBar.start()
  221. this.$nextTick(() => {
  222. this.reload = true;
  223. // if (Setting.showProgressBar) iView.LoadingBar.finish()
  224. });
  225. },
  226. clear() {
  227. this.openImage = false;
  228. },
  229. },
  230. watch: {
  231. $route(newRoute) {
  232. this.headMenuNoShow = this.$route.meta.fullScreen;
  233. let openNames = getMenuopen(newRoute, this.menuList);
  234. this.$store.commit('menus/setopenMenus', openNames);
  235. const { name, query, params, meta } = newRoute;
  236. this.addTag({
  237. route: { name, query, params, meta },
  238. type: 'push',
  239. });
  240. this.setBreadCrumb(newRoute);
  241. this.setTagNavList(getNewTagList(this.tagNavList, newRoute));
  242. this.$refs.sideMenu.updateOpenName(newRoute.path);
  243. },
  244. },
  245. mounted() {
  246. this.headMenuNoShow = this.$route.meta.fullScreen;
  247. this.getLogo();
  248. this.screenWidth = document.body.clientWidth;
  249. window.onresize = () => {
  250. return (() => {
  251. this.screenWidth = document.body.clientWidth;
  252. if (this.screenWidth <= 1060) {
  253. this.collapsed = true;
  254. setCookies('collapsed', true);
  255. } else {
  256. this.collapsed = false;
  257. setCookies('collapsed', false);
  258. }
  259. })();
  260. };
  261. /**
  262. * @description 初始化设置面包屑导航和标签导航
  263. */
  264. this.setTagNavList();
  265. this.setHomeRoute(routers);
  266. const { name, params, query, meta } = this.$route;
  267. this.addTag({
  268. route: { name, params, query, meta },
  269. });
  270. this.setBreadCrumb(this.$route);
  271. // 设置初始语言
  272. this.setLocal(this.$i18n.locale);
  273. // 如果当前打开页面不在标签栏中,跳到homeName页
  274. if (!this.tagNavList.find((item) => item.name === this.$route.name)) {
  275. this.$router.push({
  276. name: this.$config.homeName,
  277. });
  278. }
  279. // 获取未读消息条数
  280. this.getUnreadMessageCount();
  281. },
  282. };
  283. </script>
  284. <style lang="less">
  285. .main .header-con {
  286. padding: 0 0px 0 0px;
  287. display: flex;
  288. background: linear-gradient(270deg, #1570ef 0%, #1570ef 100%);
  289. }
  290. .main .logo-con img {
  291. height: 50px;
  292. transition: all 1s;
  293. }
  294. .main .tag-nav-wrapper {
  295. // height: 10px;
  296. background: unset;
  297. }
  298. .open-image {
  299. display: flex;
  300. align-items: center;
  301. justify-content: center;
  302. position: fixed;
  303. background-color: rgba(0, 0, 0, 0.6);
  304. height: 100%;
  305. width: 100%;
  306. top: 0;
  307. left: 0;
  308. z-index: 1000;
  309. img {
  310. width: 800px;
  311. }
  312. }
  313. </style>