0.0.12 • Published 5 years ago

wx-auth-order v0.0.12

Weekly downloads
2
License
ISC
Repository
-
Last release
5 years ago

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支付的参数。

参数名必填类型描述
bodyString(128)商品简单描述
moneyInt订单总金额,单位为分
openidString(128)微信用户在商户对应appid下的唯一标识
notify_urlString(256)异步接收微信支付结果通知的回调地址
timeoutInt订单失效时间,单位秒,默认600
out_trade_noString(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归属省市

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago