修改微信刷新access token的方法
This commit is contained in:
parent
56471b92b7
commit
cc7a2926ef
@ -5,20 +5,26 @@ import { Game } from '../../models/content/Game'
|
|||||||
import { generateQrFile } from '../../services/File'
|
import { generateQrFile } from '../../services/File'
|
||||||
import { getInviteeNum } from '../../services/JCFW'
|
import { getInviteeNum } from '../../services/JCFW'
|
||||||
import { UserItem } from '../../models/user/UserItem'
|
import { UserItem } from '../../models/user/UserItem'
|
||||||
|
import { checkText } from '../../services/Baidu'
|
||||||
|
import { refreshToken } from '../../services/Wechat'
|
||||||
|
|
||||||
class GameController extends BaseController {
|
class GameController extends BaseController {
|
||||||
@role('anon')
|
@role('anon')
|
||||||
@router('get /api/test')
|
@router('post /api/test')
|
||||||
async test(req) {
|
async test(req) {
|
||||||
// let gameId = '60810dd156af0e8550832a44'
|
// let gameId = '60810dd156af0e8550832a44'
|
||||||
// let version = '608117912ff0238a3e607d33'
|
// let version = '608117912ff0238a3e607d33'
|
||||||
// let shop = 'sa6xtgbmj7'
|
// let shop = 'sa6xtgbmj7'
|
||||||
// const { file, url } = await generateQrFile({ gameId, version, shop })
|
// const { file, url } = await generateQrFile({ gameId, version, shop })
|
||||||
// return { file, url }
|
// return { file, url }
|
||||||
let accountId = '7101_8004_opBlx1vRs-UIGF6rZDkkSrSTJYAs'
|
// let accountId = '7101_8004_opBlx1vRs-UIGF6rZDkkSrSTJYAs'
|
||||||
let sessionId = '1622783517_1556611531_60838f020d9c51716f144bf7a71b2af5_e5818d3fc90f8491aa285246cb6d85ce'
|
// let sessionId = '1622783517_1556611531_60838f020d9c51716f144bf7a71b2af5_e5818d3fc90f8491aa285246cb6d85ce'
|
||||||
let result = await getInviteeNum(accountId, sessionId, [1001])
|
// let result = await getInviteeNum(accountId, sessionId, [1001])
|
||||||
return result
|
// return result
|
||||||
|
const { txt } = req.params
|
||||||
|
// const res = await checkText(txt)
|
||||||
|
const token = await refreshToken('wxf8c3da4e7dfe00a2', '8c0a1e88a6b43e4be80ed6a597c0b047')
|
||||||
|
return { token }
|
||||||
}
|
}
|
||||||
@permission(['game:read', 'shop:game_setting'])
|
@permission(['game:read', 'shop:game_setting'])
|
||||||
@router('post /api/games')
|
@router('post /api/games')
|
||||||
|
36
src/services/Baidu.ts
Normal file
36
src/services/Baidu.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
|
let random = function (start, end) {
|
||||||
|
return (Math.random() * (end - start) + start) | 0
|
||||||
|
}
|
||||||
|
|
||||||
|
let getIp = function () {
|
||||||
|
return `${random(1, 254)}.${random(1, 254)}.${random(1, 254)}.${random(1, 254)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function checkText(txt: string) {
|
||||||
|
let qs = require('qs')
|
||||||
|
const url = 'https://ai.baidu.com/aidemo'
|
||||||
|
let data: string = qs.stringify({
|
||||||
|
content: txt,
|
||||||
|
type: 'textcensor',
|
||||||
|
apiType: 'censor',
|
||||||
|
})
|
||||||
|
const useragent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${
|
||||||
|
(70 + Math.random() * 10) | 0
|
||||||
|
}.0.4324.${(Math.random() * 100) | 0} Safari/537.36`
|
||||||
|
let reqConfig: any = {
|
||||||
|
method: 'post',
|
||||||
|
url,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
referer: 'https://ai.baidu.com/tech/textcensoring',
|
||||||
|
'User-Agent': useragent,
|
||||||
|
'X-Forwarded-For': getIp(),
|
||||||
|
},
|
||||||
|
responseType: 'json',
|
||||||
|
data: data,
|
||||||
|
}
|
||||||
|
const res = await axios(reqConfig)
|
||||||
|
return res
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
import axios, { AxiosRequestConfig } from 'axios'
|
import axios, { AxiosRequestConfig } from 'axios'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
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)
|
||||||
@ -27,11 +28,19 @@ export async function generateQr({ appId, appSecret, scene, file }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function refreshToken(appId: string, appSecret: string) {
|
export async function refreshToken(appId: string, appSecret: string) {
|
||||||
|
const util = new WxTokenCache()
|
||||||
|
let token = util.getToken(appId)
|
||||||
|
if (token) {
|
||||||
|
return token
|
||||||
|
}
|
||||||
const link = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
|
const link = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
|
||||||
const { data } = await axios.get(link)
|
const { data } = await axios.get(link)
|
||||||
if (!data.errcode) {
|
if (!data.errcode) {
|
||||||
|
util.updateToken(appId, data.access_token, data.expires_in)
|
||||||
return data.access_token
|
return data.access_token
|
||||||
} else {
|
} else {
|
||||||
throw new Error(data.errmsg)
|
throw new Error(data.errmsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function msgSecCheck(content: string) {}
|
||||||
|
41
src/services/WxTokenCache.ts
Normal file
41
src/services/WxTokenCache.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import { singleton } from '../decorators/singleton'
|
||||||
|
|
||||||
|
export class WxTokenClass {
|
||||||
|
public token: string
|
||||||
|
public expire: number
|
||||||
|
|
||||||
|
constructor(token: string, expire: number) {
|
||||||
|
this.token = token
|
||||||
|
this.expire = expire
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 用于缓存所有题目的分类
|
||||||
|
*/
|
||||||
|
@singleton
|
||||||
|
export class WxTokenCache {
|
||||||
|
private _cache: Map<string, WxTokenClass> = new Map()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id获取分类名
|
||||||
|
* @param {string} appId
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
|
public getToken(appId: string): string | null {
|
||||||
|
if (!this._cache.has(appId)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
let data = this._cache.get(appId)
|
||||||
|
if (data.expire - Date.now() < 10000) {
|
||||||
|
return null
|
||||||
|
} else {
|
||||||
|
return data.token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateToken(appId: string, token: string, exptime: number) {
|
||||||
|
let expire = Date.now() + exptime * 1000
|
||||||
|
let data = new WxTokenClass(token, expire)
|
||||||
|
this._cache.set(appId, data)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user