application initialize的时候, 1、不缓存 2、请求getApplication报token过期 先清除token再请求一次

This commit is contained in:
wkj 2024-01-12 11:52:44 +08:00
parent 6ff389b0c8
commit 48ca2b86c0
22 changed files with 214 additions and 128 deletions

View File

@ -20,7 +20,7 @@ export type GeneralAspectDict<ED extends EntityDict, Cxt extends BackendRuntimeC
captcha?: string;
password?: string;
mobile: string;
disableRegist?: boolean;
disableRegister?: boolean;
env: WebEnv | WechatMpEnv | NativeEnv;
}, context: Cxt) => Promise<string>;
loginWechat: ({ code, env, wechatLoginId, }: {

View File

@ -5,7 +5,7 @@ export declare function loginByMobile<ED extends EntityDict, Cxt extends Backend
captcha?: string;
password?: string;
mobile: string;
disableRegist?: boolean;
disableRegister?: boolean;
env: WebEnv | WechatMpEnv | NativeEnv;
}, context: Cxt): Promise<string>;
export declare function refreshWechatPublicUserInfo<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>({}: {}, context: Cxt): Promise<void>;

View File

@ -415,8 +415,8 @@ async function loadTokenInfo(tokenId, context) {
}, {});
}
export async function loginByMobile(params, context) {
const { mobile, captcha, password, env, disableRegister } = params;
const loginLogic = async () => {
const { mobile, captcha, password, env } = params;
const systemId = context.getSystemId();
if (captcha) {
const result = await context.select('captcha', {
@ -500,14 +500,14 @@ export async function loginByMobile(params, context) {
}
};
const closeRootMode = context.openRootMode();
if (params?.disableRegist) {
if (disableRegister) {
const [existMobile] = await context.select('mobile', {
data: {
id: 1,
mobile: 1,
},
filter: {
mobile: params.mobile,
mobile: mobile,
ableState: 'enabled',
},
}, { dontCollect: true });

View File

@ -8,6 +8,7 @@ import AspectDict from '../aspects/AspectDict';
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext';
import { MediaType, MediaVideoDescription } from '../types/WeChat';
import { Token } from './token';
export declare class Application<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>, FrontCxt extends FrontendRuntimeContext<ED, Cxt, AD>, AD extends AspectDict<ED, Cxt> & CommonAspectDict<ED, Cxt>> extends Feature {
private type;
private domain;
@ -16,7 +17,8 @@ export declare class Application<ED extends EntityDict, Cxt extends BackendRunti
private cache;
private storage;
private projection;
constructor(type: AppType, domain: string, cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, storage: LocalStorage);
private token;
constructor(type: AppType, domain: string, cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, storage: LocalStorage, token: Token<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>);
private refresh;
private getApplicationFromCache;
private loadApplicationInfo;

View File

@ -3,6 +3,7 @@ import { Feature } from 'oak-frontend-base';
import { assert } from 'oak-domain/lib/utils/assert';
import { cloneDeep, merge } from 'oak-domain/lib/utils/lodash';
import { applicationProjection } from '../types/Projection';
import { OakTokenExpiredException, OakUserDisabledException, } from '../types/Exception';
export class Application extends Feature {
type;
domain; //域名
@ -11,13 +12,15 @@ export class Application extends Feature {
cache;
storage;
projection;
constructor(type, domain, cache, storage) {
token;
constructor(type, domain, cache, storage, token) {
super();
this.cache = cache;
this.storage = storage;
this.type = type;
this.domain = domain;
this.projection = cloneDeep(applicationProjection);
this.token = token;
}
async refresh() {
const { data } = await this.cache.refresh('application', {
@ -43,33 +46,53 @@ export class Application extends Feature {
this.application = data[0];
}
async loadApplicationInfo(type, domain) {
const { result: applicationId } = await this.cache.exec('getApplication', {
type,
domain,
});
let applicationId;
try {
const { result } = await this.cache.exec('getApplication', {
type,
domain,
});
applicationId = result;
}
catch (err) {
if (err instanceof OakTokenExpiredException ||
err instanceof OakUserDisabledException) {
// 出现上面的异常先清除本地token, 重新发起一次请求
this.token.removeToken(true);
const { result } = await this.cache.exec('getApplication', {
type,
domain,
});
applicationId = result;
}
throw err;
}
this.applicationId = applicationId;
this.getApplicationFromCache();
// 如果取得的type和当前环境不同则不缓存id(未来可能有type相同的appliction上线)
if (this.application?.type === type) {
this.storage.save(LOCAL_STORAGE_KEYS.appId, applicationId);
}
// 如果取得的type和当前环境不同则不缓存id(未来可能有type相同的application上线)
// if (this.application?.type === type) {
// this.storage.save(LOCAL_STORAGE_KEYS.appId, applicationId);
// }
this.publish();
}
async initialize(appId, projection) {
const applicationId = await this.storage.load(LOCAL_STORAGE_KEYS.appId);
this.applicationId = applicationId;
// const applicationId = await this.storage.load(LOCAL_STORAGE_KEYS.appId);
// this.applicationId = applicationId;
//接收外层注入的projection
this.projection = merge(this.projection, projection);
if (process.env.NODE_ENV === 'development' && appId) {
// development环境下允许注入一个线上的appId
this.applicationId = appId;
if (this.applicationId) {
return await this.refresh();
}
}
if (this.applicationId) {
await this.refresh();
}
else {
await this.loadApplicationInfo(this.type, this.domain);
}
// if (this.applicationId) {
// await this.refresh();
// } else {
// await this.loadApplicationInfo(this.type, this.domain);
// }
return await this.loadApplicationInfo(this.type, this.domain);
}
getApplication() {
return this.application;

View File

@ -9,8 +9,8 @@ import { WechatPublicTag } from './wechatPublicTag';
import { UserWechatPublicTag } from './userWechatPublicTag';
import Theme from './theme';
export function initialize(basicFeatures, type, domain) {
const application = new Application(type, domain, basicFeatures.cache, basicFeatures.localStorage);
const token = new Token(basicFeatures.cache, basicFeatures.localStorage, basicFeatures.environment);
const application = new Application(type, domain, basicFeatures.cache, basicFeatures.localStorage, token);
const wechatMenu = new WechatMenu(basicFeatures.cache, basicFeatures.localStorage);
const wechatPublicTag = new WechatPublicTag(basicFeatures.cache, basicFeatures.localStorage);
const userWechatPublicTag = new UserWechatPublicTag(basicFeatures.cache, basicFeatures.localStorage);

View File

@ -16,7 +16,7 @@ export declare class Token<ED extends EntityDict, Cxt extends BackendRuntimeCont
private loadSavedToken;
constructor(cache: Cache<ED, Cxt, FrontCxt, AD>, storage: LocalStorage, environment: Environment);
loadTokenInfo(): Promise<void>;
loginByMobile(mobile: string, password?: string, captcha?: string, disableRegist?: boolean): Promise<void>;
loginByMobile(mobile: string, password?: string, captcha?: string, disableRegister?: boolean): Promise<void>;
loginByWechatInWebEnv(wechatLoginId: string): Promise<void>;
loginWechat(code: string, params?: {
wechatLoginId?: string;
@ -24,7 +24,7 @@ export declare class Token<ED extends EntityDict, Cxt extends BackendRuntimeCont
loginWechatMp(): Promise<void>;
syncUserInfoWechatMp(): Promise<void>;
logout(): Promise<void>;
removeToken(): void;
removeToken(disablePublish?: boolean): void;
getTokenValue(): string | undefined;
getToken(allowUnloggedIn?: boolean): Partial<ED["token"]["Schema"]> | undefined;
getUserId(allowUnloggedIn?: boolean): NonNullable<ED["token"]["Schema"]["userId"]> | undefined;

View File

@ -22,7 +22,6 @@ export class Token extends Feature {
}
if (tokenValue) {
this.tokenValue = tokenValue;
// this.loadTokenInfo();
}
else {
this.tokenValue = undefined;
@ -50,15 +49,15 @@ export class Token extends Feature {
this.isLoading = false;
}
}
async loginByMobile(mobile, password, captcha, disableRegist) {
async loginByMobile(mobile, password, captcha, disableRegister) {
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('loginByMobile', {
password,
mobile,
captcha,
disableRegist,
disableRegister,
env,
});
}, undefined, true);
this.tokenValue = result;
await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
this.publish();
@ -113,10 +112,12 @@ export class Token extends Feature {
await this.cache.exec('logout', {});
this.removeToken();
}
removeToken() {
removeToken(disablePublish) {
this.tokenValue = undefined;
this.storage.remove(LOCAL_STORAGE_KEYS.token);
this.publish();
if (!disablePublish) {
this.publish();
}
}
getTokenValue() {
return this.tokenValue;
@ -192,7 +193,6 @@ export class Token extends Feature {
}
async refreshWechatPublicUserInfo() {
await this.cache.exec('refreshWechatPublicUserInfo', {});
this.publish();
}
async getWechatMpUserPhoneNumber(code) {
const env = await this.environment.getEnv();
@ -200,7 +200,6 @@ export class Token extends Feature {
code,
env: env,
});
// this.publish();
}
async wakeupParasite(id) {
const env = await this.environment.getEnv();

View File

@ -1,2 +1,2 @@
declare const _default: (import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "account", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatMpJump", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatPublicTag", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatMenu", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "sessionMessage", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "extraFile", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "parasite", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "article", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "articleMenu", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "application", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatLogin", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "notification", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatQrCode", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "userEntityGrant", import("..").BRC> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "user", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "address", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "message", import("..").BRC>)[];
declare const _default: (import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "account", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "application", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "address", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "user", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "userEntityGrant", import("..").BRC> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatQrCode", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "message", import("..").BRC> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "notification", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatLogin", import("..").RuntimeCxt> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "articleMenu", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "article", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "parasite", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "extraFile", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "sessionMessage", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatMenu", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatPublicTag", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>> | import("oak-domain").Trigger<import("../oak-app-domain").EntityDict, "wechatMpJump", import("..").BackendRuntimeContext<import("../oak-app-domain").EntityDict>>)[];
export default _default;

View File

@ -20,7 +20,7 @@ export type GeneralAspectDict<ED extends EntityDict, Cxt extends BackendRuntimeC
captcha?: string;
password?: string;
mobile: string;
disableRegist?: boolean;
disableRegister?: boolean;
env: WebEnv | WechatMpEnv | NativeEnv;
}, context: Cxt) => Promise<string>;
loginWechat: ({ code, env, wechatLoginId, }: {

View File

@ -5,7 +5,7 @@ export declare function loginByMobile<ED extends EntityDict, Cxt extends Backend
captcha?: string;
password?: string;
mobile: string;
disableRegist?: boolean;
disableRegister?: boolean;
env: WebEnv | WechatMpEnv | NativeEnv;
}, context: Cxt): Promise<string>;
export declare function refreshWechatPublicUserInfo<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>({}: {}, context: Cxt): Promise<void>;

View File

@ -418,8 +418,8 @@ async function loadTokenInfo(tokenId, context) {
}, {});
}
async function loginByMobile(params, context) {
const { mobile, captcha, password, env, disableRegister } = params;
const loginLogic = async () => {
const { mobile, captcha, password, env } = params;
const systemId = context.getSystemId();
if (captcha) {
const result = await context.select('captcha', {
@ -503,14 +503,14 @@ async function loginByMobile(params, context) {
}
};
const closeRootMode = context.openRootMode();
if (params?.disableRegist) {
if (disableRegister) {
const [existMobile] = await context.select('mobile', {
data: {
id: 1,
mobile: 1,
},
filter: {
mobile: params.mobile,
mobile: mobile,
ableState: 'enabled',
},
}, { dontCollect: true });

View File

@ -8,6 +8,7 @@ import AspectDict from '../aspects/AspectDict';
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext';
import { MediaType, MediaVideoDescription } from '../types/WeChat';
import { Token } from './token';
export declare class Application<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>, FrontCxt extends FrontendRuntimeContext<ED, Cxt, AD>, AD extends AspectDict<ED, Cxt> & CommonAspectDict<ED, Cxt>> extends Feature {
private type;
private domain;
@ -16,7 +17,8 @@ export declare class Application<ED extends EntityDict, Cxt extends BackendRunti
private cache;
private storage;
private projection;
constructor(type: AppType, domain: string, cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, storage: LocalStorage);
private token;
constructor(type: AppType, domain: string, cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, storage: LocalStorage, token: Token<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>);
private refresh;
private getApplicationFromCache;
private loadApplicationInfo;

View File

@ -6,6 +6,7 @@ const oak_frontend_base_1 = require("oak-frontend-base");
const assert_1 = require("oak-domain/lib/utils/assert");
const lodash_1 = require("oak-domain/lib/utils/lodash");
const Projection_1 = require("../types/Projection");
const Exception_1 = require("../types/Exception");
class Application extends oak_frontend_base_1.Feature {
type;
domain; //域名
@ -14,13 +15,15 @@ class Application extends oak_frontend_base_1.Feature {
cache;
storage;
projection;
constructor(type, domain, cache, storage) {
token;
constructor(type, domain, cache, storage, token) {
super();
this.cache = cache;
this.storage = storage;
this.type = type;
this.domain = domain;
this.projection = (0, lodash_1.cloneDeep)(Projection_1.applicationProjection);
this.token = token;
}
async refresh() {
const { data } = await this.cache.refresh('application', {
@ -46,33 +49,53 @@ class Application extends oak_frontend_base_1.Feature {
this.application = data[0];
}
async loadApplicationInfo(type, domain) {
const { result: applicationId } = await this.cache.exec('getApplication', {
type,
domain,
});
let applicationId;
try {
const { result } = await this.cache.exec('getApplication', {
type,
domain,
});
applicationId = result;
}
catch (err) {
if (err instanceof Exception_1.OakTokenExpiredException ||
err instanceof Exception_1.OakUserDisabledException) {
// 出现上面的异常先清除本地token, 重新发起一次请求
this.token.removeToken(true);
const { result } = await this.cache.exec('getApplication', {
type,
domain,
});
applicationId = result;
}
throw err;
}
this.applicationId = applicationId;
this.getApplicationFromCache();
// 如果取得的type和当前环境不同则不缓存id(未来可能有type相同的appliction上线)
if (this.application?.type === type) {
this.storage.save(constants_1.LOCAL_STORAGE_KEYS.appId, applicationId);
}
// 如果取得的type和当前环境不同则不缓存id(未来可能有type相同的application上线)
// if (this.application?.type === type) {
// this.storage.save(constants_1.LOCAL_STORAGE_KEYS.appId, applicationId);
// }
this.publish();
}
async initialize(appId, projection) {
const applicationId = await this.storage.load(constants_1.LOCAL_STORAGE_KEYS.appId);
this.applicationId = applicationId;
// const applicationId = await this.storage.load(LOCAL_STORAGE_KEYS.appId);
// this.applicationId = applicationId;
//接收外层注入的projection
this.projection = (0, lodash_1.merge)(this.projection, projection);
if (process.env.NODE_ENV === 'development' && appId) {
// development环境下允许注入一个线上的appId
this.applicationId = appId;
if (this.applicationId) {
return await this.refresh();
}
}
if (this.applicationId) {
await this.refresh();
}
else {
await this.loadApplicationInfo(this.type, this.domain);
}
// if (this.applicationId) {
// await this.refresh();
// } else {
// await this.loadApplicationInfo(this.type, this.domain);
// }
return await this.loadApplicationInfo(this.type, this.domain);
}
getApplication() {
return this.application;

View File

@ -13,8 +13,8 @@ const wechatPublicTag_1 = require("./wechatPublicTag");
const userWechatPublicTag_1 = require("./userWechatPublicTag");
const theme_1 = tslib_1.__importDefault(require("./theme"));
function initialize(basicFeatures, type, domain) {
const application = new application_1.Application(type, domain, basicFeatures.cache, basicFeatures.localStorage);
const token = new token_1.Token(basicFeatures.cache, basicFeatures.localStorage, basicFeatures.environment);
const application = new application_1.Application(type, domain, basicFeatures.cache, basicFeatures.localStorage, token);
const wechatMenu = new wechatMenu_1.WechatMenu(basicFeatures.cache, basicFeatures.localStorage);
const wechatPublicTag = new wechatPublicTag_1.WechatPublicTag(basicFeatures.cache, basicFeatures.localStorage);
const userWechatPublicTag = new userWechatPublicTag_1.UserWechatPublicTag(basicFeatures.cache, basicFeatures.localStorage);

View File

@ -16,7 +16,7 @@ export declare class Token<ED extends EntityDict, Cxt extends BackendRuntimeCont
private loadSavedToken;
constructor(cache: Cache<ED, Cxt, FrontCxt, AD>, storage: LocalStorage, environment: Environment);
loadTokenInfo(): Promise<void>;
loginByMobile(mobile: string, password?: string, captcha?: string, disableRegist?: boolean): Promise<void>;
loginByMobile(mobile: string, password?: string, captcha?: string, disableRegister?: boolean): Promise<void>;
loginByWechatInWebEnv(wechatLoginId: string): Promise<void>;
loginWechat(code: string, params?: {
wechatLoginId?: string;
@ -24,7 +24,7 @@ export declare class Token<ED extends EntityDict, Cxt extends BackendRuntimeCont
loginWechatMp(): Promise<void>;
syncUserInfoWechatMp(): Promise<void>;
logout(): Promise<void>;
removeToken(): void;
removeToken(disablePublish?: boolean): void;
getTokenValue(): string | undefined;
getToken(allowUnloggedIn?: boolean): Partial<ED["token"]["Schema"]> | undefined;
getUserId(allowUnloggedIn?: boolean): NonNullable<ED["token"]["Schema"]["userId"]> | undefined;

View File

@ -25,7 +25,6 @@ class Token extends oak_frontend_base_1.Feature {
}
if (tokenValue) {
this.tokenValue = tokenValue;
// this.loadTokenInfo();
}
else {
this.tokenValue = undefined;
@ -53,15 +52,15 @@ class Token extends oak_frontend_base_1.Feature {
this.isLoading = false;
}
}
async loginByMobile(mobile, password, captcha, disableRegist) {
async loginByMobile(mobile, password, captcha, disableRegister) {
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('loginByMobile', {
password,
mobile,
captcha,
disableRegist,
disableRegister,
env,
});
}, undefined, true);
this.tokenValue = result;
await this.storage.save(constants_1.LOCAL_STORAGE_KEYS.token, result);
this.publish();
@ -116,10 +115,12 @@ class Token extends oak_frontend_base_1.Feature {
await this.cache.exec('logout', {});
this.removeToken();
}
removeToken() {
removeToken(disablePublish) {
this.tokenValue = undefined;
this.storage.remove(constants_1.LOCAL_STORAGE_KEYS.token);
this.publish();
if (!disablePublish) {
this.publish();
}
}
getTokenValue() {
return this.tokenValue;
@ -195,7 +196,6 @@ class Token extends oak_frontend_base_1.Feature {
}
async refreshWechatPublicUserInfo() {
await this.cache.exec('refreshWechatPublicUserInfo', {});
this.publish();
}
async getWechatMpUserPhoneNumber(code) {
const env = await this.environment.getEnv();
@ -203,7 +203,6 @@ class Token extends oak_frontend_base_1.Feature {
code,
env: env,
});
// this.publish();
}
async wakeupParasite(id) {
const env = await this.environment.getEnv();

View File

@ -26,7 +26,7 @@ export type GeneralAspectDict<
captcha?: string;
password?: string;
mobile: string;
disableRegist?: boolean;
disableRegister?: boolean;
env: WebEnv | WechatMpEnv | NativeEnv;
},
context: Cxt
@ -43,7 +43,7 @@ export type GeneralAspectDict<
},
context: Cxt
) => Promise<string>;
logout: ({ }, context: Cxt) => Promise<void>;
logout: ({}, context: Cxt) => Promise<void>;
loginWechatMp: (
{
code,
@ -308,10 +308,7 @@ export type GeneralAspectDict<
},
context: Cxt
) => Promise<any>;
getMessageType: (
params: {},
content: Cxt
) => Promise<string[]>;
getMessageType: (params: {}, content: Cxt) => Promise<string[]>;
syncTag: (
params: {
applicationId: string;

View File

@ -595,13 +595,14 @@ export async function loginByMobile<
captcha?: string;
password?: string;
mobile: string;
disableRegist?: boolean;
disableRegister?: boolean;
env: WebEnv | WechatMpEnv | NativeEnv;
},
context: Cxt
): Promise<string> {
const { mobile, captcha, password, env, disableRegister } = params;
const loginLogic = async () => {
const { mobile, captcha, password, env } = params;
const systemId = context.getSystemId();
if (captcha) {
const result = await context.select(
@ -697,7 +698,7 @@ export async function loginByMobile<
}
};
const closeRootMode = context.openRootMode();
if (params?.disableRegist) {
if (disableRegister) {
const [existMobile] = await context.select(
'mobile',
{
@ -706,7 +707,7 @@ export async function loginByMobile<
mobile: 1,
},
filter: {
mobile: params.mobile!,
mobile: mobile!,
ableState: 'enabled',
},
},

View File

@ -13,7 +13,12 @@ import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import { FrontendRuntimeContext } from '../context/FrontendRuntimeContext';
import { applicationProjection } from '../types/Projection';
import { MediaType, MediaVideoDescription } from '../types/WeChat'
import { MediaType, MediaVideoDescription } from '../types/WeChat';
import {
OakTokenExpiredException,
OakUserDisabledException,
} from '../types/Exception';
import { Token } from './token';
export class Application<
ED extends EntityDict,
@ -28,12 +33,14 @@ export class Application<
private cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>;
private storage: LocalStorage;
private projection: EntityDict['application']['Selection']['data'];
private token: Token<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>;
constructor(
type: AppType,
domain: string,
cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>,
storage: LocalStorage
storage: LocalStorage,
token: Token<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>
) {
super();
this.cache = cache;
@ -41,6 +48,7 @@ export class Application<
this.type = type;
this.domain = domain;
this.projection = cloneDeep(applicationProjection);
this.token = token;
}
private async refresh() {
@ -75,20 +83,35 @@ export class Application<
}
private async loadApplicationInfo(type: AppType, domain: string) {
const { result: applicationId } = await this.cache.exec(
'getApplication',
{
let applicationId;
try {
const { result } = await this.cache.exec('getApplication', {
type,
domain,
});
applicationId = result;
} catch (err) {
if (
err instanceof OakTokenExpiredException ||
err instanceof OakUserDisabledException
) {
// 出现上面的异常先清除本地token, 重新发起一次请求
this.token.removeToken(true);
const { result } = await this.cache.exec('getApplication', {
type,
domain,
});
applicationId = result;
}
);
throw err;
}
this.applicationId = applicationId;
this.getApplicationFromCache();
// 如果取得的type和当前环境不同则不缓存id(未来可能有type相同的appliction上线)
if (this.application?.type === type) {
this.storage.save(LOCAL_STORAGE_KEYS.appId, applicationId);
}
// 如果取得的type和当前环境不同则不缓存id(未来可能有type相同的application上线)
// if (this.application?.type === type) {
// this.storage.save(LOCAL_STORAGE_KEYS.appId, applicationId);
// }
this.publish();
}
@ -96,19 +119,23 @@ export class Application<
appId?: string | null,
projection?: EntityDict['application']['Selection']['data']
) {
const applicationId = await this.storage.load(LOCAL_STORAGE_KEYS.appId);
this.applicationId = applicationId;
// const applicationId = await this.storage.load(LOCAL_STORAGE_KEYS.appId);
// this.applicationId = applicationId;
//接收外层注入的projection
this.projection = merge(this.projection, projection);
if (process.env.NODE_ENV === 'development' && appId) {
// development环境下允许注入一个线上的appId
this.applicationId = appId;
if (this.applicationId) {
return await this.refresh();
}
}
if (this.applicationId) {
await this.refresh();
} else {
await this.loadApplicationInfo(this.type, this.domain);
}
// if (this.applicationId) {
// await this.refresh();
// } else {
// await this.loadApplicationInfo(this.type, this.domain);
// }
return await this.loadApplicationInfo(this.type, this.domain);
}
getApplication() {

View File

@ -26,17 +26,18 @@ export function initialize<
type: AppType,
domain: string,
): GeneralFeatures<ED, Cxt, FrontCxt, AD> {
const application = new Application<ED, Cxt, FrontCxt, AD>(
type,
domain,
basicFeatures.cache,
basicFeatures.localStorage
);
const token = new Token<ED, Cxt, FrontCxt, AD>(
basicFeatures.cache,
basicFeatures.localStorage,
basicFeatures.environment
);
const application = new Application<ED, Cxt, FrontCxt, AD>(
type,
domain,
basicFeatures.cache,
basicFeatures.localStorage,
token
);
const wechatMenu = new WechatMenu<ED, Cxt, FrontCxt, AD>(
basicFeatures.cache,

View File

@ -43,20 +43,22 @@ export class Token<
if (tokenValue) {
this.tokenValue = tokenValue;
// this.loadTokenInfo();
}
else {
} else {
this.tokenValue = undefined;
}
this.publish();
}
constructor(cache: Cache<ED, Cxt, FrontCxt, AD>, storage: LocalStorage, environment: Environment) {
constructor(
cache: Cache<ED, Cxt, FrontCxt, AD>,
storage: LocalStorage,
environment: Environment
) {
super();
this.cache = cache;
this.storage = storage;
this.environment = environment;
this.tokenValue = ''; // 置个空字符串代表还在load storage缓存的数据
this.tokenValue = ''; // 置个空字符串代表还在load storage缓存的数据
this.loadSavedToken();
}
@ -74,15 +76,25 @@ export class Token<
}
}
async loginByMobile(mobile: string, password?: string, captcha?: string, disableRegist?: boolean) {
async loginByMobile(
mobile: string,
password?: string,
captcha?: string,
disableRegister?: boolean
) {
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('loginByMobile', {
password,
mobile,
captcha,
disableRegist,
env,
});
const { result } = await this.cache.exec(
'loginByMobile',
{
password,
mobile,
captcha,
disableRegister,
env,
},
undefined,
true
);
this.tokenValue = result;
await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
this.publish();
@ -151,10 +163,12 @@ export class Token<
this.removeToken();
}
removeToken() {
removeToken(disablePublish?: boolean) {
this.tokenValue = undefined;
this.storage.remove(LOCAL_STORAGE_KEYS.token);
this.publish();
if (!disablePublish) {
this.publish();
}
}
getTokenValue() {
@ -166,15 +180,12 @@ export class Token<
throw new OakUserInfoLoadingException();
}
if (this.tokenValue) {
const token = this.cache.get(
'token',
{
data: cloneDeep(tokenProjection),
filter: {
id: this.tokenValue!,
},
const token = this.cache.get('token', {
data: cloneDeep(tokenProjection),
filter: {
id: this.tokenValue!,
},
)[0];
})[0];
if (!token) {
this.loadTokenInfo();
if (allowUnloggedIn) {
@ -219,7 +230,10 @@ export class Token<
return !!token?.player?.isRoot;
}
async sendCaptcha(mobile: string, type: 'login' | 'changePassword' | 'confirm') {
async sendCaptcha(
mobile: string,
type: 'login' | 'changePassword' | 'confirm'
) {
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('sendCaptcha', {
mobile,
@ -245,7 +259,6 @@ export class Token<
async refreshWechatPublicUserInfo() {
await this.cache.exec('refreshWechatPublicUserInfo', {});
this.publish();
}
async getWechatMpUserPhoneNumber(code: string) {
@ -254,7 +267,6 @@ export class Token<
code,
env: env as WechatMpEnv,
});
// this.publish();
}
async wakeupParasite(id: string) {