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

@pagoda-tools/qywx-node

v0.0.1

Published

## 安装

Downloads

61

Readme

@pagoda-tools/qywx-node

安装

yarn add @pagoda-tools/qywx-node

初始化

interface UserInfo {
  name: string;
  code: string;
}
export const qwMpSdk = new QwMpSdk<UserInfo>({
  // 配置企业应用相关信息
  appList: [
    {
      corpid: 'corpid',
      corpsecret: 'corpsecret',
    },
  ],
  // 数据加密时的 secret
  secret: 'secret',
  getUserInfo(authInfo) {
    // 此处能拿到企业微信授权成功之后的信息
    // { corpid: string; userid: number; sessionKey: string; }
    // 可以在这里自定义获取用户信息数据,返回的用户信息将会被存在 sesssion 或者 token 中
    return {
      code: 'code',
      name: 'name'
    }
  },
});

登录

// code 为客户端调用 wx.login 拿到的临时凭证
// 使用 jwt 鉴权
const data = await qwMpSdk.loginByJwt(code);
// 可将 token 返回客户端保存,请求时在请求头中携带 x-qwmp-token
console.log(data.token);
// 使用 session 鉴权
const data = await qwMpSdk.login(code);
// 可将 session 返回客户端保存,请求时在请求头中携带 x-qwmp-id: data.session.id, x-qwmp-skey: data.session.skey
console.log(data.session);

检验登录

可在中间件中检验登录状态,以下以 express 中间件为例

const middleware = (req, res, next) => {
  const { success, reason, tokenData } = await qwMpSdk.checkAuthByToken(
    req.headers,
  );

  if (!success) {
    res.json({
      code: -1,
      message: '登录失效,请重新登录'
    })
    return;
  }

  req.qwmpSession = tokenData!;

  next();
}

调用企微接口

skd做了以下处理:

  • 默认自动在请求前获取accessToken,并在请求query参数中携带
  • 调用企微接口返回accessToken过期时,会自动获取新的accessToken并重新发起一次请求
const data = await qwMpSdk.request({
  method: 'get',
  url: '/cgi-bin/user/get',
  params: {
    userid: 'xxxxxx',
  },
  // 需要从登录信息中拿到企业id
  corpid: req.qwmpSession.authInfo.corpid,
  // 如果当前接口不需要携带accessToken,可关闭
  // accessToken: false
});

配置项

interface QwMPSdkConfig<U> {
  /**
   * 小程序已绑定企业的企业id和应用secret
   *
   * @param corpid 企业 corpid
   *
   * 查看方式:登录企业微信管理后台-我的企业-企业信息-企业ID
   *
   * @param agentId 企业应用 id
   *
   * 查看方式:登录企业微信管理后台-应用管理-打开对应应用详情-查看AgentId
   *
   * @param corpsecret 企业应用 corpsecret
   *
   * 查看方式:登录企业微信管理后台-应用管理-打开对应应用详情-查看Secret
   */
  appList: AppInfo[];
  // 自定义处理 accessToken 过期时间,默认为企微返回的过期时间提前10分钟
  handleAccessTokenExpiresIn?(expiresIn: number): number;
  // 用来存储 accessToken 和 session 的 store,如果不配置,默认存储在内存中
  store?: {
    get(key: string): string;
    set(key: string, val: string, expiresIn: number): void;
    del(key: string): void;
  };
  // 自定义获取用户信息函数,如果不配置,则 session和token中不存储userInfo
  getUserInfo?(authData: AuthInfo): Promise<U>;
  // 自定义处理企业微信接口调用异常(errcode 不为 0时),默认为抛出 error
  onApiBusinessError?<T = QwMpBaseRes & Record<string, any>, D = any>(
    response: AxiosResponse<T, D>,
  ): void;
  /**
   * 用于 jsonwebtoken 的 secret
   */
  secret: Secret;
  /**
   * 登录态过期时间,默认 24 小时,单位:秒
   */
  maxAge?: number;
  /**
   * 是否开启调试日志,默认不开启
   */
  debugLog?: boolean;
}