xctc-utils
TypeScript icon, indicating that this package has built-in type declarations

1.6.19 • Public • Published

项目中常用的方法集合

email: dybself@163.com

安装 npm i xctc-utils

更新到最新版本 npm install xctc-utils@latest

项目中引入 import useUtils from "xctc-utils"

缓存操作集合

LocalStorage使用,存取值时数据已经过处理

存储: useUtils.useLocalStorage(key,value)
取值: useUtils.getLocalStorage(key)

sessionStorage使用,存取值时数据已经过处理

存储: useUtils.useSessionStorage(key,value)
取值: useUtils.getSessionStorage(key)

删除缓存

key:string 需要删除的缓存键
all:boolean 为true时删除调用方法的所有缓存,否则只删除当前传入的 key 值
如果 all 为 true 时,方法会优先执行缓存的clear函数, key 传任意值,
删除临时缓存: useUtils.removeSessionStorage(key,all)
删除永久缓存: useUtils.removeLocalStorage(key,all)

地址栏参数处理

设置参数

interface SetOptions {
    url:string, // 完整的 url 地址
    data:string, // 具体需要拼接在地址参数
    keyword:string, // 地址栏参数 key 值
    key?:string, // 参数加密 key
    iv?:string  // 参数加密 iv
}
useUtils.params.set( options:SetOptions )

获取参数

interface GetOptions {
    keys:string[],
    cache?:string, // 是否需要缓存地址栏数据: 不传或传空,则不做处理,session 临时缓存 local永久缓存
    cacheKey?:string, // 缓存数据的键 默认值 urlParamsData
    key?:string, // 参数解密 key 必须与设置参数 key 一致
    iv?:string  // 参数解密 iv 必须与设置参数 iv 一致
}
useUtils.params.get( options:GetOptions )

AES 加密、解密处理函数集合

AES 加密、解密,同一个数据的加密和解密传入的key 和 iv保持一致。

work: 需要加密的对象,如传入的是对象,该方法默认进行JSON序列化处理。
key:16位或者32位字符串作为密钥
iv:16位或者32位字符串作为密钥偏移量
data: encrypt方法加密后返回的数据
加密:useUtils.crypto.encrypt( work:any , key:string , iv:string )
解密:useUtils.crypto.decrypt( data:string , key:string , iv:string )

数据类型判断集合

数据类型判断

useUtils.type.isFunction(val) // 是否是函数
useUtils.type.isObject(val)   // 是否是对象 null 类型为对象,该方法已对null进行过滤
useUtils.type.isDate(val)     // 是否是时间对象
useUtils.type.isNumber(val)   // 是否是number类型
useUtils.type.isString(val)   // 是否是字符串类型
useUtils.type.isBoolean(val)  // 是否是boolean类型
useUtils.type.isWindow(val)    // 是否在浏览器环境下运行

json数据类型判断

useUtils.isJson(data) // 判断传入数据是否是 JSON对象字符串,如果是,返回序列化后的JSON对象,否则返回false

环境判断

获取当前设备环境:设备环境

当前使用设备类型: useUtils.deviceEnvironment() // android ios ,未识别到返回 other 
是否在微信浏览器环境中:  useUtils.weixinBrowser() // true false
是否是手持设备: useUtils.isMobile() // true 移动设备 false PC设备

微信授权等方法集合

微信授权登录

interface configOption {
    appId?:string, // 当前微信服务号 appId
    scope?:string,// 网页授权类型 默认 snsapi_userinfo
    http?:any, // 执行微信登录时,请求后端的接口方法,默认请求数据格式 {"app_id":"","js_code":"" },app_id为当前传入的配置参数,js_code为自动截取。
    codeKey?:string, // 回调地址栏中对 code 进行存储的键, codeKey:code
    stateKey?:string, // 回调地址栏中对 state 进行存储的键 stateKey:state
    cryptoiv?:string, // 将地址栏携带参数加密iv, 必须与 weixinShareInit 方法中的 iv 参数一致
    cryptokey?:string, // 将地址栏携带参数加密key, 必须与 weixinShareInit 方法中的 key 参数一致
    isPassLogin?:boolean, 是否忽略微信跳转登录
    redirectUri?:string, // 回调地址
    appIdKey?:string, // 向后端发起http请求时,app_id对应的键,默认键值:  app_id
    jsCodeKey?:string, // 向后端发起http请求时,js_code对应的键,默认键值:  js_code,完整格式 httpdata = { app_id:"xxxxx",js_code:"xxxxx" }
    debuggerStatus?:boolean,//是否开启 debugger 弹框提示
    cb?:any; // 回调函数
}
进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
useUtils.weixin.getUrlCode(config)

