Navbar.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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
  24. ref="qrTrigger"
  25. @mouseenter.native="initQRCode"
  26. @mouseleave.native="destroyQRCode"
  27. >
  28. <span>公众号二维码</span>
  29. </el-dropdown-item>
  30. <el-dropdown-item divided @click.native="logout">
  31. <span>退出登录</span>
  32. </el-dropdown-item>
  33. </el-dropdown-menu>
  34. </el-dropdown>
  35. </div>
  36. <div
  37. v-show="showQR"
  38. ref="qrPopup"
  39. class="qr-popup"
  40. @mouseenter.native="showQR = true"
  41. @mouseleave.native="destroyQRCode"
  42. ></div>
  43. </div>
  44. </template>
  45. <script>
  46. import { mapGetters } from 'vuex'
  47. import Breadcrumb from '@/components/Breadcrumb'
  48. import TopNav from '@/components/TopNav'
  49. import Hamburger from '@/components/Hamburger'
  50. import { getUserSign, } from '@/api/onLineChat/index'
  51. import { getInfo } from '@/api/login'
  52. import QRCode from 'qrcodejs2';
  53. import { getUserProfile } from "@/api/system/user";
  54. export default {
  55. components: {
  56. Breadcrumb,
  57. TopNav,
  58. Hamburger,
  59. },
  60. data() {
  61. return {
  62. isActive: false, // 根据状态设置 isActive 的值
  63. showQR: false,
  64. qrcodeInstance: null,
  65. triggerRect: null,
  66. dockerId: ""
  67. };
  68. },
  69. computed: {
  70. ...mapGetters([
  71. 'sidebar',
  72. 'avatar',
  73. 'device'
  74. ]),
  75. topNav: {
  76. get() {
  77. return this.$store.state.settings.topNav
  78. }
  79. }
  80. },
  81. created() {
  82. this.isActive = this.$store.getters.info.isOnline == 0
  83. this.getUser()
  84. },
  85. beforeDestroy() {
  86. this.destroyQRCode();
  87. },
  88. methods: {
  89. getUser() {
  90. getUserProfile().then(response => {
  91. this.dockerId = response.data.id;
  92. });
  93. },
  94. initQRCode(e) {
  95. // 1. 获取触发元素位置
  96. this.triggerRect = e.currentTarget.getBoundingClientRect();
  97. this.showQR = true;
  98. // 2. 创建二维码实例
  99. this.$nextTick(() => {
  100. const container = this.$refs.qrPopup;
  101. // 清空容器
  102. container.innerHTML = '';
  103. // 生成二维码
  104. this.qrcodeInstance = new QRCode(container, {
  105. text: 'http://yaorongtest.yaorongmedical.com/hcp/officialAccounts/patientData?doctorId='+this.doctorId,
  106. width: 150,
  107. height: 150,
  108. colorDark: '#000000',
  109. colorLight: '#ffffff',
  110. correctLevel: QRCode.CorrectLevel.H
  111. });
  112. // 3. 动态定位
  113. this.adjustPosition();
  114. });
  115. },
  116. adjustPosition() {
  117. if (!this.triggerRect || !this.qrcodeInstance) return;
  118. const container = this.$refs.qrPopup;
  119. const popupWidth = 170;
  120. const popupHeight = 170;
  121. // 计算最佳显示位置(左侧弹出)
  122. let left = this.triggerRect.left - popupWidth - 10;
  123. let top = this.triggerRect.top + (this.triggerRect.height - popupHeight) / 2;
  124. // 边界检测
  125. if (left < 0) left = this.triggerRect.right + 10;
  126. if (top < 0) top = 0;
  127. if (top + popupHeight > window.innerHeight) top = window.innerHeight - popupHeight;
  128. Object.assign(container.style, {
  129. left: `${left}px`,
  130. top: `${top}px`
  131. });
  132. },
  133. destroyQRCode() {
  134. this.showQR = false;
  135. if (this.qrcodeInstance) {
  136. this.qrcodeInstance.clear(); // 清理二维码
  137. this.qrcodeInstance = null;
  138. }
  139. },
  140. toggleSideBar() {
  141. this.$store.dispatch('app/toggleSideBar')
  142. },
  143. async logout() {
  144. this.$confirm('确定注销并退出系统吗?', '提示', {
  145. confirmButtonText: '确定',
  146. cancelButtonText: '取消',
  147. type: 'warning'
  148. }).then(() => {
  149. this.$store.dispatch('LogOut').then(() => {
  150. if (process.env.NODE_ENV == 'production' || process.env.NODE_ENV == 'staging') {
  151. location.href = '/hcp';
  152. } else {
  153. location.href = '/hcp-dev';
  154. }
  155. })
  156. }).catch(() => { });
  157. },
  158. // Im
  159. handelLogin() {
  160. console.log(this.$store.getters.info, 'this.$store.getters.info.')
  161. let userId = this.$store.getters.info.userId
  162. getUserSign({ id: userId }).then(res => {
  163. this.imLogin(userId, res.data)
  164. })
  165. },
  166. // IM登录
  167. imLogin(userId, userSig) {
  168. let promise = this.tim.login({
  169. userID: userId,
  170. userSig: userSig
  171. });
  172. promise.then((imResponse) => {
  173. console.log(imResponse.data.repeatLogin);
  174. this.$nextTick(() => {
  175. if (imResponse.data.repeatLogin === true) {
  176. this.$modal.msgSuccess('登录成功')
  177. setTimeout(() => {
  178. this.$store.dispatch("GetInfo").then((res) => {
  179. this.isActive = res.data.isOnline == 0
  180. });
  181. }, 5000);
  182. } else {
  183. this.handelLogin()
  184. }
  185. })
  186. }).catch(function (imError) {
  187. console.warn('login error:', imError); // 登录失败的相关信息
  188. });
  189. },
  190. // 退出Im
  191. handellogout() {
  192. let that = this
  193. let promise = this.tim.logout();
  194. promise.then((imResponse) => {
  195. console.log(imResponse); // 登出成功
  196. this.$modal.msgSuccess('已离线')
  197. this.isActive = false
  198. // this.tim.destroy();//销毁 SDK 实例。SDK 会先 logout,然后断开 WebSocket 长连接,并释放资源
  199. }).catch(function (imError) {
  200. that.isActive = false
  201. console.warn('logout error:', imError);
  202. });
  203. }
  204. }
  205. }
  206. </script>
  207. <style lang="scss" scoped>
  208. .qr-popup {
  209. position: fixed;
  210. width: 170px;
  211. height: 170px;
  212. padding: 10px;
  213. background: white;
  214. border-radius: 4px;
  215. box-shadow: 0 2px 12px rgba(0,0,0,0.15);
  216. z-index: 2000; /* 确保高于Element UI下拉菜单的2000 */
  217. display: flex;
  218. align-items: center;
  219. justify-content: center;
  220. pointer-events: none; /* 防止遮挡下层元素 */
  221. }
  222. .qr-popup:hover {
  223. pointer-events: auto; /* 允许内部交互 */
  224. }
  225. .navbar {
  226. height: 50px;
  227. overflow: hidden;
  228. position: relative;
  229. background: #fff;
  230. box-shadow: 0 1px 4px rgba(0, 21, 41, .08);
  231. .hamburger-container {
  232. line-height: 46px;
  233. height: 100%;
  234. float: left;
  235. cursor: pointer;
  236. transition: background .3s;
  237. -webkit-tap-highlight-color: transparent;
  238. &:hover {
  239. background: rgba(0, 0, 0, .025)
  240. }
  241. }
  242. .breadcrumb-container {
  243. float: left;
  244. }
  245. .topmenu-container {
  246. position: absolute;
  247. left: 50px;
  248. }
  249. .errLog-container {
  250. display: inline-block;
  251. vertical-align: top;
  252. }
  253. .right-menu {
  254. float: right;
  255. height: 100%;
  256. line-height: 50px;
  257. &:focus {
  258. outline: none;
  259. }
  260. .right-menu-item {
  261. display: inline-block;
  262. padding: 0 8px;
  263. height: 100%;
  264. font-size: 18px;
  265. color: #5a5e66;
  266. vertical-align: text-bottom;
  267. &.hover-effect {
  268. cursor: pointer;
  269. transition: background .3s;
  270. &:hover {
  271. background: rgba(0, 0, 0, .025)
  272. }
  273. }
  274. }
  275. .avatar-container {
  276. margin-right: 30px;
  277. .avatar-wrapper {
  278. margin-top: 5px;
  279. position: relative;
  280. .user-avatar {
  281. cursor: pointer;
  282. width: 40px;
  283. height: 40px;
  284. border-radius: 10px;
  285. }
  286. .el-icon-caret-bottom {
  287. cursor: pointer;
  288. position: absolute;
  289. right: -20px;
  290. top: 25px;
  291. font-size: 12px;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. .dot {
  298. width: 10px;
  299. height: 10px;
  300. border-radius: 50%;
  301. background-color: #a0a0a0;
  302. position: absolute;
  303. bottom: 18px;
  304. }
  305. .active {
  306. background-color: rgb(38, 255, 31);
  307. /* 假设激活状态下的颜色为红色 */
  308. }
  309. </style>