From 841598fd5f4b9d8f306f2601d633ca51b03677bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E4=BB=A3=E7=B6=BA=E5=87=9B?= Date: Sat, 28 Sep 2024 20:18:18 +0800 Subject: [PATCH] feat: skland generate device id --- src/main.js | 1 + src/utils/polyfills.d.ts | 17 +++++++++++++++++ src/utils/polyfills.js | 10 ++++++++++ src/utils/skland.js | 28 +++++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/utils/polyfills.d.ts create mode 100644 src/utils/polyfills.js diff --git a/src/main.js b/src/main.js index d9ac6610..3bfca382 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,5 @@ import 'mdui/dist/css/mdui.css'; +import './utils/polyfills'; import './utils/migration'; import './registerServiceWorker'; import _ from 'lodash'; diff --git a/src/utils/polyfills.d.ts b/src/utils/polyfills.d.ts new file mode 100644 index 00000000..1bfe8a6b --- /dev/null +++ b/src/utils/polyfills.d.ts @@ -0,0 +1,17 @@ +interface PromiseWithResolvers { + promise: Promise; + resolve: (value: T | PromiseLike) => void; + reject: (reason?: any) => void; +} + +interface PromiseConstructor { + /** + * Creates a new Promise and returns it in an object, along with its resolve and reject functions. + * @returns An object with the properties `promise`, `resolve`, and `reject`. + * + * ```ts + * const { promise, resolve, reject } = Promise.withResolvers(); + * ``` + */ + withResolvers(): PromiseWithResolvers; +} diff --git a/src/utils/polyfills.js b/src/utils/polyfills.js new file mode 100644 index 00000000..95bdd750 --- /dev/null +++ b/src/utils/polyfills.js @@ -0,0 +1,10 @@ +if (!Promise.withResolvers) { + Promise.withResolvers = () => { + let resolve, reject; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return { promise, resolve, reject }; + }; +} diff --git a/src/utils/skland.js b/src/utils/skland.js index 2e2f6743..e3c36758 100644 --- a/src/utils/skland.js +++ b/src/utils/skland.js @@ -1,7 +1,11 @@ // https://github.com/oott123/sklanding/blob/master/src/utils/sign.ts import md5 from 'js-md5'; +import { once } from 'lodash'; +import { v4 as uuid } from 'uuid'; import { PROXY_SERVER } from './env'; +const apiDid = uuid().toUpperCase(); + function buf2hex(buffer) { return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join(''); } @@ -9,7 +13,7 @@ function buf2hex(buffer) { async function sign(path, token) { const timestamp = `${Math.floor(Date.now() / 1000)}`; const platform = '3'; - const dId = navigator.userAgent; + const dId = apiDid; const vName = '1.0.0'; const headers = { @@ -96,6 +100,7 @@ export async function sklandOAuthLogin(token) { method: 'POST', headers: { 'Content-Type': 'application/json', + Did: await getDeviceId(), }, body: JSON.stringify({ token }), }); @@ -115,3 +120,24 @@ export async function sklandOAuthLogin(token) { export function isNotLoginError(err) { return err.code === 10002; } + +const loadSmSdk = once(() => { + const { promise, resolve, reject } = Promise.withResolvers(); + window._smReadyFuncs = [resolve]; + window._smConf = { + organization: 'UWXspnCCJN4sfYlNfqps', + appId: 'default', + publicKey: + 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmxMNr7n8ZeT0tE1R9j/mPixoinPkeM+k4VGIn/s0k7N5rJAfnZ0eMER+QhwFvshzo0LNmeUkpR8uIlU/GEVr8mN28sKmwd2gpygqj0ePnBmOW4v0ZVwbSYK+izkhVFk2V/doLoMbWy6b+UnA8mkjvg0iYWRByfRsK2gdl7llqCwIDAQAB', + protocol: 'https', + }; + import(/* webpackIgnore: true */ 'https://static.portal101.cn/dist/web/v3.0.0/fp.min.js').catch( + reject, + ); + return promise; +}); + +const getDeviceId = once(async () => { + await loadSmSdk(); + return window.SMSdk.getDeviceId(); +});