Navbar.vue 11 KB

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