修正 获取微信模版时,处理 OakApplicationLoadingException

This commit is contained in:
wkj 2024-01-31 20:22:16 +08:00
parent 917eaec869
commit 339bb88057
6 changed files with 414 additions and 280 deletions

View File

@ -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) {

View File

@ -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
);
}

View File

@ -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) {

View File

@ -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;

View File

@ -188,7 +188,7 @@ export class Token<
}
getToken(allowUnloggedIn?: boolean) {
if (this.tokenValue === '') {
if (!allowUnloggedIn && this.tokenValue === '') {
throw new OakUserInfoLoadingException();
}
if (this.tokenValue) {

View File

@ -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<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData & {
__userId: string | undefined;
}, TProperty, TMethod & {
subscribeMpMessage: (messageTypes: string[], haveToAccept?: boolean, tip?: string) => Promise<boolean>;
}>({
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<boolean>;
}
>(
{
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
);
}