@rk911/tracker-sdk 1.7.7 → 1.7.9
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/index.js +28 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -233,6 +233,34 @@ function _ensureUserId(tracker) {
|
|
|
233
233
|
if (match) uid = decodeURIComponent(match[1])
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
+
// 4. 关键兜底:从 token / appToken (JWT payload) 中解析 unionid 或 userId
|
|
237
|
+
if (!uid) {
|
|
238
|
+
try {
|
|
239
|
+
var token = adapter.getStorage('token') || adapter.getStorage('appToken')
|
|
240
|
+
if (token && typeof token === 'string' && token.indexOf('.') > -1) {
|
|
241
|
+
var tokenParts = token.split('.')
|
|
242
|
+
if (tokenParts.length >= 2) {
|
|
243
|
+
var base64Url = tokenParts[1]
|
|
244
|
+
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/')
|
|
245
|
+
var decodedStr = ''
|
|
246
|
+
if (typeof atob === 'function') {
|
|
247
|
+
try {
|
|
248
|
+
decodedStr = decodeURIComponent(escape(atob(base64)))
|
|
249
|
+
} catch (e1) {
|
|
250
|
+
decodedStr = atob(base64)
|
|
251
|
+
}
|
|
252
|
+
} else if (typeof Buffer !== 'undefined') {
|
|
253
|
+
decodedStr = Buffer.from(base64, 'base64').toString('utf-8')
|
|
254
|
+
}
|
|
255
|
+
if (decodedStr) {
|
|
256
|
+
var payload = JSON.parse(decodedStr)
|
|
257
|
+
uid = payload.unionid || payload.userId || payload.uid || payload.sub
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
} catch (e) {}
|
|
262
|
+
}
|
|
263
|
+
|
|
236
264
|
var currentUid = tracker.getSession().getUserId()
|
|
237
265
|
if (uid && uid !== currentUid) {
|
|
238
266
|
tracker.setUser(uid)
|