vite-uni-dev-tool
Version:
vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试
199 lines (177 loc) • 5.33 kB
text/typescript
/**
* 获取当前页面地址
*
* @return {*}
*/
export function getCurrentPagePath() {
let pages = getCurrentPages();
let item = pages[pages.length - 1];
if (item && item.route) {
return item.route;
}
return '';
}
/** 截图 */
export function screenshot() {
if (!plus) return;
if (!plus.nativeObj) return;
if (!plus.nativeObj.Bitmap) return;
let pages = getCurrentPages();
let page = pages[pages.length - 1];
let ws = page.$getAppWebview?.();
let bitmap = new plus.nativeObj.Bitmap('drawScreen');
// 将webview内容绘制到Bitmap对象中
ws?.draw(
bitmap,
() => {
// 保存图片到本地
bitmap.save(
`pictures/${Date.now()}.jpg`,
{
overwrite: true,
},
(res) => {
console.log('res.target' + res.target); // 图片地址
uni.showToast({
title: '截图成功:' + res.target,
icon: 'none',
duration: 60 * 1000,
});
bitmap.clear(); // 清除Bitmap对象
},
(error) => {
console.log('[DevTool] 截图失败:' + JSON.stringify(error)); // 保存失败信息
uni.showToast({
title: '[DevTool] 截图失败:' + JSON.stringify(error),
icon: 'none',
duration: 60 * 1000,
});
bitmap.clear(); // 清除Bitmap对象
},
);
// bitmap.clear(); // 清除Bitmap对象
},
(error) => {
console.log('[DevTool] 绘制失败:' + JSON.stringify(error)); // 绘制失败
uni.showToast({
title: '绘制失败:' + JSON.stringify(error),
icon: 'none',
duration: 60 * 1000,
});
},
{
check: true, // 设置为检测白屏
},
);
}
// 截屏函数
export function captureScreen(option: { success?: () => void }) {
const ctx = uni.createCanvasContext('fullscreenCanvas');
const { windowWidth, windowHeight } = uni.getSystemInfoSync();
// 绘制需要截屏的内容(示例:绘制一个矩形)
ctx.setFillStyle('#ffffff');
ctx.fillRect(0, 0, windowWidth, windowHeight); // 画布尺寸
ctx.setFillStyle('#000000');
ctx.setFontSize(20);
ctx.fillText('这是截屏内容', 100, 100);
ctx.draw(false, () => {
// 生成图片
uni.canvasToTempFilePath({
canvasId: 'fullscreenCanvas',
width: windowWidth,
height: windowHeight,
destWidth: windowWidth * 2, // 提高清晰度
destHeight: windowHeight * 2,
fileType: 'png',
success: (res) => {
console.log('res: ', res);
console.log('全屏截屏成功:', res.tempFilePath);
},
fail: (err) => {
console.error('全屏截屏失败:', err);
},
});
});
}
type Primitive = string | number | boolean | null | undefined | symbol | bigint;
function isPrimitive(value: unknown): value is Primitive {
return (
value === null || (typeof value !== 'object' && typeof value !== 'function')
);
}
export interface DeepEqualOptions {
maxDepth?: number;
}
export function deepEqual(
a: unknown,
b: unknown,
options: DeepEqualOptions = { maxDepth: 10 },
): boolean {
const { maxDepth = 10 } = options;
const seen = new WeakMap<object, number>();
let nextStamp = 1;
return deepEqualInternal(a, b, seen, () => nextStamp++, 1, maxDepth);
}
function deepEqualInternal(
a: unknown,
b: unknown,
seen: WeakMap<object, number>,
getNextStamp: () => number,
currentDepth: number,
maxDepth: number,
): boolean {
// 超过最大深度
if (currentDepth > maxDepth) return false;
// 处理原始值
if (isPrimitive(a) || isPrimitive(b)) {
// 特殊处理 NaN(两个 NaN 被视为相等)
if (typeof a === 'number' && typeof b === 'number' && isNaN(a) && isNaN(b))
return true;
return a === b;
}
// 处理对象和数组
if (a instanceof Date && b instanceof Date)
return a.getTime() === b.getTime();
if (a instanceof RegExp && b instanceof RegExp)
return a.toString() === b.toString();
// 确保都是对象
if (
typeof a !== 'object' ||
typeof b !== 'object' ||
a === null ||
b === null
)
return false;
// 检查循环引用
const aStamp = seen.get(a as object);
const bStamp = seen.get(b as object);
if (aStamp !== undefined || bStamp !== undefined) {
return aStamp === bStamp;
}
// 标记已访问对象
const stamp = getNextStamp();
seen.set(a as object, stamp);
seen.set(b as object, stamp);
// 获取所有属性(包括不可枚举属性,但排除 Symbol 类型)
const aKeys = Object.getOwnPropertyNames(a);
const bKeys = Object.getOwnPropertyNames(b);
// 检查属性数量是否相同
if (aKeys.length !== bKeys.length) return false;
// 检查属性值是否相同
for (const key of aKeys) {
if (!bKeys.includes(key)) return false;
// @ts-ignore 允许索引访问
if (
!deepEqualInternal(
(a as any)?.[key],
(b as any)?.[key],
seen,
getNextStamp,
currentDepth + 1,
maxDepth,
)
)
return false;
}
return true;
}