From 339bb88057e47124007aecff82c4b2e22e0c96be Mon Sep 17 00:00:00 2001 From: wkj <278599135@.com> Date: Wed, 31 Jan 2024 20:22:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=A8=A1=E7=89=88=E6=97=B6=EF=BC=8C=E5=A4=84?= =?UTF-8?q?=E7=90=86=20OakApplicationLoadingException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/features/token.js | 2 +- es/page.mp.js | 220 ++++++++++++++++++++++--------------- lib/features/token.js | 2 +- lib/page.mp.js | 219 ++++++++++++++++++++++--------------- src/features/token.ts | 2 +- src/page.mp.ts | 249 +++++++++++++++++++++++++----------------- 6 files changed, 414 insertions(+), 280 deletions(-) diff --git a/es/features/token.js b/es/features/token.js index 9301864ef..d80343faa 100644 --- a/es/features/token.js +++ b/es/features/token.js @@ -130,7 +130,7 @@ export class Token extends Feature { return this.tokenValue; } getToken(allowUnloggedIn) { - if (this.tokenValue === '') { + if (!allowUnloggedIn && this.tokenValue === '') { throw new OakUserInfoLoadingException(); } if (this.tokenValue) { diff --git a/es/page.mp.js b/es/page.mp.js index 5cd82a461..eb77d487d 100644 --- a/es/page.mp.js +++ b/es/page.mp.js @@ -1,5 +1,9 @@ -import { OakMpHaveToSubscribeMessage } from './types/Exception'; +import { + OakMpHaveToSubscribeMessage, + OakApplicationLoadingException, +} from './types/Exception'; import { createComponent as createBaseComponent } from 'oak-frontend-base/es/page.mp'; + /** * 这里的逻辑暴露出去,是为了让general可以被其它库重载 * @param this @@ -63,98 +67,134 @@ export function createComponent(option, features) { const { wechatMp, data, methods, lifetimes, userInsensitive, ...rest } = option; const { relatedMessageTypes } = wechatMp || {}; const { ready, attached, show, hide, ...restLifeTimes } = lifetimes || {}; - return createBaseComponent({ - data: typeof data === 'function' ? function () { - return { - __userId: undefined, - ...(data.call(this)), - }; - } : { - __userId: undefined, - ...data, - }, - methods: { - async subscribeMpMessage(messageTypes, haveToAccept, tip) { - return await subscribeMpMessage.call(this, messageTypes, haveToAccept, tip); + return createBaseComponent( + { + data: + typeof data === 'function' + ? function () { + return { + __userId: null, + ...data.call(this), + }; + } + : { + __userId: null, + ...data, + }, + methods: { + async subscribeMpMessage(messageTypes, haveToAccept, tip) { + return await subscribeMpMessage.call( + this, + messageTypes, + haveToAccept, + tip + ); + }, + getMessageTypeTemplate() { + if (relatedMessageTypes) { + try { + const applicationId = + this.features.application.getApplicationId(); + const existedOnes = this.features.cache.get( + 'messageTypeTemplate', + { + data: { + id: 1, + templateId: 1, + template: { + id: 1, + applicationId: 1, + wechatId: 1, + }, + type: 1, + }, + filter: { + type: { + $in: relatedMessageTypes, + }, + template: { + applicationId, + }, + }, + } + ); + if (existedOnes.length === 0) { + this.features.cache.refresh( + 'messageTypeTemplate', + { + data: { + id: 1, + templateId: 1, + template: { + id: 1, + applicationId: 1, + wechatId: 1, + }, + type: 1, + }, + filter: { + type: { + $in: relatedMessageTypes, + }, + template: { + applicationId, + }, + }, + } + ); + } + } catch (err) { + if (err instanceof OakApplicationLoadingException) { + if (process.env.NODE_ENV === 'development') { + console.warn( + '当Application(全局应用程序)对象正在加载和配置时,为了确保正确地获取模板消息类型的数据,我们会在Application准备就绪之后重新执行getMessageTypeTemplate方法来安全地获取并应用所需模版' + ); + } + } else { + throw err; + } + } + } + }, + ...methods, }, - ...methods, - }, - lifetimes: { - attached() { - if (!userInsensitive) { - this.addFeatureSub('token', () => this.refresh()); - } - attached && attached.call(this); - }, - ready() { - if (relatedMessageTypes) { - const applicationId = this.features.application.getApplicationId(); - const existedOnes = this.features.cache.get('messageTypeTemplate', { - data: { - id: 1, - templateId: 1, - template: { - id: 1, - applicationId: 1, - wechatId: 1, - }, - type: 1, - }, - filter: { - type: { - $in: relatedMessageTypes, - }, - template: { - applicationId, - }, - }, - }); - if (existedOnes.length === 0) { - this.features.cache.refresh('messageTypeTemplate', { - data: { - id: 1, - templateId: 1, - template: { - id: 1, - applicationId: 1, - wechatId: 1, - }, - type: 1, - }, - filter: { - type: { - $in: relatedMessageTypes, - }, - template: { - applicationId, - }, - }, + lifetimes: { + attached() { + if (!userInsensitive) { + this.addFeatureSub('token', () => this.refresh()); + } + this.addFeatureSub('application', () => + this.getMessageTypeTemplate() + ); + attached && attached.call(this); + }, + ready() { + this.getMessageTypeTemplate(); + ready && ready.call(this); + }, + show() { + show && show.call(this); + if (!userInsensitive) { + const userId = this.features.token.getUserId(true); + if (userId !== this.state.__userId) { + this.refresh(); + } + } + }, + hide() { + hide && hide.call(this); + if (!userInsensitive) { + const userId = this.features.token.getUserId(true); + this.setState({ + __userId: userId, }); } - } - ready && ready.call(this); + }, + ...restLifeTimes, }, - show() { - show && show.call(this); - if (!userInsensitive) { - const userId = this.features.token.getUserId(true); - if (userId !== this.state.__userId) { - this.refresh(); - } - } - }, - hide() { - hide && hide.call(this); - if (!userInsensitive) { - const userId = this.features.token.getUserId(true); - this.setState({ - __userId: userId, - }); - } - }, - ...restLifeTimes, + wechatMp, + ...rest, }, - wechatMp, - ...rest, - }, features); + features + ); } diff --git a/lib/features/token.js b/lib/features/token.js index afe0f920d..5f50c6a7f 100644 --- a/lib/features/token.js +++ b/lib/features/token.js @@ -133,7 +133,7 @@ class Token extends oak_frontend_base_1.Feature { return this.tokenValue; } getToken(allowUnloggedIn) { - if (this.tokenValue === '') { + if (!allowUnloggedIn && this.tokenValue === '') { throw new Exception_2.OakUserInfoLoadingException(); } if (this.tokenValue) { diff --git a/lib/page.mp.js b/lib/page.mp.js index 2e15fe2dd..837025238 100644 --- a/lib/page.mp.js +++ b/lib/page.mp.js @@ -67,99 +67,140 @@ function createComponent(option, features) { const { wechatMp, data, methods, lifetimes, userInsensitive, ...rest } = option; const { relatedMessageTypes } = wechatMp || {}; const { ready, attached, show, hide, ...restLifeTimes } = lifetimes || {}; - return (0, page_mp_1.createComponent)({ - data: typeof data === 'function' ? function () { - return { - __userId: undefined, - ...(data.call(this)), - }; - } : { - __userId: undefined, - ...data, - }, - methods: { - async subscribeMpMessage(messageTypes, haveToAccept, tip) { - return await subscribeMpMessage.call(this, messageTypes, haveToAccept, tip); + return (0, page_mp_1.createComponent)( + { + data: + typeof data === 'function' + ? function () { + return { + __userId: null, + ...data.call(this), + }; + } + : { + __userId: null, + ...data, + }, + methods: { + async subscribeMpMessage(messageTypes, haveToAccept, tip) { + return await subscribeMpMessage.call( + this, + messageTypes, + haveToAccept, + tip + ); + }, + getMessageTypeTemplate() { + if (relatedMessageTypes) { + try { + const applicationId = + this.features.application.getApplicationId(); + const existedOnes = this.features.cache.get( + 'messageTypeTemplate', + { + data: { + id: 1, + templateId: 1, + template: { + id: 1, + applicationId: 1, + wechatId: 1, + }, + type: 1, + }, + filter: { + type: { + $in: relatedMessageTypes, + }, + template: { + applicationId, + }, + }, + } + ); + if (existedOnes.length === 0) { + this.features.cache.refresh( + 'messageTypeTemplate', + { + data: { + id: 1, + templateId: 1, + template: { + id: 1, + applicationId: 1, + wechatId: 1, + }, + type: 1, + }, + filter: { + type: { + $in: relatedMessageTypes, + }, + template: { + applicationId, + }, + }, + } + ); + } + } catch (err) { + if ( + err instanceof + Exception_1.OakApplicationLoadingException + ) { + if ( + process.env.NODE_ENV === 'development' + ) { + console.warn( + '当Application(全局应用程序)对象正在加载和配置时,为了确保正确地获取模板消息类型的数据,我们会在Application准备就绪之后重新执行getMessageTypeTemplate方法来安全地获取并应用所需模版' + ); + } + } else { + throw err; + } + } + } + }, + ...methods, }, - ...methods, - }, - lifetimes: { - attached() { - if (!userInsensitive) { - this.addFeatureSub('token', () => this.refresh()); - } - attached && attached.call(this); - }, - ready() { - if (relatedMessageTypes) { - const applicationId = this.features.application.getApplicationId(); - const existedOnes = this.features.cache.get('messageTypeTemplate', { - data: { - id: 1, - templateId: 1, - template: { - id: 1, - applicationId: 1, - wechatId: 1, - }, - type: 1, - }, - filter: { - type: { - $in: relatedMessageTypes, - }, - template: { - applicationId, - }, - }, - }); - if (existedOnes.length === 0) { - this.features.cache.refresh('messageTypeTemplate', { - data: { - id: 1, - templateId: 1, - template: { - id: 1, - applicationId: 1, - wechatId: 1, - }, - type: 1, - }, - filter: { - type: { - $in: relatedMessageTypes, - }, - template: { - applicationId, - }, - }, + lifetimes: { + attached() { + if (!userInsensitive) { + this.addFeatureSub('token', () => this.refresh()); + } + this.addFeatureSub('application', () => + this.getMessageTypeTemplate() + ); + attached && attached.call(this); + }, + ready() { + this.getMessageTypeTemplate() + ready && ready.call(this); + }, + show() { + show && show.call(this); + if (!userInsensitive) { + const userId = this.features.token.getUserId(true); + if (userId !== this.state.__userId) { + this.refresh(); + } + } + }, + hide() { + hide && hide.call(this); + if (!userInsensitive) { + const userId = this.features.token.getUserId(true); + this.setState({ + __userId: userId, }); } - } - ready && ready.call(this); + }, + ...restLifeTimes, }, - show() { - show && show.call(this); - if (!userInsensitive) { - const userId = this.features.token.getUserId(true); - if (userId !== this.state.__userId) { - this.refresh(); - } - } - }, - hide() { - hide && hide.call(this); - if (!userInsensitive) { - const userId = this.features.token.getUserId(true); - this.setState({ - __userId: userId, - }); - } - }, - ...restLifeTimes, + wechatMp, + ...rest, }, - wechatMp, - ...rest, - }, features); + features + ); } exports.createComponent = createComponent; diff --git a/src/features/token.ts b/src/features/token.ts index b56b4e3b5..05102b654 100644 --- a/src/features/token.ts +++ b/src/features/token.ts @@ -188,7 +188,7 @@ export class Token< } getToken(allowUnloggedIn?: boolean) { - if (this.tokenValue === '') { + if (!allowUnloggedIn && this.tokenValue === '') { throw new OakUserInfoLoadingException(); } if (this.tokenValue) { diff --git a/src/page.mp.ts b/src/page.mp.ts index 7dcbd8fe3..469a006f5 100644 --- a/src/page.mp.ts +++ b/src/page.mp.ts @@ -5,7 +5,7 @@ import { BasicFeatures } from 'oak-frontend-base'; import { CommonAspectDict } from 'oak-common-aspect'; import { BackendRuntimeContext } from './context/BackendRuntimeContext'; import { FrontendRuntimeContext } from './context/FrontendRuntimeContext'; -import { OakMpHaveToSubscribeMessage } from './types/Exception'; +import { OakMpHaveToSubscribeMessage, OakApplicationLoadingException } from './types/Exception'; import { GAD, GFD, OakComponentOption } from './types/Page'; import { createComponent as createBaseComponent } from 'oak-frontend-base/es/page.mp'; @@ -128,111 +128,164 @@ export function createComponent< const { relatedMessageTypes } = wechatMp || {}; const { ready, attached, show, hide, ...restLifeTimes } = lifetimes || {}; - return createBaseComponent Promise; - }>({ - data: typeof data === 'function' ? function() { - return { - __userId: undefined, - ...(data.call(this)), - } as TData & { - __userId: string | undefined; - } - } : { - __userId: undefined, - ...data, - } as TData & { + return createBaseComponent< + IsList, + ED, + T, + Cxt, + FrontCxt, + AD, + FD, + FormedData, + TData & { __userId: string | undefined; }, - methods: { - async subscribeMpMessage(messageTypes: string[], haveToAccept?: boolean, tip?: string) { - return await subscribeMpMessage.call(this as any, messageTypes, haveToAccept, tip); - }, - ...(methods as TMethod), - }, - lifetimes: { - attached() { - if (!userInsensitive) { - this.addFeatureSub('token', () => this.refresh()); - } - attached && attached.call(this); - }, - ready() { - if (relatedMessageTypes) { - const applicationId = this.features.application.getApplicationId(); - const existedOnes = this.features.cache.get( - 'messageTypeTemplate', - { - data: { - id: 1, - templateId: 1, - template: { - id: 1, - applicationId: 1, - wechatId: 1, - }, - type: 1, - }, - filter: { - type: { - $in: relatedMessageTypes, - }, - template: { - applicationId, - }, - }, - } + TProperty, + TMethod & { + subscribeMpMessage: ( + messageTypes: string[], + haveToAccept?: boolean, + tip?: string + ) => Promise; + } + >( + { + data: + typeof data === 'function' + ? function () { + return { + __userId: null, + ...data.call(this), + } as TData & { + __userId: string | undefined | null; + }; + } + : ({ + __userId: null, + ...data, + } as TData & { + __userId: string | undefined | null; + }), + methods: { + async subscribeMpMessage( + messageTypes: string[], + haveToAccept?: boolean, + tip?: string + ) { + return await subscribeMpMessage.call( + this as any, + messageTypes, + haveToAccept, + tip ); - if (existedOnes.length === 0) { - this.features.cache.refresh('messageTypeTemplate', { - data: { - id: 1, - templateId: 1, - template: { - id: 1, - applicationId: 1, - wechatId: 1, - }, - type: 1, - }, - filter: { - type: { - $in: relatedMessageTypes, - }, - template: { - applicationId, - }, - }, - }); + }, + getMessageTypeTemplate() { + if (relatedMessageTypes) { + try { + const applicationId = + this.features.application.getApplicationId(); + const existedOnes = this.features.cache.get( + 'messageTypeTemplate', + { + data: { + id: 1, + templateId: 1, + template: { + id: 1, + applicationId: 1, + wechatId: 1, + }, + type: 1, + }, + filter: { + type: { + $in: relatedMessageTypes, + }, + template: { + applicationId, + }, + }, + } + ); + if (existedOnes.length === 0) { + this.features.cache.refresh( + 'messageTypeTemplate', + { + data: { + id: 1, + templateId: 1, + template: { + id: 1, + applicationId: 1, + wechatId: 1, + }, + type: 1, + }, + filter: { + type: { + $in: relatedMessageTypes, + }, + template: { + applicationId, + }, + }, + } + ); + } + } catch (err) { + if (err instanceof OakApplicationLoadingException) { + if (process.env.NODE_ENV === 'development') { + console.warn( + '当Application(全局应用程序)对象正在加载和配置时,为了确保正确地获取模板消息类型的数据,我们会在Application准备就绪之后重新执行getMessageTypeTemplate方法来安全地获取并应用所需模版' + ); + } + } else { + throw err; + } + } } - } - ready && ready.call(this); + }, + ...(methods as TMethod), }, - show() { - show && show.call(this); - if (!userInsensitive) { - const userId = this.features.token.getUserId(true); - if (userId !== this.state.__userId) { - this.refresh(); + lifetimes: { + attached() { + if (!userInsensitive) { + this.addFeatureSub('token', () => this.refresh()); } - } + this.addFeatureSub('application', () => + this.getMessageTypeTemplate() + ); + attached && attached.call(this); + }, + ready() { + this.getMessageTypeTemplate(); + ready && ready.call(this); + }, + show() { + show && show.call(this); + if (!userInsensitive) { + const userId = this.features.token.getUserId(true); + if (userId !== this.state.__userId) { + this.refresh(); + } + } + }, + hide() { + hide && hide.call(this); + if (!userInsensitive) { + const userId = this.features.token.getUserId(true); + this.setState({ + __userId: userId as string | undefined, + } as any); + } + }, + ...restLifeTimes, }, - hide() { - hide && hide.call(this); - if (!userInsensitive) { - const userId = this.features.token.getUserId(true); - this.setState({ - __userId: userId as string | undefined, - } as any); - } - }, - ...restLifeTimes, + wechatMp, + ...rest, }, - wechatMp, - ...rest, - }, features); + features + ); }