2.2.7 • Published 8 months ago

utils94 v2.2.7

Weekly downloads
27
License
MIT
Repository
-
Last release
8 months ago

utils94(工具类函数集结)

工具类功能
WeChat微信端jssdk处理
domdom 相关
bombom 相关
date时间相关(日历生成)
feature功能
storage缓存
platform平台判断

快速使用

yarn add utils94 

import Utils from 'utils94'

import { WeChat, dom, bom... } from 'utils94'

1. WeChat

  • new WeChat(WeChatJsSdk, {title,desc,link,imgUrl},{title,link,imgUrl}, requset) 初始化

    import {WeChat} from 'utils94' 
    
    // 朋友,QQ分享内容
    const friendShareConfig = {
      title: '微信分享title',
      desc: '详细',
      link: window.location.href,
      imgUrl:''
    }
    // 朋友圈,QQ空间分享内容
    const momentShareConfig = {
      title: '', // 分享标题
      link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: '', // 分享图标
    }
    
    const request =async (body) => {
      /** 
        * !划重点!!!!
        * ios中,url 必须是初次进入的url. 且ios 仅需配置一次即可.
        * android中, 需要每次进入新的页面都需要配置一次. 每次配置
        * url 需要过滤 #及之后的参数
      **/
      const {url, jsApiList} = body
      const config = await api.initJsSdk(body)
      /**  config:{
            debug?: boolean; // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
            appId: string | number; // 必填,公众号的唯一标识
            timestamp: number | string ; // 必填,生成签名的时间戳
            nonceStr: string; // 必填,生成签名的随机串
            signature: string;// 必填,签名
            jsApiList: string[] // 必填,需要使用的JS接口列表 
          }
      **/
      return config
    }
    
    // 初始化配置(必须)
    const wx = new WeChat(
      WxChatJsSdk, // 微信js 
      [
        friendShareConfig,
        momentShareConfig
      ],
      request
    )
  • pre 前置预检,

  • 返回Promise

    wx.pre().then(res => {
      // 配置成功
      // dosomething
    }).catch(err=>{
      // 配置失败
    })
  • share( {title,desc,link,imgUrl},{title,link,imgUrl}, filter:string[]) 分享设置,filter可过滤掉link上携带的query字段,如‘token’;

  • 无返回

    wx.pre().then(()=> {
      // 当momentShareConfig 不传时,默认 friendShareConfig 替代
      wx.share([friendShareConfig, momentShareConfig], ['token'])
    })
  • autoShare( {title,desc,link,imgUrl},{title,link,imgUrl}, filter:string[]) 自动分享设置,filter可过滤掉link上携带的query字段,如‘token’;

  • 返回Promise

    // 当momentShareConfig 不传时,默认 friendShareConfig 替代
    wx.autoShare([friendShareConfig, momentShareConfig], ['token']).then(()=> {
      // dosomething
    })

2. dom

  • on(element, event, function, object | boolean) 事件监听

  • 返回 监听函数

    关于passive 的情况,点击了解

    import { dom } from 'utils94'
    // ! 自动检查并开启passive 优化
    
    // 事件监听
    dom.on(
      window, 
      'scroll', 
      e => { /* dosomething */},
      {
        capture: false, // boolean 表示 listener 会在该类型的事件捕获阶段传播到该 EventTarget 时触发。
        once: false,   // boolean 表示 listener 在添加之后最多只调用一次。如果是 true, listener 会在其被调用之后自动移除。
        passive: true  // boolean 设置为true时,表示 listener 永远不会调用 preventDefault()。如果 listener 仍然调用了这个函数,客户端将会忽略它并抛出一个控制台警告。
      }
    )
    
    dom.on(
     window,
      'scroll',
      e => { /* dosomething */},
     false  // false: 冒泡 默认; true:捕获
    )
  • off(element, event, function, object | boolean) 移除事件监听

  • 返回 移除监听函数

    import { dom } from 'utils94'
    
    // 移除事件监听
    dom.off(
     window,
      'scroll',
      e => { /* dosomething */},
      {
        capture: false, 
        once: false, 
        passive: true
      }
    )
    
    dom.off(
     window,
      'scroll',
      e => { /* dosomething */},
     false
    )

