UNPKG

utils94

Version:

日常开发使用工具库

430 lines (417 loc) 13.9 kB
import Cookies from 'js-cookie'; interface JsConfig { debug?: boolean; appId: string | number; timestamp: number | string; nonceStr: string; signature: string; jsApiList: string[]; } interface ShareConfig { title: string; desc: string; link?: string; imgUrl: string; } declare class WeChat { private WeChatJsSdk; private shareConfig; private getJsSdk; private iosSdkStatus; constructor(WeChatJsSdk: any, shareConfig: ShareConfig[], getJsSdk: Promise<JsConfig>); /** * 使用微信js api的前置条件 * 所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用 * 同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web app的页面会导致签名失败,此问题会在Android6.2中修复 * @ios 在ios中,初始配置一次之后即可通用使用(ios 中请求的url需要初次进入的url来获取签名,否则会出错) * @android 在安卓中,需要在每次路由变化时重新配置 */ pre(): Promise<any>; /** * 微信sdk 分享配置(聊天 朋友圈 qq qq空间) * config中,若link 并不存在,即自动将当前url 贴上去 * @params config: ShareConfig[] | ShareConfig * 当为数组时, * array[0]: 朋友分享内容 * array[1]: 朋友圈分享内容 array[1] 为空则默认array[0]为朋友圈分享内容 * @params filter string[] url上可过滤的字段 */ share(config?: ShareConfig[], filter?: string[]): void; /** * 自动配置分享.预检sdk config后,自动配置分享内容. * 相当于 this.pre().then(()=> this.share()) * @param config 分享配置 * @param filter url上过滤字段 * @return Promise 返回一个promise 可接续在后面添加其他处理 */ autoShare(config?: ShareConfig[], filter?: string[]): Promise<any>; } interface ListenOptions { capture?: boolean; once?: boolean; passive?: boolean; } /** * 开始事件监听 * @param element 监听对象 * @param event 监听事件 * @param handler 监听执行函数 * @param config 监听配置, 默认 false, 且开启passive(当执行函数内部执行preventDefault时关闭passive). 可传对象参数 * @return function 取消监听函数 */ declare function on(element: HTMLElement | any, event: Event | string, handler: Function, config?: ListenOptions | boolean): () => void; /** * 移除事件监听 * @param element 监听对象 * @param event 监听事件 * @param handler 监听执行函数 * @param config 监听配置, 默认 false, 且开启passive(当执行函数内部执行preventDefault时关闭passive). 可传对象参数 */ declare function off(element: HTMLElement | any, event: Event | string, handler: Function, config?: ListenOptions | boolean): void; declare const dom: { on: typeof on; off: typeof off; }; declare enum LOOP_STATUS { CONTINUE = 0, FINISH = 1 } /** * 返回对象类型, 首字母大写 * @param variable any * @return String 'Object'|'Boolean'|'Number'|'String'|'Undefined'|'Null'|'Array'|'Function'|'Symbol' | 'Map' | 'Set' */ type VarType = 'Object' | 'Boolean' | 'Number' | 'String' | 'Undefined' | 'Null' | 'Array' | 'Function' | 'Symbol' | 'Map' | 'Set'; declare function getVarType(variable: any): VarType; /** * 等分切割数组 * * @static * @param {*} arr 数组 * @param {*} limit 份数 * @returns */ declare function sliceArray(arr: any[], limit: number): any[]; /** * 过滤url search 中的字符串 * @param url * @param keys */ declare function filterUrlSearch(url: string, keys?: string[]): string; /** * 图片转化base64 * @param img 图片dom */ declare function imageToBase64(img: HTMLElement | any): string; /** * 获取图片的base64 * @param src 图片地址 */ declare function getBase64Img(src: string): Promise<any>; /** * guid 生成 * @returns guid */ declare function guid(): string; /** * 数组拍平 * */ declare function flatten(arr: any[]): any[]; /** * 数据格式化 * @param data 数据 * @return parse后的数据 */ declare function jsonParse(data: any): any; /** * 将非string类型数据 json化 不然无法存储本地 * @param data 数据 * @return 字符串数据 */ declare function toString(data: any): string; /** * 补0操作 * @param num * @return sting */ declare function fillZero(num: number): string; /** * 深度克隆 * @param origin 源数据 * @param hash WeekMap(optional) * @returns 深度克隆对象 */ declare function deepClone(data: any, hash?: WeakMap<WeakKey, any>): any; /** * 通过key找值 * @param obj 数据 * @param key key ; 支持点语法, 支持数组选择 * @param isDeepClone boolean 是否支持深度克隆;与传入数据的引用 解耦 * @return value:any 未找到返回undefined */ declare function getValueByKey(obj: any, key: string, isDeepClone?: boolean): any; /** * 设置对象的值,直接修改obj的元数据 * @param obj 数据,也支持Vue的Ref对象 [{value: any, prop: string, [propName: string]: any}] * @param source 数据源 */ declare function setValue(obj: { value: any; prop: string; parse: (v: any) => any; [propName: string]: any; }[] | { value: any; prop: string; parse: (v: any) => any; [propName: string]: any; }, source: any): void; /** * 重置对象的值 * * @param obj 要重置的对象 * @returns 重置后的新对象 * @description * 此函数会创建一个新对象,其结构与输入对象相同,但值被重置: * - 数组被重置为空数组 * - 对象被递归重置 * - 字符串被重置为空字符串 * - 其他类型被重置为 undefined */ declare function resetObject(obj: any): any; /** * 顺序执行队列中的任务,执行完成后tasks将被清空 * * @param tasks 任务数组,每个元素都是一个返回 Promise 的函数 * @returns Promise<void> * * @description * 此函数会按顺序执行传入的任务数组。 * 每个任务都会等待前一个任务完成后才开始执行。 * 如果任何任务抛出错误,后续任务将不会执行,错误会被抛出。 * * @example * const tasks = [ * async () => { await someAsyncOperation() }, * async () => { await anotherAsyncOperation() } * ]; * await executeAsyncQueue(tasks); */ declare function executeAsyncQueue(tasks: any[]): Promise<void>; /** * 周期性执行异步回调函数,直到其返回 true 或达到超时时间。 * * @param cb - 一个返回 Promise<boolean> 的异步函数。返回 true 表示任务完成。 * @param ms - 每次尝试之间的等待时间(毫秒),默认值为 400ms。 * @param timeout - 最大等待时间(毫秒),可选。若超时则抛出错误。 * @returns 一个 Promise<boolean>,当 cb 返回 true 时 resolve,若超时则 reject。 */ declare function waitResult(cb: () => Promise<boolean>, ms?: number, timeout?: number): Promise<boolean>; /** * sleep函数 * @param ms 毫秒 ,默认5000 * @returns Promise<void> */ declare function sleep(ms?: number): Promise<void>; /** * 轮询函数,可配置间隔和间隔轮询次数 * @param cb 执行函数,必须为 promise<{status:LOOP_STATUS, result:any}> * @param timeConfig 执行间隔+次数,传二维数组[ms, count][],ex: [[20:1],[3:10], [1:Infinity]] * @param timeout 超时时间,超过即停止 reject * @returns 返回 cb 的 result */ declare function polling(cb: () => Promise<{ status: LOOP_STATUS; result: any; }>, timeConfig: number[][], timeout?: number): Promise<any>; declare const feature: { getVarType: typeof getVarType; sliceArray: typeof sliceArray; filterUrlSearch: typeof filterUrlSearch; getBase64Img: typeof getBase64Img; imageToBase64: typeof imageToBase64; guid: typeof guid; flatten: typeof flatten; jsonParse: typeof jsonParse; toString: typeof toString; fillZero: typeof fillZero; getValueByKey: typeof getValueByKey; setValue: typeof setValue; deepClone: typeof deepClone; resetObject: typeof resetObject; executeAsyncQueue: typeof executeAsyncQueue; polling: typeof polling; sleep: typeof sleep; waitResult: typeof waitResult; }; declare function isIos(): boolean; declare function isAndroid(): boolean; declare function isWxApp(): boolean; declare function isMobile(): boolean; declare function isPc(): boolean; declare function isWindows(): boolean; declare function isMac(): boolean; declare const platform: { isWxApp: typeof isWxApp; isIos: typeof isIos; isAndroid: typeof isAndroid; isPc: typeof isPc; isMobile: typeof isMobile; isWindows: typeof isWindows; isMac: typeof isMac; }; declare class BrowserStorage { target: any; constructor(target: any); /** * 获取数据 * @param key 变量名 * @return 格式化后的数据 */ get(key: string): any; /** * 设置数据 * @param key 变量名 * @param value 初始数据:非string类型数据均要json转化 */ set(key: string, value: any): void; /** * 移除数据 * @param key 变量名 */ remove(key: string): void; /** * 清除数据 */ clear(): void; } declare const LocalStorage: BrowserStorage; declare const SessionStorage: BrowserStorage; declare const CookieStorage: { /** * 获取数据 * @param key 变量名 * @return 格式化后的数据 */ get: (key: string) => any; /** * 设置数据 * @param key 变量名 * @param value 初始数据:非string类型数据均要json转化 */ set: (key: string, value: any, option?: Cookies.CookieAttributes) => void; /** * 移除数据 * @param key 变量名 */ remove: (key: string, option?: Cookies.CookieAttributes) => void; }; declare const storage: { BrowserStorage: typeof BrowserStorage; LocalStorage: BrowserStorage; SessionStorage: BrowserStorage; CookieStorage: { /** * 获取数据 * @param key 变量名 * @return 格式化后的数据 */ get: (key: string) => any; /** * 设置数据 * @param key 变量名 * @param value 初始数据:非string类型数据均要json转化 */ set: (key: string, value: any, option?: Cookies.CookieAttributes) => void; /** * 移除数据 * @param key 变量名 */ remove: (key: string, option?: Cookies.CookieAttributes) => void; }; }; interface Day { year: number; month: number; day: number; text: string; week: number; current: boolean; isToday: boolean; source: Date; data?: any; } interface DayData { [key: string]: any; } interface CreateMonthArguments { date?: Date | number | string | undefined; data?: DayData; weekStart?: number; isSliceByWeek?: boolean; } declare function getYMDW(date: any): { year: any; month: any; day: any; week: any; }; /** * 首补足 * @param week 周几 * @param weekStart 周起始 */ declare function getStartMend(week: number, weekStart: number): number; /** * 尾补足 * @param week 周几 * @param weekStart 周起始 */ declare function getEndMend(week: number, weekStart: number): number; /** * 返回月份数据 * @param date Date 对象 或 可被new Date对象解析; * @param data 在对应日期中插入数据,以时间为key的对象;{'2022/01/01': {}} * @param weekStart number 周开始 0 - 6 ,默认1 从周一开始; 0 代表星期日,1 代表星期一,2 代表星期二,依次类推。 * @return Day[][] 二维数组 || Day[] 一维数组 */ declare function createMonth({ date, data, weekStart, isSliceByWeek }?: CreateMonthArguments): Day[][] | Day[]; /** * 检测时间是否重叠 * @param arr dateBeginEnd[] 数组对象,ex: [{s:1,e:2}] * @returns boolean * @description * 基本的思路,日期也可以当成字符串进行比较,把开始日期,结束日期分别存进两个数组,并用sort排序,循环遍历数组, * 从开始时间的第二个元素去比较结束时间的第一个元素, * 如果小于,就代表时间段有交叉,直接跳出,不然就继续遍历,遍历结束,说明时间没有重复,可以提交。 */ interface dateBeginEnd { s: number; e: number; } declare function checkOverlap(arr: dateBeginEnd[]): boolean; declare const date: { createMonth: typeof createMonth; checkOverlap: typeof checkOverlap; getYMDW: typeof getYMDW; getStartMend: typeof getStartMend; getEndMend: typeof getEndMend; }; /** * 复制文字 * @param {*} string 需要复制的文字 string * @return Boolean 值, true 则为复制成功, false 失败 */ declare function copy(text: string): Promise<boolean>; /** * 设置当前页面 title * @param title string */ declare function setWindowTitle(title: string): void; declare const bom: { copy: typeof copy; setWindowTitle: typeof setWindowTitle; }; export { CookieStorage, LOOP_STATUS, LocalStorage, SessionStorage, WeChat, bom, checkOverlap, copy, createMonth, date, deepClone, dom, executeAsyncQueue, feature, fillZero, filterUrlSearch, flatten, getBase64Img, getEndMend, getStartMend, getValueByKey, getVarType, getYMDW, guid, imageToBase64, isAndroid, isIos, isMac, isMobile, isPc, isWindows, isWxApp, jsonParse, off, on, platform, polling, resetObject, setValue, setWindowTitle, sleep, sliceArray, storage, toString, waitResult }; export type { CreateMonthArguments, Day, DayData, JsConfig, ListenOptions, ShareConfig, VarType, dateBeginEnd };