ipink-util
Version:
util.js
262 lines (261 loc) • 8.06 kB
TypeScript
export interface IErrorBase {
errMsg?: string;
errCode?: string;
}
export interface ISuccessBase extends IErrorBase {
ok: boolean;
msg?: string;
}
/**
* @desc Base64 编码
* @param str { string }
* @return: {string}
*/
export declare function base64encode(str: string): string;
/**
* @desc Base64 解码
* @param str { string }
* @return: {string}
*/
export declare function base64decode(str: string): string;
/**
* @description 返回上一级
* @param delta { number } 返回的层级数量
*/
export declare function navigateBack(delta?: number): void;
/**
* @description 对比版本
* @param v1 { string } 新版本
* @param v2 { string } 旧版本
* @return 0:版本一致,1: 版本过旧,-1:版本过新
*/
export declare const compareVersion: (v1: string, v2: string) => -1 | 0 | 1;
/**
* @desc 生成GUID
* @return: { string }
*/
export declare function uuid(sign?: string): string;
/**
* @desc 填充字符串
* @param target 源字符串 { type }
* @param fill 需要填充的字符 { type }
* @param len 填充长度 { type }
* @return: (0.1, 0, 2) => "0.100"
*/
export declare function fillStr(target: string | number, fill: string, len: number): string;
/**
* @desc 转换数字格式 11000 -> 11K(en) -> 1万1(cn) -> 11,000(sp)
* @param num { number } 目标数字
* @param type { "en" | "cn" | "sp" } 目标数字
* @param fixed { number } 小数部分长度
* @return:
*/
export declare const converNumber: (num: number | string, type: "en" | "cn" | "sp", fixed?: number) => string;
/** @desc 首字母大写 **/
export declare const firstCharCase: (str: string) => string;
/**
* @desc 从数组中找到目标,并插入到数组最前方
* @param arr 目标数组 { T[] }
* @param target 查找对象 { T[] }
* @param options 扩展
* @param options.key 如果是对象,需要提供唯一 key { keyof T }
* @param options.type push | unshift { push | unshift }
* @return: { T[] }
*/
export declare function findTargetAndRemove<T>(arr: T[], target: T, options: {
key?: string;
type?: "push" | "unshift";
}): T[];
/**
* @desc Promise.all 解决ALl catch 中断
* @param param { type }
* @return:
*/
export declare function allSettled<T, E>(promiseList: Promise<T | E>[]): Promise<(Awaited<T> | Awaited<E>)[]>;
/**
* @desc 防抖, 一定时间范围被只执行最后一次
* @param func { Function }
* @param time { number } ms
* @return:
*/
export declare function debounce(func: Function, time: number, immediate?: boolean): Function;
/**
* @desc 节流, 一定范围内只执行第一次
* @param func { Function }
* @param time { number } ms
* @return:
*/
export declare function throttle(func: Function, time: number): (...args: any[]) => void;
export type CopyOptionsType = {
/** @desc 复制成功提示, 不传不提示 **/
tip: string;
success?: Function;
fail?: Function;
};
/**
* @description 复制文本内容
* @param val { string } 复制文本内容
* @param options { CopyOptionsType } 可选内容
* @param options.tip { string } 复制成功提示文本,不传则不提示
* @param options.success { Function } 复制成功回调
* @param options.fail { Function } 复制失败回调
*/
export declare function copyValue(val: string, options?: CopyOptionsType): void;
/** @desc key 是否在对象上 **/
export declare function hasKey(obj: {
[propName: string]: any;
}, key: string): boolean;
/**
* 设置页面title
* @param title
* 如果title被base64编码且需要解析,页面query需要拼上 base64=1
* @example `
* import { base64encode, jump } from "ipink-util"
* const title = base64encode("呵呵")
* jump("/pages/home/index?title=" + title + "&base64=1");
*
* 当前页面的url: localhost:8080/app/#/pages/home/index?title=dsadqweqeqweqwe&base64=1
* setTitleName(title) => "呵呵"
* 当前页面的url: localhost:8080/app/#/pages/home/index?title=dsadqweqeqweqwe
* setTitleName(title) => "dsadqweqeqweqwe"
* `
*/
export declare const setTitleName: (title?: string) => void;
/**
* 字符脱敏
* @param str
* @param begin 从第几位开始
* @param end 倒数从第几位结束
*/
export declare const desensitization: (str: string, begin?: number, end?: number) => string;
interface IGetRefInfoOption {
size?: boolean;
rect?: boolean;
scrollOffset?: boolean;
}
/***
* 在UniApp中 获取元素的布局信息
* @param {*} ref : #id .class tag
* @return {*} that : this
* @return { IGetRefInfoOption } options
*/
export declare const getRefInfo: (ref: string, that: any, options?: IGetRefInfoOption) => Promise<unknown>;
interface IGetLocationOption {
success?: Function;
fail?: Function;
complete?: Function;
/** @deprecated 已废弃 **/
callback?: Function;
isToast?: boolean;
/**
* 使用后今日不再掉用成功
*/
isUseRefuseTime?: boolean;
/**
* 初始化WxJsSdk的函数, 可以将初始化微信jssdk的逻辑写在该函数内
*/
initWxSdkCallback?: () => Promise<any>;
}
/**
* 获取经纬度
* @param { IGetLocationOption } options
*/
export declare const getLocation: (options: IGetLocationOption) => Promise<unknown>;
/**
* 计算两个坐标之间的距离, 单位 米(m)
* @param {number} lng1 坐标经度1
* @param {number} lat1 坐标纬度1
* @param {number} lng2 坐标经度2
* @param {number} lat2 坐标纬度2
* @example `
* const result = getGreatCircleDistance(116.2317, 39.5427, 116.1127, 38.1456)
* console.log(result) //155866.2671
* `
*
*/
export declare function getGreatCircleDistance(lng1: number, lat1: number, lng2: number, lat2: number): number;
interface IPostMessageOption {
/**
* 自己定义的通信时间标识
*/
type: string;
/**
* 通信传递参数
*/
params: AnyObject;
success: (data: IPostMessageResult) => void;
fail: (err: IErrorBase) => void;
complete: (data: IPostMessageResult | IErrorBase) => void;
}
interface IPostMessageResult extends ISuccessBase {
}
/**
* @desc h5 与 父级应用通信
* @param { Object } options
* params.type 事件类型
* params.success
* params.fail
* params.options Data
*/
export declare function postMessage(options: IPostMessageOption): Promise<IPostMessageResult | IErrorBase>;
/**
* webview内返回主应用首页
* @param delta { number }
*/
export declare const backMainAppHome: (delta?: number) => boolean;
/**
* uniapp 检查白屏
* @param nextTick { Vue.nextTick }
* @param ref
* @example `
* const ctx = getCurrentInstance()?.proxy
* checkWhiteScreen(ctx, ".content")
*
* checkWhiteScreen(this, ".content")
* `
*/
export declare const checkWhiteScreen: (that: any, ref?: string) => void;
interface IScanCodeOption {
success?: (opt: {
code: string;
}) => void;
/**
* 小程序webview扫码完成调用api指定key为【cacheKey】存储到云端,webview找时机使用【cacheKey】去云端取值
*/
cacheKey?: string;
/**
* 微信公众号该参数比传
*/
jsCodeInfo?: {};
}
export declare const BAR_CODE_TYPE: string[];
/**
* 扫码
* @param options { IScanCodeOption }
*/
export declare const scanCode: (options: IScanCodeOption) => Promise<unknown>;
/**
* 数组扁平化 多维转一维
* @param {T[]} arr
*/
export declare function flattenArray<T>(arr: any[]): T[];
export type FormatTArray<T> = T & {
children: T[];
};
/**
* 将一位数组根据指定的key(例如pid), 进行分组为子父级结构
* 类似函数 formatTree, 如果对id也自定义可以使用 formatTree
* @param {T[]} arr
* @param {String} key = "pid"
*/
export declare function buildHierarchyArray<T extends {
id: string | number;
}>(arr: T[], key: keyof T): FormatTArray<T>[];
/**
* 格式化二级数据 【1,2】 =》 【【】】
* 类似函数 formatTree
* @param param { type }
* @return:
*/
export declare function formatTree<T>(nodeList: T[], childKey: string, parentKey: string): FormatTArray<T>[];
export {};