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

anger-wechat

v1.0.3

Published

封装微信api

Downloads

8

Readme

Directory

  • Global 全局方法
    • isWechat 判断是否是微信环境
    • getGlobalAccessToken 获取 access_token
  • Web 适用于网页公众号开发
    • getJsTicket 获取 js api_ticket
    • getAuthUrl 获取 Auth地址
    • getAuthAccessTokenByCode 根据code获取 openid
    • getUserInfo 获取用户信息
    • getJsSdkConfig 获取js sdk config配置
  • Mini 适用于小程序开发
    • getOpenidByCode 根据code换取 openid等
  • angerPay 适用于支付
    • 支付文档

Getting started

npm install anger-wechat --save

Use

var AngerWechat = require('anger-wechat')
var weixinApi = new AngerWechat({
    appId: '[your appId]', // appId 必传
    appSecret: '[your appSecret]', // appSecret 必传
    authUrl: 'http://www.test.cc/get-weixin-code.html'
})

如果需要支付的话

var AngerWechat = require('anger-wechat')
var weixinApi = new AngerWechat({
    appId: '[your appId]', // appId 必传
    appSecret: '[your appSecret]', // appSecret 必传
    authUrl: 'http://www.test.cc/get-weixin-code.html', // 可选 微信auth2.0授权公共页面
    payment: { // 可选 如果需要支付模块的话
        mchId: '123456',
        partnerKey: '456789',
        pfx: path.join(__dirname, 'apiclient_cert.p12'), //【可选】证书路径,不传大多接口掉不了
        notifyUrl: '' // 【可选】微信支付接受到结果'
    }
})

Global

getGlobalAccessToken

全局使用,建议保存在数据库

获取 access_token 戳我查看官方文档

access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。

let access_token = await weixinApi.getGlobalAccessToken()
{ 
	"code": 200,
	"msg": "请求成功",
	"data": { 
		"access_token": "11_oTBrYVsT9wqa_-q3WDNEBOtfz1XKdM7YKIcNBEiu29Wfh5yTnlqaj5W0hMuxZ7C9FlY7CxD0RjR35V1ik1M3Nyi5QENcgFKhh0gYoBnAXEQ2oV93sVtO7IRqhh1kd9QLG8fwyA3vFRdifpJCOVLgAGAVCS",
		"expires_in": 7200 
	} 
}

isWechat

判断是否是微信环境

let ua = request.headers["user-agent"]
let isWeixin = weixinApi.isWechat(ua) // 传入 `UserAgent` 字符串,返回 true | false

Web

[AngerWechat.Web]

getJsTicket

全局使用,建议保存在数据库

获取 js api_ticket 戳我查看官方文档

api_ticket 是用于调用微信卡券JS API的临时票据,有效期为7200 秒,通过access_token 来获取。

let jsTrickt = await weixinApi.Web.getJsTicket({
    access_token: '11_oTBrYVsT9wqa_-q3WDNEBOtfz1XKdM7YKIcNBEiu29Wfh5yTnlqaj5W0hMuxZ7C9FlY7CxD0RjR35V1ik1M3Nyi5QENcgFKhh0gYoBnAXEQ2oV93sVtO7IRqhh1kd9QLG8fwyA3vFRdifpJCOVLgAGAVCS' // 以上一步获取到的access_token 获取 js api_ticket
  })
{
	"code": 200,
	"msg": "ok",
	"data": {
		"errcode": 0,
		"errmsg": "ok",
		"ticket": "HoagFKDcsGMVCIY2vOjf9oKOmI5MRTBgKJJwCDQ2BnyLbfAJHCZXUIs992xQP246Nzp7LCNupv5Jablw8COZ_w",
		"expires_in": 7200
	}
}

getAuthUrl

获取 Auth地址,重定向之后用来获取code

