This commit is contained in:
HongRun Liu
2026-05-26 15:52:30 +08:00
commit b0744c8ab6
849 changed files with 130587 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
/**
* @desc 函数防抖
* @param func 目标函数
* @param wait 延迟执行毫秒数
* @param immediate true - 立即执行, false - 延迟执行
*/
let timeout = null;
export const debounce = function (func, wait = 500, immediate = true) {
// 清除定时器
if (timeout !== null) clearTimeout(timeout);
// 立即执行,此类情况一般用不到
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(function() {
timeout = null;
}, wait);
if (callNow) typeof func === 'function' && func();
} else {
// 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
timeout = setTimeout(function() {
console.log(typeof func)
typeof func === 'function' && func();
}, wait);
}
}
/**
* @desc 函数节流
* @param func 函数
* @param wait 延迟执行毫秒数
* @param type 1 使用表时间戳,在时间段开始的时候触发 2 使用表定时器,在时间段结束的时候触发
*/
export const throttle = (func, wait = 1000, type = 1) => {
let previous = 0;
let timeout;
return function () {
let context = this;
let args = arguments;
if (type === 1) {
let now = Date.now();
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
} else if (type === 2) {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
}
+22
View File
@@ -0,0 +1,22 @@
export function go(url, params={}) {
// 将对象转换为查询字符串
params = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
uni.navigateTo({
url: url + '?' + params
})
}
export function redirectTo(url, params={}) {
// 将对象转换为查询字符串
params = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
uni.redirectTo({
url: url + '?' + params
})
}
export function toast(msg) {
uni.showToast({
title: msg,
icon: 'none',
duration: 1000
})
}
+34
View File
@@ -0,0 +1,34 @@
// sheep/utils/textUtils.js
export function measureTextWidth(text, fontSize = 14, fontFamily = 'sans-serif') {
// 钉钉小程序没有 uni.createCanvasContext 方法
if (typeof uni === 'undefined' || typeof uni.createCanvasContext !== 'function') {
return estimateTextWidth(text, fontSize);
}
try {
const ctx = uni.createCanvasContext('tempCanvasForText');
ctx.setFontSize(fontSize);
ctx.font = `${fontSize}px ${fontFamily}`;
const metrics = ctx.measureText(text);
return metrics.width;
} catch (e) {
// 某些平台可能不支持 measureText,降级使用估算
return estimateTextWidth(text, fontSize);
}
}
// 简单估算中文和英文字符宽度
function estimateTextWidth(text, fontSize = 14) {
let width = 0;
for (let i = 0; i < text.length; i++) {
const charCode = text.charCodeAt(i);
if (charCode >= 0x4e00 && charCode <= 0x9fff) {
// 中文字符
width += fontSize;
} else {
// 英文字符
width += fontSize * 0.5;
}
}
return width;
}