@anker-in/campaign-ui 0.4.0-beta.7 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/components/LiveChatWidget/LiveChatWidget.js +1 -1
- package/dist/cjs/components/LiveChatWidget/LiveChatWidget.js.map +3 -3
- package/dist/cjs/components/LiveChatWidget/components/MessageContent/ProductComparison.d.ts +1 -0
- package/dist/cjs/components/LiveChatWidget/components/MessageContent/ProductComparison.js +1 -1
- package/dist/cjs/components/LiveChatWidget/components/MessageContent/ProductComparison.js.map +3 -3
- package/dist/cjs/components/LiveChatWidget/hooks/useChatState.d.ts +11 -2
- package/dist/cjs/components/LiveChatWidget/hooks/useChatState.js +1 -1
- package/dist/cjs/components/LiveChatWidget/hooks/useChatState.js.map +3 -3
- package/dist/cjs/components/LiveChatWidget/index.d.ts +1 -1
- package/dist/cjs/components/LiveChatWidget/index.js +1 -1
- package/dist/cjs/components/LiveChatWidget/index.js.map +2 -2
- package/dist/cjs/components/LiveChatWidget/types.d.ts +14 -0
- package/dist/cjs/components/LiveChatWidget/types.js.map +1 -1
- package/dist/cjs/components/LiveChatWidget/utils/userId.d.ts +7 -2
- package/dist/cjs/components/LiveChatWidget/utils/userId.js +1 -1
- package/dist/cjs/components/LiveChatWidget/utils/userId.js.map +3 -3
- package/dist/cjs/stories/LiveChatWidget.stories.js +1 -1
- package/dist/cjs/stories/LiveChatWidget.stories.js.map +2 -2
- package/dist/esm/components/LiveChatWidget/LiveChatWidget.js +1 -1
- package/dist/esm/components/LiveChatWidget/LiveChatWidget.js.map +3 -3
- package/dist/esm/components/LiveChatWidget/components/MessageContent/ProductComparison.d.ts +1 -0
- package/dist/esm/components/LiveChatWidget/components/MessageContent/ProductComparison.js +1 -1
- package/dist/esm/components/LiveChatWidget/components/MessageContent/ProductComparison.js.map +3 -3
- package/dist/esm/components/LiveChatWidget/hooks/useChatState.d.ts +11 -2
- package/dist/esm/components/LiveChatWidget/hooks/useChatState.js +1 -1
- package/dist/esm/components/LiveChatWidget/hooks/useChatState.js.map +3 -3
- package/dist/esm/components/LiveChatWidget/index.d.ts +1 -1
- package/dist/esm/components/LiveChatWidget/index.js +1 -1
- package/dist/esm/components/LiveChatWidget/index.js.map +3 -3
- package/dist/esm/components/LiveChatWidget/types.d.ts +14 -0
- package/dist/esm/components/LiveChatWidget/utils/userId.d.ts +7 -2
- package/dist/esm/components/LiveChatWidget/utils/userId.js +1 -1
- package/dist/esm/components/LiveChatWidget/utils/userId.js.map +3 -3
- package/dist/esm/stories/LiveChatWidget.stories.js +1 -1
- package/dist/esm/stories/LiveChatWidget.stories.js.map +2 -2
- package/package.json +1 -1
- package/src/components/LiveChatWidget/LiveChatWidget.tsx +44 -8
- package/src/components/LiveChatWidget/components/MessageContent/ProductComparison.tsx +6 -13
- package/src/components/LiveChatWidget/hooks/useChatState.ts +26 -7
- package/src/components/LiveChatWidget/index.tsx +1 -1
- package/src/components/LiveChatWidget/types.ts +15 -0
- package/src/components/LiveChatWidget/utils/userId.ts +13 -62
- package/src/stories/LiveChatWidget.stories.tsx +1 -1
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
* 策略:
|
|
6
6
|
* 1. 优先从 localStorage 读取
|
|
7
7
|
* 2. 尝试获取 Google Analytics ID (GAID)
|
|
8
|
-
* 3.
|
|
8
|
+
* 3. 返回空字符串,由后端生成
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
const STORAGE_KEY = 'livechat_user_id'
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* 获取用户唯一标识符(异步版本)
|
|
15
|
-
* @returns userId (GAID
|
|
15
|
+
* @returns userId (GAID 或空字符串,空字符串由后端生成)
|
|
16
16
|
*/
|
|
17
17
|
export async function getUserId(): Promise<string> {
|
|
18
18
|
// 1. 尝试从 localStorage 读取
|
|
@@ -30,21 +30,17 @@ export async function getUserId(): Promise<string> {
|
|
|
30
30
|
return gaid
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
// 3.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (typeof window !== 'undefined') {
|
|
45
|
-
localStorage.setItem(STORAGE_KEY, fallback)
|
|
46
|
-
}
|
|
47
|
-
return fallback
|
|
33
|
+
// 3. 返回空字符串,由后端生成
|
|
34
|
+
return ''
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 保存后端返回的 userId 到 localStorage
|
|
39
|
+
* @param id 后端生成的 userId
|
|
40
|
+
*/
|
|
41
|
+
export function saveUserId(id: string): void {
|
|
42
|
+
if (id && typeof window !== 'undefined') {
|
|
43
|
+
localStorage.setItem(STORAGE_KEY, id)
|
|
48
44
|
}
|
|
49
45
|
}
|
|
50
46
|
|
|
@@ -85,51 +81,6 @@ function getGAID(): string | null {
|
|
|
85
81
|
}
|
|
86
82
|
}
|
|
87
83
|
|
|
88
|
-
/**
|
|
89
|
-
* 生成哈希用户 ID(兜底方案)
|
|
90
|
-
* @returns 格式为 user-{hash} 的用户 ID,hash 由 timestamp + random 通过 SHA-256 生成
|
|
91
|
-
*/
|
|
92
|
-
async function generateHashedUserId(): Promise<string> {
|
|
93
|
-
const timestamp = Date.now()
|
|
94
|
-
const random = Math.random().toString(36).substring(2, 15) // 生成随机字符串
|
|
95
|
-
|
|
96
|
-
// 将 timestamp 和 random 组合成字符串
|
|
97
|
-
const rawString = `${timestamp}${random}`
|
|
98
|
-
|
|
99
|
-
// 使用 Web Crypto API 生成 SHA-256 哈希
|
|
100
|
-
const encoder = new TextEncoder()
|
|
101
|
-
const data = encoder.encode(rawString)
|
|
102
|
-
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
|
|
103
|
-
|
|
104
|
-
// 将 ArrayBuffer 转换为 16 进制字符串
|
|
105
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
|
106
|
-
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
|
|
107
|
-
|
|
108
|
-
// 取前 16 位作为用户 ID(保持简洁)
|
|
109
|
-
return `user-${hashHex.substring(0, 16)}`
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 同步版本的哈希用户 ID 生成(兜底的兜底)
|
|
114
|
-
* 当 Web Crypto API 不可用时使用
|
|
115
|
-
*/
|
|
116
|
-
function generateHashedUserIdSync(): string {
|
|
117
|
-
const timestamp = Date.now()
|
|
118
|
-
const random = Math.random().toString(36).substring(2, 15)
|
|
119
|
-
const rawString = `${timestamp}${random}`
|
|
120
|
-
|
|
121
|
-
// 使用简单的哈希算法作为兜底
|
|
122
|
-
let hash = 0
|
|
123
|
-
for (let i = 0; i < rawString.length; i++) {
|
|
124
|
-
const char = rawString.charCodeAt(i)
|
|
125
|
-
hash = (hash << 5) - hash + char
|
|
126
|
-
hash = hash & hash
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const hashString = Math.abs(hash).toString(16).padStart(8, '0')
|
|
130
|
-
return `user-${hashString}`
|
|
131
|
-
}
|
|
132
|
-
|
|
133
84
|
/**
|
|
134
85
|
* 清除保存的 userId(用于测试或重置)
|
|
135
86
|
*/
|
|
@@ -148,7 +148,7 @@ export const Default: Story = {
|
|
|
148
148
|
args: {
|
|
149
149
|
// 基础配置
|
|
150
150
|
loginUserId: 'test_test1',
|
|
151
|
-
apiBaseUrl: '
|
|
151
|
+
apiBaseUrl: 'https://beta-api-v2-livechat.anker.com',
|
|
152
152
|
site: 'beta.eufy.com',
|
|
153
153
|
channelCode: 'dtc',
|
|
154
154
|
title: 'eufy AI Assistant',
|