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

wx-auth-order

v0.0.12

Published

- [install](#install) - [demo](#demo) - [WxAuth](#wxauth) - [WxPay](#wxpay) - [lbs](#lbs)

Downloads

5

Readme

wx-auth-order

目录

install

npm install wx-auth-order

demo

import { WxAuth, WxPay } from 'wx-auth-order'
import { wxAuthConf, wxPayConf } from './config'

/**
 * 微信授权初始化
 */
export let myWechatAuth: WxAuth
export function authInit(): void {
    if (myWechatAuth) {
        throw new Error("alreay init wechat auth!")
    }

    myWechatAuth = new WxAuth({
        component_appid: wxAuthConf.component_appid,
        component_appsecret: wxAuthConf.component_appsecret,
        aesToken: wxAuthConf.aesToken,
        aesKey: wxAuthConf.aesKey,
        lastTicket: 'ticket@@@aaaaaaa',
        getAuthInfoByAppid: (authorizer_appid: string) => {
            //查询数据库
            return {
                authorizer_refresh_token: 'refreshtoken@@@aaaaa',
                authorizer_access_token: 'refreshtoken@@@aa-aaaaa',
                expires_at: 125456464
            }
        },
        saveAuthAccessToken: (token: {
            authorizer_appid: string,
            authorizer_access_token: string,
            expires_at: number
        }) => {
            console.log('-----save token----', token)
        },
    })
}

/**
 * 微信支付初始化
 */
export let myWechatPay: WxPay
export function payInit() {
    if (myWechatPay) {
        throw new Error("alreay init wechat pay!")
    }

    myWechatPay = new WxPay({
        appid: wxPayConf.appid,
        mch_id: wxPayConf.mch_id,
        key: wxPayConf.key,
    })
}

WxAuth

构造器参数

  • component_appid: 第三方平台的appid
  • component_appsecret: 第三方平台appsecret
  • aesToken: 消息校验Token
  • aesKey: 消息加解密Key
  • lastTicket: 最新的ticket, 可以为空字符串
  • getAuthInfoByAppid: 根据授权appid查询授权信息的函数
  • saveAuthAccessToken: 保存授权信息的函数
  • logFunc: 打印信息函数

getComponentConf()

获取当前缓存的第三方配置,方便手动验证请求结果

myWechatAuth.getComponentConf()

setLogFunc(func: (s: any) => void)

更改打印函数

myWechatAuth.setLogFunc(console.log)

decryption(body: string)

针对微信事件推送消息的解密, 包含授权事件接收URL, 和消息通知接收URL。

const dJson = await myWechatAuth.getComponentConf(body)

getPreAuthCode(redirect_uri: string)

获取预授权码, 提示:redirect_uri需要/:id, 方便绑定账号和公众号

const account_id = 10
const webAuthUrl = await myWechatAuth.getPreAuthCode(`http://cloud.cn/wechat/notice/${account_id}`)

getAuthInfo(auth_code: string)

根据授权码获取授权信息和授权公众号的信息

const { auth_code } = req.query
const data = await myWechatAuth.getAuthInfo(auth_code)

getOAuthURL(appid: string, redirect_uri: string)

代公众号发起网页授权第一步:根据授权公众号的appid,生成自定义菜单链接

const url = await myWechatAuth.getOAuthURL('wx2fcdbd568s4', 'http://cloud.cn/h5/code')

getOAuthAccessToken(appid: string, code: string)

代公众号发起网页授权第二步:根据回调地址得到的参数,获取用户openid和下一步请求需要的token

const { appid, code } = req.query
const data = await myWechatAuth.getOAuthAccessToken(appid, code)

getUserInfo(openid: string, access_token: string)

代公众号发起网页授权第三步, 查询openid对应的微信账号基本信息

//access_token: getOAuthAccessToken返回值中有
const user = await myWechatAuth.getUserInfo(openid, access_token)

sendTemplateMessage(authorizer_appid: string, body: any)

发送模板消息, body为post请求的内容

const body = {
    touser: 'oXgezs1sdafaW_2s',
    template_id: 'VqiCeQfawdDbds-fZ60',
    url: "",
    data: {
        first: {
            value: "尊敬的用户您好,您的一笔返现已到账。",
            color: "#173177"
        },
        keyword1: {
            value: `5元`,
            color: "#173177"
        },
        keyword2: {
            value: `订单已确认完成,返现5元`,
            color: "#173177"
        },
        remark: {
            value: "感谢你的使用,谢谢!",
            color: "#173177"
        }
    }
}
const r = await myWechatAuth.sendTemplateMessage('wx2fcdbd568s4', body)

WxPay

构造器参数

  • appid: 微信支付分配的公众账号ID
  • mch_id: 微信支付分配的商户号
  • key: 商户平台key
  • logFunc: 打印信息函数

setLogFunc(func: (s: any) => void)

更改打印函数

jsapi(opt: any)

h5支付, 返回值包含调起H5支付的参数。

参数名 | 必填 | 类型 | 描述 ------ | ----- | ------- | --- body | 是 | String(128) | 商品简单描述 money | 是 | Int | 订单总金额,单位为分 openid | 是 | String(128) | 微信用户在商户对应appid下的唯一标识 notify_url | 是 | String(256) | 异步接收微信支付结果通知的回调地址 timeout | 否 | Int | 订单失效时间,单位秒,默认600 out_trade_no | 否 | String(32) | 商户系统内部订单号

native(opt: any)

扫码支付 参数名 | 必填 | 类型 | 描述 ------ | ----- | ------- | --- body | 是 | String(128) | 商品简单描述 money | 是 | Int | 订单总金额,单位为分 openid | 否 | String(128) | 微信用户在商户对应appid下的唯一标识 notify_url | 是 | String(256) | 异步接收微信支付结果通知的回调地址 timeout | 否 | Int | 订单失效时间,单位秒,默认600 out_trade_no | 否 | String(32) | 商户系统内部订单号

validateNotify(body: string)

校验微信推送的支付结果签名, 并返回格式化之后的数据

orderQueryByTransactionId(transaction_id: string)

根据微信订单号查询订单状态

orderQueryByOutTradeNo(out_trade_no: string)

根据商户订单号查询订单状态

lbs

demo

import { BaiDuApi, GaoDeApi } from 'wx-auth-order'

async function lbsDemo() {
    const bd = new BaiDuApi({
        ak: 'aaaaaaaaa'
    })
    const a = await bd.location('111.11.1.1')
    console.log(a)

    const gd = new GaoDeApi({
        key: 'aaaaaaaaa'
    })
    const b = await gd.location('111.11.1.1')
    console.log(b)
}

location(ip: string)

获取ip归属省市