From 67ff1f8c767885b35fe17aec85b75b1bb66c29d0 Mon Sep 17 00:00:00 2001 From: MrKBear Date: Tue, 11 Jan 2022 17:39:51 +0800 Subject: [PATCH] Extend core API(#24) --- miniprogram/core/Api.ts | 60 ++++++++++++++++++++++++++++++++++++++ miniprogram/core/Config.ts | 11 +++++++ 2 files changed, 71 insertions(+) diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index d0027ef..9ae9328 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -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 extends Emitter) => 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; + }; } /** diff --git a/miniprogram/core/Config.ts b/miniprogram/core/Config.ts index 1a9dca9..6a968e8 100644 --- a/miniprogram/core/Config.ts +++ b/miniprogram/core/Config.ts @@ -36,3 +36,14 @@ export const LOGGER_FILTER:Array[] = [ // 输出警告和错误 // ["WARN", "ERROR", "FATAL"], ]; + +/** + * 请求失败的提示用语 + * 请求失败时如果选择自动显示消息 + * 则会从以下内容中选择 + */ +export const API_FAILED_SHOW_MESSAGE: string[] = [ + "失败啦(ó_ò。)", "服务器睡着了", "数据移民火星了", + "数据在半路走丢了", "服务器打了个瞌睡", "服务器被玩坏了", + "服务器在扶老奶奶过马路", "服务器累了", "服务器在拯救世界" +] \ No newline at end of file