Skip to content

Commit

Permalink
feat: skland generate device id
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuk1ko committed Sep 28, 2024
1 parent a1f6318 commit 841598f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'mdui/dist/css/mdui.css';
import './utils/polyfills';
import './utils/migration';
import './registerServiceWorker';
import _ from 'lodash';
Expand Down
17 changes: 17 additions & 0 deletions src/utils/polyfills.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
interface PromiseWithResolvers<T> {
promise: Promise<T>;
resolve: (value: T | PromiseLike<T>) => 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<T>();
* ```
*/
withResolvers<T>(): PromiseWithResolvers<T>;
}
10 changes: 10 additions & 0 deletions src/utils/polyfills.js
Original file line number Diff line number Diff line change
@@ -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 };
};
}
28 changes: 27 additions & 1 deletion src/utils/skland.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// 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('');
}

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 = {
Expand Down Expand Up @@ -96,6 +100,7 @@ export async function sklandOAuthLogin(token) {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Did: await getDeviceId(),
},
body: JSON.stringify({ token }),
});
Expand All @@ -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();
});

0 comments on commit 841598f

Please sign in to comment.