Extend core API(#24)

This commit is contained in:
MrKBear 2022-01-11 17:39:51 +08:00
parent d52ddee947
commit 67ff1f8c76
2 changed files with 71 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import { Emitter } from "./Emitter";
import { API_FAILED_SHOW_MESSAGE } from "./Config";
import { Logger, LogLabel, LevelLogLabel, colorRadio, StatusLabel } from "./Logger";
interface IAppAPIParam {
api: {
@ -491,6 +492,65 @@ class API<I extends IAnyData, O extends IAnyData> extends Emitter<IAPIEvent<I, O
});
}
}
/**
*
* @param message
* @param mask 使穿
*/
public showLoading(message: string | ((data?: Partial<I>) => string), mask: boolean = false): this {
// 获取标题
let title: string = message instanceof Function ? message(this.data) : message;
this.on("request", () => {
wx.showLoading({
title, mask
})
});
this.on("complete", () => {
wx.hideLoading();
});
return this;
};
/**
*
*/
public showNavigationBarLoading(): this {
this.on("request", () => {
wx.showNavigationBarLoading()
});
this.on("complete", () => {
wx.hideNavigationBarLoading();
});
return this;
}
/**
*
*/
public showFailed(): this {
// 生成随机索引值
let randomIndex = Math.floor(
Math.random() * API_FAILED_SHOW_MESSAGE.length
);
this.on("fail", () => {
wx.showToast({
title: API_FAILED_SHOW_MESSAGE[randomIndex],
icon: "none"
});
});
return this;
};
}
/**

View File

@ -36,3 +36,14 @@ export const LOGGER_FILTER:Array<RegExp | string>[] = [
// 输出警告和错误
// ["WARN", "ERROR", "FATAL"],
];
/**
*
*
*
*/
export const API_FAILED_SHOW_MESSAGE: string[] = [
"失败啦(ó_ò。)", "服务器睡着了", "数据移民火星了",
"数据在半路走丢了", "服务器打了个瞌睡", "服务器被玩坏了",
"服务器在扶老奶奶过马路", "服务器累了", "服务器在拯救世界"
]