Преглед на файлове

添加单点登录功能

wkw преди 6 месеца
родител
ревизия
45ec34f323

+ 84 - 0
comment/socket.js

@@ -0,0 +1,84 @@
+// src/utils/socket.js
+import { ElMessage } from 'element-plus'
+import { ROOTPATH2 } from '../comment/httpUrl.js'
+var WSS = null // 全局唯一的 WebSocket 实例
+
+/**
+ * 连接 WebSocket
+ * @param {string} userId 当前用户 ID
+ */
+export function connectSocket(userId, store, router) {
+    if (!userId) return
+    // 若已有连接则关闭
+    if (WSS) {
+        try {
+            WSS.close()
+        } catch { }
+    }
+
+    const wsUrl = `${ROOTPATH2}${userId}`
+    WSS = new WebSocket(wsUrl)
+    console.log('🔗 WebSocket 连接中:', wsUrl)
+    console.log(WSS)
+    WSS.onopen = () => {
+        console.log('✅ WebSocket 已连接:', wsUrl)
+    }
+
+    WSS.onmessage = (event) => {
+        if (event.data === 'HeartBeat') return
+        let msg
+        try {
+            msg = JSON.parse(event.data)
+        } catch (e) {
+            console.warn('非 JSON 消息:', event.data)
+            return
+        }
+
+        if (msg.type === 'kickOut') {
+            ElMessage({
+                message: msg.content || '您的账号已在其他设备登录',
+                type: 'warning',
+                duration: 1500
+            })
+
+            // 清空本地存储
+            localStorage.removeItem('token')
+            localStorage.removeItem('userId')
+            localStorage.removeItem('userType')
+            localStorage.removeItem('companyId')
+
+            if (store) store.dispatch('SET_DATA')
+            if (router) router.push({ name: 'login' })
+        }
+    }
+
+    WSS.onerror = (err) => {
+        console.error('❌ WebSocket 错误:', err)
+    }
+
+    WSS.onclose = () => {
+        console.log('🔌 WebSocket 已关闭')
+    }
+}
+
+/** 获取 WebSocket 实例 */
+export function getSocket() {
+    return WSS
+}
+
+/** 发送消息 */
+export function sendSocketMsg(data) {
+    if (WSS && WSS.readyState === WebSocket.OPEN) {
+        WSS.send(data)
+    } else {
+        console.warn('⚠️ WebSocket 未连接,无法发送')
+    }
+}
+
+/** 关闭连接 */
+export function closeSocket() {
+    if (WSS) {
+        WSS.close()
+        WSS = null
+    }
+}

+ 13 - 6
src/components/messageCom/messageCom.vue

@@ -1125,12 +1125,19 @@
 			},
 			//初始化并链接webscoket
 			initScoket() {
-				//创建WebSocket对象并进行初始化连接
-				this.webSco = new WebSocket(ROOTPATH2 + '' + this.userId)
-				this.webSco.onopen = this.websocketonopen;
-				this.webSco.onmessage = this.websocketonmessage;
-				this.webSco.onerror = this.websocketonerror;
-				this.webSco.onclose = this.websocketclose;
+				this.webSco = this.$socket.getSocket();
+				// 如果 WebSocket 已经连接,直接把状态标记为 true
+				if (this.webSco.readyState === WebSocket.OPEN) {
+					this.webSocketState = true
+					// console.log('✅ WebSocket 已经连接,状态直接标记为 true')
+				} else {
+					// 还未连接,绑定 onopen
+					this.webSco.onopen = this.websocketonopen.bind(this)
+				}
+				// 绑定其他回调
+				this.webSco.onmessage = this.websocketonmessage.bind(this)
+				this.webSco.onerror = this.websocketonerror.bind(this)
+				this.webSco.onclose = this.websocketclose.bind(this)
 			},
 			//监听接收到消息的回调
 			websocketonmessage(event) {

+ 2 - 0
src/main.js

@@ -17,11 +17,13 @@ import '/public/style/index.css'
 
 import api from '../comment/api.js'
 import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
+import * as socket from '../comment/socket.js'
 
 
 
 const app = createApp(App);
 app.config.globalProperties.$Request = api;
+app.config.globalProperties.$socket = socket;
 for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
 	app.component(key, component)
 }

