index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <div class="page-header-box" style="line-height: 40px;position: relative;padding:0 12px">
  3. <el-row :gutter="24">
  4. <el-col :span="12" v-if="path=='/login'" class="layui-col-md6 layui-col-xs4 header-left">
  5. <img class="header-icon" src="../../assets/images/icon_tel2.png" />
  6. <span class="p-l-small">服务热线:400-1797-985</span>
  7. <img class="m-l header-icon" src="../../assets/images/icon_phone.png" />
  8. <span class="pos-r" id="app_download" @mouseover="app=true" @mouseleave="app=false">
  9. <a href="javascript:void(0);">APP下载</a>
  10. <div id="appQrcode" v-show="app">
  11. <img style="width:110px;height:110px;" src="../../assets/images/app.png" />
  12. <p style="width:110px;color:#000;font-size:12px;margin-top:5px;">扫描下载APP</p>
  13. </div>
  14. </span>
  15. </el-col>
  16. <el-col v-if="false" :span="12" class="layui-col-md6 layui-col-xs8" style="text-align:right;">
  17. 您好!名学金榜欢迎你,
  18. <a href="#" class="color-orange">请登录&gt;&gt;</a>
  19. </el-col>
  20. <el-col :span="12" v-if="path!='/login'" class="layui-col-md6 layui-col-xs4 header-left">
  21. <span @click="to('/login')" style="margin-right:20px;cursor:pointer">首页</span>
  22. <span v-for="(item, index) in visibleMenus" :key="index" style="margin-right:20px;cursor:pointer" @click="handleClick(item)">{{item.meta.title}}</span>
  23. </el-col>
  24. <el-col v-if="userInfo" :span="12" class="layui-col-md6 layui-col-xs8" style="text-align:right;">
  25. 您好!
  26. <span style="color:#FFA400;">{{userInfo.nickName}}{{userInfo.userName}} [{{userInfo.provinceName}} {{userInfo.schoolName}}],</span>
  27. 欢迎登录名学金榜
  28. <el-dropdown v-if="roleList.length > 1" @command="handleRoleExchange">
  29. <span class="el-dropdown-link pointer">[{{currentRole.roleName}}]</span>
  30. <el-dropdown-menu slot="dropdown">
  31. <el-dropdown-item disabled class="role-change-title">
  32. 角色切换
  33. <i class="el-icon-refresh"></i>
  34. </el-dropdown-item>
  35. <el-dropdown-item v-for="(r,i) in roleList" :key="i" :disabled="r==currentRole" :command="r.roleId" :divided="i==0">{{r.roleName}}</el-dropdown-item>
  36. </el-dropdown-menu>
  37. </el-dropdown>
  38. <img style="height:16px;margin-left:5px;position:relative;bottom:-2px;" :src="userInfo.avatar||'https://www.mingxuejc.com/images/photo/4_2.png'" />
  39. <a @click="userTo">个人中心</a>
  40. <a style="padding-left:30px;color:#FB731F;" @click="logout">退出登录</a>
  41. </el-col>
  42. </el-row>
  43. </div>
  44. </template>
  45. <script>
  46. import auth from "@/utils/auth";
  47. import { mapGetters } from "vuex";
  48. import { changeRole } from "@/api/login.js";
  49. export default {
  50. data() {
  51. return {
  52. app: false,
  53. path: "",
  54. menuList: [],
  55. };
  56. },
  57. computed: {
  58. ...mapGetters(["currentUser", "currentRole", "roleList"]),
  59. userInfo() {
  60. if (!auth.getToken()) return null;
  61. return this.currentUser;
  62. },
  63. // 所有的路由信息
  64. routers() {
  65. return this.$store.state.permission.topbarRouters;
  66. },
  67. visibleMenus() {
  68. return this.menuList.filter((m) => !m.hidden);
  69. },
  70. },
  71. created() {
  72. this.path = this.$route.path;
  73. },
  74. mounted() {
  75. this.menuList = this.topMenus();
  76. },
  77. methods: {
  78. logout() {
  79. this.$confirm("确定注销并退出系统吗?", "提示", {
  80. confirmButtonText: "确定",
  81. cancelButtonText: "取消",
  82. type: "warning",
  83. }).then(() => {
  84. this.$store.dispatch("LogOut").then(() => {
  85. this.$router.push({ path: "/login" });
  86. window.location.reload();
  87. });
  88. });
  89. },
  90. to(url) {
  91. this.$router.push({
  92. path: url,
  93. });
  94. },
  95. // 顶部显示菜单
  96. topMenus() {
  97. let topMenus = [];
  98. this.routers.map((menu) => {
  99. menu.level = 1;
  100. if (menu.hidden !== true) {
  101. // 兼容顶部栏一级菜单内部跳转
  102. if (menu.path === "/") {
  103. if (menu.children) {
  104. topMenus.push(menu.children[0]);
  105. }
  106. } else {
  107. topMenus.push(menu);
  108. }
  109. }
  110. });
  111. return topMenus;
  112. },
  113. userTo() {
  114. //跳转至个人中心
  115. this.menuList = this.topMenus();
  116. this.menuList.forEach((item) => {
  117. if (item.path == "/user") {
  118. this.handleClick(item);
  119. } else if ("index" == item.path) {
  120. this.$emit("userNavTo");
  121. }
  122. });
  123. },
  124. handleClick(data) {
  125. auth.setMenuTab(data);
  126. auth.setActiveMenu(data.path + "/" + data.children[0].path);
  127. let item = JSON.parse(JSON.stringify(data));
  128. let routes = []; //三级菜单
  129. let second = []; //二级
  130. let routesTo = [];
  131. if (item.children) {
  132. //有二级菜单
  133. item.children.forEach((item2) => {
  134. if (item2.children) {
  135. item2.children.forEach((item3) => {
  136. item3.path = item.path + "/" + item2.path + "/" + item3.path;
  137. });
  138. if (routes.length == 0 && item2.children.length) {
  139. //一级菜单下面第一个有3个菜单的二级菜单
  140. routes = item2.children;
  141. routesTo = item2.children;
  142. }
  143. } else {
  144. routesTo = item.children;
  145. }
  146. item2.path = item.path + "/" + item2.path;
  147. item2.level = 2;
  148. second.push(item2);
  149. });
  150. let fal = false;
  151. second.forEach((item) => {
  152. if (!fal && item.children && item.children.length > 0) {
  153. auth.setActiveMenu(item.path);
  154. fal = true;
  155. }
  156. });
  157. auth.setMenuTab(second);
  158. this.three = true;
  159. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  160. } else {
  161. //没有二级菜单
  162. this.$store.commit("SET_SIDEBAR_ROUTERS", []);
  163. this.$router.push({
  164. path: item.path,
  165. });
  166. }
  167. let path = this.gouAction(routesTo);
  168. if (path) {
  169. this.$router.push({
  170. path: path,
  171. });
  172. auth.setMenuListAction(path);
  173. } else {
  174. }
  175. auth.setMenuList(routes);
  176. },
  177. toPath(data, path, url) {
  178. data.forEach((item) => {
  179. if (!path && item.children && item.children.length > 0) {
  180. let ac = url ? url + "/" : "";
  181. path = this.toPath(item.children, path, ac + item.path);
  182. } else if (!path) {
  183. let ac = url ? url + "/" : "";
  184. path = ac + item.path;
  185. }
  186. });
  187. return path;
  188. },
  189. gouAction(data) {
  190. if (data && data.length > 0) {
  191. if (data[0].children && data[0].children.length > 0) {
  192. if (
  193. data[0].children[0].children &&
  194. data[0].children[0].children.length > 0
  195. ) {
  196. return data[0].children[0].children.path;
  197. } else {
  198. return data[0].path + "/" + data[0].children[0].path;
  199. }
  200. } else {
  201. return data[0].path;
  202. }
  203. } else {
  204. return "";
  205. }
  206. },
  207. handleRoleExchange(roleId) {
  208. changeRole(roleId).then((_) => {
  209. // IMPORTANT: 此处会触发GetInfo/GetRouters
  210. //window.location.reload();
  211. window.location.href = "/login";
  212. });
  213. },
  214. },
  215. };
  216. </script>
  217. <style scoped>
  218. .page-header-box > .layui-row {
  219. width: 1184px;
  220. margin: 0 auto;
  221. padding: 8px 0;
  222. font-size: 14px;
  223. }
  224. .page-header-box .header-icon {
  225. height: 16px;
  226. vertical-align: middle;
  227. }
  228. img {
  229. border: none;
  230. }
  231. .p-l-small {
  232. padding-left: 8px;
  233. }
  234. .page-header-box .header-left > span {
  235. display: inline-block;
  236. vertical-align: middle;
  237. }
  238. #appQrcode {
  239. background: #fff;
  240. border: 1px solid #cdcdcd;
  241. box-shadow: 0 0 8px #cdcdcd;
  242. border-radius: 3px;
  243. padding: 10px;
  244. position: absolute;
  245. left: 0;
  246. top: 40px;
  247. z-index: 9999;
  248. text-align: center;
  249. }
  250. .pos-r {
  251. position: relative;
  252. }
  253. .m-l {
  254. margin-left: 16px;
  255. }
  256. .color-orange {
  257. color: #47c6a2;
  258. }
  259. .role-change-title {
  260. color: #666666;
  261. font-size: 12px;
  262. font-weight: bold;
  263. }
  264. </style>