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

szyd-child-sdk

v0.0.2

Published

数字盐电专用子系统 iframe 与父系统通信 SDK,提供 messageBus、request、fetchUserInfo 和 wxBridge 模块

Downloads

155

Readme

szyd-child-sdk

数字盐电专用子系统接入 SDK,提供与父系统通信所需的 messageBus、request、userInfo 获取、wx 桥接四个模块。


安装

npm install szyd-child-sdk

快速开始

在子系统入口文件(如 main.ts)中初始化:

import { messageBus, createRequest, initFetchUserInfo, initWxBridge } from 'szyd-child-sdk'

// 1. 配置父系统 origin(开发联调时指定,生产同域可省略)
messageBus.config({ parentOrigin: 'http://localhost:8000' })

// 2. 创建 request 实例
export const request = createRequest({ appSecretId: import.meta.env.VITE_APP_SECRET_ID })

// 3. 初始化 userInfo 获取(一次性,收到响应后自动卸载)
initFetchUserInfo({
  onUserInfo: (userInfo) => {
    // 在此处理用户信息,如存入 store、跳转路由等
    store.setUserInfo(userInfo)
    router.push('/home')
  }
})

// 4. 初始化 wx 桥接(调用一次,之后全局可直接使用 wx.xxx())
initWxBridge()

API

messageBus

全局唯一的 postMessage 消息总线,各模块内部使用,通常无需直接调用。

// 配置允许的父系统 origin
messageBus.config({ parentOrigin: 'http://localhost:8000' })

// 订阅消息
messageBus.on('SOME_TYPE', (event) => { ... })

// 取消订阅
messageBus.off('SOME_TYPE', handler)

createRequest(options)

创建通过父系统代理的 request 实例,子系统所有网络请求均走此链路。

| 参数 | 类型 | 说明 | | ----------------------- | ---------- | --------------------------- | | options.appSecretId | string | 子系统鉴权 ID,由父系统分配 |

const request = createRequest({ appSecretId: 'your-app-secret-id' })

// 使用方式与 axios 一致
const res = await request<UserInfo>({ url: '/api/user', method: 'get' })

// 强制走父系统 API 实例
const res = await request({ url: '/api/data', method: 'post', isParentApi: true, data: { ... } })

initFetchUserInfo(options)

向父系统请求用户信息,通过回调返回结果。收到一次响应后自动卸载监听,无需手动清理。

| 参数 | 类型 | 说明 | | ---------------------- | --------------------------------------- | -------------------- | | options.onUserInfo | (userInfo: UserInfo \| null) => void | 收到用户信息时的回调 |

initFetchUserInfo({
  onUserInfo: (userInfo) => {
    if (userInfo) {
      store.setUserInfo(userInfo)
    }
  }
})

initWxBridge()

替换全局 window.wx 为代理对象,之后业务代码中可直接使用原生 wx.xxx() 调用方式,内部自动通过 postMessage 代理到父系统执行。

在入口处调用一次即可:

initWxBridge()

之后正常使用:

wx.startRecord()

wx.stopRecord({
  success: async (res) => {
    const localId = res.localId
    wx.translateVoice({
      localId,
      isShowProgressTips: 1,
      success: (res) => {
        console.log(res.translateResult)
      },
    })
  },
})