+ 2 - 51
src/views/home.vue

@@ -17,8 +17,6 @@
 </template>
 
 <script>
-	import {ElMessage} from 'element-plus'
-	import {ROOTPATH2} from '../../comment/httpUrl.js'
 	import headers from '../components/header/header.vue'
 	import sidebars from '../components/sidebar/sidebar.vue'
 	import copyright from '../components/copyright/copyright.vue'
@@ -35,7 +33,7 @@
 		},
 		data() {
 			return {
-				ws: null,
+				
 			}
 		},
 		mounted() {
@@ -44,57 +42,10 @@
 			}
 			const userId = localStorage.getItem('userId');
 			// 连接websocket
-			// this.connectSocket(userId);
+			this.$socket.connectSocket(userId, this.$store, this.$router)
 			
 		},
-		beforeDestroy() {
-			if (this.ws && this.ws.readyState === WebSocket.OPEN) {
-				console.log('♻️ 组件销毁前关闭 WebSocket');
-				this.ws.close();
-			}
-		},
 		methods: {
-			connectSocket(userId) {
-				if (!userId) return;
-				// 若已有连接则关闭
-				if (this.ws) {
-					this.ws.close();
-				}
-				this.ws = new WebSocket(ROOTPATH2 + '' + userId)
-				// 连接成功
-				this.ws.onopen = () => {
-					console.log('WebSocket 已连接:', ROOTPATH2 + '' + userId);
-				};
-				// 收到消息
-				this.ws.onmessage = (event) => {
-					console.log(event)
-					if (event.data === 'HeartBeat') return;
-					let msg;
-					try {
-						msg = JSON.parse(event.data);
-					} catch (e) {
-						console.warn('非JSON消息:', event.data);
-						return;
-					}
-					if (msg.type === 'kickOut') {
-						ElMessage({
-							message: msg.content || '您的账号已在其他设备登录',
-							type: 'warning',
-							duration: 1500,
-							offset: this.screenHeight / 2
-						})
-						localStorage.removeItem('token');
-						localStorage.removeItem('userId');
-						localStorage.removeItem('userType');
-						localStorage.removeItem('companyId');
-						// 调用重置状态的函数  
-						this.$store.dispatch('SET_DATA');
-						this.$router.push({
-							name: 'login'
-						})
-					}
-				};
-			},
 			//获取用户信息
 			getUserInfo() {
 				this.$Request.get('/app/user/selectUserById').then(res => {

+ 2 - 1
src/views/index/indexCompany.vue

@@ -934,7 +934,8 @@
 						name: 'resumeInfo',
 						query: {
 							resumesId: item.resumesId,
-							postPushId: this.tabs[this.tabsCurr].postPushId
+							postPushId: this.tabs[this.tabsCurr].postPushId,
+							ruleClassifyId: this.tabs[this.tabsCurr].ruleClassifyId,
 						}
 					})
 				} else {

+ 17 - 9
src/views/message/index.vue

@@ -75,7 +75,7 @@
 									<div v-else-if="item.messageType == 5" class="content-l-box-item-con-cons">
 										手机号请求
 									</div>
-									<div v-if="item.messageType == 99" class="content-l-box-item-con-cons">
+									<div v-else-if="item.messageType == 99" class="content-l-box-item-con-cons">
 										[面试邀约]
 									</div>
 									<div v-else class="content-l-box-item-con-cons">
@@ -565,7 +565,9 @@
 				sysLimit: 10,
 				jobList:[],
 				isShowCommon:false, //是否显示常用语
-				changYongList:[]
+				changYongList:[],
+				webSocketState:false,
+				webSco:null
 			}
 		},
 		computed: {
@@ -810,14 +812,20 @@
 				});
 
 			},
