home.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div>
  3. <el-affix :offset="0" :z-index="2001">
  4. <div class="headers">
  5. <headers ref="headers" />
  6. </div>
  7. </el-affix>
  8. <div class="content">
  9. <router-view></router-view>
  10. </div>
  11. <!-- 底部版权信息 -->
  12. <copyright />
  13. <!-- <div class="sidebars">
  14. <sidebars ref="sidebars" :userType="userType" />
  15. </div> -->
  16. </div>
  17. </template>
  18. <script>
  19. import {ElMessage} from 'element-plus'
  20. import {ROOTPATH2} from '../../comment/httpUrl.js'
  21. import headers from '../components/header/header.vue'
  22. import sidebars from '../components/sidebar/sidebar.vue'
  23. import copyright from '../components/copyright/copyright.vue'
  24. export default {
  25. components: {
  26. headers,
  27. sidebars,
  28. copyright
  29. },
  30. computed: {
  31. userType() {
  32. return this.$store.state.userType;
  33. }
  34. },
  35. data() {
  36. return {
  37. ws: null,
  38. }
  39. },
  40. mounted() {
  41. if (localStorage.getItem('token')) {
  42. this.getUserInfo();
  43. }
  44. const userId = localStorage.getItem('userId');
  45. // 连接websocket
  46. // this.connectSocket(userId);
  47. },
  48. beforeDestroy() {
  49. if (this.ws && this.ws.readyState === WebSocket.OPEN) {
  50. console.log('♻️ 组件销毁前关闭 WebSocket');
  51. this.ws.close();
  52. }
  53. },
  54. methods: {
  55. connectSocket(userId) {
  56. if (!userId) return;
  57. // 若已有连接则关闭
  58. if (this.ws) {
  59. this.ws.close();
  60. }
  61. this.ws = new WebSocket(ROOTPATH2 + '' + userId)
  62. // 连接成功
  63. this.ws.onopen = () => {
  64. console.log('WebSocket 已连接:', ROOTPATH2 + '' + userId);
  65. };
  66. // 收到消息
  67. this.ws.onmessage = (event) => {
  68. console.log(event)
  69. if (event.data === 'HeartBeat') return;
  70. let msg;
  71. try {
  72. msg = JSON.parse(event.data);
  73. } catch (e) {
  74. console.warn('非JSON消息:', event.data);
  75. return;
  76. }
  77. if (msg.type === 'kickOut') {
  78. ElMessage({
  79. message: msg.content || '您的账号已在其他设备登录',
  80. type: 'warning',
  81. duration: 1500,
  82. offset: this.screenHeight / 2
  83. })
  84. localStorage.removeItem('token');
  85. localStorage.removeItem('userId');
  86. localStorage.removeItem('userType');
  87. localStorage.removeItem('companyId');
  88. // 调用重置状态的函数
  89. this.$store.dispatch('SET_DATA');
  90. this.$router.push({
  91. name: 'login'
  92. })
  93. }
  94. };
  95. },
  96. //获取用户信息
  97. getUserInfo() {
  98. this.$Request.get('/app/user/selectUserById').then(res => {
  99. if (res.code == 0) {
  100. // this.avatar = res.data.avatar
  101. //记录直属佣金比例
  102. localStorage.setItem('zhiRate', res.data.zhiRate)
  103. this.$store.commit('SET_ZHI_RATE', res.data.zhiRate)
  104. //设置手机号
  105. this.$store.commit('SET_PHONE', res.data.phone)
  106. //设置企业id
  107. // this.$store.commit('SET_COMPANY_ID', res.data.companyId)
  108. // localStorage.setItem('companyId', res.data.companyId)
  109. //设置邀请码
  110. this.$store.commit('SET_INVITATION_CODE', res.data.invitationCode)
  111. //设置头像
  112. this.$store.commit('SET_AVATAR', res.data.avatar);
  113. //设置昵称
  114. this.$store.commit('SET_NICK_NAME', res.data.userName);
  115. //设置微信号
  116. if (res.data.weChatNum) {
  117. this.$store.commit('SET_WE_CHAT_NUM', res.data.weChatNum);
  118. } else {
  119. this.$store.commit('SET_WE_CHAT_NUM', '');
  120. }
  121. if (res.data.userType == 1 || res.data.userType == null) {
  122. this.$store.commit('SET_USER_TYPE', 1);
  123. localStorage.setItem('userType', 1)
  124. if (res.data.isUserVip == 1) { //判断用户是否是vip
  125. this.$store.commit('SET_IS_VIP', true);
  126. } else {
  127. this.$store.commit('SET_IS_VIP', false);
  128. }
  129. } else {
  130. this.$store.commit('SET_USER_TYPE', 2);
  131. localStorage.setItem('userType', 2)
  132. if (res.data.isUserVip == 1) { //判断企业用户是否是vip
  133. this.$store.commit('SET_IS_COMPANY_VIP', true);
  134. } else {
  135. this.$store.commit('SET_IS_COMPANY_VIP', false);
  136. }
  137. }
  138. }
  139. })
  140. },
  141. },
  142. }
  143. </script>
  144. <style scoped>
  145. .headers {
  146. width: 100%;
  147. }
  148. .content {
  149. width: 1600px;
  150. margin: 0 auto;
  151. }
  152. .sidebars {
  153. position: fixed;
  154. right: 20px;
  155. top: 70%;
  156. transform: translate(0, -50%);
  157. }
  158. </style>