fix:最新版本
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { API_CLIENT_ID } from '@/api/request'
|
||||
import { getApiBaseUrl, getMessageWsUrl } from '@/config/app-env'
|
||||
import { getStoredToken } from '@/utils/auth-token'
|
||||
import {
|
||||
decodeSocketMessageData,
|
||||
@@ -7,82 +9,122 @@ import {
|
||||
import type { MessagePushPayload } from '@/types/message'
|
||||
|
||||
const HEARTBEAT_INTERVAL_MS = 25000
|
||||
const RECONNECT_DELAY_MS = 25000
|
||||
const RECONNECT_BASE_MS = 10000
|
||||
const RECONNECT_MAX_MS = 30000
|
||||
const RUNTIME_KEY = '__CZZJ_MESSAGE_WS_RUNTIME__'
|
||||
|
||||
let socketTask: UniApp.SocketTask | null = null
|
||||
let heartbeatTimer: ReturnType<typeof setInterval> | null = null
|
||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let connecting = false
|
||||
let manualClosed = false
|
||||
let socketOpened = false
|
||||
let disconnectHandled = false
|
||||
interface MessageWsRuntime {
|
||||
socketTask: UniApp.SocketTask | null
|
||||
connecting: boolean
|
||||
socketOpened: boolean
|
||||
manualClosed: boolean
|
||||
connectionSeq: number
|
||||
disconnectHandled: boolean
|
||||
heartbeatTimer: ReturnType<typeof setInterval> | null
|
||||
reconnectTimer: ReturnType<typeof setTimeout> | null
|
||||
reconnectAttempt: number
|
||||
}
|
||||
|
||||
const listeners = new Set<(payload: MessagePushPayload) => void>()
|
||||
|
||||
function createRuntime(): MessageWsRuntime {
|
||||
return {
|
||||
socketTask: null,
|
||||
connecting: false,
|
||||
socketOpened: false,
|
||||
manualClosed: false,
|
||||
connectionSeq: 0,
|
||||
disconnectHandled: false,
|
||||
heartbeatTimer: null,
|
||||
reconnectTimer: null,
|
||||
reconnectAttempt: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/** 全局单例,避免 HMR / 重复 import 后丢失连接状态又新建多条 WS */
|
||||
function runtime(): MessageWsRuntime {
|
||||
if (typeof globalThis !== 'undefined') {
|
||||
const holder = globalThis as typeof globalThis & {
|
||||
[RUNTIME_KEY]?: MessageWsRuntime
|
||||
}
|
||||
if (!holder[RUNTIME_KEY]) {
|
||||
holder[RUNTIME_KEY] = createRuntime()
|
||||
}
|
||||
return holder[RUNTIME_KEY]
|
||||
}
|
||||
return createRuntime()
|
||||
}
|
||||
|
||||
function resolveMessageWsBase(): string {
|
||||
const configured = import.meta.env.VITE_MESSAGE_WS_URL?.trim()
|
||||
const configured = getMessageWsUrl().trim()
|
||||
if (configured) {
|
||||
return configured.replace(/[?#].*$/, '').replace(/\/$/, '')
|
||||
}
|
||||
|
||||
const envBase = import.meta.env.VITE_API_BASE_URL?.trim() || ''
|
||||
const fallback = import.meta.env.VITE_API_PROXY_TARGET?.trim() || 'http://192.168.0.222:8082'
|
||||
const httpBase = envBase || fallback
|
||||
const httpBase = getApiBaseUrl() || 'http://192.168.0.222:8082'
|
||||
const wsBase = httpBase.replace(/^https/i, 'wss').replace(/^http/i, 'ws')
|
||||
return `${wsBase}/app/ws/message`
|
||||
}
|
||||
|
||||
function resolveMessageWsUrl(token: string): string {
|
||||
const base = resolveMessageWsBase()
|
||||
return `${base}?token=${encodeURIComponent(token)}`
|
||||
const query = `token=${encodeURIComponent(token)}&clientId=${encodeURIComponent(API_CLIENT_ID)}`
|
||||
return `${base}?${query}`
|
||||
}
|
||||
|
||||
function stopHeartbeat() {
|
||||
if (heartbeatTimer) clearInterval(heartbeatTimer)
|
||||
heartbeatTimer = null
|
||||
function stopHeartbeat(state: MessageWsRuntime) {
|
||||
if (state.heartbeatTimer) clearInterval(state.heartbeatTimer)
|
||||
state.heartbeatTimer = null
|
||||
}
|
||||
|
||||
function sendPing() {
|
||||
if (!socketOpened || !socketTask) return
|
||||
socketTask.send({ data: 'ping' })
|
||||
function sendPing(state: MessageWsRuntime) {
|
||||
if (!state.socketOpened || !state.socketTask) return
|
||||
state.socketTask.send({ data: 'ping' })
|
||||
}
|
||||
|
||||
function startHeartbeat() {
|
||||
stopHeartbeat()
|
||||
sendPing()
|
||||
heartbeatTimer = setInterval(sendPing, HEARTBEAT_INTERVAL_MS)
|
||||
function startHeartbeat(state: MessageWsRuntime) {
|
||||
stopHeartbeat(state)
|
||||
sendPing(state)
|
||||
state.heartbeatTimer = setInterval(() => {
|
||||
sendPing(state)
|
||||
}, HEARTBEAT_INTERVAL_MS)
|
||||
}
|
||||
|
||||
function clearReconnectTimer() {
|
||||
if (reconnectTimer) clearTimeout(reconnectTimer)
|
||||
reconnectTimer = null
|
||||
function clearReconnectTimer(state: MessageWsRuntime) {
|
||||
if (state.reconnectTimer) clearTimeout(state.reconnectTimer)
|
||||
state.reconnectTimer = null
|
||||
}
|
||||
|
||||
function scheduleReconnect() {
|
||||
if (manualClosed || !getStoredToken()) return
|
||||
clearReconnectTimer()
|
||||
reconnectTimer = setTimeout(() => {
|
||||
function resetSocketState(state: MessageWsRuntime) {
|
||||
stopHeartbeat(state)
|
||||
state.socketTask = null
|
||||
state.socketOpened = false
|
||||
state.connecting = false
|
||||
state.disconnectHandled = false
|
||||
}
|
||||
|
||||
function scheduleReconnect(state: MessageWsRuntime) {
|
||||
if (state.manualClosed || !getStoredToken()) return
|
||||
clearReconnectTimer(state)
|
||||
const delay = Math.min(
|
||||
RECONNECT_BASE_MS * Math.pow(1.5, state.reconnectAttempt),
|
||||
RECONNECT_MAX_MS,
|
||||
)
|
||||
state.reconnectAttempt += 1
|
||||
state.reconnectTimer = setTimeout(() => {
|
||||
connectMessageSocket()
|
||||
}, RECONNECT_DELAY_MS)
|
||||
}, delay)
|
||||
}
|
||||
|
||||
function cleanupSocketTask() {
|
||||
stopHeartbeat()
|
||||
socketTask = null
|
||||
socketOpened = false
|
||||
connecting = false
|
||||
disconnectHandled = false
|
||||
}
|
||||
|
||||
function handleDisconnect(reason: string) {
|
||||
if (disconnectHandled) return
|
||||
disconnectHandled = true
|
||||
function handleDisconnect(state: MessageWsRuntime, reason: string) {
|
||||
if (state.disconnectHandled) return
|
||||
state.disconnectHandled = true
|
||||
if (import.meta.env.DEV) {
|
||||
console.log(`[Message WS] ${reason}`)
|
||||
}
|
||||
cleanupSocketTask()
|
||||
if (!manualClosed) {
|
||||
scheduleReconnect()
|
||||
resetSocketState(state)
|
||||
if (!state.manualClosed) {
|
||||
scheduleReconnect(state)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,12 +144,44 @@ function handleSocketMessage(raw: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function bindSocketTask(task: UniApp.SocketTask, seq: number, state: MessageWsRuntime) {
|
||||
state.socketTask = task
|
||||
|
||||
task.onOpen(() => {
|
||||
if (seq !== state.connectionSeq) return
|
||||
state.socketOpened = true
|
||||
state.connecting = false
|
||||
state.reconnectAttempt = 0
|
||||
if (import.meta.env.DEV) {
|
||||
console.log('[Message WS] connected')
|
||||
}
|
||||
startHeartbeat(state)
|
||||
})
|
||||
|
||||
task.onMessage((res) => {
|
||||
if (seq !== state.connectionSeq) return
|
||||
handleSocketMessage(decodeSocketMessageData(res.data))
|
||||
})
|
||||
|
||||
task.onClose(() => {
|
||||
if (seq !== state.connectionSeq) return
|
||||
handleDisconnect(state, 'closed')
|
||||
})
|
||||
|
||||
task.onError(() => {
|
||||
if (seq !== state.connectionSeq) return
|
||||
handleDisconnect(state, 'error')
|
||||
})
|
||||
}
|
||||
|
||||
export function isMessageSocketConnected(): boolean {
|
||||
return socketOpened && !!socketTask
|
||||
const state = runtime()
|
||||
return state.socketOpened && !!state.socketTask
|
||||
}
|
||||
|
||||
export function isMessageSocketConnecting(): boolean {
|
||||
return connecting || (!!socketTask && !socketOpened)
|
||||
const state = runtime()
|
||||
return state.connecting || (!!state.socketTask && !state.socketOpened)
|
||||
}
|
||||
|
||||
export function onMessagePush(listener: (payload: MessagePushPayload) => void) {
|
||||
@@ -116,64 +190,78 @@ export function onMessagePush(listener: (payload: MessagePushPayload) => void) {
|
||||
}
|
||||
|
||||
export function connectMessageSocket() {
|
||||
const state = runtime()
|
||||
const token = getStoredToken()
|
||||
if (!token || manualClosed) return
|
||||
if (socketOpened && socketTask) return
|
||||
// 握手进行中:不要 close,否则 DevTools 会报 "closed before connection is established"
|
||||
if (isMessageSocketConnecting()) return
|
||||
if (!token || state.manualClosed) return
|
||||
if (state.socketOpened && state.socketTask) return
|
||||
if (state.connecting) return
|
||||
|
||||
manualClosed = false
|
||||
connecting = true
|
||||
disconnectHandled = false
|
||||
clearReconnectTimer()
|
||||
state.manualClosed = false
|
||||
const seq = ++state.connectionSeq
|
||||
clearReconnectTimer(state)
|
||||
|
||||
const task = uni.connectSocket({
|
||||
url: resolveMessageWsUrl(token),
|
||||
fail: (err) => {
|
||||
if (import.meta.env.DEV) {
|
||||
console.log('[Message WS] connect fail', err)
|
||||
}
|
||||
handleDisconnect('connect fail')
|
||||
},
|
||||
})
|
||||
|
||||
socketTask = task
|
||||
|
||||
task.onOpen(() => {
|
||||
socketOpened = true
|
||||
connecting = false
|
||||
if (import.meta.env.DEV) {
|
||||
console.log('[Message WS] connected')
|
||||
}
|
||||
startHeartbeat()
|
||||
})
|
||||
|
||||
task.onMessage((res) => {
|
||||
handleSocketMessage(decodeSocketMessageData(res.data))
|
||||
})
|
||||
|
||||
task.onClose(() => {
|
||||
handleDisconnect('closed')
|
||||
})
|
||||
|
||||
task.onError(() => {
|
||||
handleDisconnect('error')
|
||||
})
|
||||
}
|
||||
|
||||
export function disconnectMessageSocket() {
|
||||
manualClosed = true
|
||||
clearReconnectTimer()
|
||||
stopHeartbeat()
|
||||
disconnectHandled = true
|
||||
if (socketTask) {
|
||||
const previousTask = state.socketTask
|
||||
state.socketTask = null
|
||||
state.socketOpened = false
|
||||
if (previousTask) {
|
||||
try {
|
||||
socketTask.close({})
|
||||
previousTask.close({})
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
state.connecting = true
|
||||
state.disconnectHandled = false
|
||||
|
||||
const url = resolveMessageWsUrl(token)
|
||||
if (import.meta.env.DEV) {
|
||||
console.log('[Message WS] connect', url.replace(/token=[^&]+/, 'token=***'))
|
||||
}
|
||||
|
||||
const task = uni.connectSocket({
|
||||
url,
|
||||
fail: (err) => {
|
||||
if (seq !== state.connectionSeq) return
|
||||
if (import.meta.env.DEV) {
|
||||
console.log('[Message WS] connect fail', err)
|
||||
}
|
||||
resetSocketState(state)
|
||||
scheduleReconnect(state)
|
||||
},
|
||||
})
|
||||
|
||||
if (!task || typeof task.onOpen !== 'function') {
|
||||
state.connecting = false
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn('[Message WS] invalid SocketTask')
|
||||
}
|
||||
scheduleReconnect(state)
|
||||
return
|
||||
}
|
||||
|
||||
bindSocketTask(task, seq, state)
|
||||
}
|
||||
|
||||
export function disconnectMessageSocket() {
|
||||
const state = runtime()
|
||||
state.manualClosed = true
|
||||
state.connectionSeq += 1
|
||||
state.reconnectAttempt = 0
|
||||
clearReconnectTimer(state)
|
||||
stopHeartbeat(state)
|
||||
state.disconnectHandled = true
|
||||
|
||||
const task = state.socketTask
|
||||
state.socketTask = null
|
||||
state.socketOpened = false
|
||||
state.connecting = false
|
||||
|
||||
if (task) {
|
||||
try {
|
||||
task.close({})
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
socketTask = null
|
||||
socketOpened = false
|
||||
connecting = false
|
||||
}
|
||||
|
||||
@@ -3,103 +3,72 @@ import logoCenter from '@/static/logo-center@2x.png'
|
||||
import iconPhone from '@/static/icon-phone@2x.png'
|
||||
import iconCode from '@/static/icon-code@2x.png'
|
||||
import iconChecked from '@/static/icon-checked@2x.png'
|
||||
import iconPassword from '@/static/路径.png'
|
||||
import iconDeviceDetailSettings from '@/static/路径1.png'
|
||||
import deviceDetailHeroBg from '@/static/组 15226@2x.png'
|
||||
import iconRunModeInfo from '@/static/组 15227@2x.png'
|
||||
import iconHomeSelector from '@/static/图标 4.png'
|
||||
import iconHomeArrow from '@/static/组 9.png'
|
||||
import iconHomeSelected from '@/static/完成@2x.png'
|
||||
import iconNotify from '@/static/通知 (1).png'
|
||||
import iconDeviceEmpty from '@/static/图标 8.png'
|
||||
import iconDeviceNameEdit from '@/static/图标 9.png'
|
||||
import iconBindComplete from '@/static/组 41@2x.png'
|
||||
import iconBluetooth from '@/static/蓝牙-F.png'
|
||||
import iconBindDiscover from '@/static/组 281@2x.png'
|
||||
import iconBindPair from '@/static/组 286@2x.png'
|
||||
import iconBindSecure from '@/static/组 291@2x.png'
|
||||
import iconScanDevice from '@/static/组 600@2x.png'
|
||||
import iconScanDeviceAction from '@/static/组 468@2x.png'
|
||||
import iconScanDeviceActionDisabled from '@/static/组 476@2x.png'
|
||||
import iconPassword from '@/static/icon-password.png'
|
||||
import iconDeviceDetailSettings from '@/static/icon-device-settings.png'
|
||||
import deviceDetailHeroBg from '@/static/device-detail-hero-bg@2x.png'
|
||||
import iconRunModeInfo from '@/static/icon-run-mode-info@2x.png'
|
||||
import iconHomeSelector from '@/static/icon-home-selector.png'
|
||||
import iconHomeArrow from '@/static/icon-home-arrow.png'
|
||||
import iconHomeSelected from '@/static/icon-home-selected@2x.png'
|
||||
import iconNotify from '@/static/icon-notify.png'
|
||||
import iconDeviceEmpty from '@/static/icon-device-empty.png'
|
||||
import iconDeviceNameEdit from '@/static/icon-device-name-edit.png'
|
||||
import iconBindComplete from '@/static/icon-bind-complete@2x.png'
|
||||
import iconBluetooth from '@/static/icon-bluetooth.png'
|
||||
import iconBindDiscover from '@/static/icon-bind-discover@2x.png'
|
||||
import iconBindPair from '@/static/icon-bind-pair@2x.png'
|
||||
import iconBindSecure from '@/static/icon-bind-secure@2x.png'
|
||||
import iconScanDevice from '@/static/icon-scan-device@2x.png'
|
||||
import iconScanDeviceAction from '@/static/icon-scan-device-action@2x.png'
|
||||
import iconScanDeviceActionDisabled from '@/static/icon-scan-device-action-disabled@2x.png'
|
||||
import tabDevice from '@/static/tab-device-active.png'
|
||||
import tabDeviceActive from '@/static/tab-device.png'
|
||||
import tabMine from '@/static/tab-mine-active.png'
|
||||
import tabMineActive from '@/static/tab-mine.png'
|
||||
import iconChevronLeft from '@/static/chevron.left.png'
|
||||
import iconSupportMic from '@/static/组 134.png'
|
||||
import iconSupportPhoto from '@/static/图片.png'
|
||||
import iconSupportSend from '@/static/图标 14.png'
|
||||
import iconSupportQueue from '@/static/组 98@2x.png'
|
||||
import iconMineNotify from '@/static/组 14919@2x.png'
|
||||
import iconMineOrder from '@/static/订单@2x.png'
|
||||
import iconMineCoupon from '@/static/优惠券@2x.png'
|
||||
import iconMineAddress from '@/static/组 14923@2x.png'
|
||||
import iconChevronLeft from '@/static/icon-chevron-left.png'
|
||||
import iconSupportMic from '@/static/icon-support-mic.png'
|
||||
import iconSupportPhoto from '@/static/icon-support-photo.png'
|
||||
import iconSupportSend from '@/static/icon-support-send.png'
|
||||
import iconSupportQueue from '@/static/icon-support-queue@2x.png'
|
||||
import iconMineNotify from '@/static/icon-mine-notify@2x.png'
|
||||
import iconMineOrder from '@/static/icon-mine-order@2x.png'
|
||||
import iconMineCoupon from '@/static/icon-mine-coupon@2x.png'
|
||||
import iconMineAddress from '@/static/icon-mine-address@2x.png'
|
||||
|
||||
export const staticAssets = {
|
||||
/** 组 15106@2x.png — 登录区中心大图标 */
|
||||
logo: logoCenter,
|
||||
/** 手机号@2x.png */
|
||||
iconPhone,
|
||||
/** 验证码@2x.png */
|
||||
iconCode,
|
||||
/** 选中@2x.png */
|
||||
iconChecked,
|
||||
/** 路径.png — 密码框 */
|
||||
iconPassword,
|
||||
/** 路径1.png — 设备详情右上角设置 */
|
||||
iconDeviceDetailSettings,
|
||||
/** 组 15226@2x.png — 设备详情页上半部分背景 */
|
||||
deviceDetailHeroBg,
|
||||
/** 组 15227@2x.png — 设备详情「运行模式」旁提示图标 */
|
||||
iconRunModeInfo,
|
||||
/** 图标 4.png — 首页「我的家」 */
|
||||
iconHomeSelector,
|
||||
/** 组 9.png — 「我的家」下拉箭头 */
|
||||
iconHomeArrow,
|
||||
/** 完成@2x.png — 「我的家」下拉选中图标 */
|
||||
iconHomeSelected,
|
||||
/** 通知 (1).png — 设备页右上角通知 */
|
||||
iconNotify,
|
||||
/** 图标 8.png — 设备空状态中心图标 */
|
||||
iconDeviceEmpty,
|
||||
/** 图标 9.png — 完成配置「设备名称」编辑 */
|
||||
iconDeviceNameEdit,
|
||||
/** 组 41@2x.png — 完成配置成功示意图 */
|
||||
iconBindComplete,
|
||||
/** 蓝牙-F.png — 添加设备页蓝牙示意图 */
|
||||
iconBluetooth,
|
||||
/** 组 281@2x.png — 添加设备「发现附近设备」 */
|
||||
iconBindDiscover,
|
||||
/** 组 286@2x.png — 添加设备「快速配对连接」 */
|
||||
iconBindPair,
|
||||
/** 组 291@2x.png — 添加设备「本地安全控制」 */
|
||||
iconBindSecure,
|
||||
/** 组 600@2x.png — 扫描设备页示意图 */
|
||||
iconScanDevice,
|
||||
/** 组 468@2x.png — 发现设备列表「可添加」箭头 */
|
||||
iconScanDeviceAction,
|
||||
/** 组 476@2x.png — 发现设备列表「不可添加」箭头 */
|
||||
iconScanDeviceActionDisabled,
|
||||
tabDevice,
|
||||
tabDeviceActive,
|
||||
tabMine,
|
||||
tabMineActive,
|
||||
/** chevron.left.png — 列表右箭头(旋转 180° 使用) */
|
||||
iconChevronLeft,
|
||||
/** 组 134.png — 在线客服页麦克风 */
|
||||
iconSupportMic,
|
||||
/** 图片.png — 在线客服页发送图片 */
|
||||
iconSupportPhoto,
|
||||
/** 图标 14.png — 在线客服页发送 */
|
||||
iconSupportSend,
|
||||
/** 组 98@2x.png — 在线客服排队卡片标题图标 */
|
||||
iconSupportQueue,
|
||||
/** 组 14919@2x.png — 我的页消息通知 */
|
||||
iconMineNotify,
|
||||
/** 订单@2x.png — 我的页我的订单 */
|
||||
iconMineOrder,
|
||||
/** 优惠券@2x.png — 我的页优惠券 */
|
||||
iconMineCoupon,
|
||||
/** 组 14923@2x.png — 我的页收货地址 */
|
||||
iconMineAddress,
|
||||
} as const
|
||||
|
||||
@@ -6,11 +6,17 @@ export function isValidSmsCode(code: string): boolean {
|
||||
return /^\d{4,6}$/.test(code.trim())
|
||||
}
|
||||
|
||||
/** 登录时输入校验(兼容历史短密码账号) */
|
||||
export function isValidPassword(password: string): boolean {
|
||||
return password.length >= 6 && password.length <= 32
|
||||
}
|
||||
|
||||
/** 设置/修改登录密码:8-20 位,且字母、数字、符号至少包含 2 种 */
|
||||
/** 注册 / 修改密码统一规则说明 */
|
||||
export const PASSWORD_RULE_HINT = '8-20位字符,须包含字母、数字、符号中的至少2种'
|
||||
|
||||
export const PASSWORD_INVALID_TOAST = `密码需${PASSWORD_RULE_HINT.replace('。', '')}`
|
||||
|
||||
/** 注册 / 修改密码:8-20 位,且字母、数字、符号至少包含 2 种 */
|
||||
export function isValidStrongPassword(password: string): boolean {
|
||||
const value = password.trim()
|
||||
if (value.length < 8 || value.length > 20) return false
|
||||
|
||||
Reference in New Issue
Block a user