add method of generate wechat url link and schema

This commit is contained in:
zhl 2021-08-05 12:54:59 +08:00
parent 530c8ea6c7
commit d098219706

View File

@ -2,6 +2,36 @@ import axios, { AxiosRequestConfig } from 'axios'
import fs from 'fs'
import { WxTokenCache } from './WxTokenCache'
const URL_QRCODE = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit'
const URL_TOKEN = 'https://api.weixin.qq.com/cgi-bin/token'
const URL_MSG_CHECK = 'https://api.weixin.qq.com/wxa/msg_sec_check'
const URL_SUBSCRIBE_MSG = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send'
const URL_GENERATE_URL_LINK = 'https://api.weixin.qq.com/wxa/generate_urllink'
const URL_GENERATE_SCHEMA = 'https://api.weixin.qq.com/wxa/generatescheme'
/**
* api的access token
* @param {string} appId
* @param {string} appSecret
* @param {boolean} refresh
* @return {Promise<any>}
*/
export async function refreshToken(appId: string, appSecret: string, refresh: boolean = false) {
const util = new WxTokenCache()
let token = util.getToken(appId)
if (token && !refresh) {
return token
}
const link = `${URL_TOKEN}?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
const { data } = await axios.get(link)
if (!data.errcode) {
util.updateToken(appId, data.access_token, data.expires_in)
return data.access_token
} else {
throw new Error(data.errmsg)
}
}
/**
*
* @param {any} appId
@ -21,7 +51,7 @@ import { WxTokenCache } from './WxTokenCache'
export async function generateQr({ appId, appSecret, scene, file }) {
const stream = fs.createWriteStream(file)
const token = await refreshToken(appId, appSecret)
const url = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`
const url = `${URL_QRCODE}?access_token=${token}`
const reqParams = {
scene: scene,
width: 430,
@ -44,26 +74,45 @@ export async function generateQr({ appId, appSecret, scene, file }) {
}
/**
* api的access token
* @param {string} appId
* @param {string} appSecret
* @param {boolean} refresh
* @return {Promise<any>}
* URL Link
* @param {any} appId
* @param {any} appSecret
*/
export async function refreshToken(appId: string, appSecret: string, refresh: boolean = false) {
const util = new WxTokenCache()
let token = util.getToken(appId)
if (token && !refresh) {
return token
export async function generateUrlLink({ appId, appSecret }) {
const data = {}
const token = await refreshToken(appId, appSecret)
const url = `${URL_GENERATE_URL_LINK}?access_token=${token}`
let reqConfig: AxiosRequestConfig = {
method: 'post',
url,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
},
data,
}
const link = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
const { data } = await axios.get(link)
if (!data.errcode) {
util.updateToken(appId, data.access_token, data.expires_in)
return data.access_token
} else {
throw new Error(data.errmsg)
return axios(reqConfig)
}
/**
* Schema
* @param {any} appId
* @param {any} appSecret
*/
export async function generateSchema({ appId, appSecret }) {
const data = {}
const token = await refreshToken(appId, appSecret)
const url = `${URL_GENERATE_SCHEMA}?access_token=${token}`
let reqConfig: AxiosRequestConfig = {
method: 'post',
url,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
},
data,
}
return axios(reqConfig)
}
/**
@ -73,7 +122,7 @@ export async function refreshToken(appId: string, appSecret: string, refresh: bo
export async function msgSecCheck(content: string) {
let data = { content }
const token = await refreshToken('wxf8c3da4e7dfe00a2', '8c0a1e88a6b43e4be80ed6a597c0b047')
const url = `https://api.weixin.qq.com/wxa/msg_sec_check?access_token=${token}`
const url = `${URL_MSG_CHECK}?access_token=${token}`
let reqConfig: AxiosRequestConfig = {
method: 'post',
url,
@ -97,7 +146,7 @@ export async function msgSecCheck(content: string) {
export async function sendSubscribeMessage({ appId, appSecret, openId, templateId, data }) {
const params = { touser: openId, template_id: templateId, data }
const token = await refreshToken(appId, appSecret)
const url = `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${token}`
const url = `${URL_SUBSCRIBE_MSG}?access_token=${token}`
let reqConfig: AxiosRequestConfig = {
method: 'post',
url,