Navbar.vue 7.3 KB

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