diff --git a/miniapp/utils/api.js b/miniapp/utils/api.js index 61c15ff..d2f1a2d 100644 --- a/miniapp/utils/api.js +++ b/miniapp/utils/api.js @@ -1,5 +1,5 @@ const SESSION_KEY = 'tkmind_session'; -const { createUuid } = require('./uuid'); +const { createUuid } = require('./config'); let apiBaseUrl = ''; let cookie = ''; diff --git a/miniapp/utils/config.js b/miniapp/utils/config.js index 1014d2f..7200644 100644 --- a/miniapp/utils/config.js +++ b/miniapp/utils/config.js @@ -2,6 +2,17 @@ // Local Portal debug: set LOCAL_DEV = true, or copy config.local.example.js → config.local.js const LOCAL_DEV = false; +function createUuid() { + const bytes = []; + for (let i = 0; i < 16; i += 1) { + bytes.push(Math.floor(Math.random() * 256)); + } + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + const hex = bytes.map((byte) => byte.toString(16).padStart(2, '0')).join(''); + return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; +} + const defaults = { API_BASE_URL: LOCAL_DEV ? 'http://127.0.0.1:8081' : 'https://m.tkmind.cn', MINIAPP_LOGIN_PATH: '/auth/wechat-miniapp/login', @@ -18,6 +29,7 @@ try { } module.exports = { + createUuid, ...defaults, ...localOverrides, ...(localOverrides.API_BASE_URL diff --git a/miniapp/utils/messages.js b/miniapp/utils/messages.js index b5b0625..107848e 100644 --- a/miniapp/utils/messages.js +++ b/miniapp/utils/messages.js @@ -1,4 +1,4 @@ -const { createUuid } = require('./uuid'); +const { createUuid } = require('./config'); const { buildMessageView } = require('./message-display'); function createUserMessage(text) { diff --git a/miniapp/utils/uuid.js b/miniapp/utils/uuid.js deleted file mode 100644 index 1c0b4b4..0000000 --- a/miniapp/utils/uuid.js +++ /dev/null @@ -1,21 +0,0 @@ -function createUuid() { - if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { - return crypto.randomUUID(); - } - const bytes = new Uint8Array(16); - if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') { - crypto.getRandomValues(bytes); - } else { - for (let i = 0; i < 16; i += 1) { - bytes[i] = Math.floor(Math.random() * 256); - } - } - bytes[6] = (bytes[6] & 0x0f) | 0x40; - bytes[8] = (bytes[8] & 0x3f) | 0x80; - const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join(''); - return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; -} - -module.exports = { - createUuid, -};