优化 usePage
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
export * from "./refs";
|
||||
export * from "./page";
|
||||
export * from "./pager";
|
||||
export * from "./long-press";
|
||||
export * from "./cache";
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
import { config } from "@/config";
|
||||
import { router } from "../router";
|
||||
import { getPx, isH5, isHarmony } from "../utils";
|
||||
import { ctx } from "../ctx";
|
||||
|
||||
class Page {
|
||||
scrolls: Map<string, ((top: number) => void)[]> = new Map();
|
||||
|
||||
path() {
|
||||
return router.path();
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发滚动事件
|
||||
* @param top 滚动距离
|
||||
*/
|
||||
triggerScroll(top: number) {
|
||||
const callbacks = this.scrolls.get(this.path()) ?? [];
|
||||
callbacks.forEach((cb) => {
|
||||
cb(top);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册滚动事件回调
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
onPageScroll(callback: (top: number) => void) {
|
||||
const callbacks = this.scrolls.get(this.path()) ?? [];
|
||||
callbacks.push(callback);
|
||||
this.scrolls.set(this.path(), callbacks);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否需要计算 tabBar 高度
|
||||
* @returns boolean
|
||||
*/
|
||||
hasCustomTabBar() {
|
||||
if (router.isTabPage()) {
|
||||
if (isHarmony()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return config.isCustomTabBar || isH5();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否存在自定义 topbar
|
||||
* @returns boolean
|
||||
*/
|
||||
hasCustomTopbar() {
|
||||
return router.route()?.isCustomNavbar ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 tabBar 高度
|
||||
* @returns tabBar 高度
|
||||
*/
|
||||
getTabBarHeight() {
|
||||
let h = ctx.tabBar.height == null ? 50 : getPx(ctx.tabBar.height!);
|
||||
|
||||
if (this.hasCustomTabBar()) {
|
||||
h += this.getSafeAreaHeight("bottom");
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安全区域高度
|
||||
* @param type 类型
|
||||
* @returns 安全区域高度
|
||||
*/
|
||||
getSafeAreaHeight(type: "top" | "bottom") {
|
||||
const { safeAreaInsets } = uni.getWindowInfo();
|
||||
|
||||
let h: number;
|
||||
|
||||
if (type == "top") {
|
||||
h = safeAreaInsets.top;
|
||||
} else {
|
||||
h = safeAreaInsets.bottom;
|
||||
|
||||
// #ifdef APP-ANDROID
|
||||
if (h == 0) {
|
||||
h = 16;
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
}
|
||||
|
||||
export const page = new Page();
|
||||
|
||||
export function usePage(): Page {
|
||||
return page;
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
import { page } from "./hooks";
|
||||
import { initTheme, setH5 } from "./theme";
|
||||
import { initLocale } from "@/locale";
|
||||
|
||||
export function cool(app: VueApp) {
|
||||
app.mixin({
|
||||
onPageScroll(e) {
|
||||
page.triggerScroll(e.scrollTop);
|
||||
},
|
||||
onShow() {
|
||||
// #ifdef H5
|
||||
setTimeout(() => {
|
||||
|
||||
@@ -81,18 +81,10 @@ export function request<T = any>(options: RequestOptions): Promise<T> {
|
||||
timeout,
|
||||
|
||||
success(res) {
|
||||
if (!isObject(res.data as any)) {
|
||||
resolve(res.data as T);
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析响应数据
|
||||
const { code, message, data } = parse<Response>(res.data ?? { code: 0 })!;
|
||||
|
||||
// 401 无权限
|
||||
if (res.statusCode == 401) {
|
||||
user.logout();
|
||||
return reject({ message } as Response);
|
||||
return reject({ message: t("无权限") } as Response);
|
||||
}
|
||||
|
||||
// 502 服务异常
|
||||
@@ -111,6 +103,14 @@ export function request<T = any>(options: RequestOptions): Promise<T> {
|
||||
|
||||
// 200 正常响应
|
||||
if (res.statusCode == 200) {
|
||||
if (!isObject(res.data as any)) {
|
||||
resolve(res.data as T);
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析响应数据
|
||||
const { code, message, data } = parse<Response>(res.data ?? { code: 0 })!;
|
||||
|
||||
switch (code) {
|
||||
case 1000:
|
||||
resolve(data as T);
|
||||
|
||||
@@ -4,4 +4,5 @@ export * from "./device";
|
||||
export * from "./file";
|
||||
export * from "./parse";
|
||||
export * from "./path";
|
||||
export * from "./rect";
|
||||
export * from "./storage";
|
||||
|
||||
68
cool/utils/rect.ts
Normal file
68
cool/utils/rect.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { config } from "@/config";
|
||||
import { router } from "../router";
|
||||
import { isH5, isHarmony } from "./comm";
|
||||
import { ctx } from "../ctx";
|
||||
import { getPx } from "./parse";
|
||||
|
||||
/**
|
||||
* 是否需要计算 tabBar 高度
|
||||
* @returns boolean
|
||||
*/
|
||||
export function hasCustomTabBar() {
|
||||
if (router.isTabPage()) {
|
||||
if (isHarmony()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return config.isCustomTabBar || isH5();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否存在自定义 topbar
|
||||
* @returns boolean
|
||||
*/
|
||||
export function hasCustomTopbar() {
|
||||
return router.route()?.isCustomNavbar ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安全区域高度
|
||||
* @param type 类型
|
||||
* @returns 安全区域高度
|
||||
*/
|
||||
export function getSafeAreaHeight(type: "top" | "bottom") {
|
||||
const { safeAreaInsets } = uni.getWindowInfo();
|
||||
|
||||
let h: number;
|
||||
|
||||
if (type == "top") {
|
||||
h = safeAreaInsets.top;
|
||||
} else {
|
||||
h = safeAreaInsets.bottom;
|
||||
|
||||
// #ifdef APP-ANDROID
|
||||
if (h == 0) {
|
||||
h = 16;
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 tabBar 高度
|
||||
* @returns tabBar 高度
|
||||
*/
|
||||
export function getTabBarHeight() {
|
||||
let h = ctx.tabBar.height == null ? 50 : getPx(ctx.tabBar.height!);
|
||||
|
||||
if (hasCustomTabBar()) {
|
||||
h += getSafeAreaHeight("bottom");
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
Reference in New Issue
Block a user