npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wecity/mp-sdk

v1.0.4

Published

常用工具方法

Downloads

5

Readme

常用工具方法

数据缓存 storage

示例代码

const sdk = require('@wecity/mp-sdk');

// 设置24小时后过期的缓存
sdk.storage.setStorageSync('info', {
  id: 1,
  type: 'other'
}, 24 * 60 * 60)

// 获取本地缓存
sdk.storage.getStorageSync('info')

// 删除指定 key 的本地缓存
sdk.storage.removeStorageSync('info')

// 清理已过期的本地缓存
sdk.storage.clearExpiredStorage()

API 列表

setStorageSync(key, value, expires) 设置指定 key 的本地缓存

由于小程序官方提供 10m 存储空间,当设备剩余存储空间比设置的 value 空间小的话,

会优先清除已过期的本地缓存再重试,如果存储空间还不够的话,则取消本次存储操作,并在控制台输出相关警告!

参数 | 类型 | 说明 --- | --- | --- key | string | 本地缓存中指定的 key value | object/string | 需要存储的内容 expires? | timestamp | 以秒为单位,默认过期时间 7 天,7 * 24 * 60 * 60

getStorageSync(key) 获取指定 key 的本地缓存

如果当前获取的 key 已过期,则返回null

参数 | 类型 | 说明 --- | --- | --- key | string | 本地缓存中指定的 key

removeStorageSync(key) 删除指定 key 的本地缓存

参数 | 类型 | 说明 --- | --- | --- key | string | 本地缓存中指定的 key

clearExpiredStorage() 清理已过期的本地缓存

加密解密 crypto

示例代码

const sdk = require('@wecity/mp-sdk');

// 加密(AES方法)
sdk.crypto.encryptByAES('未加密的字符串', '0123456789012345') // 密钥 key 必须是十六位字符串

// 解密(AES方法)
sdk.crypto.decryptByAES('已加密的乱码串', '0123456789012345')

API 列表

encryptByAES(word, secretKey) 加密

参数 | 类型 | 说明 --- | --- | --- word | string | 未加密的字符串 secretKey | string | 密钥 key 必须是十六位字符串,推荐用 sessionId 的后16位作为密钥

默认加密解密配置,请联系后端保持一致

{
  "mode": CryptoJS.mode.ECB,
  "padding": CryptoJS.pad.Pkcs7
}

decryptByAES(word, secretKey) 解密

参数 | 类型 | 说明 --- | --- | --- word | string | 已加密的乱码串 secretKey | string | 密钥 key 必须是十六位字符串,推荐用 sessionId 的后16位作为密钥

更多API,请参考 CryptoJS文档CryptoJS 已挂载到 sdk.crypto

事件通讯 event

示例代码

const sdk = require('@wecity/mp-sdk');

// 监听事件
sdk.event.addEventListener('eventName', (res) => {
  console.log(res.type)    // eventName
  console.log(res.target)  // 派发时传入的数据,可以是任意值
  // do something
}, this)

// 监听同个事件,绑定多个 handler
sdk.event.addEventListener('eventName', handler1, this)
sdk.event.addEventListener('eventName', handler2, this)
sdk.event.addEventListener('eventName', handler3, this)

// 触发事件
sdk.event.dispatch('eventName', '派发时传入的数据,可以是任意值')

// 取消监听事件名下 所有 处理函数
sdk.event.removeEventListener('eventName')

// 取消监听事件名下 单个 处理函数
sdk.event.removeSingleEventListener('eventName', handler1, this)

API 列表

addEventListener(type, callback, scope)

参数 | 类型 | 说明 --- | --- | --- type | string | 事件名 callback | function | 事件函数 scope | scope | 作用域

dispatch(type, target) 触发事件

参数 | 类型 | 说明 --- | --- | --- type | string | 事件名 target | any | 传递的数据

removeEventListener(type) 取消监听事件名下 所有 处理函数

参数 | 类型 | 说明 --- | --- | --- type | string | 事件名

removeSingleEventListener(type, callback, scope) 取消监听事件名下 单个 处理函数

参数 | 类型 | 说明 --- | --- | --- type | string | 事件名 callback | function | 事件函数 scope | scope | 作用域

hasEventListener(type, callback, scope) 是否存在当前事件监听的处理函数

参数 | 类型 | 说明 --- | --- | --- type | string | 事件名 callback | function | 事件函数 scope | scope | 作用域

流程创建工具

例如存在场景:查询公积金,首先判断有没有做实名认证,没有的话跳转去做实名认证(实名认证:包括输入姓名/身份证页、人脸页等页面)。实名认证完后,跳转回公积金查询页显示公积金查询结果。

这种情况可以使用流程工具,把实名认证作为一个流程,不与任何业务耦合,执行完后,像函数一样返回结果给到调用方。

示例代码

公积金页

const sdk = require('@wecity/mp-sdk');
// 去做人脸
sdk.flow.createFlow('/pages/faceVerify', options)
  .then((res) => {
    // 人脸完成后,获取公积金结果显示
  }).catch((e)=>{
    // 人脸失败处理
});