-			//初始化并链接webscoket
 			initScoket() {
-				//创建WebSocket对象并进行初始化连接
-				this.webSco = new WebSocket(ROOTPATH2 + '' + this.userId)
-				this.webSco.onopen = this.websocketonopen;
-				this.webSco.onmessage = this.websocketonmessage;
-				this.webSco.onerror = this.websocketonerror;
-				this.webSco.onclose = this.websocketclose;
+				this.webSco = this.$socket.getSocket();
+				// 如果 WebSocket 已经连接,直接把状态标记为 true
+				if (this.webSco.readyState === WebSocket.OPEN) {
+					this.webSocketState = true
+					// console.log('✅ WebSocket 已经连接,状态直接标记为 true')
+				} else {
+					// 还未连接,绑定 onopen
+					this.webSco.onopen = this.websocketonopen.bind(this)
+				}
+				// 绑定其他回调
+				this.webSco.onmessage = this.websocketonmessage.bind(this)
+				this.webSco.onerror = this.websocketonerror.bind(this)
+				this.webSco.onclose = this.websocketclose.bind(this)
 			},
 			//监听接收到消息的回调
 			websocketonmessage(event) {

+ 11 - 4
src/views/resume/resumeInfo.vue

@@ -73,11 +73,11 @@
 		<!-- 工作经历/教育经历 -->
 		<div class="postJs flex align-center justify-center">
 			<div class="postJs-box">
-				<div class="postJs-box-title" v-if="info.intentionList && info.intentionList.length > 0">
+				<div class="postJs-box-title" v-if="info.ruleClassifyArr && info.ruleClassifyArr.length > 0">
 					求职期望
 				</div>
-				<div style="margin: 20px 0;" v-if="info.intentionList && info.intentionList.length > 0">
-					<div v-for="(item, index) in info.intentionList" :key="index" class="flex expectation align-center">
+				<div style="margin: 20px 0;" v-if="info.ruleClassifyArr && info.ruleClassifyArr.length > 0">
+					<div v-for="(item, index) in info.ruleClassifyArr" :key="index" class="flex expectation align-center">
 						{{ item.ruleClassifyName || '无' }}<el-divider direction="vertical" />{{ item.citys || '无' }}
 						<span v-if="item.postType">({{ item.postType }})</span>
 						<el-divider direction="vertical" />{{ item.salaryRange ? item.salaryRange + '元' : '薪资未填写' }}
@@ -237,12 +237,14 @@
 				postPushId: '', //岗位id
 				imgs: [], //上传图片
 				loading: false,
+				ruleClassifyId:''
 			}
 		},
 		mounted() {
 			this.screenHeight = window.innerHeight
 			this.resumesId = this.$route.query.resumesId
-			this.postPushId = this.$route.query.postPushId || localStorage.getItem('postPushId')
+			this.postPushId = this.$route.query.postPushId || localStorage.getItem('postPushId');
+			this.ruleClassifyId = this.$route.query.ruleClassifyId || '';
 			let inviterCode = this.$route.query.inviterCode
 			if (inviterCode) {
 				localStorage.setItem('inviterCode', inviterCode)
@@ -690,6 +692,11 @@
 							major: this.info.major,
 							resumesEducation: this.info.resumesEducation
 						}]
+						if(this.ruleClassifyId){
+							this.info.ruleClassifyArr = res.data.intentionList.filter(item => item.ruleClassifyId == this.ruleClassifyId)
+						}else{
+							this.info.ruleClassifyArr = res.data.intentionList
+						}
 						this.info.schoolList = arr
 					} else {
 						ElMessage({

+ 2 - 1
src/views/search/searchCom.vue

@@ -443,7 +443,8 @@
 					this.$router.push({
 						name: 'resumeInfo',
 						query: {
-							resumesId: item.resumesId
+							resumesId: item.resumesId,
+							ruleClassifyId: this.tabs[this.tabsCurr].ruleClassifyId,
 						}
 					})
 				} else {