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 fs from 'fs'
|
||||||
import { WxTokenCache } from './WxTokenCache'
|
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
|
* @param {any} appId
|
||||||
@ -21,7 +51,7 @@ import { WxTokenCache } from './WxTokenCache'
|
|||||||
export async function generateQr({ appId, appSecret, scene, file }) {
|
export async function generateQr({ appId, appSecret, scene, file }) {
|
||||||
const stream = fs.createWriteStream(file)
|
const stream = fs.createWriteStream(file)
|
||||||
const token = await refreshToken(appId, appSecret)
|
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 = {
|
const reqParams = {
|
||||||
scene: scene,
|
scene: scene,
|
||||||
width: 430,
|
width: 430,
|
||||||
@ -44,26 +74,45 @@ export async function generateQr({ appId, appSecret, scene, file }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 刷新微信api的access token
|
* 微信小程序生成 URL Link
|
||||||
* @param {string} appId
|
* @param {any} appId
|
||||||
* @param {string} appSecret
|
* @param {any} appSecret
|
||||||
* @param {boolean} refresh 是否强制刷新
|
|
||||||
* @return {Promise<any>}
|
|
||||||
*/
|
*/
|
||||||
export async function refreshToken(appId: string, appSecret: string, refresh: boolean = false) {
|
export async function generateUrlLink({ appId, appSecret }) {
|
||||||
const util = new WxTokenCache()
|
const data = {}
|
||||||
let token = util.getToken(appId)
|
const token = await refreshToken(appId, appSecret)
|
||||||
if (token && !refresh) {
|
const url = `${URL_GENERATE_URL_LINK}?access_token=${token}`
|
||||||
return 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}`
|
return axios(reqConfig)
|
||||||
const { data } = await axios.get(link)
|
}
|
||||||
if (!data.errcode) {
|
|
||||||
util.updateToken(appId, data.access_token, data.expires_in)
|
/**
|
||||||
return data.access_token
|
* 微信小程序生成 Schema
|
||||||
} else {
|
* @param {any} appId
|
||||||
throw new Error(data.errmsg)
|
* @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) {
|
export async function msgSecCheck(content: string) {
|
||||||
let data = { content }
|
let data = { content }
|
||||||
const token = await refreshToken('wxf8c3da4e7dfe00a2', '8c0a1e88a6b43e4be80ed6a597c0b047')
|
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 = {
|
let reqConfig: AxiosRequestConfig = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
url,
|
url,
|
||||||
@ -97,7 +146,7 @@ export async function msgSecCheck(content: string) {
|
|||||||
export async function sendSubscribeMessage({ appId, appSecret, openId, templateId, data }) {
|
export async function sendSubscribeMessage({ appId, appSecret, openId, templateId, data }) {
|
||||||
const params = { touser: openId, template_id: templateId, data }
|
const params = { touser: openId, template_id: templateId, data }
|
||||||
const token = await refreshToken(appId, appSecret)
|
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 = {
|
let reqConfig: AxiosRequestConfig = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
url,
|
url,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user