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

@yyjeffrey/wxutil

v1.2.0

Published

WeChat Mini-Program High-frequency API toolkit - 一款使用 promise 封装的小程序高频 API 工具库,已兼容 uni-app

Downloads

11

Readme

wxutil

一款使用 promise 封装的小程序高频 API 工具库,已兼容 uni-app

build license

快速上手

步骤一:使用命令 npm i @yyjeffrey/wxutil 安装 wxutil,或直接下载引入 wxutil.js 文件

步骤二:在使用处引入 wxutil

// 引入wxutil模块
import wxutil from './miniprogram_npm/@yyjeffrey/wxutil/index'

工具模块

网络请求

封装了微信小程序 wx.request() 实现 http 请求

get

直接调用 url 请求数据

wxutil.request.get(url).then(res => {
  console.log(res)
})

调用 url 并填写参数请求数据

wxutil.request
  .get(url, (data = {}), (header = {}))
  .then(res => {
    // 业务处理
    console.log(res)
  })
  .catch(error => {
    // 异常处理
    console.log(error)
  })

post

wxutil.request.post(url, (data = {}), (header = {})).then(res => {
  console.log(res)
})

put

wxutil.request.put(url, (data = {}), (header = {})).then(res => {
  console.log(res)
})

delete

wxutil.request.delete(url, (data = {}), (header = {})).then(res => {
  console.log(res)
})

全局 header

注:可以在 app.js 编写 getHeader() 函数并返回 header 对象,可以实现全局请求头功能

// app.js
// 全局请求带上Token令牌的示例代码
getHeader() {
    const userDetail = wxutil.getStorage('userDetail')
    let header = {}
    if (userDetail) {
        header['Authorization'] = 'Token ' + userDetail.token
    }
    return header
}

文件请求

封装微信小程序 wx.downloadFile()wx.uploadFile()

download

wxutil.file.download({ url }).then(res => {
  console.log(res)
})

upload

wxutil.file
  .upload({
    url: url,
    fileKey: fileKey,
    filePath: filePath,
    data: {},
    header: {}
  })
  .then(res => {
    console.log(res)
  })

socket

封装微信小程序的 websocket 部分方法,实现 socket 流程如下

// socket连接标识
let socketOpen = false
wxutil.socket.connect(url)

// 监听socket通信
wx.onSocketMessage(res => {
  console.log(res)
})

wx.onSocketOpen(() => {
  socketOpen = true
  if (socketOpen) {
    // 发送socket消息
    wxutil.socket.send('hello wxutil').then(res => {
      console.log(res)
    })
  }

  // 关闭socket连接
  wxutil.socket.close(url)
})

图片操作

封装微信小程序的 wx.saveImageToPhotosAlbum()wx.previewImage()wx.chooseImage(),用于保存图片到本机相册、预览图片以及从相机或相册选择图片

save

wxutil.image.save(path).then(res => {
  console.log(res)
})

preview

wxutil.image.preview(['imageUrl'])

choose

参数:count, sourceType

wxutil.image.choose(1).then(res => {
  console.log(res)
})

交互

封装微信小程序的 wx.showToast()、wx.showModal()、wx.showLoading()、wx.showActionSheet()

showToast

wxutil.showToast('hello')

showModal

wxutil.showModal('提示', '这是一个模态弹窗')

亦可传入参数并在回调函数中处理自己的业务

wxutil.showModal(title: title, content: content, handler = {
    showCancel: showCancel,
    cancelText: cancelText,
    confirmText: confirmText,
    cancelColor: cancelColor,
    confirmColor: confirmColor
}).then(res => {
    console.log(res)
})

showLoading

wxutil.showLoading('加载中')

showActionSheet

wxutil.showActionSheet(['A', 'B', 'C']).then(res => {
  console.log(res)
})

缓存

封装微信小程序的 wx.setStorageSync()wx.getStorageSync(),同步设置缓存和获取缓存内容,并实现设置过期时间功能

setStorage

wxutil.setStorage('userInfo', userInfo)

为缓存设置过期时间,单位:秒

wxutil.setStorage('userInfo', userInfo, 86400)

getStorage

wxutil.getStorage('userInfo')

授权

封装了需要用户授权微信小程序的方法

getLocation

封装了微信小程序的 getLocation(),获取用户的地理坐标

wxutil.getLocation().then(res => {
  console.log(res)
})

getUserProfile

封装了微信小程序的 getUserProfile(),获取用户公开信息,包括头像、昵称等

wxutil.getUserProfile().then(res => {
  console.log(res)
})

requestPayment

封装了微信小程序的 requestPayment(),需要传递后端的 timeStamp、nonceStr、packageValue、paySign 参数,加密方式默认为 MD5

wxutil
  .requestPayment({
    timeStamp: timeStamp,
    nonceStr: nonceStr,
    packageValue: packageValue,
    paySign: paySign
  })
  .then(res => {
    console.log(res)
  })

其他工具

封装了常用的微信小程序方法,便于高效开发

autoUpdate

app.js 中引用该方法,可在微信小程序发布新版本后提醒用户自动更新

wxutil.autoUpdate()

isNotNull

判断是否为非空字符串

wxutil.isNotNull('text')

getDateTime

获取当前日期时间,格式:yy-mm-dd hh:MM:ss

const datetime = wxutil.getDateTime()
console.log(datetime)

getTimestamp

获取当前时间戳

const timestamp = wxutil.getTimestamp()
console.log(timestamp)

calculate

精度计算

wxutil.calculate.add(0.1, 0.2)
wxutil.calculate.sub(1, 0.8)
wxutil.calculate.mul(6, 0.7)
wxutil.calculate.div(1.2, 0.2)

getUUID

获取 UUID

const uuid = wxutil.getUUID()
console.log(uuid)