人脸页-姓名填写页

const sdk = require('@wecity/mp-sdk');

// 姓名填写完,去人脸页面,需要使用flow提供的跳转api,功能与wx.navigateTo一致
sdk.flow.navigateTo({
  url:"/pages/face",
});

人脸页-实名认证

const sdk = require('@wecity/mp-sdk');

// 实名认证
doface();

// 返回成功结果,失败结果调用sdk.flow.reject({err})
sdk.flow.resolve({
  data
});

API 列表

createFlow(url, options)

参数 | 类型 | 说明 --- | --- | --- url | string | 小程序页面路径 options | object | 传入的参数,如果不需要回到当前调用页面,则传入navigateType=keep

destory()

该 API 用于在流程起始页,onUnload 处用于卸载流程跳转的监听

const sdk = require('@wecity/mp-sdk');

Page({
  onUnload() {
    flow.destory()
  }
})

navigateTo(options)

参数 | 类型 | 说明 --- | --- | --- options | object | 与小程序wx.navigateTo的参数一致

redirectTo(options)

参数 | 类型 | 说明 --- | --- | --- options | object | 与小程序wx.navigateTo的参数一致

resolve(retData)

参数 | 类型 | 说明 --- | --- | --- retData | object/string | 成功的返回结果

reject(errData)

参数 | 类型 | 说明 --- | --- | --- errData | object/string | 失败的返回结果

其他工具方法

deepClone(object) 深度克隆

参数 | 类型 | 说明 --- | --- | --- object | any | 被深度克隆的源

const sdk = require('@wecity/mp-sdk');
const src = {}
const newSrc = sdk.deepClone(src)

console.log(src === newSrc) // false

urlJoinParams(url, params) 拼接 Url 参数

参数 | 类型 | 说明 --- | --- | --- url | string | 网址 params | object | 参数对象,如 { a: 2, b: 3 }

const sdk = require('@wecity/mp-sdk');
const url = sdk.urlJoinParams('https://www.xx.com', {
  a: 2,
  b: 3
})

console.log(url) // https://www.xx.com?a=2&b=3

navigateBack(delta) 返回 N 层页面后再执行

由于微信官方的 navigateBack 函数是调用后立刻执行 success 函数,并没有返回 N 层页面后再执行的功能。

sdk.navigateBack 提供了此功能。

参数 | 类型 | 说明 --- | --- | --- delta | nubmer | 返回页面层数

const sdk = require('@wecity/mp-sdk');

sdk.navigateBack(2)
  .then(() => {
    // 返回成功后再执行
  })

compareVersion(version1, version2) 版本对比

参数 | 类型 | 说明 --- | --- | --- version1 | string | 版本1 version2 | string | 版本2

  • 版本1比版本2大 返回数字类型 1
  • 版本1和版本2相同 返回数字类型 0
  • 版本2比版本1小 返回数字类型 -1
const sdk = require('@wecity/mp-sdk');

console.log(sdk.compareVersion('1.0.2', '1.0.1')) // 1
console.log(sdk.compareVersion('1.0.0', '1.0.0')) // 0
console.log(sdk.compareVersion('1.0.1', '1.0.2')) // -1

hideIdCard(idCard, keepLength) 身份证号码脱敏

参数 | 类型 | 说明 --- | --- | --- idCard | string | 版本1 keepLength | Number | 前后保留的位数

const sdk = require('@wecity/mp-sdk');

console.log(sdk.hideIdCard('400000000123456789', 1)) // 4****************9
console.log(sdk.hideIdCard('400000000123456789', 2)) // 40**************89
console.log(sdk.hideIdCard('400000000123456789', 4)) // 4000**********6789

hideName(name) 姓名脱敏

参数 | 类型 | 说明 --- | --- | --- name | string | 姓名

const sdk = require('@wecity/mp-sdk');

console.log(sdk.hideName('张三')) // 张*
console.log(sdk.hideName('李大安')) // 李**

debounce(callback, wait, immediate) 防抖

参数 | 类型 | 说明 --- | --- | --- callback | Function | 回调函数 wait | Number | 时间窗口的间隔,大约100或250(甚至更高)的值是最有用 immediate | Boolean | 是否立即调用

返回函数连续调用时,空闲时间必须大于或等于 wait,func 才会执行

const sdk = require('@wecity/mp-sdk');

Page({
  onLoad() {
    const doSomeThing = () => console.log('do do do')
    wx.onWindowResize(sdk.debounce(doSomeThing, 200))
  }
})

throttle(callback, delay) 节流

参数 | 类型 | 说明 --- | --- | --- callback | Function | 回调函数 delay | Number | 时间窗口的间隔

返回函数连续调用时,func 执行频率限定为 1 次 / delay

const sdk = require('@wecity/mp-sdk');

Page({
  onLoad() {
    const doSomeThing = () => console.log('do do do')
    wx.onWindowResize(sdk.throttle(doSomeThing, 200))
  }
})