async function checkPermission() {
// 模拟异步验证逻辑,这里简单返回 true 表示有权限
return true;
}
function withPermission(target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function(...args) {
const hasPermission = await checkPermission();
if (hasPermission) {
return originalMethod.apply(this, args);
} else {
throw new Error('权限不足');
}
};
return descriptor;
}
class MathOperations {
@withPermission
add(a, b) {
return a + b;
}
@withPermission
subtract(a, b) {
return a - b;
}
}