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

@liuhlightning/wx-mp

v0.4.2

Published

communicate to wx mp

Downloads

149

Readme

wx-mp

Interface to wx

According to Low Coupling and High cohesion, using as less dependencies as possibel.

Has no side effect feature, such as storage.

How to use

npm install --save @liuhlightning/wx-mp
# or
yarn add @liuhlightning/wx-mp
import { WxMp } from "@liuhlightning/wx-mp";

const wxMp = new WxMp({
  appId: "your app id",
  appSecret: "your app secret",
});

Config

/**
 * param for wx share platform
 */
export interface WxMpParam {
  /**
   * appid ask on wx share platform
   * https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Interface_field_description.html
   */
  appId: string;
  /**
   * app secret ask on wx share platform
   * https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Interface_field_description.html
   */
  appSecret: string;
  /**
   * end point, override the default end point
   * https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Interface_field_description.html
   */
  baseURL?: string;
  /**
   * using backup end point
   * https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Interface_field_description.html
   */
  useBackupBaseURL?: boolean;
  /**
   * timeout, override axios config
   */
  timeout?: number;
  /**
   * axios config
   */
  axiosConfig?: CreateAxiosDefaults;
}

access token

access token get by request will store in store, could use the value in store before expire.

// get from store
const accessToken = wxMp.accessToken;
// get by request
const accessToken = await wxMp.getAccessToken();
// check if expired, then get
const accessToken = wxMp.checkAccessTokenExpire()
  ? await wxMp.getAccessToken()
  : wxMp.accessToken;

ticket

ticket get by request will store in store, could use the value in store before expire.

// get from store
const ticket = wxMp.ticket;
// get by request
const ticket = await wxMp.getTicket();
// check if expired, then get
const ticket = wxMp.checkTicketExpire()
  ? await wxMp.getTicket()
  : wxMp.ticket;

URL sign

sign the url open in wx

const {
  nonceStr,
  timestamp,
  url,
  signature
} = wxMp.getSignature("http://mp.weixin.qq.com?params=value");

the signatrue should send to front for wxjssdk.

Mini program login

const {
  openid,
  unionid,
  session_key
} = await wxMp.code2Session(codeFromMiniapp);

using openid unionid and session_key maintance your login state.

Mini program get user phone number

const { phone_info } = await wxMp.getUserPhoneNumber(codeFromMiniapp);
if(!phone_info) throw new Error("null phone info");
const { phoneNumber } = phone_info;

OAuth url auth

const wxMp = new WxMp({
  appId: "your app id",
  appSecret: "your app secret",
  redirectUrl: "https://domain.com/some_url",
});

const url = wxMp.generateOAuthUrl("snsapi_userinfo");
// or
const url = wxMp.generateOAuthUrl({
  redirectUrl: "https://domain.com/some_url",
  scope: "snsapi_userinfo",
  state: "some_state",
});

// get access token, this token is form user auth, not access token above.
const accessToken = await wxMp.getOAuthAccessToken(tokenFromFront);

// get user info
const { openid, nickname, sex, headimgurl } = await wxMp.getOAuthUserInfo(accessTokenGetByAbove);

Maintance access token and ticket

using node-schedule

import { scheduleJob } from "node-schedule";
import { WxMp } from "../src";

const wxMp = new WxMp({
  appId: "your app id",
  appSecret: "your app secret",
});

async function initWxMp() {
  // init access token
  await wxMp.getAccessToken();
  // init ticket
  await wxMp.getTicket();
  // check every 5 min
  scheduleJob({ second: 0, minute: "*/5" }, () => {
    if (wxMp.checkAccessTokenExpire()) wxMp.getAccessToken();
  });
  // check every 5 min
  scheduleJob({ second: 15, minute: "*/5" }, () => {
    if (wxMp.checkTicketExpire()) wxMp.getTicket();
  });
}

initWxMp();