/**
* 接口try 装饰器
* @param loading 是否有loading效果
* @param cb 异常处理函数d 函数名
* @constructor
*/
export function Try(loading?: boolean, cb?: string) {
return function (target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function (...args) {
try {
if (loading) {
Tips.showLoading();
}
return await method.call(this, ...args);
} catch (e) {
if (cb && target[cb]) {
target[cb](e);
} else {
Ehr.handleError(e);
}
} finally {
if (loading) {
Tips.hideLoading();
}
}
};
};
}
/**
* 节流装饰器
* @param delay
* @constructor
*/
export function Throttle(delay = 300) {
let previous = 0;
return function (target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = function (...args) {
const now = Date.now();
if (now - previous > delay) {
previous = now;
return method.call(this, ...args);
}
};
};
}
/**
* 防抖装饰器
* @param delay
* @constructor
*/
export const Debounce = (delay: number = 200) => {
let timer: any = null;
return function (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = function (...args) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
return method.call(this, ...args);
}, delay);
};
};
};
@Debounce()
@Try()
async login() {
if (this.isValid) return;
await LoginService.login();
await Service.becomeVip(this.userId);
YNavigator.redirectTo("/shop/pages/shopIndex/ShopIndexPage");
}
网友评论