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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jiayouzuo/uniapp-shared-js

v1.4.0

Published

uni-app 通用工具函数库

Readme

@jiayouzuo/uniapp-shared-js

uni-app 通用工具函数库

安装

npm install @jiayouzuo/uniapp-shared-js

功能模块

📡 网络请求(request)

import { createRequest } from '@jiayouzuo/uniapp-shared-js'

// 创建请求实例
const api = createRequest({
  baseUrl: 'https://api.example.com',
  tokenKey: 'token',
  onUnauthorized: () => {
    // 登录过期处理
  }
})

// 发起请求
const res = await api.get('/user/info')
const res = await api.post('/user/update', { name: 'John' })

// 文件上传
const res = await api.upload({
  url: '/upload',
  filePath: tempFilePath,
  onProgress: (progress) => console.log(progress)
})

📤 文件上传(upload)

import { uploadFile, chooseAndUploadImage, registerUploadCallback } from '@jiayouzuo/uniapp-shared-js'

// 上传文件(返回taskId,结果通过WebSocket通知)
const { taskId } = await uploadFile({
  url: 'https://api.example.com/upload',
  filePath: tempFilePath,
  name: 'file'
})

// 注册上传回调
const unregister = registerUploadCallback(taskId, {
  onSuccess: (result) => console.log('上传成功:', result),
  onError: (result) => console.error('上传失败:', result)
})

// 选择并上传图片
const { taskId } = await chooseAndUploadImage({
  url: 'https://api.example.com/upload',
  count: 1
})

💾 本地存储(storage)

import { setStorage, getStorage, removeStorage } from '@jiayouzuo/uniapp-shared-js'

// 设置存储(支持过期时间)
setStorage({ key: 'user', data: { name: 'John' }, expire: 3600 })

// 获取存储
const user = getStorage<User>('user')

// 移除存储
removeStorage('user')

🖼️ 图片处理(image)

import { compressImage, chooseImage, previewImage } from '@jiayouzuo/uniapp-shared-js'

// 压缩图片
const path = await compressImage({ src: imagePath, quality: 80 })

// 选择图片
const paths = await chooseImage(9)

// 预览图片
previewImage(['url1', 'url2'], 0)

📁 文件系统(file)

import { readFile, writeFile, removeFile } from '@jiayouzuo/uniapp-shared-js'

// 读取文件
const content = await readFile(filePath)

// 写入文件
await writeFile(filePath, 'Hello World')

// 删除文件
await removeFile(filePath)

📸 保存相册(album)

import { saveImageToAlbum } from '@jiayouzuo/uniapp-shared-js'

// 保存图片到相册(自动处理权限)
await saveImageToAlbum(tempFilePath)

🔄 小程序更新(update)

import { checkUpdate } from '@jiayouzuo/uniapp-shared-js'

// 检查小程序更新
checkUpdate()

📺 广告封装(ad)

import { createRewardedVideoAd, showRewardedVideoAd, getWxCode } from '@jiayouzuo/uniapp-shared-js'

// 创建激励视频广告
const ad = createRewardedVideoAd('adunit-xxx')

// 播放广告
const success = await showRewardedVideoAd(ad)

// 获取微信code
const code = await getWxCode()

🎨 Canvas工具(canvas)

import { createOffscreenCanvas, canvasToTempFilePath } from '@jiayouzuo/uniapp-shared-js'

// 创建离屏Canvas
const canvas = createOffscreenCanvas(300, 300)

// Canvas转图片
const path = await canvasToTempFilePath({ canvasId: 'myCanvas' })

⏱️ 定时器(timer)

import { delay, debounce, throttle, createTimer } from '@jiayouzuo/uniapp-shared-js'

// 延迟执行
await delay(1000)

// 防抖
const debouncedFn = debounce(() => console.log('搜索'), 300)

// 节流
const throttledFn = throttle(() => console.log('滚动'), 100)

// 创建定时器
const timer = createTimer(() => console.log('tick'), { interval: 1000, count: 10 })
timer.stop() // 停止定时器

🔌 WebSocket(websocket)

import { createWebSocketManager } from '@jiayouzuo/uniapp-shared-js'

// 创建WebSocket管理器
const ws = createWebSocketManager({
  url: 'ws://example.com/ws',
  getToken: () => uni.getStorageSync('token'),
  heartbeatInterval: 30000,
  reconnectInterval: 3000,
  maxReconnectAttempts: 5
})

// 连接
ws.connect()

// 监听消息
const unsubscribe = ws.onMessage('upload_complete', (data) => {
  console.log('上传完成:', data)
})

// 发送消息
ws.send({ type: 'ping' })

// 断开连接
ws.disconnect()

👷 Worker(worker)

import { createWorker, runInWorker } from '@jiayouzuo/uniapp-shared-js'

// 创建可复用Worker
const worker = createWorker('compute')
const result = await worker.run({ data: [1, 2, 3] })
worker.terminate()

// 一次性执行
const result = await runInWorker('compute', { data: [1, 2, 3] }, { timeout: 5000 })

🔒 安全工具(safe)

import { jsonParse, jsonStringify, toNumber, arrayAt } from '@jiayouzuo/uniapp-shared-js'

// 安全的JSON解析
const obj = jsonParse(jsonStr, {})

// 安全的JSON序列化
const str = jsonStringify(obj, '')

// 安全的数字转换
const num = toNumber('123', 0)

// 安全的数组访问
const item = arrayAt([1, 2, 3], -1) // 3

🔐 加密工具(crypto)

import { base64Encode, base64Decode, simpleHash } from '@jiayouzuo/uniapp-shared-js'

// Base64编码
const encoded = base64Encode('Hello')

// Base64解码
const decoded = base64Decode(encoded)

// 简单哈希
const hash = simpleHash('password')

📝 格式化(format)

import { formatDate, formatFileSize, formatNumber, formatPhone } from '@jiayouzuo/uniapp-shared-js'

// 格式化日期
formatDate(new Date(), 'YYYY-MM-DD') // 2026-02-02

// 格式化文件大小
formatFileSize(1024) // 1.00 KB

// 格式化数字
formatNumber(1234567) // 1,234,567

// 格式化手机号
formatPhone('13800138000') // 138****8000

✅ 校验(validate)

import { isPhone, isEmail, isUrl, isEmpty } from '@jiayouzuo/uniapp-shared-js'

// 校验手机号
isPhone('13800138000') // true

// 校验邮箱
isEmail('[email protected]') // true

// 校验URL
isUrl('https://example.com') // true

// 校验是否为空
isEmpty('') // true

🆔 UUID生成(uid)

import { uuid, shortId, timestampId } from '@jiayouzuo/uniapp-shared-js'

// 生成UUID
uuid() // 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'

// 生成短ID
shortId() // 'a1b2c3d4'

// 生成时间戳ID
timestampId() // 'lz1a2b3c4'

License

MIT