From 1b4bfac63cec8419eba24378c95405185cbbd113 Mon Sep 17 00:00:00 2001 From: Xc Date: Sat, 12 Aug 2023 15:23:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86env=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8C=AA=E5=88=B0=E4=BA=86frontend=E5=BD=93?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - scripts/buildGeneralEntityDict.ts | 2 +- src/aspects/AspectDict.ts | 4 +-- src/aspects/application.ts | 2 +- src/aspects/token.ts | 3 +-- src/entities/Token.ts | 43 +------------------------------ src/entities/WechatLogin.ts | 2 +- src/features/extraFile.ts | 4 +-- src/features/index.ts | 6 +++-- src/features/token.ts | 25 +++++++++--------- src/features/weiXinJsSdk.web.ts | 10 ++++--- src/utils/env.mp.ts | 24 ----------------- src/utils/env.ts | 7 ----- src/utils/env.web.ts | 23 ----------------- 14 files changed, 31 insertions(+), 125 deletions(-) delete mode 100644 src/utils/env.mp.ts delete mode 100644 src/utils/env.ts delete mode 100644 src/utils/env.web.ts diff --git a/package.json b/package.json index 617044b35..1418317c3 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "src/entities/*" ], "dependencies": { - "@fingerprintjs/fingerprintjs": "^3.3.3", "@uiw/react-amap": "^4.0.2", "@wangeditor/editor": "^5.1.22", "@wangeditor/editor-for-react": "^1.0.4", diff --git a/scripts/buildGeneralEntityDict.ts b/scripts/buildGeneralEntityDict.ts index 67d6d448c..bf0aa7906 100644 --- a/scripts/buildGeneralEntityDict.ts +++ b/scripts/buildGeneralEntityDict.ts @@ -1,5 +1,5 @@ import { unset } from 'oak-domain/lib/utils/lodash'; -import { buildSchema, analyzeEntities } from 'oak-domain/src/compiler/schemalBuilder'; +import { buildSchema, analyzeEntities } from 'oak-domain/lib/compiler/schemalBuilder'; process.env.NODE_ENV = 'development'; process.env.COMPLING_AS_LIB = 'yes'; diff --git a/src/aspects/AspectDict.ts b/src/aspects/AspectDict.ts index 7689e1fef..1888736e9 100644 --- a/src/aspects/AspectDict.ts +++ b/src/aspects/AspectDict.ts @@ -1,11 +1,9 @@ -import { WebEnv, WechatMpEnv } from '../general-app-domain/Token/Schema'; +import { WebEnv, WechatMpEnv } from 'oak-domain/lib/types/Environment'; import { AppType } from '../general-app-domain/Application/Schema'; import { EntityDict } from '../general-app-domain'; import { QiniuUploadInfo } from 'oak-frontend-base/lib/types/Upload'; import { Config, Origin } from '../types/Config'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; -import { CommonAspectDict } from 'oak-common-aspect'; -import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext'; export type GeneralAspectDict< ED extends EntityDict, diff --git a/src/aspects/application.ts b/src/aspects/application.ts index de0613fad..15408db0d 100644 --- a/src/aspects/application.ts +++ b/src/aspects/application.ts @@ -5,7 +5,7 @@ import { BackendRuntimeContext } from "../context/BackendRuntimeContext"; import { applicationProjection } from '../types/projection'; import { WebEnv, -} from '../general-app-domain/Token/Schema'; +} from 'oak-domain/lib/types/Environment'; import { WechatPublicInstance, WechatSDK, diff --git a/src/aspects/token.ts b/src/aspects/token.ts index d0fea363c..eb99cb4b9 100644 --- a/src/aspects/token.ts +++ b/src/aspects/token.ts @@ -12,10 +12,9 @@ import { WebConfig, } from '../general-app-domain/Application/Schema'; import { - CreateOperationData as CreateToken, WebEnv, WechatMpEnv, -} from '../general-app-domain/Token/Schema'; +} from 'oak-domain/lib/types/Environment'; import { CreateOperationData as CreateWechatUser } from '../general-app-domain/WechatUser/Schema'; import { UpdateOperationData as UpdateWechatLoginData } from '../general-app-domain/WechatLogin/Schema'; import { Operation as ExtraFileOperation } from '../general-app-domain/ExtraFile/Schema'; diff --git a/src/entities/Token.ts b/src/entities/Token.ts index 6a27fdcab..cb85cacd7 100644 --- a/src/entities/Token.ts +++ b/src/entities/Token.ts @@ -5,48 +5,7 @@ import { AbleAction, AbleState, makeAbleActionDef } from 'oak-domain/lib/actions import { EntityShape } from 'oak-domain/lib/types/Entity'; import { ActionDef } from 'oak-domain/lib/types/Action'; import { EntityDesc } from 'oak-domain/lib/types/EntityDesc'; -// https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfoSync.html -export type WechatMpEnv = { - type: 'wechatMp', - brand: string; // 设备品牌 - model: string; // 设备型号 - pixelRatio: number; // 设备像素比 - screenWidth: number; // 屏幕宽度 - screenHeight: number; // 屏幕高度 - windowWidth: number; // 窗口宽度 - windowHeight: number; // 窗口高度 - statusBarHeight: number; // 状态栏高度 - language: string; // 语言 - version: string; // 微信版本号 - system: string; // 操作系统及版本 - platform: string; // 平台 - fontSizeSetting: number; // 字体大小 - SDKVersion: string; // 基础库版本 -}; - - -export type WebEnv = { - type: 'web', - visitorId: string; - platform: { - value: string; - }; - timezone: { - value: string; - }; - vendor: { - value: string; - }; - vendorFlavors: { - value: string[]; - }; -}; - -export type ServerEnv = { - type: 'server', -} - -export type Environment = WechatMpEnv | WebEnv | ServerEnv; +import { Environment } from 'oak-domain/lib/types/Environment'; export interface Schema extends EntityShape { application?: Application; diff --git a/src/entities/WechatLogin.ts b/src/entities/WechatLogin.ts index f52080a95..5e476b3ad 100644 --- a/src/entities/WechatLogin.ts +++ b/src/entities/WechatLogin.ts @@ -1,4 +1,4 @@ -import { String, Text, Datetime, Int } from 'oak-domain/lib/types/DataType'; +import { Boolean, Text, Datetime, Int } from 'oak-domain/lib/types/DataType'; import { EntityShape } from 'oak-domain/lib/types/Entity'; import { LocaleDef } from 'oak-domain/lib/types/Locale'; import { Index } from 'oak-domain/lib/types/Storage'; diff --git a/src/features/extraFile.ts b/src/features/extraFile.ts index 4134ba641..cc7e5150e 100644 --- a/src/features/extraFile.ts +++ b/src/features/extraFile.ts @@ -20,11 +20,11 @@ export class ExtraFile< > extends Feature { private cache: Cache>; private application: Application; - private locales: Locales; + private locales: Locales; constructor( cache: Cache>, application: Application, - locales: Locales, + locales: Locales, ) { super(); this.cache = cache; diff --git a/src/features/index.ts b/src/features/index.ts index 8f436708f..070643958 100644 --- a/src/features/index.ts +++ b/src/features/index.ts @@ -31,7 +31,8 @@ export function initialize< ); const token = new Token( basicFeatures.cache, - basicFeatures.localStorage + basicFeatures.localStorage, + basicFeatures.environment ); // 临时代码,合并后再删 @@ -39,7 +40,8 @@ export function initialize< const config = new Config(basicFeatures.cache); const weiXinJsSdk = new WeiXinJsSdk( basicFeatures.cache, - basicFeatures.localStorage + basicFeatures.localStorage, + basicFeatures.environment ); const theme = new Theme(basicFeatures.cache, basicFeatures.localStorage); diff --git a/src/features/token.ts b/src/features/token.ts index 06750b512..40aa24e86 100644 --- a/src/features/token.ts +++ b/src/features/token.ts @@ -6,14 +6,13 @@ import { } from 'oak-domain/lib/types/Exception'; import { Cache } from 'oak-frontend-base/lib/features/cache'; import { LocalStorage } from 'oak-frontend-base/lib/features/localStorage'; +import { Environment } from 'oak-frontend-base/lib/features/environment'; import { CommonAspectDict } from 'oak-common-aspect'; -import { WebEnv, WechatMpEnv } from '../general-app-domain/Token/Schema'; +import { WebEnv, WechatMpEnv } from 'oak-domain/lib/types/Environment'; import { EntityDict } from '../general-app-domain'; -import { getEnv } from '../utils/env'; import AspectDict from '../aspects/AspectDict'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext'; -import { ROOT_ROLE_ID } from '../constants'; import { tokenProjection } from '../types/projection'; import { OakUserInfoLoadingException } from '../types/Exception'; @@ -24,14 +23,16 @@ export class Token< AD extends AspectDict & CommonAspectDict > extends Feature { private tokenValue?: string; + private environment: Environment; private cache: Cache>; private storage: LocalStorage; private isLoading = false; - constructor(cache: Cache, storage: LocalStorage) { + constructor(cache: Cache, storage: LocalStorage, environment: Environment) { super(); this.cache = cache; this.storage = storage; + this.environment = environment; const tokenValue = storage.load('token:token'); if (tokenValue) { this.tokenValue = tokenValue; @@ -54,7 +55,7 @@ export class Token< } async loginByMobile(mobile: string, password?: string, captcha?: string) { - const env = await getEnv(); + const env = await this.environment.getEnv(); const { result } = await this.cache.exec('loginByMobile', { password, mobile, @@ -67,9 +68,9 @@ export class Token< } async loginByWechatInWebEnv(wechatLoginId: string) { - const env = await getEnv(); + const env = await this.environment.getEnv(); const { result } = await this.cache.exec('loginByWechat', { - env, + env: env as WebEnv, wechatLoginId, }); this.tokenValue = result; @@ -78,7 +79,7 @@ export class Token< } async loginWechat(code: string, params?: { wechatLoginId?: string }) { - const env = await getEnv(); + const env = await this.environment.getEnv(); const { result } = await this.cache.exec('loginWechat', { code, env: env as WebEnv, @@ -92,7 +93,7 @@ export class Token< async loginWechatMp() { const { code } = await wx.login(); - const env = await getEnv(); + const env = await this.environment.getEnv(); const { result } = await this.cache.exec('loginWechatMp', { code, env: env as WechatMpEnv, @@ -196,7 +197,7 @@ export class Token< } async sendCaptcha(mobile: string) { - const env = await getEnv(); + const env = await this.environment.getEnv(); const { result } = await this.cache.exec('sendCaptcha', { mobile, env: env as WebEnv, @@ -227,7 +228,7 @@ export class Token< } async getWechatMpUserPhoneNumber(code: string) { - const env = await getEnv(); + const env = await this.environment.getEnv(); await this.cache.exec('getWechatMpUserPhoneNumber', { code, env: env as WechatMpEnv, @@ -236,7 +237,7 @@ export class Token< } async wakeupParasite(id: string) { - const env = await getEnv(); + const env = await this.environment.getEnv(); const { result } = await this.cache.exec('wakeupParasite', { id, env: env as WechatMpEnv, diff --git a/src/features/weiXinJsSdk.web.ts b/src/features/weiXinJsSdk.web.ts index 1306405d3..c50cdfab4 100644 --- a/src/features/weiXinJsSdk.web.ts +++ b/src/features/weiXinJsSdk.web.ts @@ -9,12 +9,12 @@ import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext'; import { Cache } from 'oak-frontend-base/lib/features/cache'; import { LocalStorage } from 'oak-frontend-base/lib/features/localStorage'; -import { getEnv } from '../utils/env'; -import { WebEnv } from '../general-app-domain/Token/Schema'; +import { WebEnv } from 'oak-domain/lib/types/Environment'; import { uniq } from 'oak-domain/lib/utils/lodash'; // const weixin = require('weixin-js-sdk'); import weixin from 'weixin-js-sdk'; +import { Environment } from 'oak-frontend-base/lib/features/environment'; type Options = | WeixinJsSdk.CheckJsApiOptions @@ -50,17 +50,19 @@ export class WeiXinJsSdk< > extends Feature { private cache: Cache>; private storage: LocalStorage; + private environment: Environment; private landingUrl?: string; //解决在IOS上,无论路由切换到哪个页面,实际真正有效的的签名URL是【第一次进入应用时的URL】; - constructor(cache: Cache, storage: LocalStorage) { + constructor(cache: Cache, storage: LocalStorage, environment: Environment) { super(); this.cache = cache; this.storage = storage; this.landingUrl = undefined; + this.environment = environment; } async signatureJsSDK(url: string) { - const env = await getEnv(); + const env = await this.environment.getEnv(); const { result } = await this.cache.exec('signatureJsSDK', { url, env: env as WebEnv, diff --git a/src/utils/env.mp.ts b/src/utils/env.mp.ts deleted file mode 100644 index 0c5c1b699..000000000 --- a/src/utils/env.mp.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { pick } from "oak-domain/lib/utils/lodash"; - -export async function getEnv() { - const env = await wx.getSystemInfo(); - const env2 = pick(env, [ - 'brand', - 'model', - 'pixelRatio', - 'screenWidth', - 'screenHeight', - 'windowWidth', - 'windowHeight', - 'statusBarHeight', - 'language', - 'version', - 'system', - 'platform', - 'fontSizeSetting', - 'SDKVersion' - ]); - return Object.assign(env2, { - type: 'wechatMp', - }) as any; -} \ No newline at end of file diff --git a/src/utils/env.ts b/src/utils/env.ts deleted file mode 100644 index b38a7858c..000000000 --- a/src/utils/env.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { getEnv } from './env.web'; - -export { - getEnv, -}; - -console.log('不应该走到这里[utils/env]'); diff --git a/src/utils/env.web.ts b/src/utils/env.web.ts deleted file mode 100644 index a6781c22d..000000000 --- a/src/utils/env.web.ts +++ /dev/null @@ -1,23 +0,0 @@ -import FingerprintJS from '@fingerprintjs/fingerprintjs'; -import { pick } from 'oak-domain/lib/utils/lodash'; - -/** - * fingerprintJs当中的一些敏感项 - * @returns - */ -export async function getEnv() { - const fp = await FingerprintJS.load(); - const result = await fp.get(); - - const { visitorId, components } = result; - return Object.assign( - pick(components, [ - 'platform', - 'timezone', - 'vendor', - 'vendorFlavors' - ]), { - type: 'web', - visitorId, - }) as any; -}