let authURl = weixinApi.Web.getAuthUrl({
	redirect_uri: 'http://www.baidu.com',
	scope: 'snsapi_userinfo' // snsapi_base:静默授权,snsapi_userinfo:提示授权(可以后续获取用户信息)
})
console.log(authURl)
// http://www.toolos.cc/get-weixin-code.html?appid=wxf638c1f64239e786&redirect_uri=http://www.baidu.com&scope=snsapi_userinfo&state=STATE

getAuthAccessTokenByCode

根据code获取 openid

let codeInfo = await weixinApi.Web.getAuthAccessTokenByCode({
    code: '061729xv1BtSAa09g8yv1hr0xv1729xB'
})
{
	"code": 200,
	"msg": "请求成功",
	"data": {
		"access_token": "11_Vc7D8AoYURWoECzJgD6Q1ccUOOHypO6mU0RQF7BnliKKCY5arfVvl0h3PWURwpK7QFgPLfDkHLX-9Dif6BTntw",
		"expires_in": 7200,
		"refresh_token": "11_ii3uONcMPA-04RjuLlckMMiwaDGF2MjW2SB5vOI3Sshz39PAVn7kOyC80_pPmmRJxqbfJ3Rdw07WG154AeP83Q",
		"openid": "oI-Aa04T6FrpFFpTfyAaXR4SKacU",
		"scope": "snsapi_userinfo"
	}
}

getUserInfo

根据 getAuthAccessTokenByCode 获取的 access_tokenopenid 获取用户信息,注意这里的获取回调url方法 getAuthUrlscope 需要传 snsapi_userinfo

let userInfo = await weixinApi.Web.getUserInfo({
	access_token: codeInfo.data.access_token,
	openid: codeInfo.data.openid
})
{
	"code": 200,
	"msg": "请求成功",
	"data": {
		"openid": "oI-Aa04T6FrpFFpTfyAaXR4SKacU",
		"nickname": "吃鱼的帆",
		"sex": 1,
		"language": "zh_CN",
		"city": "**",
		"province": "**",
		"country": "中国",
		"headimgurl": "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIS3x9dFdptD1s2ZZMTDCriaiaXiaDPtyZw3vfMmJLyQ8PU8laBv4MNnJh5c9QWtTQey0m4FYUPVEvAQ/132",
		"privilege": []
	}
}

getJsSdkConfig

获取js sdk config配置 微信 JS 接口签名校验工具

let jsTricktConfig = await weixinApi.Web.getJsSdkConfig({
	ticket: 'HoagFKDcsGMVCIY2vOjf9oKOmI5MRTBgKJJwCDQ2Bnx34ua3MVs9zUZpV0wQPe8h83AwFSZQREHGgmuKpqvdsg',
	url: 'http://www.toolos.cc' // 调用js sdk 页面地址
})
{
	"appId": "wxf638c1f64239e786",
	"signature": "2c18eb8d6adaa5e02a9df517e776f5eef40ed402",
	"noncestr": "04b21a6a-caea-4878-9fdb-8b2c1ff699b0",
	"timestamp": 1530636997,
	"jsapi_ticket": "HoagFKDcsGMVCIY2vOjf9oKOmI5MRTBgKJJwCDQ2Bnx34ua3MVs9zUZpV0wQPe8h83AwFSZQREHGgmuKpqvdsg"
}

Mini

[AngerWechat.Mini]

getOpenidByCode

根据小程序传来的code获取openid等信息 临时登录凭证code 获取 session_key 和 openid 等。

let info = await weixinApi.Mini.getOpenidByCode({
    code: 'xxxxxxxxxxxxxx'
})
{
    "openid" : "oynY34-0Err_YssQIFsK-Ht1eGs",
    "session_key" : "sPxOFsssssCP54n6cCfshw=="
}

支付

戳我查看文档地址

附录

关于 authUrl 参数

参考 GetWeixinCode

校验工具

  • 微信 JS 接口签名校验工具
  • 微信公众平台接口调试工具

官方文档目录(真心想吐槽,找个文档真难)

  • js sdk
  • 红包文档
  • access_token

请我喝杯咖啡,支持更多开源

1024.png