Navbar.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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" v-if="!topNav" />
  6. <top-nav id="topmenu-container" class="topmenu-container" v-if="topNav" />
  7. <div class="right-menu">
  8. <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
  9. <div class="avatar-wrapper">
  10. <div class="dot" :class="{ 'active': isActive }"></div>
  11. <img :src="avatar" class="user-avatar">
  12. <!-- <i class="el-icon-caret-bottom" /> -->
  13. </div>
  14. <el-dropdown-menu slot="dropdown">
  15. <router-link to="/user/profile">
  16. <el-dropdown-item>个人中心</el-dropdown-item>
  17. </router-link>
  18. <el-dropdown-item
  19. :style="{ 'background-color': isActive ? 'rgb(38, 255, 31)' : '', 'color': isActive ? '#FFF' : '' }"
  20. @click.native="handelLogin">在线</el-dropdown-item>
  21. <el-dropdown-item :style="{ 'background-color': !isActive ? '#a0a0a0' : '', 'color': !isActive ? '#FFF' : '' }"
  22. @click.native="handellogout">离线</el-dropdown-item>
  23. <el-dropdown-item divided @click.native="logout">
  24. <span>退出登录</span>
  25. </el-dropdown-item>
  26. </el-dropdown-menu>
  27. </el-dropdown>
  28. </div>
  29. </div>
  30. </template>
  31. <script>
  32. import { mapGetters } from 'vuex'
  33. import Breadcrumb from '@/components/Breadcrumb'
  34. import TopNav from '@/components/TopNav'
  35. import Hamburger from '@/components/Hamburger'
  36. import { getUserSign, } from '@/api/onLineChat/index'
  37. import { getInfo } from '@/api/login'
  38. export default {
  39. components: {
  40. Breadcrumb,
  41. TopNav,
  42. Hamburger,
  43. },
  44. data() {
  45. return {
  46. isActive: false // 根据状态设置 isActive 的值
  47. };
  48. },
  49. computed: {
  50. ...mapGetters([
  51. 'sidebar',
  52. 'avatar',
  53. 'device'
  54. ]),
  55. topNav: {
  56. get() {
  57. return this.$store.state.settings.topNav
  58. }
  59. }
  60. },
  61. created() {
  62. this.isActive = this.$store.getters.info.isOnline == 0
  63. },
  64. methods: {
  65. toggleSideBar() {
  66. this.$store.dispatch('app/toggleSideBar')
  67. },
  68. async logout() {
  69. this.$confirm('确定注销并退出系统吗?', '提示', {
  70. confirmButtonText: '确定',
  71. cancelButtonText: '取消',
  72. type: 'warning'
  73. }).then(() => {
  74. this.$store.dispatch('LogOut').then(() => {
  75. if (process.env.NODE_ENV == 'production') {
  76. location.href = '/hcp';
  77. } else {
  78. location.href = '/hcp-dev';
  79. }
  80. })
  81. }).catch(() => { });
  82. },
  83. // Im
  84. handelLogin() {
  85. console.log(this.$store.getters.info, 'this.$store.getters.info.')
  86. let userId = this.$store.getters.info.userId
  87. getUserSign({ id: userId }).then(res => {
  88. this.imLogin(userId, res.data)
  89. })
  90. },
  91. // IM登录
  92. imLogin(userId, userSig) {
  93. let promise = this.tim.login({
  94. userID: userId,
  95. userSig: userSig
  96. });
  97. promise.then((imResponse) => {
  98. console.log(imResponse.data.repeatLogin);
  99. this.$nextTick(() => {
  100. if (imResponse.data.repeatLogin === true) {
  101. this.$modal.msgSuccess('登录成功')
  102. setTimeout(() => {
  103. this.$store.dispatch("GetInfo").then((res) => {
  104. this.isActive = res.data.isOnline == 0
  105. });
  106. }, 5000);
  107. } else {
  108. this.handelLogin()
  109. }
  110. })
  111. }).catch(function (imError) {
  112. console.warn('login error:', imError); // 登录失败的相关信息
  113. });
  114. },
  115. // 退出Im
  116. handellogout() {
  117. let that = this
  118. let promise = this.tim.logout();
  119. promise.then((imResponse) => {
  120. console.log(imResponse); // 登出成功
  121. this.$modal.msgSuccess('已离线')
  122. this.isActive = false
  123. // this.tim.destroy();//销毁 SDK 实例。SDK 会先 logout,然后断开 WebSocket 长连接,并释放资源
  124. }).catch(function (imError) {
  125. that.isActive = false
  126. console.warn('logout error:', imError);
  127. });
  128. }
  129. }
  130. }
  131. </script>
  132. <style lang="scss" scoped>
  133. .navbar {
  134. height: 50px;
  135. overflow: hidden;
  136. position: relative;
  137. background: #fff;
  138. box-shadow: 0 1px 4px rgba(0, 21, 41, .08);
  139. .hamburger-container {
  140. line-height: 46px;
  141. height: 100%;
  142. float: left;
  143. cursor: pointer;
  144. transition: background .3s;
  145. -webkit-tap-highlight-color: transparent;
  146. &:hover {
  147. background: rgba(0, 0, 0, .025)
  148. }
  149. }
  150. .breadcrumb-container {
  151. float: left;
  152. }
  153. .topmenu-container {
  154. position: absolute;
  155. left: 50px;
  156. }
  157. .errLog-container {
  158. display: inline-block;
  159. vertical-align: top;
  160. }
  161. .right-menu {
  162. float: right;
  163. height: 100%;
  164. line-height: 50px;
  165. &:focus {
  166. outline: none;
  167. }
  168. .right-menu-item {
  169. display: inline-block;
  170. padding: 0 8px;
  171. height: 100%;
  172. font-size: 18px;
  173. color: #5a5e66;
  174. vertical-align: text-bottom;
  175. &.hover-effect {
  176. cursor: pointer;
  177. transition: background .3s;
  178. &:hover {
  179. background: rgba(0, 0, 0, .025)
  180. }
  181. }
  182. }
  183. .avatar-container {
  184. margin-right: 30px;
  185. .avatar-wrapper {
  186. margin-top: 5px;
  187. position: relative;
  188. .user-avatar {
  189. cursor: pointer;
  190. width: 40px;
  191. height: 40px;
  192. border-radius: 10px;
  193. }
  194. .el-icon-caret-bottom {
  195. cursor: pointer;
  196. position: absolute;
  197. right: -20px;
  198. top: 25px;
  199. font-size: 12px;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. .dot {
  206. width: 10px;
  207. height: 10px;
  208. border-radius: 50%;
  209. background-color: #a0a0a0;
  210. position: absolute;
  211. bottom: 18px;
  212. }
  213. .active {
  214. background-color: rgb(38, 255, 31);
  215. /* 假设激活状态下的颜色为红色 */
  216. }
  217. </style>