main.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. <Reload @on-reload="handleReload"></Reload>
  13. <fullscreen v-model="isFullscreen" style="margin-right: 10px" />
  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="sider.length ? 220 : 90"
  27. :collapsed-width="isMobile ? 0 : 90"
  28. v-model="collapsed"
  29. :style="{ overflow: 'hidden' }"
  30. v-if="!headMenuNoShow"
  31. >
  32. <side-menu
  33. accordion
  34. ref="sideMenu"
  35. @on-coll-change="handleCollapsedChange"
  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. <div class="tag-nav-wrapper" v-if="!headMenuNoShow">
  47. <tags-nav :value="$route" @input="handleClick" :list="tagNavList" @on-close="handleCloseTag" />
  48. </div>
  49. <Content class="content-wrapper">
  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" class="main-warper"></router-view>
  55. </keep-alive>
  56. <router-view v-if="!$route.meta.keepAlive && reload" class="main-warper"></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. <i-copyright v-if="!headMenuNoShow" />
  60. </Content>
  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. watch: {
  120. sider(val) {
  121. console.log(val);
  122. },
  123. },
  124. computed: {
  125. ...mapGetters(['errorCount']),
  126. ...mapState('menu', ['sider']),
  127. ...mapState('media', ['isMobile']),
  128. tagNavList() {
  129. return this.$store.state.app.tagNavList;
  130. },
  131. tagRouter() {
  132. return this.$store.state.app.tagRouter;
  133. },
  134. userAvatar() {
  135. return this.$store.state.user.avatarImgPath;
  136. },
  137. cacheList() {
  138. const list = [
  139. 'ParentView',
  140. ...(this.tagNavList.length
  141. ? this.tagNavList.filter((item) => !(item.meta && item.meta.notCache)).map((item) => item.name)
  142. : []),
  143. ];
  144. return list;
  145. },
  146. menuList() {
  147. let menus = this.$store.state.menus.menusName;
  148. let newArray = [];
  149. menus.forEach((now, index) => {
  150. newArray[index] = now;
  151. if (newArray[index].children && now.children) {
  152. newArray[index].children = now.children.filter((item) => {
  153. return !item.auth;
  154. });
  155. }
  156. });
  157. return newArray;
  158. // return this.$store.state.menus.menusName
  159. },
  160. local() {
  161. return this.$store.state.app.local;
  162. },
  163. hasReadErrorPage() {
  164. return this.$store.state.app.hasReadErrorPage;
  165. },
  166. unreadCount() {
  167. return this.$store.state.user.unreadCount;
  168. },
  169. },
  170. methods: {
  171. ...mapMutations(['setBreadCrumb', 'setTagNavList', 'addTag', 'setLocal', 'setHomeRoute', 'closeTag']),
  172. ...mapActions(['handleLogin', 'getUnreadMessageCount']),
  173. turnToPage(route, all) {
  174. let { path, name, params, query } = {};
  175. if (typeof route === 'string' && !all) path = route;
  176. else if (typeof route === 'string' && all) name = route;
  177. else {
  178. path = route.path;
  179. name = route.name;
  180. params = route.params;
  181. query = route.query;
  182. }
  183. this.$router.push({
  184. path,
  185. name,
  186. params,
  187. query,
  188. });
  189. },
  190. handleCollapsedChange(state) {
  191. this.collapsed = state;
  192. setCookies('collapsed', state);
  193. },
  194. handleCloseTag(res, type, route) {
  195. if (type !== 'others') {
  196. if (type === 'all') {
  197. this.turnToPage(this.$config.homeName, 'all');
  198. } else {
  199. if (routeEqual(this.$route, route)) {
  200. this.closeTag(route);
  201. }
  202. }
  203. }
  204. if (res.length === 1 && res[0].name === this.$config.homeName) {
  205. this.$router.push({ name: this.$config.homeName });
  206. }
  207. this.setTagNavList(res);
  208. },
  209. handleClick(item) {
  210. this.turnToPage(item);
  211. },
  212. getLogo() {
  213. let logo = this.$store.state.userInfo.logo;
  214. let logoSmall = this.$store.state.userInfo.logoSmall;
  215. this.maxLogo = logo || this.maxLogo;
  216. this.minLogo = logoSmall || this.minLogo;
  217. getLogo().then((res) => {
  218. localStorage.setItem('ADMIN_TITLE', res.data.site_name);
  219. this.minLogo = res.data.logo_square;
  220. this.maxLogo = res.data.logo;
  221. });
  222. },
  223. handleReload() {
  224. this.reload = false;
  225. // if (Setting.showProgressBar) iView.LoadingBar.start()
  226. this.$nextTick(() => {
  227. this.reload = true;
  228. // if (Setting.showProgressBar) iView.LoadingBar.finish()
  229. });
  230. },
  231. clear() {
  232. this.openImage = false;
  233. },
  234. },
  235. watch: {
  236. $route(newRoute) {
  237. this.headMenuNoShow = this.$route.meta.fullScreen;
  238. let openNames = getMenuopen(newRoute, this.menuList);
  239. this.$store.commit('menus/setopenMenus', openNames);
  240. const { name, query, params, meta } = newRoute;
  241. this.addTag({
  242. route: { name, query, params, meta },
  243. type: 'push',
  244. });
  245. this.setBreadCrumb(newRoute);
  246. this.setTagNavList(getNewTagList(this.tagNavList, newRoute));
  247. this.$refs.sideMenu.updateOpenName(newRoute.path);
  248. },
  249. },
  250. mounted() {
  251. this.headMenuNoShow = this.$route.meta.fullScreen;
  252. this.getLogo();
  253. this.screenWidth = document.body.clientWidth;
  254. window.onresize = () => {
  255. return (() => {
  256. this.screenWidth = document.body.clientWidth;
  257. if (this.screenWidth <= 1060) {
  258. this.collapsed = true;
  259. setCookies('collapsed', true);
  260. } else {
  261. this.collapsed = false;
  262. setCookies('collapsed', false);
  263. }
  264. })();
  265. };
  266. /**
  267. * @description 初始化设置面包屑导航和标签导航
  268. */
  269. this.setTagNavList();
  270. this.setHomeRoute(routers);
  271. const { name, params, query, meta } = this.$route;
  272. this.addTag({
  273. route: { name, params, query, meta },
  274. });
  275. this.setBreadCrumb(this.$route);
  276. // 设置初始语言
  277. this.setLocal(this.$i18n.locale);
  278. // 如果当前打开页面不在标签栏中,跳到homeName页
  279. if (!this.tagNavList.find((item) => item.name === this.$route.name)) {
  280. this.$router.push({
  281. name: this.$config.homeName,
  282. });
  283. }
  284. // 获取未读消息条数
  285. this.getUnreadMessageCount();
  286. },
  287. };
  288. </script>
  289. <style lang="less">
  290. .main .header-con {
  291. padding: 0 0px 0 0px;
  292. display: flex;
  293. background: linear-gradient(270deg, #1570ef 0%, #1570ef 100%);
  294. }
  295. .main .logo-con img {
  296. height: 50px;
  297. transition: all 1s;
  298. }
  299. .main .tag-nav-wrapper {
  300. // height: 10px;
  301. background: unset;
  302. padding: 0 15px;
  303. }
  304. .open-image {
  305. display: flex;
  306. align-items: center;
  307. justify-content: center;
  308. position: fixed;
  309. background-color: rgba(0, 0, 0, 0.6);
  310. height: 100%;
  311. width: 100%;
  312. top: 0;
  313. left: 0;
  314. z-index: 1000;
  315. img {
  316. width: 800px;
  317. }
  318. }
  319. .main-warper {
  320. min-height: calc(~'100vh - 166px');
  321. }
  322. </style>