微信config接口权限注入

interface ShareConfig{
    http:any, // 微信接口权限注入接口,后端接口,执行成功后返回数据格式:{appId:"",timestamp:"",noncestr:"",signature:""},成功后自动调取微信 wx.config 接口
    cb?:any, // wx.ready 成功后的回调方法
    appId:string, // 当前微信服务号 appId
    jsApiList?:string[],
}

useUtils.weixin.configReady(config:ShareConfig)

微信分享接口加载

interface ShareOptions{
    title?:string, // 分享标题 默认值 微信分享
    desc?:string, // 分享描述 默认值 微信分享描述
    link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致,默认取当前访问地址
    imgUrl: '', // 分享图标
    data?:any,//微信分享时需要携带的数据,默认传键值格式 {id:1,name:"test",path:"微信分享跳转地址,如: /user/login,好友点击后自动跳转到/user/login页面"},系统自动将该数据保存到缓存中,同时将数据拼接到地址栏,跳转 path 页面后可通过地址栏获取拼接数据
    iv?:string, // 分享链接中对 state 数据加密的iv 必须与 weixinUrlCode 方法中的 cryptoiv 参数一致
    key?:string, // 分享链接中对 state 数据加密的 key 必须与 weixinUrlCode 方法中的 cryptokey 参数一致
}
useUtils.weixin.shareReady(config:ShareOptions)

唤起微信支付

interface ConfigPay{
    timeStamp:string, // 支付签名时间戳
    nonceStr:string,  // 支付签名随机串,不长于 32 位
    package:string, // 统一支付接口返回的prepay_id参数值
    signType:string, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
    paySign:string,// 支付签名
    cb?:any,//回调
}
useUtils.weixin.onlinePay(config:ConfigPay)

时间相关方法

获取当前时间的时间戳

useUtils.date.getTime() // 返回秒

时间戳转任意格式时间字符串

/**
 * 
 * @param num 传入的时间戳(秒):如 1675667655
 * @param format 解析后展示时间字符串格式,默认 "YYYY-MM-DD HH:mm:ss" 格式, 可传格式如: YYYY年MM月DD日 HH时 YYYY/MM/DD HH
 * @returns 
 */
useUtils.date.formatTimeStamp(num,format) // 返回 format 格式时间戳

时间字符串转时间戳

/**
 * 
 * @param str 字符串格式必须为: 2017/03/03 12:23:55 格式,避免兼容性,不能传 “-” 分割的时间字符串 2017-03-03
 * @returns 
 */
useUtils.date.formatStrTime(str) // 返回时间戳 秒

DOM处理集合

获取ID元素

 useUtils.$id("domid") // 如果存在当前dom元素,则返回dom对象,否则返回false

ueditor 富文本操作函数集合

计算当前富文本内容高度

var option = {
    id:"ueditor" , // 富文本DOM容器 id 默认不传
    content:"" , // ueditor  富文本内容
}
// 返回的富文本内容包含input dom元素,input元素的content-data-height属性计算富文本高度
// <input type="hidden" content-data-height="${height}" id="ueditorHeight" />
useUtils.checkIframeContentHeight(option) 

获取指定富文本内容高度

// 返回富文本元素所占页面高度,默认返回 0 
useUtils.getIframeContentHeight("ueditor")

其它处理函数集合

通过身份证号码的出生年月日,获取出生日期

// 传入身份证号码,返回出生具体时间,默认返回空
正确返回: {
    year:year,
    month:month,
    day:day,
    hour:hour,
    minute:minute,
    second:second,
    num:nowNumber // 具体时间戳
}
useUtils.findIdcardAge(idcard)

剪贴板操作集合

将数据复制到剪贴板函数

useUtils.handleCopyValue(val)

Package Sidebar

Install

npm i xctc-utils

Weekly Downloads

362

Version

1.6.19

License

ISC

Unpacked Size

67.4 kB

Total Files

30

Last publish

Collaborators

  • dyb_website