oak-general-business/lib/page.mp.js

164 lines
5.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createComponent = exports.subscribeMpMessage = void 0;
const Exception_1 = require("./types/Exception");
const page_mp_1 = require("oak-frontend-base/es/page.mp");
/**
* 这里的逻辑暴露出去是为了让general可以被其它库重载
* @param this
* @param messageTypes
* @param haveToAccept
* @param action
* @param messageProps
* @returns
*/
async function subscribeMpMessage(messageTypes, haveToAccept, tip) {
const mttIds = this.features.cache.get('messageTypeTemplate', {
data: {
id: 1,
templateId: 1,
template: {
id: 1,
wechatId: 1,
},
type: 1,
},
filter: {
type: {
$in: messageTypes,
},
},
});
if (mttIds.length > 0) {
const tmplIds = mttIds.map(ele => ele.template?.wechatId);
const result = await wx.requestSubscribeMessage({
tmplIds
});
const rejected = Object.keys(result).filter(ele => {
// 排除errMsg
if (ele === 'errMsg') {
return false;
}
else if (result[ele] === 'reject') {
return true;
}
else if (result[ele] !== 'accept') {
this.setMessage({
type: 'warning',
content: `类型${ele}的模板消息被${result[ele]},请管理员查看后台`,
});
}
});
if (rejected.length > 0 && haveToAccept) {
if (tip) {
this.setMessage({
type: 'warning',
content: tip,
});
return false;
}
throw new Exception_1.OakMpHaveToSubscribeMessage(rejected);
}
}
return true;
}
exports.subscribeMpMessage = subscribeMpMessage;
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);
},
...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,
},
},
});
}
}
ready && ready.call(this);
},
show() {
if (!userInsensitive) {
const userId = this.features.token.getUserId(true);
if (userId !== this.state.__userId) {
this.refresh();
}
}
},
hide() {
if (!userInsensitive) {
const userId = this.features.token.getUserId(true);
this.setState({
__userId: userId,
});
}
},
...restLifeTimes,
},
wechatMp,
...rest,
}, features);
}
exports.createComponent = createComponent;