oak-general-business/es/aspects/application.js

396 lines
14 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.

import { assert } from 'oak-domain/lib/utils/assert';
import { applicationProjection } from '../types/Projection';
import WechatSDK from 'oak-external-sdk/lib/WechatSDK';
import fs from 'fs';
import { cloneDeep } from 'oak-domain/lib/utils/lodash';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
export async function getApplication(params, context) {
const { type, domain, data, appId } = params;
// const [application] = await context.select(
// 'application',
// {
// data: cloneDeep(applicationProjection),
// filter: {
// type,
// system: {
// domain$system: {
// url: domain,
// },
// },
// },
// },
// {}
// );
// //微信小程序环境下 没有就报错
// if (type === 'wechatMp') {
// assert(
// application,
// '微信小程序环境下 application必须存在小程序相关配置'
// );
// } else if (type === 'native') {
// assert(application, 'APP环境下 application必须存在APP相关配置');
// } else {
// //web 或 wechatPublic
// if (type === 'wechatPublic') {
// // 如果微信公众号环境下 application不存在公众号配置但又在公众号访问这时可以使用web的application
// if (!application) {
// const [application2] = await context.select(
// 'application',
// {
// data: cloneDeep(applicationProjection),
// filter: {
// type: 'web',
// system: {
// domain$system: {
// url: domain,
// },
// },
// },
// },
// {}
// );
// assert(
// application2,
// '微信公众号环境下 application不存在公众号配置但必须存在web相关配置'
// );
// return application2.id as string;
// }
// } else {
// assert(application, 'web环境下 application必须存在web相关配置');
// }
// }
// return application.id as string;
// 先找指定domain的应用如果不存在再找系统下面的domain 但无论怎么样都必须一项
//
let applications = await context.select('application', {
data,
filter: {
type,
system: {
domain$system: {
url: domain,
},
},
domain: {
url: domain,
},
},
}, {});
assert(applications.length <= 1, `指定域名的应用 只能存在一项或未指定`);
if (applications.length === 0) {
applications = await context.select('application', {
data: data,
filter: {
type,
system: {
domain$system: {
url: domain,
},
},
domainId: {
$exists: false,
},
},
}, {});
}
switch (type) {
case 'wechatMp': {
assert(applications.length === 1, `微信小程序环境下,同一个系统必须存在唯一的【${type}】应用`);
const application = applications[0];
return application.id;
}
case 'native': {
assert(applications.length === 1, `APP环境下同一个系统必须存在唯一的【${type}】应用`);
const application = applications[0];
return application.id;
}
case 'wechatPublic': {
// 微信公众号环境下未配置公众号可以使用web的application
if (applications.length === 0) {
let applications2 = await context.select('application', {
data,
filter: {
type: 'web',
system: {
domain$system: {
url: domain,
},
},
domain: {
url: domain,
},
},
}, {});
assert(applications2.length <= 1, `指定域名的应用 只能存在一项或未指定`);
if (applications2.length === 0) {
applications2 = await context.select('application', {
data,
filter: {
type: 'web',
system: {
domain$system: {
url: domain,
},
},
domainId: {
$exists: false,
},
},
}, {});
}
assert(applications2.length === 1, '微信公众号环境下, 可以未配置公众号但必须存在web的application');
const application = applications2[0];
return application.id;
}
assert(applications.length === 1, `微信公众号环境下,同一个系统必须存在唯一的【${type}】应用 或 多个${type}应用必须配置域名`);
const application = applications[0];
return application.id;
}
case 'web': {
assert(applications.length === 1, `web环境下同一个系统必须存在唯一的【${type}】应用 或 多个${type}应用必须配置域名`);
const application = applications[0];
return application.id;
}
default: {
assert(false, `不支持的类型【${type}`);
return;
}
}
}
export async function signatureJsSDK({ url, env }, context) {
const application = context.getApplication();
const { type, config } = application;
assert(type === 'wechatPublic' && config.type === 'wechatPublic');
const config2 = config;
const { appId, appSecret } = config2;
const wechatInstance = WechatSDK.getInstance(appId, 'wechatPublic', appSecret);
const result = await wechatInstance.signatureJsSDK({ url });
return result;
}
export async function uploadWechatMedia(params, // FormData表单提交 isPermanent 变成 'true' | 'false'
context) {
const { applicationId, file, type: mediaType, description, extraFileId, } = params;
assert(applicationId);
const isPermanent = params.isPermanent === 'true';
const filename = file.originalFilename;
const filetype = file.mimetype;
const fileLength = file.size;
const fileStream = fs.createReadStream(file.filepath);
const [application] = await context.select('application', {
data: cloneDeep(applicationProjection),
filter: {
id: applicationId,
},
}, {
dontCollect: true,
});
const { type, config } = application;
assert(type === 'wechatPublic' || type === 'wechatMp');
let wechatInstance;
if (type === 'wechatPublic') {
assert(config.type === 'wechatPublic');
const config2 = config;
const { appId, appSecret } = config2;
wechatInstance = WechatSDK.getInstance(appId, 'wechatPublic', appSecret);
}
else {
assert(config.type === 'wechatMp');
const config2 = config;
const { appId, appSecret } = config2;
wechatInstance = WechatSDK.getInstance(appId, 'wechatPublic', appSecret);
}
let mediaId;
if (isPermanent) {
// 只有公众号才能上传永久素材
assert(type === 'wechatPublic');
const result = (await wechatInstance.createMaterial({
type: mediaType,
media: fileStream,
filename,
filetype,
fileLength,
description: description ? JSON.parse(description) : null,
}));
mediaId = result.media_id;
}
else {
const result = (await wechatInstance.createTemporaryMaterial({
type: mediaType,
media: fileStream,
filename,
filetype,
fileLength,
}));
mediaId = result.media_id;
}
if (extraFileId) {
const closeRootMode = context.openRootMode();
try {
await context.operate('extraFile', {
id: await generateNewIdAsync(),
action: 'update',
data: {
extra1: mediaId,
},
filter: {
id: extraFileId,
},
}, {
dontCollect: true,
});
closeRootMode();
}
catch (err) {
closeRootMode();
throw err;
}
}
return {
mediaId,
};
}
export async function getMaterial(params, context) {
const { mediaId, applicationId, isPermanent = false } = params;
assert(applicationId);
assert(mediaId);
const [application] = await context.select('application', {
data: cloneDeep(applicationProjection),
filter: {
id: applicationId,
},
}, {
dontCollect: true,
});
assert(application);
const { type, config } = application;
assert(type === 'wechatPublic' || type === 'wechatMp');
let wechatInstance;
if (type === 'wechatPublic') {
const config2 = config;
const { appId, appSecret } = config2;
wechatInstance = WechatSDK.getInstance(appId, type, appSecret);
}
else {
const config2 = config;
const { appId, appSecret } = config2;
wechatInstance = WechatSDK.getInstance(appId, type, appSecret);
}
let result;
if (isPermanent) {
// 只有公众号才能获取永久素材
assert(type === 'wechatPublic');
result = await wechatInstance.getMaterial({
mediaId,
});
}
else {
result = await wechatInstance.getTemporaryMaterial({
mediaId,
});
}
if (result instanceof ArrayBuffer) {
return Buffer.from(result).toString('base64');
}
return result;
}
export async function deleteMaterial(params, context) {
const { mediaId, applicationId } = params;
assert(applicationId);
assert(mediaId);
const [application] = await context.select('application', {
data: cloneDeep(applicationProjection),
filter: {
id: applicationId,
},
}, {
dontCollect: true,
});
assert(application);
const { type, config } = application;
assert(type === 'wechatPublic');
const config2 = config;
const { appId, appSecret } = config2;
const wechatInstance = WechatSDK.getInstance(appId, type, appSecret);
const result = await wechatInstance.deleteMaterial({
mediaId,
});
return result;
}
export async function batchGetArticle(params, context) {
const { applicationId, offset, count, noContent } = params;
assert(applicationId);
const [application] = await context.select('application', {
data: cloneDeep(applicationProjection),
filter: {
id: applicationId,
},
}, {
dontCollect: true,
});
assert(application);
const { type, config } = application;
assert(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK.getInstance(appId, type, appSecret);
const result = await wechatInstance.batchGetArticle({
offset,
count,
noContent,
});
return result;
}
export async function getArticle(params, context) {
const { applicationId, articleId } = params;
assert(applicationId);
const [application] = await context.select('application', {
data: cloneDeep(applicationProjection),
filter: {
id: applicationId,
},
}, {
dontCollect: true,
});
assert(application);
const { type, config } = application;
assert(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK.getInstance(appId, type, appSecret);
const result = await wechatInstance.getArticle({
articleId,
});
return result;
}
export async function batchGetMaterialList(params, context) {
const { applicationId } = params;
assert(applicationId);
const [application] = await context.select('application', {
data: cloneDeep(applicationProjection),
filter: {
id: applicationId,
},
}, {
dontCollect: true,
});
assert(application);
const { type, config } = application;
assert(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK.getInstance(appId, type, appSecret);
const { type: materialType, offset, count } = params;
const result = await wechatInstance.batchGetMaterialList({
type: materialType,
offset,
count,
});
return result;
}