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

wechat-oauth-lib

v0.0.3

Published

微信授权协议(服务端)

Downloads

3

Readme

wechat-oauth-lib

Dependency Downloads Version

根据官方文档,使用 TypeScript 编写的微信授权后接口调用类库

使用方法

首先引入该类库

import WechatOauth from "wechat-oauth-lib";

初始化类库

const oauth = new WechatOauth("appid", "secret");

调用

const { code = "" }: IReqBody = req.body;
if (!code) {
  // 此处请自行处理参数错误的返回
  res.json(msgCode.parmsError);
  return;
}
const token = await oauth.getAccessToken(code);
let accesstoken: string = "";
let openid: string = "";
// 如果有错误信息
// 此处自行处理获取token失败的问题
if (token.errcode) {
  res.json({ code: token.errcode, msg: token.errmsg });
  return;
}
openid = token.openid; // 获取openid
accesstoken = token.access_token; // 获取accesstoken
// 判断如果accesstoken过期,则续期
if (token.expires_in === 0) {
  const refresh = await oauth.refreshToken(accesstoken);
  if (refresh.errcode) {
    res.json({ code: refresh.errcode, msg: token.errmsg });
    return;
  }
  accesstoken = refresh.access_token;
  openid = refresh.openid;
}
// 验证access_token是否有效
const checktoken = await oauth.checkToken(openid, accesstoken);
// 验证无效
if (checktoken.errcode) {
  res.json({ code: checktoken.errcode, msg: checktoken.errmsg });
  return;
}
// 获取用户信息
const userinfo = await oauth.userInfo(openid, accesstoken);
if (userinfo.errcode) {
  res.json({ code: userinfo.errcode, msg: userinfo.errmsg });
  return;
}
const loginResult = await dbUsers.thirdPartLogin({
  nickname,
  avatar,
  sex,
  os,
  ip,
  deviceid,
  openid
});
DoSomethingWhithResult(loginResult);

优化

调用微信登录授权之前,可以先查询数据库中是否存在该 openid 的用户,如果有则直接返回用户信息,而不用频繁调用授权接口

其他

详细的返回参数请参阅官方文档

图示

例子