Navbar.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <div class="navbar">
  3. <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
  4. <breadcrumb id="breadcrumb-container" class="breadcrumb-container" v-if="!topNav"/>
  5. <top-nav id="topmenu-container" class="breadcrumb-container" v-if="topNav"/>
  6. <div class="right-menu">
  7. <template v-if="device!=='mobile'">
  8. <div class="right-menu-item hover-effect">
  9. <el-badge v-if="msgCount>0" :value="msgCount" :max="99" class="dot">
  10. </el-badge>
  11. <div class="msg" @click="openMsg()"><i class="el-icon-message-solid"></i> </div>
  12. </div>
  13. <!-- <search id="header-search" class="right-menu-item" />
  14. -->
  15. <screenfull id="screenfull" class="right-menu-item hover-effect" />
  16. <el-tooltip content="布局大小" effect="dark" placement="bottom">
  17. <size-select id="size-select" class="right-menu-item hover-effect" />
  18. </el-tooltip>
  19. </template>
  20. <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
  21. <div class="avatar-wrapper">
  22. <img :src="logImg" class="user-avatar">
  23. <i class="el-icon-caret-bottom" />
  24. </div>
  25. <el-dropdown-menu slot="dropdown">
  26. <router-link to="/user/profile">
  27. <el-dropdown-item>个人中心</el-dropdown-item>
  28. </router-link>
  29. <el-dropdown-item @click.native="setting = true">
  30. <span>布局设置</span>
  31. </el-dropdown-item>
  32. <el-dropdown-item divided @click.native="logout">
  33. <span>退出登录</span>
  34. </el-dropdown-item>
  35. </el-dropdown-menu>
  36. </el-dropdown>
  37. </div>
  38. <el-drawer
  39. :append-to-body="true"
  40. size="35%"
  41. title="消息中心"
  42. :with-header="false"
  43. :title="msg.title" :visible.sync="msg.open"
  44. >
  45. <msg ref="msg" @update-count="getMsgCount" />
  46. </el-drawer>
  47. </div>
  48. </template>
  49. <script>
  50. import { mapGetters } from 'vuex'
  51. import Breadcrumb from '@/components/Breadcrumb'
  52. import TopNav from '@/components/TopNav'
  53. import Hamburger from '@/components/Hamburger'
  54. import Screenfull from '@/components/Screenfull'
  55. import SizeSelect from '@/components/SizeSelect'
  56. import Search from '@/components/HeaderSearch'
  57. import msg from "@/views/crm/components/msg";
  58. import { getMsg,getMsgList,getMsgCount,setRead } from "@/api/crm/msg";
  59. export default {
  60. components: {
  61. Breadcrumb,
  62. TopNav,
  63. Hamburger,
  64. Screenfull,
  65. SizeSelect,
  66. Search,
  67. msg
  68. },
  69. computed: {
  70. ...mapGetters([
  71. 'sidebar',
  72. 'avatar',
  73. 'device'
  74. ]),
  75. setting: {
  76. get() {
  77. return this.$store.state.settings.showSettings
  78. },
  79. set(val) {
  80. this.$store.dispatch('settings/changeSetting', {
  81. key: 'showSettings',
  82. value: val
  83. })
  84. }
  85. },
  86. topNav: {
  87. get() {
  88. return this.$store.state.settings.topNav
  89. }
  90. }
  91. },
  92. data() {
  93. return {
  94. msgCount:0,
  95. previousMsgCount: 0, // 保存上一次的消息计数
  96. msg:{
  97. open:false,
  98. title:'通知消息'
  99. },
  100. msgPollingTimer: null, // 轮询定时器
  101. }
  102. },
  103. created() {
  104. this.getMsgCount();
  105. // 启动消息轮询,每30秒检查一次新消息
  106. this.startMsgPolling();
  107. },
  108. beforeDestroy() {
  109. // 组件销毁前清除定时器
  110. this.stopMsgPolling();
  111. },
  112. methods: {
  113. startMsgPolling() {
  114. // 清除已存在的定时器
  115. if (this.msgPollingTimer) {
  116. clearInterval(this.msgPollingTimer);
  117. }
  118. // 设置定时器,每30秒获取一次消息计数
  119. this.msgPollingTimer = setInterval(() => {
  120. this.getMsgCount();
  121. }, 30000); // 30秒轮询一次
  122. },
  123. stopMsgPolling() {
  124. if (this.msgPollingTimer) {
  125. clearInterval(this.msgPollingTimer);
  126. this.msgPollingTimer = null;
  127. }
  128. },
  129. getMsgCount(){
  130. getMsg().then(response => {
  131. // 计算所有未读消息总数
  132. let totalCount = 0;
  133. response.counts.forEach(item => {
  134. totalCount += item.total;
  135. });
  136. // 保存旧的消息计数
  137. this.previousMsgCount = this.msgCount;
  138. // 更新消息计数
  139. this.msgCount = totalCount;
  140. // 如果消息计数增加了,显示通知弹框
  141. if (this.msgCount > this.previousMsgCount && this.msgCount > 0) {
  142. this.$notify({
  143. title: '提示',
  144. message: '您有'+this.msgCount+"条消息通知",
  145. position: 'top-right',
  146. type: 'info'
  147. });
  148. }
  149. }).catch(error => {
  150. console.error("获取消息计数失败:", error);
  151. });
  152. },
  153. openMsg(){
  154. console.log(11);
  155. this.msg.open=true;
  156. // 每次打开消息界面时刷新数据
  157. if (this.$refs.msg) {
  158. this.$refs.msg.getMsg();
  159. }
  160. // 打开消息后重新获取消息计数来更新角标
  161. this.getMsgCount();
  162. },
  163. toggleSideBar() {
  164. this.$store.dispatch('app/toggleSideBar')
  165. },
  166. async logout() {
  167. this.$confirm('确定注销并退出系统吗?', '提示', {
  168. confirmButtonText: '确定',
  169. cancelButtonText: '取消',
  170. type: 'warning'
  171. }).then(() => {
  172. this.$store.dispatch('LogOut').then(() => {
  173. location.href = '/index';
  174. })
  175. })
  176. }
  177. }
  178. }
  179. </script>
  180. <style lang="scss" scoped>
  181. .navbar {
  182. height: 50px;
  183. overflow: hidden;
  184. position: relative;
  185. background: #fff;
  186. box-shadow: 0 1px 4px rgba(0,21,41,.08);
  187. .hamburger-container {
  188. line-height: 46px;
  189. height: 100%;
  190. float: left;
  191. cursor: pointer;
  192. transition: background .3s;
  193. -webkit-tap-highlight-color:transparent;
  194. &:hover {
  195. background: rgba(0, 0, 0, .025)
  196. }
  197. }
  198. .breadcrumb-container {
  199. float: left;
  200. }
  201. .errLog-container {
  202. display: inline-block;
  203. vertical-align: top;
  204. }
  205. .right-menu {
  206. float: right;
  207. height: 100%;
  208. line-height: 50px;
  209. &:focus {
  210. outline: none;
  211. }
  212. .right-menu-item {
  213. position: relative;
  214. display: inline-block;
  215. padding: 0 8px;
  216. height: 100%;
  217. font-size: 18px;
  218. color: #5a5e66;
  219. vertical-align: text-bottom;
  220. .msg{
  221. text-align: center;
  222. width:40px;
  223. }
  224. .dot{
  225. position: absolute;
  226. right:0px;
  227. top:0px;
  228. }
  229. &.hover-effect {
  230. cursor: pointer;
  231. transition: background .3s;
  232. &:hover {
  233. background: rgba(0, 0, 0, .025)
  234. }
  235. }
  236. }
  237. .avatar-container {
  238. margin-right: 30px;
  239. .avatar-wrapper {
  240. margin-top: 5px;
  241. position: relative;
  242. .user-avatar {
  243. cursor: pointer;
  244. width: 40px;
  245. height: 40px;
  246. border-radius: 10px;
  247. }
  248. .el-icon-caret-bottom {
  249. cursor: pointer;
  250. position: absolute;
  251. right: -20px;
  252. top: 25px;
  253. font-size: 12px;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. </style>