3. bom

  • copy(text:string) 复制dom中的文字

  • 返回 boolean,true 成功;false 失败

4. date

  • createMonth(date?:Date | string | number = Date.now(),weekStart?: number = 1 ) 不传默认当前机器时间月份, weekStart: 1 默认周一开始

  • 返回二维对象数组,以周为单位分割

    import { date } from 'utils94'
    const arr = date.createMonth()
    /* 返回 
    [
      [
        {
          "date": "2020-12-27T16:00:00.000Z",  // Date 对象
          "data": {               // 数据
            "day": "2020/12/28",        // 当前日期
            "week": 1,             // 周几 
            "current": false          // 是否为当前月数据
          }
        }
        ...
      ],
      ...
    ]
    */
  • checkOverlap(add:{s:number, e:number}[]):boolean 检测时间是否重叠

5. feature

函数功能
getVarType(var:any) : string获取变量类型
sliceArray(arr:any[], limit: number) : any[any[]]等分切割数组
filterUrlSearch(url:string, keys: string[]) : string过滤url search 中的字符串
resetObject(obj: any): any重置对象的值
getBase64Img(url: string): Promise图片地址转base64
imageToBase64(img: HTMLElement): string图片标签转化base64
guid():stringguid生成
flatten(arr: any[]): any[]数组拍平
jsonParse(data: any): any数据格式化,将被转为string的数据 parse出来
toString(data: any) : string将数据stringfy
fillZero(num: number | string) :string前置补0
deepClone(origin: any)深度克隆
getValueByKey(row:any, key: string)通过key找值,支持数组下标
setValue(obj: {prop:string,value:string}[] | Ref( {prop:string,value:string}[]), source:any ):void设置对象的值,直接修改obj的元数据
executeAsyncQueue(tasks: any[]): Promise顺序执行队列中的任务,执行完成后tasks将被清空

6. storage

工具类功能
LocalStoragewindow.localStorage 的 get, set, remove,clear
SessionStoragewindow.sessionStorage 的 get, set, remove,clear
CookieStoragejs-cookie的 get,set, remove
import { LocalStorage, SessionStorage, CookieStorage } from 'utils94/storage'
LocalStorage.set('test', {a:1})  // test '{"a":1}'
LocalStorage.get('test' )  // {a: 1}
LocalStorage.remove('test') // void
LocalStorage.clear() // void

// 其他两个同上使用,只不过CookieStorage 支持传递配置项,且没有clear函数

7. platform

| 函数                | 功能           |
| ------------------- | -------------- |
| isWxApp():boolean   | 是否为微信环境 |
| isIos():boolean     | 是否为ios      |
| isAndroid():boolean | 是否为android  |
| isPc():boolean      | 是否为pc       |
| isMobile():boolean  | 是否为mobile   |
| isWindows():boolean  | 是否为windows   |
| isMac():boolean  | 是否为mac   |
2.2.7

8 months ago

2.2.6

9 months ago

2.2.5

10 months ago

2.2.1

2 years ago

2.2.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.4

2 years ago

2.1.69

2 years ago

2.1.67

2 years ago

2.1.68

2 years ago

2.1.65

2 years ago

2.1.66

2 years ago

2.1.72

2 years ago

2.1.70

2 years ago

2.1.71

2 years ago

2.1.49

2 years ago

2.1.47

2 years ago

2.1.48

2 years ago

2.1.45

2 years ago

2.1.46

2 years ago

2.1.44

2 years ago

2.1.58

2 years ago

2.1.56

2 years ago

2.1.54

2 years ago

2.1.55

2 years ago

2.1.53

2 years ago

2.1.50

2 years ago

2.1.51

2 years ago

2.1.63

2 years ago

2.1.64

2 years ago

2.1.61

2 years ago

2.1.62

2 years ago

2.1.4

2 years ago

2.1.43

2 years ago

2.1.41

2 years ago

2.1.42

2 years ago

2.1.421

2 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.4

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.10

5 years ago

0.0.11

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago