main.vue 9.3 KB

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