From 591ecc7199e5a892beb0bc5aa44b3bfbc338d945 Mon Sep 17 00:00:00 2001 From: bob <312777916@qq.com> Date: Sat, 14 Sep 2024 11:31:50 +0800 Subject: [PATCH] =?UTF-8?q?ws=E8=BF=9E=E6=8E=A5=E8=AF=B7=E6=B1=82=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0sign=E9=AA=8C=E7=AD=BE=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/wsConnect.js | 16 ++++++++-------- src/utils/common.js | 11 +++++++++++ src/utils/request.js | 19 +++++-------------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/api/wsConnect.js b/src/api/wsConnect.js index 5d3faff..2b1bffb 100644 --- a/src/api/wsConnect.js +++ b/src/api/wsConnect.js @@ -1,6 +1,8 @@ import { Msg, Header, MsgType } from '@/proto/msg' import { proto } from '@/const/msgConst' import { userStore } from '@/stores' +import { v4 as uuidv4 } from 'uuid' +import { generateSign } from '@/utils/common' class WsConnect { /** @@ -18,11 +20,6 @@ class WsConnect { return WsConnect.instance } - /** - * 用户数据 - */ - userData - /** * WebSockt连接对象 */ @@ -80,7 +77,6 @@ class WsConnect { * 构造函数 */ constructor() { - this.userData = userStore() this.buffer = new Uint8Array() this.isConnect = false @@ -101,8 +97,12 @@ class WsConnect { return } console.log('create websocket') - const token = await this.userData.getAccessToken() - this.url = `${import.meta.env.VITE_WS_URL}?token=${token}` + const userData = userStore() + const token = await userData.getAccessToken() + const traceId = uuidv4() + const timestamp = Math.floor(new Date().getTime() / 1000) + const sign = generateSign(userData.at.secret, `${traceId}${timestamp}`) + this.url = `${import.meta.env.VITE_WS_URL}?traceId=${traceId}×tamp=${timestamp}&sign=${sign}&token=${token}` this.connect = new WebSocket(this.url) this.connect.onmessage = this.onMessage.bind(this) this.connect.onclose = this.onClose.bind(this) diff --git a/src/utils/common.js b/src/utils/common.js index 412122e..7e94a90 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -1,3 +1,5 @@ +import CryptoJS from 'crypto-js' + export const maskPhoneNum = (str) => { if (str.length < 7) { return '*' @@ -138,3 +140,12 @@ export const messageBoxShowTime = (datatime) => { return `${year}-${month}-${day} ${hours}:${minutes}` } + +export const generateSign = (key, content) => { + try { + const hash = CryptoJS.HmacSHA256(content, key) + return CryptoJS.enc.Base64.stringify(hash) + } catch (e) { + return null + } +} diff --git a/src/utils/request.js b/src/utils/request.js index fac86b5..79d1653 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,7 +1,7 @@ import axios from 'axios' import { userStore } from '@/stores' import router from '@/router' -import CryptoJS from 'crypto-js' +import { generateSign } from './common' import { v4 as uuidv4 } from 'uuid' const baseURL = '/api' //配合vite.config.js中的代理配置解决跨域问题 @@ -11,15 +11,6 @@ const instance = axios.create({ timeout: 3000 }) -const generateSign = (key, content) => { - try { - const hash = CryptoJS.HmacSHA256(content, key) - return CryptoJS.enc.Base64.stringify(hash) - } catch (e) { - return null - } -} - // 请求拦截器 instance.interceptors.request.use( async (config) => { @@ -27,19 +18,19 @@ instance.interceptors.request.use( if (config.url === '/user/refreshToken' && userData.rt.token !== '') { const traceId = uuidv4() const timestamp = Math.floor(new Date().getTime() / 1000) - const sigh = generateSign(userData.rt.secret, `${traceId}${timestamp}`) + const sign = generateSign(userData.rt.secret, `${traceId}${timestamp}`) config.headers.traceId = traceId config.headers.timestamp = timestamp - config.headers.sign = sigh + config.headers.sign = sign config.headers.refreshToken = userData.rt.token } else if (userData.at.token !== '') { const token = await userData.getAccessToken() const traceId = uuidv4() const timestamp = Math.floor(new Date().getTime() / 1000) - const sigh = generateSign(userData.at.secret, `${traceId}${timestamp}`) + const sign = generateSign(userData.at.secret, `${traceId}${timestamp}`) config.headers.traceId = traceId config.headers.timestamp = timestamp - config.headers.sign = sigh + config.headers.sign = sign config.headers.accessToken = token } return config