add method of generate wechat url link and schema
This commit is contained in:
parent
530c8ea6c7
commit
d098219706
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user